http-request.js 14 KB

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