http-request.js 13 KB

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