http-request.js 14 KB

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