interceptor.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import axios from 'axios';
  2. import type { AxiosRequestConfig, AxiosResponse } from 'axios';
  3. import { Message, Modal } from '@arco-design/web-vue';
  4. import { useUserStore } from '@/store';
  5. import {
  6. getAuthorization,
  7. getToken,
  8. setAuthorization,
  9. setToken,
  10. } from '@/utils/auth';
  11. export interface HttpResponse<T = unknown> {
  12. status: number;
  13. msg: string;
  14. code: number;
  15. data: T;
  16. }
  17. if (import.meta.env.VITE_API_BASE_URL) {
  18. // axios.defaults.baseURL = import.meta.env.VITE_API_BASE_URL;
  19. }
  20. axios.defaults.withCredentials = true;
  21. axios.interceptors.request.use(
  22. (config: AxiosRequestConfig) => {
  23. // let each request carry token
  24. // this example using the JWT token
  25. // Authorization is a custom headers key
  26. // please modify it according to the actual situation
  27. // const token = getToken();
  28. // if (token) {
  29. // if (!config.headers) {
  30. // config.headers = {};
  31. // }
  32. // config.headers.Authorization = `Bearer ${token}`;
  33. // }
  34. const authorization = getAuthorization();
  35. if (authorization) {
  36. if (!config.headers) {
  37. config.headers = {};
  38. }
  39. config.headers.Authorization = `${authorization}`;
  40. }
  41. config.headers.Authorization = 'ImE4NGQ4ZWNjNGVmYjExZWZiZjRkMDI0MmFjMTIwMDA2Ig.ZqnIBw.ZX7_UxnSBKu8x_riQ5FrHLMAS78'
  42. return config;
  43. },
  44. (error) => {
  45. // do something
  46. return Promise.reject(error);
  47. }
  48. );
  49. // add response interceptors
  50. axios.interceptors.response.use(
  51. (response: AxiosResponse<HttpResponse>) => {
  52. const res = response.data;
  53. // if the custom code is not 20000, it is judged as an error.
  54. if (
  55. (res.retcode && res.retcode !== 0) ||
  56. (res.code && res.code !== 20000)
  57. ) {
  58. Message.error({
  59. content: res.msg || 'Error',
  60. duration: 5 * 1000,
  61. });
  62. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  63. if (
  64. [50008, 50012, 50014].includes(res.code) &&
  65. response.config.url !== '/api/user/info'
  66. ) {
  67. Modal.error({
  68. title: 'Confirm logout',
  69. content:
  70. 'You have been logged out, you can cancel to stay on this page, or log in again',
  71. okText: 'Re-Login',
  72. async onOk() {
  73. const userStore = useUserStore();
  74. await userStore.logout();
  75. window.location.reload();
  76. },
  77. });
  78. }
  79. return Promise.reject(new Error(res.msg || 'Error'));
  80. }
  81. if (
  82. response.config.url === '/v1/user/login' ||
  83. response.config.url === '/base/login'
  84. ) {
  85. setAuthorization(response.headers.authorization);
  86. }
  87. return res;
  88. },
  89. (error) => {
  90. Message.error({
  91. content: error.msg || 'Request Error',
  92. duration: 5 * 1000,
  93. });
  94. return Promise.reject(error);
  95. }
  96. );