api-service.js 8.1 KB

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