axiosCommonService.js 3.5 KB

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