http-request.js 13 KB

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