http-request.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. (function($) {
  2. var publish_version = false;
  3. var isInner = true; //发布线上后有内外网的配置
  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 = "http://172.26.0.118:9092/wlyy/" //测试环境
  30. // server = "http://192.168.131.190:8080/" //桃红
  31. // server = "http://192.168.131.144:8080/" //冬梅
  32. imserver = "http://172.26.0.118:3000/api/v2";
  33. socketUrl = "http://172.26.0.118:3000";
  34. // server = "http://192.168.131.154:8080/"
  35. // articleServer="http://172.26.0.118:9092/wlyy/"
  36. // server = "http://192.168.131.130:8080/";
  37. // server = "http://192.168.131.118:8080/"; // 明芬
  38. // server = "http://192.168.131.127:8060/"; //逸祥
  39. // server = "http://192.168.131.24:8080/"; //仕杰
  40. // server = "http://192.168.131.112:8080/"; // 志南
  41. // server = "http://192.168.131.24:8082/"; // 文彬
  42. // imserver = "http://192.168.131.24:3000/api/v2"
  43. // socketUrl = "http://192.168.131.24:3000";
  44. // server = "http://192.168.131.144:8080/";//冬梅
  45. loginUrl = "../../login/login.html";
  46. // loginUrl = "/PC-prescription/login.html"; //自己本地测试时的路径
  47. imgServer = "http://172.26.0.110:8888/";
  48. teamworkService="//ehr.yihu.com/hlwyy/ims-web";
  49. videoChatIntranetFlag = false;
  50. }
  51. var baseInfo = window.localStorage.getItem(docInfo);
  52. userAgent = window.localStorage.getItem(agentName)
  53. if(userAgent || baseInfo) {
  54. try {
  55. if(baseInfo){
  56. baseInfo = JSON.parse(baseInfo);
  57. window.localStorage.setItem(docAgentName, JSON.stringify(baseInfo.api_login_doctor));
  58. userAgent = {
  59. 'id': baseInfo.api_login_doctor.id,
  60. 'uid': baseInfo.api_login_doctor.uid,
  61. 'imei': baseInfo.IMEI,
  62. 'token': baseInfo.api_login_doctor.token,
  63. 'platform': '4'
  64. };
  65. window.localStorage.setItem(agentName, JSON.stringify(userAgent));
  66. localStorage.setItem('WLYY_IMEI', baseInfo.IMEI)
  67. }else{
  68. userAgent = JSON.parse(userAgent)
  69. }
  70. } catch(e) {
  71. toastr && toastr.warning("登录失效,请关闭当前弹窗前往PC IM重新登录")
  72. // toLoginPage()
  73. }
  74. } else {
  75. toLoginPage()
  76. }
  77. var isRelogined = sessionStorage.getItem('wlyy_relogin')
  78. if(isRelogined && location.href.indexOf("login.html") < 0){
  79. sessionStorage.removeItem('wlyy_relogin')
  80. }
  81. function httpGet(url, options) {
  82. //发送ajax请求
  83. return new Promise(function(resolve, reject) {
  84. $.ajax(server + url,
  85. $.extend({}, {
  86. type: 'GET',
  87. dataType: 'JSON',
  88. beforeSend: function(request) {
  89. var agent = userAgent || {
  90. imei: localStorage.getItem('WLYY_IMEI'),
  91. platform: 4
  92. }
  93. request.setRequestHeader("userAgent", JSON.stringify(agent));
  94. },
  95. error: function(res) {
  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. httpRequest = {
  298. agentName: agentName,
  299. server: server,
  300. imserver: imserver,
  301. socketUrl: socketUrl,
  302. userAgent: userAgent,
  303. get: httpGet,
  304. post: httpPost,
  305. imHttpGet: imHttpGet,
  306. imHttpPost: imHttpPost,
  307. getImgUrl: getImgUrl,
  308. imgServer:imgServer,
  309. teamworkService:teamworkService,
  310. articleGet:articleGet,
  311. articlePost:articlePost,
  312. failCodeHandle: failCodeHandle,
  313. uuid: uuid,
  314. videoChatPost: videoChatPost,
  315. loginIm: function(data){
  316. return imHttpPost('/users/login', data);
  317. },
  318. getDoctorInfo: function() {
  319. return httpGet('doctor/baseinfo');
  320. }
  321. }
  322. window.httpRequest = httpRequest;
  323. })(jQuery)