http-request.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. (function($) {
  2. var publish_version = false;
  3. var isInner = false; //发布线上后有内外网的配置
  4. var agentName = "wlyyAgentForDoc";
  5. var server, userAgent, loginUrl, imserver, socketUrl, imgServer, grant_type, client_id;
  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/wlyytest/"
  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. grant_type = "password";
  24. client_id = "WYo0l73F8e";
  25. server = "http://172.19.103.73:10001";
  26. // server = "http://172.19.103.88:9092/wlyy/"
  27. imserver = "http://172.19.103.88:3000/api/v2";
  28. socketUrl = "http://172.19.103.88:3000";
  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 = 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 httpPost(url, options) {
  78. //发送ajax请求
  79. return new Promise(function(resolve, reject) {
  80. $.ajax(server + url,
  81. $.extend({}, {
  82. type: 'POST',
  83. dataType: 'JSON',
  84. beforeSend: function(request) {
  85. var agent = userAgent || {
  86. imei: localStorage.getItem('WLYY_IMEI')
  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 imHttpGet(url, options) {
  101. //发送ajax请求
  102. return new Promise(function(resolve, reject) {
  103. $.ajax(imserver + url,
  104. $.extend({}, {
  105. type: 'GET',
  106. dataType: 'JSON',
  107. beforeSend: function(request) {
  108. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  109. },
  110. error: function(res) {
  111. reject(res)
  112. },
  113. success: function(res) {
  114. failCodeHandle(res)
  115. resolve(res)
  116. }
  117. }, options));
  118. })
  119. }
  120. function imHttpPost(url, options) {
  121. //发送ajax请求
  122. return new Promise(function(resolve, reject) {
  123. $.ajax(imserver + url,
  124. $.extend({}, {
  125. type: 'POST',
  126. dataType: 'JSON',
  127. beforeSend: function(request) {
  128. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  129. },
  130. error: function(res) {
  131. reject(res)
  132. },
  133. success: function(res) {
  134. failCodeHandle(res)
  135. resolve(res)
  136. }
  137. }, options));
  138. })
  139. }
  140. function toLoginPage() {
  141. if(location.href.indexOf('login.html') > -1) {
  142. return;
  143. }
  144. setTimeout(function() {
  145. sessionStorage.setItem("wlyy_relogin", 1)
  146. // var path = top.location.pathname,
  147. // rootPath = path.split("/")[1];
  148. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  149. }, 2000)
  150. }
  151. var count = 0;
  152. function failCodeHandle(res) {
  153. var tip = "";
  154. if(res.status == 999) {
  155. tip = "此账号已在别处登录,请重新登录";
  156. } else if(res.status == 998) {
  157. tip = "登录超时,请重新登录";
  158. } else if(res.status == 997) {
  159. tip = "此账号未登录,请先登录"
  160. }
  161. if(tip) {
  162. count ++;
  163. if(count == 1){
  164. // toastr && toastr.warning(tip)
  165. toLoginPage()
  166. }
  167. }
  168. }
  169. function uuid(len, radix) {
  170. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  171. var uuid = [],
  172. i;
  173. radix = radix || chars.length;
  174. if(len) {
  175. // Compact form
  176. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  177. } else {
  178. // rfc4122, version 4 form
  179. var r;
  180. // rfc4122 requires these characters
  181. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  182. uuid[14] = '4';
  183. // Fill in random data. At i==19 set the high bits of clock sequence as
  184. // per rfc4122, sec. 4.1.5
  185. for(i = 0; i < 36; i++) {
  186. if(!uuid[i]) {
  187. r = 0 | Math.random() * 16;
  188. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  189. }
  190. }
  191. }
  192. return uuid.join('');
  193. }
  194. /*
  195. * 获取图片路径方法修改
  196. */
  197. function getImgUrl(str) {
  198. if(typeof str != 'string') {
  199. return "";
  200. }
  201. if(str.length == 0) {
  202. return "";
  203. } else {
  204. if(str.indexOf("../") > -1) {
  205. //访问本地路径
  206. return str;
  207. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  208. return str;
  209. } else {
  210. //服务器上的图片路径
  211. return imgServer + str;
  212. }
  213. }
  214. }
  215. httpRequest = {
  216. agentName: agentName,
  217. server: server,
  218. imserver: imserver,
  219. socketUrl: socketUrl,
  220. userAgent: userAgent,
  221. get: httpGet,
  222. post: httpPost,
  223. imHttpGet: imHttpGet,
  224. imHttpPost: imHttpPost,
  225. getImgUrl: getImgUrl,
  226. grant_type: grant_type,
  227. client_id: client_id,
  228. imgServer:imgServer,
  229. failCodeHandle: failCodeHandle,
  230. loginIm: function(data){
  231. return imHttpPost('/users/login', data);
  232. },
  233. getDoctorInfo: function() {
  234. return httpGet('doctor/baseinfo');
  235. }
  236. }
  237. window.httpRequest = httpRequest;
  238. })(jQuery)