http-request.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. function httpGet(url, options) {
  39. //发送ajax请求
  40. return new Promise(function(resolve, reject) {
  41. $.ajax(server + url,
  42. $.extend({}, {
  43. type: 'GET',
  44. dataType: 'JSON',
  45. beforeSend: function(request) {
  46. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  47. if (oauthInfo) {
  48. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  49. request.setRequestHeader("token", oauthInfo.accessToken);
  50. }
  51. },
  52. error: function(res) {
  53. reject(res)
  54. },
  55. success: function(res) {
  56. failCodeHandle(res)
  57. resolve(res)
  58. }
  59. }, options));
  60. })
  61. }
  62. function httpPost(url, options) {
  63. //发送ajax请求
  64. return new Promise(function(resolve, reject) {
  65. $.ajax(server + url,
  66. $.extend({}, {
  67. type: 'POST',
  68. dataType: 'JSON',
  69. beforeSend: function(request) {
  70. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  71. if (oauthInfo) {
  72. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  73. request.setRequestHeader("token", oauthInfo.accessToken);
  74. }
  75. },
  76. error: function(res) {
  77. reject(res)
  78. },
  79. success: function(res) {
  80. failCodeHandle(res)
  81. resolve(res)
  82. }
  83. }, options));
  84. })
  85. }
  86. //调用福州总部的接口的方法
  87. function fzHttpPost(url, options){
  88. return new Promise(function(resolve, reject) {
  89. var url2 = "/basic/api/v1.0/fz/open/api",
  90. params = {
  91. apiUrl: url, //相对接口路径,不用"/"开头
  92. paramsJson: JSON.stringify(options)
  93. }
  94. $.ajax(server + url2, {
  95. type: 'POST',
  96. data: params,
  97. dataType: 'JSON',
  98. error: function(res) {
  99. console.log("error", res);
  100. reject(res)
  101. },
  102. success: function(res) {
  103. console.log("success", res)
  104. failCodeHandle(res)
  105. resolve(res)
  106. }
  107. });
  108. })
  109. }
  110. function imHttpGet(url, options) {
  111. //发送ajax请求
  112. return new Promise(function(resolve, reject) {
  113. $.ajax(imserver + url,
  114. $.extend({}, {
  115. type: 'GET',
  116. dataType: 'JSON',
  117. beforeSend: function(request) {
  118. // request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  119. },
  120. error: function(res) {
  121. reject(res)
  122. },
  123. success: function(res) {
  124. failCodeHandle(res)
  125. resolve(res)
  126. }
  127. }, options));
  128. })
  129. }
  130. function imHttpPost(url, options) {
  131. //发送ajax请求
  132. return new Promise(function(resolve, reject) {
  133. $.ajax(imserver + url,
  134. $.extend({}, {
  135. type: 'POST',
  136. dataType: 'JSON',
  137. beforeSend: function(request) {
  138. // request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  139. },
  140. error: function(res) {
  141. reject(res)
  142. },
  143. success: function(res) {
  144. failCodeHandle(res)
  145. resolve(res)
  146. }
  147. }, options));
  148. })
  149. }
  150. function toLoginPage() {
  151. if(location.href.indexOf('login.html') > -1) {
  152. return;
  153. }
  154. setTimeout(function() {
  155. sessionStorage.setItem("wlyy_relogin", 1)
  156. // var path = top.location.pathname,
  157. // rootPath = path.split("/")[1];
  158. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  159. }, 2000)
  160. }
  161. var count = 0;
  162. function failCodeHandle(res) {
  163. var tip = "";
  164. if(res.status == 999) {
  165. tip = "此账号已在别处登录,请重新登录";
  166. } else if(res.status == 998) {
  167. tip = "登录超时,请重新登录";
  168. } else if(res.status == 997) {
  169. tip = "此账号未登录,请先登录"
  170. }
  171. if(tip) {
  172. count ++;
  173. if(count == 1){
  174. // toastr && toastr.warning(tip)
  175. toLoginPage()
  176. }
  177. }
  178. }
  179. function uuid(len, radix) {
  180. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  181. var uuid = [],
  182. i;
  183. radix = radix || chars.length;
  184. if(len) {
  185. // Compact form
  186. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  187. } else {
  188. // rfc4122, version 4 form
  189. var r;
  190. // rfc4122 requires these characters
  191. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  192. uuid[14] = '4';
  193. // Fill in random data. At i==19 set the high bits of clock sequence as
  194. // per rfc4122, sec. 4.1.5
  195. for(i = 0; i < 36; i++) {
  196. if(!uuid[i]) {
  197. r = 0 | Math.random() * 16;
  198. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  199. }
  200. }
  201. }
  202. return uuid.join('');
  203. }
  204. /*
  205. * 获取图片路径方法修改
  206. */
  207. function getImgUrl(str) {
  208. if(typeof str != 'string') {
  209. return "";
  210. }
  211. if(str.length == 0) {
  212. return "";
  213. } else {
  214. if(str.indexOf("../") > -1) {
  215. //访问本地路径
  216. return str;
  217. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  218. return str;
  219. } else {
  220. //服务器上的图片路径
  221. return imgServer + str;
  222. }
  223. }
  224. }
  225. function httpPostContent(url, options) {
  226. //发送ajax请求
  227. return new Promise(function (resolve, reject) {
  228. $.ajax(server + url,
  229. $.extend({}, {
  230. type: 'POST',
  231. beforeSend: function (request) {
  232. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  233. if (oauthInfo) {
  234. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  235. request.setRequestHeader("token", oauthInfo.accessToken);
  236. }
  237. },
  238. error: function (xhr, type, errorThrown) {
  239. reject(xhr, type, errorThrown)
  240. },
  241. success: function (res) {
  242. resolve(res)
  243. }
  244. }, options));
  245. })
  246. }
  247. function httpPutContent(url, options) { //加contentType限制
  248. //发送ajax请求
  249. return new Promise(function (resolve, reject) {
  250. $.ajax(server + url,
  251. $.extend({}, {
  252. type: 'PUT',
  253. dataType: 'JSON',
  254. contentType:'application/json; charset=utf-8',
  255. beforeSend: function (request) {
  256. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  257. if (oauthInfo) {
  258. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  259. request.setRequestHeader("token", oauthInfo.accessToken);
  260. }
  261. },
  262. error: function (xhr, type, errorThrown) {
  263. errTip(xhr.status);
  264. reject(xhr, type, errorThrown)
  265. },
  266. success: function (res) {
  267. resolve(res)
  268. },
  269. }, options));
  270. })
  271. }
  272. httpRequest = {
  273. agentName: agentName,
  274. server: server,
  275. imserver: imserver,
  276. socketUrl: socketUrl,
  277. // userAgent: userAgent,
  278. get: httpGet,
  279. post: httpPost,
  280. imHttpGet: imHttpGet,
  281. imHttpPost: imHttpPost,
  282. fzPost: fzHttpPost,
  283. getImgUrl: getImgUrl,
  284. grant_type: grant_type,
  285. client_id: client_id,
  286. imgServer:imgServer,
  287. failCodeHandle: failCodeHandle,
  288. httpPostContent:httpPostContent,
  289. httpPutContent:httpPutContent,
  290. loginIm: function(data){
  291. return imHttpPost('/users/login', data);
  292. },
  293. getDoctorInfo: function() {
  294. return httpGet('doctor/baseinfo');
  295. }
  296. }
  297. window.httpRequest = httpRequest;
  298. })(jQuery)