http-request.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/html/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 oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  62. if (oauthInfo) {
  63. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  64. request.setRequestHeader("token", oauthInfo.accessToken);
  65. }
  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. },
  86. error: function(res) {
  87. reject(res)
  88. },
  89. success: function(res) {
  90. failCodeHandle(res)
  91. resolve(res)
  92. }
  93. }, options));
  94. })
  95. }
  96. //调用福州总部的接口的方法
  97. function hzHttpPost(url, options){
  98. return new Promise(function(resolve, reject) {
  99. var url2 = "api/v1.0/fz/open/api",
  100. params = {
  101. apiUrl: url, //相对接口路径,不用"/"开头
  102. paramsJson: JSON.stringify(options)
  103. }
  104. $.ajax(server + url2, {
  105. type: 'POST',
  106. data: params,
  107. dataType: 'JSON',
  108. error: function(res) {
  109. reject(res)
  110. },
  111. success: function(res) {
  112. failCodeHandle(res)
  113. resolve(res)
  114. }
  115. });
  116. })
  117. }
  118. function imHttpGet(url, options) {
  119. //发送ajax请求
  120. return new Promise(function(resolve, reject) {
  121. $.ajax(imserver + url,
  122. $.extend({}, {
  123. type: 'GET',
  124. dataType: 'JSON',
  125. beforeSend: function(request) {
  126. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  127. },
  128. error: function(res) {
  129. reject(res)
  130. },
  131. success: function(res) {
  132. failCodeHandle(res)
  133. resolve(res)
  134. }
  135. }, options));
  136. })
  137. }
  138. function imHttpPost(url, options) {
  139. //发送ajax请求
  140. return new Promise(function(resolve, reject) {
  141. $.ajax(imserver + url,
  142. $.extend({}, {
  143. type: 'POST',
  144. dataType: 'JSON',
  145. beforeSend: function(request) {
  146. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  147. },
  148. error: function(res) {
  149. reject(res)
  150. },
  151. success: function(res) {
  152. failCodeHandle(res)
  153. resolve(res)
  154. }
  155. }, options));
  156. })
  157. }
  158. function toLoginPage() {
  159. if(location.href.indexOf('login.html') > -1) {
  160. return;
  161. }
  162. setTimeout(function() {
  163. sessionStorage.setItem("wlyy_relogin", 1)
  164. // var path = top.location.pathname,
  165. // rootPath = path.split("/")[1];
  166. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  167. }, 2000)
  168. }
  169. var count = 0;
  170. function failCodeHandle(res) {
  171. var tip = "";
  172. if(res.status == 999) {
  173. tip = "此账号已在别处登录,请重新登录";
  174. } else if(res.status == 998) {
  175. tip = "登录超时,请重新登录";
  176. } else if(res.status == 997) {
  177. tip = "此账号未登录,请先登录"
  178. }
  179. if(tip) {
  180. count ++;
  181. if(count == 1){
  182. // toastr && toastr.warning(tip)
  183. toLoginPage()
  184. }
  185. }
  186. }
  187. function uuid(len, radix) {
  188. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  189. var uuid = [],
  190. i;
  191. radix = radix || chars.length;
  192. if(len) {
  193. // Compact form
  194. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  195. } else {
  196. // rfc4122, version 4 form
  197. var r;
  198. // rfc4122 requires these characters
  199. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  200. uuid[14] = '4';
  201. // Fill in random data. At i==19 set the high bits of clock sequence as
  202. // per rfc4122, sec. 4.1.5
  203. for(i = 0; i < 36; i++) {
  204. if(!uuid[i]) {
  205. r = 0 | Math.random() * 16;
  206. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  207. }
  208. }
  209. }
  210. return uuid.join('');
  211. }
  212. /*
  213. * 获取图片路径方法修改
  214. */
  215. function getImgUrl(str) {
  216. if(typeof str != 'string') {
  217. return "";
  218. }
  219. if(str.length == 0) {
  220. return "";
  221. } else {
  222. if(str.indexOf("../") > -1) {
  223. //访问本地路径
  224. return str;
  225. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  226. return str;
  227. } else {
  228. //服务器上的图片路径
  229. return imgServer + str;
  230. }
  231. }
  232. }
  233. httpRequest = {
  234. agentName: agentName,
  235. server: server,
  236. imserver: imserver,
  237. socketUrl: socketUrl,
  238. userAgent: userAgent,
  239. get: httpGet,
  240. post: httpPost,
  241. imHttpGet: imHttpGet,
  242. imHttpPost: imHttpPost,
  243. fzPost: hzHttpPost,
  244. getImgUrl: getImgUrl,
  245. grant_type: grant_type,
  246. client_id: client_id,
  247. imgServer:imgServer,
  248. failCodeHandle: failCodeHandle,
  249. loginIm: function(data){
  250. return imHttpPost('/users/login', data);
  251. },
  252. getDoctorInfo: function() {
  253. return httpGet('doctor/baseinfo');
  254. }
  255. }
  256. window.httpRequest = httpRequest;
  257. })(jQuery)