http-request.js 12 KB

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