http-request.js 10 KB

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