doctor-http-request.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/html/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. reject(res)
  101. },
  102. success: function(res) {
  103. failCodeHandle(res)
  104. resolve(res)
  105. }
  106. });
  107. })
  108. }
  109. function imHttpGet(url, options) {
  110. //发送ajax请求
  111. return new Promise(function(resolve, reject) {
  112. $.ajax(imserver + url,
  113. $.extend({}, {
  114. type: 'GET',
  115. dataType: 'JSON',
  116. beforeSend: function(request) {
  117. // request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  118. },
  119. error: function(res) {
  120. reject(res)
  121. },
  122. success: function(res) {
  123. failCodeHandle(res)
  124. resolve(res)
  125. }
  126. }, options));
  127. })
  128. }
  129. function imHttpPost(url, options) {
  130. //发送ajax请求
  131. return new Promise(function(resolve, reject) {
  132. $.ajax(imserver + url,
  133. $.extend({}, {
  134. type: 'POST',
  135. dataType: 'JSON',
  136. beforeSend: function(request) {
  137. // request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  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 toLoginPage() {
  150. if(location.href.indexOf('login.html') > -1||location.href.indexOf('register.html') > -1) {
  151. return;
  152. }
  153. setTimeout(function() {
  154. sessionStorage.setItem("wlyy_relogin", 1)
  155. // var path = top.location.pathname,
  156. // rootPath = path.split("/")[1];
  157. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  158. }, 2000)
  159. }
  160. var count = 0;
  161. function failCodeHandle(res) {
  162. var tip = "";
  163. if(res.status == 999) {
  164. tip = "此账号已在别处登录,请重新登录";
  165. } else if(res.status == 998) {
  166. tip = "登录超时,请重新登录";
  167. } else if(res.status == 997) {
  168. tip = "此账号未登录,请先登录"
  169. }
  170. if(tip) {
  171. count ++;
  172. if(count == 1){
  173. // toastr && toastr.warning(tip)
  174. toLoginPage()
  175. }
  176. }
  177. }
  178. function uuid(len, radix) {
  179. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  180. var uuid = [],
  181. i;
  182. radix = radix || chars.length;
  183. if(len) {
  184. // Compact form
  185. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  186. } else {
  187. // rfc4122, version 4 form
  188. var r;
  189. // rfc4122 requires these characters
  190. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  191. uuid[14] = '4';
  192. // Fill in random data. At i==19 set the high bits of clock sequence as
  193. // per rfc4122, sec. 4.1.5
  194. for(i = 0; i < 36; i++) {
  195. if(!uuid[i]) {
  196. r = 0 | Math.random() * 16;
  197. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  198. }
  199. }
  200. }
  201. return uuid.join('');
  202. }
  203. /*
  204. * 获取图片路径方法修改
  205. */
  206. function getImgUrl(str) {
  207. if(typeof str != 'string') {
  208. return "";
  209. }
  210. if(str.length == 0) {
  211. return "";
  212. } else {
  213. if(str.indexOf("../") > -1) {
  214. //访问本地路径
  215. return str;
  216. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  217. return str;
  218. } else {
  219. //服务器上的图片路径
  220. return imgServer + str;
  221. }
  222. }
  223. }
  224. function httpPostContent(url, options) {
  225. //发送ajax请求
  226. return new Promise(function (resolve, reject) {
  227. $.ajax(server + url,
  228. $.extend({}, {
  229. type: 'POST',
  230. beforeSend: function (request) {
  231. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  232. if (oauthInfo) {
  233. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  234. request.setRequestHeader("token", oauthInfo.accessToken);
  235. }
  236. },
  237. error: function (xhr, type, errorThrown) {
  238. reject(xhr, type, errorThrown)
  239. },
  240. success: function (res) {
  241. resolve(res)
  242. }
  243. }, options));
  244. })
  245. }
  246. function httpPutContent(url, options) { //加contentType限制
  247. //发送ajax请求
  248. return new Promise(function (resolve, reject) {
  249. $.ajax(server + url,
  250. $.extend({}, {
  251. type: 'PUT',
  252. dataType: 'JSON',
  253. contentType:'application/json; charset=utf-8',
  254. beforeSend: function (request) {
  255. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  256. if (oauthInfo) {
  257. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  258. request.setRequestHeader("token", oauthInfo.accessToken);
  259. }
  260. },
  261. error: function (xhr, type, errorThrown) {
  262. reject(xhr, type, errorThrown)
  263. },
  264. success: function (res) {
  265. resolve(res)
  266. },
  267. }, options));
  268. })
  269. }
  270. function httpPut(url, options) {
  271. //发送ajax请求
  272. return new Promise(function (resolve, reject) {
  273. $.ajax(server + url,
  274. $.extend({}, {
  275. type: 'PUT',
  276. dataType: 'JSON',
  277. beforeSend: function (request) {
  278. var oauthInfo = JSON.parse(sessionStorage.getItem("oauthInfo"));
  279. if (oauthInfo) {
  280. request.setRequestHeader("accessToken", oauthInfo.accessToken);
  281. request.setRequestHeader("token", oauthInfo.accessToken);
  282. }
  283. },
  284. error: function (xhr, type, errorThrown) {
  285. reject(xhr, type, errorThrown)
  286. },
  287. success: function (res) {
  288. resolve(res)
  289. },
  290. }, options));
  291. })
  292. }
  293. httpRequest = {
  294. agentName: agentName,
  295. server: server,
  296. imserver: imserver,
  297. socketUrl: socketUrl,
  298. // userAgent: userAgent,
  299. get: httpGet,
  300. post: httpPost,
  301. imHttpGet: imHttpGet,
  302. imHttpPost: imHttpPost,
  303. fzPost: fzHttpPost,
  304. getImgUrl: getImgUrl,
  305. grant_type: grant_type,
  306. client_id: client_id,
  307. imgServer:imgServer,
  308. failCodeHandle: failCodeHandle,
  309. httpPostContent:httpPostContent,
  310. httpPutContent:httpPutContent,
  311. put:httpPut,
  312. loginIm: function(data){
  313. return imHttpPost('/users/login', data);
  314. },
  315. getDoctorInfo: function() {
  316. return httpGet('doctor/baseinfo');
  317. }
  318. }
  319. window.httpRequest = httpRequest;
  320. })(jQuery)