http-request.js 13 KB

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