http-request.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ;(function ($) {
  2. window.ajaxInsArr = {}
  3. window.eventBuss = new Vue()
  4. // var publish_version = false
  5. var publish_version = 1 // 1 正式 2 预发布 3 内网 4 测试
  6. var isInner = false //发布线上后有内外网的配置
  7. var docInfo = 'app_storage'
  8. var agentName = 'wlyyAgentForDoc'
  9. var docAgentName = 'wlyyAgent'
  10. var server, userAgent, loginUrl, imserver, socketUrl, imgServer, teamworkService
  11. var IMEI = localStorage.getItem('WLYY_IMEI') || uuid(16, 16)
  12. localStorage.setItem('WLYY_IMEI', IMEI)
  13. $.support.cors = true
  14. if (publish_version == 1) {
  15. // 生产环境配置
  16. server = '//ijk.xmyys.com.cn/wlyy/'
  17. imserver = '//ijk.xmyys.com.cn/api/v2'
  18. socketUrl = '//ijk.xmyys.com.cn'
  19. articleServer = '//ijk.xmyys.com.cn/wlyy/'
  20. loginUrl = '../../login/login.html'
  21. imgServer = '//ijk.xmyys.com.cn/'
  22. teamworkService = '//hlwyy.xmzsh.com/hlwyy/ims-web'
  23. videoChatIntranetFlag = false
  24. } else if (publish_version == 2) {
  25. // 预发布环境配置
  26. server = '//ijk.xmyys.com.cn/wlyytest/'
  27. imserver = '//ijk.xmyys.com.cn/api/v2'
  28. socketUrl = '//ijk.xmyys.com.cn'
  29. articleServer = '//ijk.xmyys.com.cn/wlyy/'
  30. loginUrl = '../../login/login.html'
  31. imgServer = '//ijk.xmyys.com.cn/'
  32. teamworkService = '//hlwyy.xmzsh.com/hlwyy/ims-web'
  33. videoChatIntranetFlag = false
  34. } else if (publish_version == 3) {
  35. // 内网环境配置
  36. teamworkService = '//hlwyy.xmzsh.com/hlwyy/ims-web'
  37. videoChatIntranetFlag = false
  38. server = 'http://10.95.22.10:8011/wlyy/'
  39. imserver = 'http://10.95.22.138:3000/api/v2'
  40. socketUrl = 'http://10.95.22.138:3000'
  41. articleServer = 'http://10.95.22.10:8011/wlyy/'
  42. loginUrl = '../../login/login.html'
  43. imgServer = 'http://10.95.22.10:8011/'
  44. } else if (publish_version == 4) {
  45. // 测试环境配置
  46. // server = "https://ehr.yihu.com/wlyy/" //测试环境
  47. server = 'http://172.26.0.118:8081/wlyy/' //ip
  48. imserver = 'http://172.26.0.118:3000/api/v2'
  49. socketUrl = 'https://172.26.0.118:3000'
  50. loginUrl = '../../login/login.html'
  51. // loginUrl = "/PC-prescription/login.html"; //自己本地测试时的路径
  52. imgServer = 'http://172.26.0.110:8888/'
  53. teamworkService = '//ehr.yihu.com/hlwyy/ims-web'
  54. videoChatIntranetFlag = false
  55. // server = "http://172.26.0.118:8081/wlyy/" //测试环境
  56. // imserver = "https://172.26.0.118:3000/api/v2";
  57. // socketUrl = "https://172.26.0.118:3000";
  58. // loginUrl = "../../login/login.html";
  59. // // loginUrl = "/PC-prescription/login.html"; //自己本地测试时的路径
  60. // imgServer = "http://172.26.0.110:8888/";
  61. // teamworkService="//ehr.yihu.com/hlwyy/ims-web";
  62. // videoChatIntranetFlag = false;
  63. }
  64. var baseInfo = window.localStorage.getItem(docInfo)
  65. userAgent = window.localStorage.getItem(agentName)
  66. if (userAgent || baseInfo) {
  67. try {
  68. if (baseInfo) {
  69. baseInfo = JSON.parse(baseInfo)
  70. window.localStorage.setItem(docAgentName, JSON.stringify(baseInfo.api_login_doctor))
  71. userAgent = {
  72. id: baseInfo.api_login_doctor.id,
  73. uid: baseInfo.api_login_doctor.uid,
  74. imei: baseInfo.IMEI,
  75. token: baseInfo.api_login_doctor.token,
  76. platform: '4'
  77. }
  78. window.localStorage.setItem(agentName, JSON.stringify(userAgent))
  79. localStorage.setItem('WLYY_IMEI', baseInfo.IMEI)
  80. } else {
  81. userAgent = JSON.parse(userAgent)
  82. }
  83. } catch (e) {
  84. toastr && toastr.warning('登录失效,请关闭当前弹窗前往PC IM重新登录')
  85. // toLoginPage()
  86. }
  87. } else {
  88. toLoginPage()
  89. }
  90. var isRelogined = sessionStorage.getItem('wlyy_relogin')
  91. if (isRelogined && location.href.indexOf('login.html') < 0) {
  92. sessionStorage.removeItem('wlyy_relogin')
  93. }
  94. function httpGet(url, options) {
  95. //发送ajax请求
  96. return new Promise(function (resolve, reject) {
  97. window.ajaxInsArr[url] = $.ajax(
  98. server + url,
  99. $.extend(
  100. {},
  101. {
  102. type: 'GET',
  103. dataType: 'JSON',
  104. beforeSend: function (request) {
  105. var agent = userAgent || {
  106. imei: localStorage.getItem('WLYY_IMEI'),
  107. platform: 4
  108. }
  109. request.setRequestHeader('userAgent', JSON.stringify(agent))
  110. },
  111. error: function (res) {
  112. if (res.statusText == 'abort') {
  113. }
  114. reject(res)
  115. },
  116. success: function (res) {
  117. failCodeHandle(res)
  118. resolve(res)
  119. }
  120. },
  121. options
  122. )
  123. )
  124. })
  125. }
  126. function articleGet(url, options) {
  127. //发送ajax请求
  128. return new Promise(function (resolve, reject) {
  129. $.ajax(
  130. articleServer + url,
  131. $.extend(
  132. {},
  133. {
  134. type: 'GET',
  135. dataType: 'JSON',
  136. beforeSend: function (request) {
  137. var agent = userAgent || {
  138. imei: localStorage.getItem('WLYY_IMEI'),
  139. platform: 4
  140. }
  141. request.setRequestHeader('userAgent', JSON.stringify(agent))
  142. },
  143. error: function (res) {
  144. reject(res)
  145. },
  146. success: function (res) {
  147. failCodeHandle(res)
  148. resolve(res)
  149. }
  150. },
  151. options
  152. )
  153. )
  154. })
  155. }
  156. function httpPost(url, options) {
  157. //发送ajax请求
  158. return new Promise(function (resolve, reject) {
  159. $.ajax(
  160. server + url,
  161. $.extend(
  162. {},
  163. {
  164. type: 'POST',
  165. dataType: 'JSON',
  166. beforeSend: function (request) {
  167. var agent = userAgent || {
  168. imei: localStorage.getItem('WLYY_IMEI')
  169. }
  170. request.setRequestHeader('userAgent', JSON.stringify(agent))
  171. },
  172. error: function (res) {
  173. reject(res)
  174. },
  175. success: function (res) {
  176. failCodeHandle(res)
  177. resolve(res)
  178. }
  179. },
  180. options
  181. )
  182. )
  183. })
  184. }
  185. function articlePost(url, options) {
  186. //发送ajax请求
  187. return new Promise(function (resolve, reject) {
  188. $.ajax(
  189. articleServer + url,
  190. $.extend(
  191. {},
  192. {
  193. type: 'POST',
  194. dataType: 'JSON',
  195. beforeSend: function (request) {
  196. var agent = userAgent || {
  197. imei: localStorage.getItem('WLYY_IMEI')
  198. }
  199. request.setRequestHeader('userAgent', JSON.stringify(agent))
  200. },
  201. error: function (res) {
  202. reject(res)
  203. },
  204. success: function (res) {
  205. failCodeHandle(res)
  206. resolve(res)
  207. }
  208. },
  209. options
  210. )
  211. )
  212. })
  213. }
  214. function imHttpGet(url, options) {
  215. //发送ajax请求
  216. return new Promise(function (resolve, reject) {
  217. $.ajax(
  218. imserver + url,
  219. $.extend(
  220. {},
  221. {
  222. type: 'GET',
  223. dataType: 'JSON',
  224. beforeSend: function (request) {
  225. request.setRequestHeader('userAgent', JSON.stringify(userAgent))
  226. },
  227. error: function (res) {
  228. reject(res)
  229. },
  230. success: function (res) {
  231. failCodeHandle(res)
  232. resolve(res)
  233. }
  234. },
  235. options
  236. )
  237. )
  238. })
  239. }
  240. function imHttpPost(url, options) {
  241. //发送ajax请求
  242. return new Promise(function (resolve, reject) {
  243. $.ajax(
  244. imserver + url,
  245. $.extend(
  246. {},
  247. {
  248. type: 'POST',
  249. dataType: 'JSON',
  250. beforeSend: function (request) {
  251. request.setRequestHeader('userAgent', JSON.stringify(userAgent))
  252. },
  253. error: function (res) {
  254. reject(res)
  255. },
  256. success: function (res) {
  257. failCodeHandle(res)
  258. resolve(res)
  259. }
  260. },
  261. options
  262. )
  263. )
  264. })
  265. }
  266. function videoChatPost(url, options) {
  267. if (options && options.data) {
  268. options.data.intranetFlag = videoChatIntranetFlag
  269. }
  270. return httpPost(url, options)
  271. }
  272. function toLoginPage() {
  273. if (location.href.indexOf('login.html') > -1) {
  274. return
  275. }
  276. setTimeout(function () {
  277. sessionStorage.setItem('wlyy_relogin', 1)
  278. // var path = top.location.pathname,
  279. // rootPath = path.split("/")[1];
  280. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  281. }, 2000)
  282. }
  283. var count = 0
  284. function failCodeHandle(res) {
  285. var tip = ''
  286. if (res.status == 999) {
  287. tip = '此账号已在别处登录,请关闭当前弹窗前往PC IM重新登录'
  288. } else if (res.status == 998) {
  289. tip = '登录超时,请关闭当前弹窗前往PC IM重新登录'
  290. } else if (res.status == 997) {
  291. tip = '此账号未登录,请先登录'
  292. }
  293. if (tip) {
  294. count++
  295. if (count == 1) {
  296. // console.log(tip)
  297. toastr && toastr.warning(tip)
  298. // toLoginPage()
  299. }
  300. }
  301. }
  302. function uuid(len, radix) {
  303. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  304. var uuid = [],
  305. i
  306. radix = radix || chars.length
  307. if (len) {
  308. // Compact form
  309. for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  310. } else {
  311. // rfc4122, version 4 form
  312. var r
  313. // rfc4122 requires these characters
  314. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  315. uuid[14] = '4'
  316. // Fill in random data. At i==19 set the high bits of clock sequence as
  317. // per rfc4122, sec. 4.1.5
  318. for (i = 0; i < 36; i++) {
  319. if (!uuid[i]) {
  320. r = 0 | (Math.random() * 16)
  321. uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
  322. }
  323. }
  324. }
  325. return uuid.join('')
  326. }
  327. /*
  328. * 获取图片路径方法修改
  329. */
  330. function getImgUrl(str) {
  331. if (typeof str != 'string') {
  332. return ''
  333. }
  334. if (str.length == 0) {
  335. return ''
  336. } else {
  337. if (str.indexOf('../') > -1) {
  338. //访问本地路径
  339. return str
  340. } else if (str.indexOf('http://') > -1 || str.indexOf('https://') > -1) {
  341. return str
  342. } else {
  343. //服务器上的图片路径
  344. return imgServer + str
  345. }
  346. }
  347. }
  348. // 导出xls
  349. function downLoadFileForAjax(url, fileName, data) {
  350. // 判断内网走url
  351. if (publish_version && isInner) {
  352. var p = []
  353. for (var k in data) {
  354. p.push(k + '=' + data[k])
  355. }
  356. var ps = p.join('&')
  357. var str = server + url + '?' + ps
  358. setTimeout(function () {
  359. window.location.href = str
  360. }, 1000)
  361. return Promise.resolve()
  362. } else {
  363. // 外网做ajax
  364. return new Promise(function (resolve, reject) {
  365. if (window.URL) {
  366. const xhr = new XMLHttpRequest()
  367. if (data) {
  368. var p = []
  369. for (var k in data) {
  370. p.push(k + '=' + data[k])
  371. }
  372. url += (url.indexOf('?') != -1 ? '&' : '?') + p.join('&')
  373. }
  374. xhr.open('GET', server + url, true)
  375. xhr.responseType = 'blob'
  376. xhr.onload = function () {
  377. if (xhr.readyState == 4 && xhr.status == 200) {
  378. const blob = xhr.response
  379. const blobUrl = window.URL.createObjectURL(blob)
  380. const a = document.createElement('a')
  381. a.style.display = 'none'
  382. a.download = fileName
  383. a.href = blobUrl
  384. a.target = '_blank'
  385. a.click()
  386. resolve()
  387. } else {
  388. reject(xhr)
  389. }
  390. }
  391. xhr.onerror = function (err) {
  392. console.error(err)
  393. reject(xhr)
  394. }
  395. xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
  396. var agent = userAgent || {
  397. imei: localStorage.getItem('WLYY_IMEI'),
  398. platform: 4
  399. }
  400. xhr.setRequestHeader('userAgent', JSON.stringify(agent))
  401. xhr.send()
  402. } else {
  403. window.toastr && window.toastr.warning('您正使用外网访问系统,浏览器版本低,请更新浏览器后再操作!')
  404. reject()
  405. }
  406. })
  407. }
  408. }
  409. httpRequest = {
  410. downLoadFileForAjax: downLoadFileForAjax,
  411. agentName: agentName,
  412. server: server,
  413. imserver: imserver,
  414. socketUrl: socketUrl,
  415. userAgent: userAgent,
  416. get: httpGet,
  417. post: httpPost,
  418. imHttpGet: imHttpGet,
  419. imHttpPost: imHttpPost,
  420. getImgUrl: getImgUrl,
  421. imgServer: imgServer,
  422. teamworkService: teamworkService,
  423. articleGet: articleGet,
  424. articlePost: articlePost,
  425. failCodeHandle: failCodeHandle,
  426. uuid: uuid,
  427. videoChatPost: videoChatPost,
  428. loginIm: function (data) {
  429. return imHttpPost('/users/login', data)
  430. },
  431. getDoctorInfo: function () {
  432. return httpGet('doctor/baseinfo')
  433. }
  434. }
  435. window.httpRequest = httpRequest
  436. })(jQuery)