http-request.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. (function($) {
  2. var publish_version = false;
  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. loginUrl = "../../login/login.html";
  14. imgServer = "http://www.xmtyw.cn/";
  15. if(isInner){
  16. server = "http://10.95.22.10:8011/wlyy/"
  17. imserver = "http://10.95.22.138:3000/api/v2";
  18. socketUrl = "http://10.95.22.138:3000";
  19. loginUrl = "../../login/login.html";
  20. imgServer = "http://10.95.22.10:8011/";
  21. }
  22. } else { // 测试环境配置
  23. server = "http://172.19.103.88:9092/wlyy/"
  24. imserver = "http://172.19.103.88:3000/api/v2";
  25. socketUrl = "http://172.19.103.88:3000";
  26. // server = "http://192.168.131.154:8080/"
  27. articleServer="http://172.19.103.88:9092/wlyy/"
  28. // server = "http://192.168.131.133:8080/";
  29. // server = "http://192.168.131.127:8060/"; //逸祥
  30. // server = "http://192.168.131.24:8080/"; //仕杰
  31. // imserver = "http://192.168.131.24:3000/api/v2"
  32. // socketUrl = "http://192.168.131.24:3000";
  33. loginUrl = "../../login/login.html";
  34. // loginUrl = "/PC-prescription/login.html"; //自己本地测试时的路径
  35. imgServer = "http://172.19.103.54/";
  36. }
  37. userAgent = window.localStorage.getItem(agentName)
  38. if(userAgent) {
  39. try {
  40. userAgent = JSON.parse(userAgent)
  41. } catch(e) {
  42. toastr && toastr.warning("登录失效,请重新登录")
  43. toLoginPage()
  44. }
  45. } else {
  46. toLoginPage()
  47. }
  48. var isRelogined = sessionStorage.getItem('wlyy_relogin')
  49. if(isRelogined && location.href.indexOf("login.html") < 0){
  50. sessionStorage.removeItem('wlyy_relogin')
  51. }
  52. function httpGet(url, options) {
  53. //发送ajax请求
  54. return new Promise(function(resolve, reject) {
  55. $.ajax(server + url,
  56. $.extend({}, {
  57. type: 'GET',
  58. dataType: 'JSON',
  59. beforeSend: function(request) {
  60. var agent = userAgent || {
  61. imei: localStorage.getItem('WLYY_IMEI'),
  62. platform: 4
  63. }
  64. request.setRequestHeader("userAgent", JSON.stringify(agent));
  65. },
  66. error: function(res) {
  67. reject(res)
  68. },
  69. success: function(res) {
  70. failCodeHandle(res)
  71. resolve(res)
  72. }
  73. }, options));
  74. })
  75. }
  76. function articleGet(url, options) {
  77. //发送ajax请求
  78. return new Promise(function(resolve, reject) {
  79. $.ajax(articleServer + url,
  80. $.extend({}, {
  81. type: 'GET',
  82. dataType: 'JSON',
  83. beforeSend: function(request) {
  84. var agent = userAgent || {
  85. imei: localStorage.getItem('WLYY_IMEI'),
  86. platform: 4
  87. }
  88. request.setRequestHeader("userAgent", JSON.stringify(agent));
  89. },
  90. error: function(res) {
  91. reject(res)
  92. },
  93. success: function(res) {
  94. failCodeHandle(res)
  95. resolve(res)
  96. }
  97. }, options));
  98. })
  99. }
  100. function httpPost(url, options) {
  101. //发送ajax请求
  102. return new Promise(function(resolve, reject) {
  103. $.ajax(server + url,
  104. $.extend({}, {
  105. type: 'POST',
  106. dataType: 'JSON',
  107. beforeSend: function(request) {
  108. var agent = userAgent || {
  109. imei: localStorage.getItem('WLYY_IMEI')
  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 articlePost(url, options) {
  124. //发送ajax请求
  125. return new Promise(function(resolve, reject) {
  126. $.ajax(articleServer + 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 imHttpGet(url, options) {
  147. //发送ajax请求
  148. return new Promise(function(resolve, reject) {
  149. $.ajax(imserver + url,
  150. $.extend({}, {
  151. type: 'GET',
  152. dataType: 'JSON',
  153. beforeSend: function(request) {
  154. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  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 imHttpPost(url, options) {
  167. //发送ajax请求
  168. return new Promise(function(resolve, reject) {
  169. $.ajax(imserver + url,
  170. $.extend({}, {
  171. type: 'POST',
  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 toLoginPage() {
  187. return
  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)