http-request.js 11 KB

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