| | |
| | | import axios from 'axios' |
| | | import axiosClient from 'axios' |
| | | import type { AxiosRequestConfig } from 'axios' |
| | | import { ElMessage } from 'element-plus' |
| | | import { getToken } from '@/common/utils/index' |
| | | |
| | | const Axios = axios.create({ |
| | | const instance = axiosClient.create({ |
| | | responseType: 'json', |
| | | withCredentials: true // 允许携带 cookie |
| | | }) |
| | |
| | | /** |
| | | * 请求拦截器 |
| | | */ |
| | | Axios.interceptors.request.use( |
| | | instance.interceptors.request.use( |
| | | (config) => { |
| | | // 若是有做鉴权token , 就给头部带上token |
| | | const token = getToken() |
| | |
| | | /** |
| | | * 响应拦截器 |
| | | */ |
| | | Axios.interceptors.response.use( |
| | | instance.interceptors.response.use( |
| | | (res) => { |
| | | if (res.data.code == 200) { |
| | | return res.data ?? {} |
| | | return res.data |
| | | } else { |
| | | ElMessage({ |
| | | message: res.data.msg, |
| | | type: 'error', |
| | | duration: 3 * 1000 |
| | | }) |
| | | return res.data ?? {} |
| | | return Promise.reject(res?.data) |
| | | } |
| | | }, |
| | | (error) => { |
| | |
| | | } |
| | | ) |
| | | |
| | | export default Axios |
| | | const axios = <T>(cfg: AxiosRequestConfig) => instance.request<any, T>(cfg) |
| | | |
| | | export default axios |