http-request.js 8.1 KB

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