api-service.js 7.8 KB

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