http-request.js 11 KB

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