http-request.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 = "http://www.xmtyw.cn/wlyytest/"
  13. imserver = "http://27.155.101.77:3000/api/v2";
  14. socketUrl = "http://27.155.101.77:3000";
  15. articleServer = "http://www.xmtyw.cn/wlyytest/"
  16. loginUrl = "../../login/login.html";
  17. imgServer = "http://www.xmtyw.cn/";
  18. if(isInner){
  19. server = "http://10.95.22.10:8011/wlyy/"
  20. imserver = "http://10.95.22.138:3000/api/v2";
  21. socketUrl = "http://10.95.22.138:3000";
  22. articleServer = "http://10.95.22.10:8011/wlyy/"
  23. loginUrl = "../../login/login.html";
  24. imgServer = "http://10.95.22.10:8011/";
  25. }
  26. } else { // 测试环境配置
  27. server = "http://172.19.103.88:9092/wlyy/"
  28. imserver = "http://172.19.103.88:3000/api/v2";
  29. socketUrl = "http://172.19.103.88:3000";
  30. // server = "http://192.168.131.154:8080/"
  31. articleServer="http://172.19.103.88:9092/wlyy/"
  32. // server = "http://192.168.131.130:8080/";
  33. // server = "http://192.168.131.123:8080/"; // 明芬
  34. // server = "http://192.168.131.127:8060/"; //逸祥
  35. // server = "http://192.168.131.24:8080/"; //仕杰
  36. // imserver = "http://192.168.131.24:3000/api/v2"
  37. // socketUrl = "http://192.168.131.24:3000";
  38. loginUrl = "../../login/login.html";
  39. // loginUrl = "/PC-prescription/login.html"; //自己本地测试时的路径
  40. imgServer = "http://172.19.103.54/";
  41. }
  42. var baseInfo = window.localStorage.getItem(docInfo);
  43. userAgent = window.localStorage.getItem(agentName)
  44. if(baseInfo) {
  45. try {
  46. baseInfo = JSON.parse(baseInfo);
  47. window.localStorage.setItem(docAgentName, JSON.stringify(baseInfo.api_login_doctor));
  48. userAgent = {
  49. 'id': baseInfo.api_login_doctor.id,
  50. 'uid': baseInfo.api_login_doctor.uid,
  51. 'imei': baseInfo.IMEI,
  52. 'token': baseInfo.api_login_doctor.token,
  53. 'platform': '4'
  54. };
  55. localStorage.setItem('WLYY_IMEI', baseInfo.IMEI)
  56. } catch(e) {
  57. toastr && toastr.warning("登录失效,请关闭当前弹窗前往PC IM重新登录")
  58. // toLoginPage()
  59. }
  60. } else {
  61. toLoginPage()
  62. }
  63. var isRelogined = sessionStorage.getItem('wlyy_relogin')
  64. if(isRelogined && location.href.indexOf("login.html") < 0){
  65. sessionStorage.removeItem('wlyy_relogin')
  66. }
  67. function httpGet(url, options) {
  68. //发送ajax请求
  69. return new Promise(function(resolve, reject) {
  70. $.ajax(server + url,
  71. $.extend({}, {
  72. type: 'GET',
  73. dataType: 'JSON',
  74. beforeSend: function(request) {
  75. var agent = userAgent || {
  76. imei: localStorage.getItem('WLYY_IMEI'),
  77. platform: 4
  78. }
  79. request.setRequestHeader("userAgent", JSON.stringify(agent));
  80. },
  81. error: function(res) {
  82. reject(res)
  83. },
  84. success: function(res) {
  85. failCodeHandle(res)
  86. resolve(res)
  87. }
  88. }, options));
  89. })
  90. }
  91. function articleGet(url, options) {
  92. //发送ajax请求
  93. return new Promise(function(resolve, reject) {
  94. $.ajax(articleServer + url,
  95. $.extend({}, {
  96. type: 'GET',
  97. dataType: 'JSON',
  98. beforeSend: function(request) {
  99. var agent = userAgent || {
  100. imei: localStorage.getItem('WLYY_IMEI'),
  101. platform: 4
  102. }
  103. request.setRequestHeader("userAgent", JSON.stringify(agent));
  104. },
  105. error: function(res) {
  106. reject(res)
  107. },
  108. success: function(res) {
  109. failCodeHandle(res)
  110. resolve(res)
  111. }
  112. }, options));
  113. })
  114. }
  115. function httpPost(url, options) {
  116. //发送ajax请求
  117. return new Promise(function(resolve, reject) {
  118. $.ajax(server + url,
  119. $.extend({}, {
  120. type: 'POST',
  121. dataType: 'JSON',
  122. beforeSend: function(request) {
  123. var agent = userAgent || {
  124. imei: localStorage.getItem('WLYY_IMEI')
  125. }
  126. request.setRequestHeader("userAgent", JSON.stringify(agent));
  127. },
  128. error: function(res) {
  129. reject(res)
  130. },
  131. success: function(res) {
  132. failCodeHandle(res)
  133. resolve(res)
  134. }
  135. }, options));
  136. })
  137. }
  138. function articlePost(url, options) {
  139. //发送ajax请求
  140. return new Promise(function(resolve, reject) {
  141. $.ajax(articleServer + url,
  142. $.extend({}, {
  143. type: 'POST',
  144. dataType: 'JSON',
  145. beforeSend: function(request) {
  146. var agent = userAgent || {
  147. imei: localStorage.getItem('WLYY_IMEI')
  148. }
  149. request.setRequestHeader("userAgent", JSON.stringify(agent));
  150. },
  151. error: function(res) {
  152. reject(res)
  153. },
  154. success: function(res) {
  155. failCodeHandle(res)
  156. resolve(res)
  157. }
  158. }, options));
  159. })
  160. }
  161. function imHttpGet(url, options) {
  162. //发送ajax请求
  163. return new Promise(function(resolve, reject) {
  164. $.ajax(imserver + url,
  165. $.extend({}, {
  166. type: 'GET',
  167. dataType: 'JSON',
  168. beforeSend: function(request) {
  169. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  170. },
  171. error: function(res) {
  172. reject(res)
  173. },
  174. success: function(res) {
  175. failCodeHandle(res)
  176. resolve(res)
  177. }
  178. }, options));
  179. })
  180. }
  181. function imHttpPost(url, options) {
  182. //发送ajax请求
  183. return new Promise(function(resolve, reject) {
  184. $.ajax(imserver + url,
  185. $.extend({}, {
  186. type: 'POST',
  187. dataType: 'JSON',
  188. beforeSend: function(request) {
  189. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  190. },
  191. error: function(res) {
  192. reject(res)
  193. },
  194. success: function(res) {
  195. failCodeHandle(res)
  196. resolve(res)
  197. }
  198. }, options));
  199. })
  200. }
  201. function toLoginPage() {
  202. if(location.href.indexOf('login.html') > -1) {
  203. return;
  204. }
  205. setTimeout(function() {
  206. sessionStorage.setItem("wlyy_relogin", 1)
  207. // var path = top.location.pathname,
  208. // rootPath = path.split("/")[1];
  209. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  210. }, 2000)
  211. }
  212. var count = 0;
  213. function failCodeHandle(res) {
  214. var tip = "";
  215. if(res.status == 999) {
  216. tip = "此账号已在别处登录,请关闭当前弹窗前往PC IM重新登录";
  217. } else if(res.status == 998) {
  218. tip = "登录超时,请关闭当前弹窗前往PC IM重新登录";
  219. } else if(res.status == 997) {
  220. tip = "此账号未登录,请先登录"
  221. }
  222. if(tip) {
  223. count ++;
  224. if(count == 1){
  225. // console.log(tip)
  226. toastr && toastr.warning(tip)
  227. // toLoginPage()
  228. }
  229. }
  230. }
  231. function uuid(len, radix) {
  232. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  233. var uuid = [],
  234. i;
  235. radix = radix || chars.length;
  236. if(len) {
  237. // Compact form
  238. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  239. } else {
  240. // rfc4122, version 4 form
  241. var r;
  242. // rfc4122 requires these characters
  243. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  244. uuid[14] = '4';
  245. // Fill in random data. At i==19 set the high bits of clock sequence as
  246. // per rfc4122, sec. 4.1.5
  247. for(i = 0; i < 36; i++) {
  248. if(!uuid[i]) {
  249. r = 0 | Math.random() * 16;
  250. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  251. }
  252. }
  253. }
  254. return uuid.join('');
  255. }
  256. /*
  257. * 获取图片路径方法修改
  258. */
  259. function getImgUrl(str) {
  260. if(typeof str != 'string') {
  261. return "";
  262. }
  263. if(str.length == 0) {
  264. return "";
  265. } else {
  266. if(str.indexOf("../") > -1) {
  267. //访问本地路径
  268. return str;
  269. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  270. return str;
  271. } else {
  272. //服务器上的图片路径
  273. return imgServer + str;
  274. }
  275. }
  276. }
  277. httpRequest = {
  278. agentName: agentName,
  279. server: server,
  280. imserver: imserver,
  281. socketUrl: socketUrl,
  282. userAgent: userAgent,
  283. get: httpGet,
  284. post: httpPost,
  285. imHttpGet: imHttpGet,
  286. imHttpPost: imHttpPost,
  287. getImgUrl: getImgUrl,
  288. imgServer:imgServer,
  289. articleGet:articleGet,
  290. articlePost:articlePost,
  291. failCodeHandle: failCodeHandle,
  292. loginIm: function(data){
  293. return imHttpPost('/users/login', data);
  294. },
  295. getDoctorInfo: function() {
  296. return httpGet('doctor/baseinfo');
  297. }
  298. }
  299. window.httpRequest = httpRequest;
  300. })(jQuery)