http-request.js 12 KB

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