import axios from 'axios' import { decodeResponseParams } from './decode' import { useUserStore } from '@/stores/user' const { VITE_GLOB_API_URL } = import.meta.env const service = axios.create({ baseURL: import.meta.env.MODE === 'development' ? '/api' : VITE_GLOB_API_URL, withCredentials: true }) // 请求拦截 service.interceptors.request.use( (config) => { const user = useUserStore() let url = config.url if (config.params) { if (!config.params.Token) config.params.Token = user.getToken url += '?' const keys = Object.keys(config.params) for (const key of keys) { try { url += `${key}=${decodeURIComponent(config.params[key])}&` } catch (e) { throw new Error('不受支持的字符') } } url = url?.substring(0, url?.length - 1) config.params = {} } config.url = url // config.headers = { // 'Content-Type': 'multipart/form-data', // }; return config }, (error) => { console.error(error) return Promise.reject(error) } ) service.interceptors.response.use( (response) => { const { data } = response // if (!!data && typeof data === 'string') { // if (data.startsWith('err@')) { // //const errMsg = data.replace('err@', ''); // if (data.indexOf('err@登录信息丢失') != -1) { // setTimeout(() => { // // @ts-ignore // window?.top?.vm?.logoutExt() // }) // } // return Promise.reject(data.replace('err@', '')) // } // if (data.startsWith('url@')) { // return decodeResponseParams(data) // // window.location.href = data.replace('url@', ''); // // return Promise.resolve(); // } // try { data.data = JSON.parse(data.data); return data; // } catch (_) { // return data // } // } // return data }, (error) => { return Promise.reject(error) } ) export default service