http-request.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if(location.href.indexOf('login.html') > -1) {
  188. return;
  189. }
  190. setTimeout(function() {
  191. sessionStorage.setItem("wlyy_relogin", 1)
  192. // var path = top.location.pathname,
  193. // rootPath = path.split("/")[1];
  194. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  195. }, 2000)
  196. }
  197. var count = 0;
  198. function failCodeHandle(res) {
  199. var tip = "";
  200. if(res.status == 999) {
  201. tip = "此账号已在别处登录,请重新登录";
  202. } else if(res.status == 998) {
  203. tip = "登录超时,请重新登录";
  204. } else if(res.status == 997) {
  205. tip = "此账号未登录,请先登录"
  206. }
  207. if(tip) {
  208. count ++;
  209. if(count == 1){
  210. // toastr && toastr.warning(tip)
  211. toLoginPage()
  212. }
  213. }
  214. }
  215. function uuid(len, radix) {
  216. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  217. var uuid = [],
  218. i;
  219. radix = radix || chars.length;
  220. if(len) {
  221. // Compact form
  222. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  223. } else {
  224. // rfc4122, version 4 form
  225. var r;
  226. // rfc4122 requires these characters
  227. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  228. uuid[14] = '4';
  229. // Fill in random data. At i==19 set the high bits of clock sequence as
  230. // per rfc4122, sec. 4.1.5
  231. for(i = 0; i < 36; i++) {
  232. if(!uuid[i]) {
  233. r = 0 | Math.random() * 16;
  234. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  235. }
  236. }
  237. }
  238. return uuid.join('');
  239. }
  240. /*
  241. * 获取图片路径方法修改
  242. */
  243. function getImgUrl(str) {
  244. if(typeof str != 'string') {
  245. return "";
  246. }
  247. if(str.length == 0) {
  248. return "";
  249. } else {
  250. if(str.indexOf("../") > -1) {
  251. //访问本地路径
  252. return str;
  253. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  254. return str;
  255. } else {
  256. //服务器上的图片路径
  257. return imgServer + str;
  258. }
  259. }
  260. }
  261. httpRequest = {
  262. agentName: agentName,
  263. server: server,
  264. imserver: imserver,
  265. socketUrl: socketUrl,
  266. userAgent: userAgent,
  267. get: httpGet,
  268. post: httpPost,
  269. imHttpGet: imHttpGet,
  270. imHttpPost: imHttpPost,
  271. getImgUrl: getImgUrl,
  272. imgServer:imgServer,
  273. articleGet:articleGet,
  274. articlePost:articlePost,
  275. failCodeHandle: failCodeHandle,
  276. loginIm: function(data){
  277. return imHttpPost('/users/login', data);
  278. },
  279. getDoctorInfo: function() {
  280. return httpGet('doctor/baseinfo');
  281. }
  282. }
  283. window.httpRequest = httpRequest;
  284. })(jQuery)