http-request.js 12 KB

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