http-request.js 11 KB

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