http-request.js 11 KB

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