http-request.js 13 KB

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