api-service.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. loginUrl = "http://172.19.103.88:9092/wlyy/auth/login.html"
  10. }
  11. userAgent = localStorage.getItem(agentName)
  12. if(userAgent) {
  13. try{
  14. userAgent = JSON.parse(userAgent)
  15. }catch(e){
  16. location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
  17. }
  18. } else {
  19. location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
  20. }
  21. // var server = "http://172.19.103.88:9092/wlyy/",
  22. // userAgent = {"id":4800,"uid":"zbqD201703150222","imei":"864394010176834","token":"fcf8310ee6d911c3f25cdecd28b9bcdd","platform":2,"hospital":"3502050100","appVersion":"1.3.6"};
  23. // userAgent = JSON.stringify(userAgent);
  24. function httpGet(url,options) {
  25. //发送ajax请求
  26. return new Promise(function(resolve, reject) {
  27. $.ajax(server + url,
  28. $.extend({},{
  29. type: 'GET',
  30. dataType: 'JSON',
  31. beforeSend: function(request) {
  32. request.setRequestHeader("userAgent", userAgent);
  33. },
  34. error: function(res) {
  35. reject(res)
  36. },
  37. success: function(res) {
  38. failCodeHandle(res)
  39. resolve(res)
  40. }
  41. },options));
  42. })
  43. }
  44. function httpPost(url,options) {
  45. //发送ajax请求
  46. return new Promise(function(resolve, reject) {
  47. $.ajax(server + url,
  48. $.extend({},{
  49. type: 'POST',
  50. dataType: 'JSON',
  51. beforeSend: function(request) {
  52. request.setRequestHeader("userAgent", userAgent);
  53. },
  54. error: function(res) {
  55. reject(res)
  56. },
  57. success: function(res) {
  58. failCodeHandle(res)
  59. resolve(res)
  60. }
  61. },options));
  62. })
  63. }
  64. function failCodeHandle(res) {
  65. var tip = "";
  66. if(res.status == 999) {
  67. tip = "此账号已在别处登录,请重新登录";
  68. } else if(res.status == 998) {
  69. tip = "登录超时,请重新登录";
  70. } else if(res.status == 997) {
  71. tip = "此账号未登录,请先登录"
  72. }
  73. if(tip) {
  74. toastr && toastr.warning(tip)
  75. location.replace(loginUrl+'?redirect_url='+encodeURIComponent(location.href))
  76. }
  77. }
  78. APIService = {
  79. server: server,
  80. httpGet: httpGet,
  81. httpPost: httpPost,
  82. getTeamInfo: function(doctor,data){
  83. return httpGet('/doctor/admin-teams/team/'+doctor+'/teams', data);
  84. }
  85. }
  86. window.APIService = APIService;
  87. })(jQuery)