http-request.js 10 KB

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