http-request.js 8.3 KB

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