| | |
| | | import type { AxiosRequestConfig, AxiosResponse } from 'axios'; |
| | | import { Message, Modal } from '@arco-design/web-vue'; |
| | | import { useUserStore } from '@/store'; |
| | | import { getAuthorization, getToken, setAuthorization } from "@/utils/auth"; |
| | | import { |
| | | getAuthorization, |
| | | getToken, |
| | | setAuthorization, |
| | | setToken, |
| | | } from '@/utils/auth'; |
| | | |
| | | export interface HttpResponse<T = unknown> { |
| | | status: number; |
| | |
| | | } |
| | | |
| | | if (import.meta.env.VITE_API_BASE_URL) { |
| | | axios.defaults.baseURL = import.meta.env.VITE_API_BASE_URL; |
| | | // axios.defaults.baseURL = import.meta.env.VITE_API_BASE_URL; |
| | | } |
| | | |
| | | axios.defaults.withCredentials = true; |
| | | axios.interceptors.request.use( |
| | | (config: AxiosRequestConfig) => { |
| | | // let each request carry token |
| | |
| | | // config.headers.Authorization = `Bearer ${token}`; |
| | | // } |
| | | |
| | | |
| | | const authorization = getAuthorization(); |
| | | if (authorization) { |
| | | if (!config.headers) { |
| | | config.headers = {}; |
| | | } |
| | | config.headers.Authorization = authorization; |
| | | config.headers.token = getToken(); |
| | | config.headers.Authorization = `${authorization}`; |
| | | } |
| | | config.headers.Authorization = |
| | | 'ImE4NGQ4ZWNjNGVmYjExZWZiZjRkMDI0MmFjMTIwMDA2Ig.ZqnIBw.ZX7_UxnSBKu8x_riQ5FrHLMAS78'; |
| | | return config; |
| | | }, |
| | | (error) => { |
| | |
| | | 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.retcode && res.retcode !== 0) || (res.code && res.code !== 20000)) { |
| | | /*if ( |
| | | (res.retcode && res.retcode !== 0) || |
| | | (res.code && res.code !== 20000 && res.code !== 200) |
| | | ) { |
| | | Message.error({ |
| | | content: res.msg || 'Error', |
| | | duration: 5 * 1000, |
| | |
| | | okText: 'Re-Login', |
| | | async onOk() { |
| | | const userStore = useUserStore(); |
| | | |
| | | await userStore.logout(); |
| | | window.location.reload(); |
| | | }, |
| | |
| | | return Promise.reject(new Error(res.msg || 'Error')); |
| | | } |
| | | |
| | | if(response.config.url === '/v1/user/login') { |
| | | if ( |
| | | response.config.url === '/v1/user/login' || |
| | | response.config.url === '/base/login' |
| | | ) { |
| | | setAuthorization(response.headers.authorization); |
| | | } |
| | | }*/ |
| | | return res; |
| | | }, |
| | | (error) => { |