axiosApp.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Vue from "vue";
  2. import axios from "axios";
  3. import { loadProgressBar } from "axios-progress-bar";
  4. import cachingGet from "./axiosCache";
  5. import { getKeyToken, removeKeyToken } from "../auth/auth";
  6. // Full config: https://github.com/axios/axios#request-config
  7. // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
  8. // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  9. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  10. let config = {
  11. // baseURL: process.env.baseURL || process.env.apiUrl || ""
  12. timeout: 60 * 1000, // Timeout
  13. withCredentials: true, // Check cross-site Access-Control
  14. };
  15. const _axiosApp = axios.create(config);
  16. /**
  17. * B. new token lifecycle
  18. * 1. 任何接口地址都尝试加上 key & token (第三方请求,单独做新的$http)
  19. * 2. 如果没有 key & token,就当做这是无需认证的api
  20. * 3. 做好token失效的处理,在interceptors.response 统一删除
  21. * 4. key & token 由外部管理,不在此插件管理。一般是在登录成功后设置。
  22. * xx. 额外:通过js-cookie将key等存入cookie
  23. */
  24. _axiosApp.interceptors.request.use(
  25. function(config) {
  26. if (config.url.endsWith("Login") === false) {
  27. const [wk_token, wk_key] = getKeyToken();
  28. if (wk_token) {
  29. config.headers.common["token"] = wk_token;
  30. config.headers.common["key"] = wk_key;
  31. }
  32. }
  33. return config;
  34. },
  35. function(error) {
  36. // Do something with request error
  37. Vue.prototype.$notify({
  38. showClose: true,
  39. message: error,
  40. type: "error",
  41. });
  42. return Promise.reject(error);
  43. }
  44. );
  45. // Add a response interceptor
  46. _axiosApp.interceptors.response.use(
  47. response => {
  48. return response;
  49. },
  50. error => {
  51. if (!error.response) {
  52. // "Network Error" 网络不通,直接返回
  53. Vue.prototype.$notify({
  54. showClose: true,
  55. message: "网络连接异常,请检查网络设置。",
  56. type: "error",
  57. });
  58. return Promise.reject(error);
  59. }
  60. // 这里是返回状态码不为200时候的错误处理
  61. let status = error.response.status;
  62. // 登录失效 跳转登录页面
  63. if (status == 403 || status == 401) {
  64. if (
  65. window.___lastInvalidDate === undefined ||
  66. window.___lastInvalidDate < Date.now() - 300
  67. ) {
  68. Vue.prototype.$notify({
  69. showClose: true,
  70. message: "登录失效,请重新登录!",
  71. type: "error",
  72. });
  73. window.___lastInvalidDate = Date.now();
  74. }
  75. removeKeyToken();
  76. return Promise.reject(error);
  77. } else if (status == 405) {
  78. Vue.prototype.$notify({
  79. showClose: true,
  80. message: "没有权限!",
  81. type: "error",
  82. });
  83. return Promise.reject(error);
  84. } else if (status == 502) {
  85. Vue.prototype.$notify({
  86. showClose: true,
  87. message: "服务器异常(502)!",
  88. type: "error",
  89. });
  90. return Promise.reject(error);
  91. }
  92. if (status != 200) {
  93. const data = error.response.data;
  94. if (data && data.desc) {
  95. Vue.prototype.$notify({
  96. showClose: true,
  97. message: data.desc,
  98. type: "error",
  99. });
  100. } else {
  101. Vue.prototype.$notify({
  102. showClose: true,
  103. message: "未定义异常: " + JSON.stringify(data, 2),
  104. type: "error",
  105. });
  106. }
  107. return Promise.reject(error);
  108. }
  109. }
  110. );
  111. _axiosApp.get = cachingGet(_axiosApp.get, []);
  112. loadProgressBar(_axiosApp);
  113. Plugin.install = function(Vue) {
  114. Object.defineProperties(Vue.prototype, {
  115. $http: {
  116. get() {
  117. return _axiosApp;
  118. },
  119. },
  120. });
  121. };
  122. Vue.use(Plugin);
  123. export default Plugin;
  124. export const httpApp = _axiosApp;