http-request.js 10 KB

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