api-service.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. (function($) {
  2. var publish_version = false;
  3. var isInner = false; //发布线上后有内外网的配置
  4. var agentName = "wlyyAgentForDoc";
  5. var server, userAgent, loginUrl, imserver, socketUrl, imgServer;
  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/wlyy/"
  11. imserver = "http://27.155.101.77:3000/api/v2";
  12. socketUrl = "http://27.155.101.77:3000";
  13. loginUrl = "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";
  20. imgServer = "http://10.95.22.140:23001/";
  21. }
  22. } else { // 测试环境配置
  23. server = "http://172.19.103.88:9092/wlyy/"
  24. imserver = "http://172.19.103.88:3000/api/v2";
  25. socketUrl = "http://172.19.103.88:3000";
  26. // server = "http://192.168.131.127:8060/"; //逸祥
  27. // server = "http://192.168.131.24:8080/"; //仕杰
  28. // imserver = "http://192.168.131.24:3000/api/v2"
  29. // socketUrl = "http://192.168.131.24:3000";
  30. loginUrl = "login.html";
  31. imgServer = "http://172.19.103.54/";
  32. }
  33. userAgent = localStorage.getItem(agentName)
  34. //hard code userAgent for test start
  35. //全科
  36. // userAgent = {"id":4800,"uid":"zbqD201703150222","imei":"864394010176834","token":"9b23e4ec0bcd3b9fa72a52f91544a4e9","platform":4,"hospital":"3502050100"}
  37. //健管
  38. // userAgent = {"id":4821,"uid":"zbqD201703150228","imei":"864394010176834","token":"73f5ace5c7d4cdb811f743902fd705d9","platform":4,"hospital":"3502050100"};
  39. // userAgent = JSON.stringify(userAgent);
  40. //hard code userAgent for test end
  41. if(userAgent) {
  42. try {
  43. userAgent = JSON.parse(userAgent)
  44. } catch(e) {
  45. toastr && toastr.warning("登录失效,请重新登录")
  46. toLoginPage()
  47. }
  48. } else {
  49. toLoginPage()
  50. }
  51. var isRelogined = sessionStorage.getItem('wlyy_relogin')
  52. // if(isRelogined && top !== window && location.href.indexOf("login.html") < 0) {
  53. if(isRelogined && location.href.indexOf("login.html") < 0){
  54. sessionStorage.removeItem('wlyy_relogin')
  55. top.location.reload(true)
  56. }
  57. function httpGet(url, options) {
  58. //发送ajax请求
  59. return new Promise(function(resolve, reject) {
  60. $.ajax(server + url,
  61. $.extend({}, {
  62. type: 'GET',
  63. dataType: 'JSON',
  64. beforeSend: function(request) {
  65. var agent = userAgent || {
  66. imei: localStorage.getItem('WLYY_IMEI'),
  67. platform: 4
  68. }
  69. request.setRequestHeader("userAgent", JSON.stringify(agent));
  70. },
  71. error: function(res) {
  72. reject(res)
  73. },
  74. success: function(res) {
  75. failCodeHandle(res)
  76. resolve(res)
  77. }
  78. }, options));
  79. })
  80. }
  81. function httpPost(url, options) {
  82. //发送ajax请求
  83. return new Promise(function(resolve, reject) {
  84. $.ajax(server + url,
  85. $.extend({}, {
  86. type: 'POST',
  87. dataType: 'JSON',
  88. beforeSend: function(request) {
  89. var agent = userAgent || {
  90. imei: localStorage.getItem('WLYY_IMEI')
  91. }
  92. request.setRequestHeader("userAgent", JSON.stringify(agent));
  93. },
  94. error: function(res) {
  95. reject(res)
  96. },
  97. success: function(res) {
  98. failCodeHandle(res)
  99. resolve(res)
  100. }
  101. }, options));
  102. })
  103. }
  104. function imHttpGet(url, options) {
  105. //发送ajax请求
  106. return new Promise(function(resolve, reject) {
  107. $.ajax(imserver + url,
  108. $.extend({}, {
  109. type: 'GET',
  110. dataType: 'JSON',
  111. beforeSend: function(request) {
  112. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  113. },
  114. error: function(res) {
  115. reject(res)
  116. },
  117. success: function(res) {
  118. failCodeHandle(res)
  119. resolve(res)
  120. }
  121. }, options));
  122. })
  123. }
  124. function imHttpPost(url, options) {
  125. //发送ajax请求
  126. return new Promise(function(resolve, reject) {
  127. $.ajax(imserver + url,
  128. $.extend({}, {
  129. type: 'POST',
  130. dataType: 'JSON',
  131. beforeSend: function(request) {
  132. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  133. },
  134. error: function(res) {
  135. reject(res)
  136. },
  137. success: function(res) {
  138. failCodeHandle(res)
  139. resolve(res)
  140. }
  141. }, options));
  142. })
  143. }
  144. function toLoginPage() {
  145. if(location.href.indexOf('login.html') > -1) {
  146. return;
  147. }
  148. setTimeout(function() {
  149. sessionStorage.setItem("wlyy_relogin", 1)
  150. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  151. }, 2000)
  152. }
  153. var count = 0;
  154. function failCodeHandle(res) {
  155. var tip = "";
  156. if(res.status == 999) {
  157. tip = "此账号已在别处登录,请重新登录";
  158. } else if(res.status == 998) {
  159. tip = "登录超时,请重新登录";
  160. } else if(res.status == 997) {
  161. tip = "此账号未登录,请先登录"
  162. }
  163. if(tip) {
  164. count ++;
  165. if(count == 1){
  166. toastr && toastr.warning(tip)
  167. toLoginPage()
  168. }
  169. }
  170. }
  171. function uuid(len, radix) {
  172. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  173. var uuid = [],
  174. i;
  175. radix = radix || chars.length;
  176. if(len) {
  177. // Compact form
  178. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  179. } else {
  180. // rfc4122, version 4 form
  181. var r;
  182. // rfc4122 requires these characters
  183. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  184. uuid[14] = '4';
  185. // Fill in random data. At i==19 set the high bits of clock sequence as
  186. // per rfc4122, sec. 4.1.5
  187. for(i = 0; i < 36; i++) {
  188. if(!uuid[i]) {
  189. r = 0 | Math.random() * 16;
  190. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  191. }
  192. }
  193. }
  194. return uuid.join('');
  195. }
  196. /*
  197. * 获取图片路径方法修改
  198. */
  199. function getImgUrl(str) {
  200. if(typeof str != 'string') {
  201. return "";
  202. }
  203. if(str.length == 0) {
  204. return "";
  205. } else {
  206. if(str.indexOf("../") > -1) {
  207. //访问本地路径
  208. return str;
  209. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  210. return str;
  211. } else {
  212. //服务器上的图片路径
  213. return imgServer + str;
  214. }
  215. }
  216. }
  217. APIService = {
  218. agentName: agentName,
  219. server: server,
  220. imserver: imserver,
  221. socketUrl: socketUrl,
  222. userAgent: userAgent,
  223. httpGet: httpGet,
  224. httpPost: httpPost,
  225. imHttpGet: imHttpGet,
  226. imHttpPost: imHttpPost,
  227. getImgUrl: getImgUrl,
  228. loginIm: function(data){
  229. return imHttpPost('/users/login', data);
  230. },
  231. getDoctorInfo: function() {
  232. return httpGet('doctor/baseinfo');
  233. },
  234. getTeamInfo: function(doctor) {
  235. return httpGet('/doctor/admin-teams/team/' + doctor + '/teams');
  236. }
  237. }
  238. window.APIService = APIService;
  239. })(jQuery)