api-service.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. (function($){
  2. var publish_version = false;
  3. var agentName = "wlyyAgent";
  4. var server, userAgent, loginUrl;
  5. $.support.cors = true;
  6. if(publish_version) { // 生产环境配置
  7. } else { // 测试环境配置
  8. server = "http://172.19.103.88:9092/wlyy/"
  9. // server = "http://192.168.131.127:8060/"; //逸祥
  10. // server = "http://192.168.131.24:8080/"; //仕杰
  11. loginUrl = "http://172.19.103.88:9092/wlyy/auth/login.html"
  12. }
  13. userAgent = localStorage.getItem(agentName)
  14. //hard code userAgent for test
  15. //全科
  16. // userAgent = {"id":4800,"uid":"zbqD201703150222","imei":"864394010176834","token":"8a987068ce47f9709080eda4897e25a1","platform":2,"hospital":"3502050100"};
  17. //健管
  18. // userAgent = {"id":4821,"uid":"zbqD201703150228","imei":"864394010176834","token":"73f5ace5c7d4cdb811f743902fd705d9","platform":2,"hospital":"3502050100"};
  19. // userAgent = JSON.stringify(userAgent);
  20. if(userAgent) {
  21. try{
  22. userAgent = JSON.parse(userAgent)
  23. }catch(e){
  24. location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
  25. }
  26. } else {
  27. location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
  28. }
  29. function httpGet(url,options) {
  30. //发送ajax请求
  31. return new Promise(function(resolve, reject) {
  32. $.ajax(server + url,
  33. $.extend({},{
  34. type: 'GET',
  35. dataType: 'JSON',
  36. beforeSend: function(request) {
  37. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  38. },
  39. error: function(res) {
  40. reject(res)
  41. },
  42. success: function(res) {
  43. failCodeHandle(res)
  44. resolve(res)
  45. }
  46. },options));
  47. })
  48. }
  49. function httpPost(url,options) {
  50. //发送ajax请求
  51. return new Promise(function(resolve, reject) {
  52. $.ajax(server + url,
  53. $.extend({},{
  54. type: 'POST',
  55. dataType: 'JSON',
  56. beforeSend: function(request) {
  57. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  58. },
  59. error: function(res) {
  60. reject(res)
  61. },
  62. success: function(res) {
  63. failCodeHandle(res)
  64. resolve(res)
  65. }
  66. },options));
  67. })
  68. }
  69. function failCodeHandle(res) {
  70. var tip = "";
  71. if(res.status == 999) {
  72. tip = "此账号已在别处登录,请重新登录";
  73. } else if(res.status == 998) {
  74. tip = "登录超时,请重新登录";
  75. } else if(res.status == 997) {
  76. tip = "此账号未登录,请先登录"
  77. }
  78. if(tip) {
  79. toastr && toastr.warning(tip)
  80. location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
  81. }
  82. }
  83. APIService = {
  84. server: server,
  85. userAgent: userAgent,
  86. httpGet: httpGet,
  87. httpPost: httpPost,
  88. getDoctorInfo: function(){
  89. return httpGet('doctor/baseinfo');
  90. },
  91. getTeamInfo: function(doctor){
  92. return httpGet('/doctor/admin-teams/team/'+doctor+'/teams');
  93. }
  94. }
  95. window.APIService = APIService;
  96. })(jQuery)