http-request.js 11 KB

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