http-request.js 13 KB

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