| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import axios from 'axios';
- import type { AxiosRequestConfig, AxiosResponse } from 'axios';
- import { Message, Modal } from '@arco-design/web-vue';
- import { useUserStore } from '@/store';
- import { getToken } from '@/utils/auth';
- export interface HttpResponse<T = unknown> {
- status: number;
- msg: string;
- code: number;
- data: T;
- }
- if (import.meta.env.VITE_API_BASE_URL) {
- axios.defaults.baseURL = import.meta.env.VITE_API_BASE_URL;
- }
- let API_SCOPE = 'admin';
- if (import.meta.env.VITE_API_BASE_SCOPE) {
- API_SCOPE = import.meta.env.VITE_API_BASE_SCOPE;
- }
- axios.interceptors.request.use(
- (config: AxiosRequestConfig) => {
- // let each request carry token
- // this example using the JWT token
- // Authorization is a custom headers key
- // please modify it according to the actual situation
- const token = getToken();
- if (token) {
- if (!config.headers) {
- config.headers = {
- };
- }
- if (API_SCOPE) {
- config.headers.scope = API_SCOPE;
- }
- config.headers.Authorization = `Bearer ${token}`;
- }
- return config;
- },
- (error) => {
- // do something
- return Promise.reject(error);
- }
- );
- // add response interceptors
- axios.interceptors.response.use(
- (response: AxiosResponse<HttpResponse>) => {
- const res = response.data;
- // if the custom code is not 20000, it is judged as an error.
- if (res.code !== 200) {
- Message.error({
- content: res.msg || 'Error',
- duration: 5 * 1000,
- });
- // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
- if (
- [401].includes(res.code) &&
- response.config.url !== '/api/user/info'
- ) {
- Modal.error({
- title: 'Confirm logout',
- content:
- 'You have been logged out, you can cancel to stay on this page, or log in again',
- okText: 'Re-Login',
- async onOk() {
- const userStore = useUserStore();
- await userStore.logout();
- window.location.reload();
- },
- });
- }
- return Promise.reject(new Error(res.msg || 'Error'));
- }
- return res;
- },
- (error) => {
- const result = error.response;
- if (error.response) {
- if (error.response.request.responseType === 'arraybuffer' && error.response.data.toString() === '[object ArrayBuffer]') {
- const text = Buffer.from(error.response.data).toString('utf8');
- result.data = JSON.parse(text);
- }
- } else {
- Message.error({
- content: error.msg || 'Request Error',
- duration: 5 * 1000,
- });
- return Promise.reject(error);
- }
- if (result.data.code !== 200) {
- const url = result.data.config?.url
- // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
- if (result.data.code === 401 && url !== '/api/user/info') {
- Modal.error({
- title: 'Confirm logout',
- content:
- 'You have been logged out, you can cancel to stay on this page, or log in again',
- okText: 'Re-Login',
- async onOk() {
- const userStore = useUserStore();
- await userStore.logout();
- window.location.reload();
- },
- });
- } else {
- Message.error({
- content: result.data.msg || 'Error',
- duration: 5 * 1000,
- });
- }
- }
- if (result && typeof (result.data.code) !== 'undefined') {
- const err = new Error(result.data.msg);
- Message.error({
- content: err.message || 'Request Error',
- duration: 5 * 1000,
- });
- return Promise.reject(err);
- }
- Message.error({
- content: error.msg || 'Request Error',
- duration: 5 * 1000,
- });
- return Promise.reject(error);
- }
- );
|