This commit is contained in:
李志鹏
2025-10-27 11:52:42 +08:00
9 changed files with 282 additions and 229 deletions

View File

@@ -6,20 +6,31 @@ import { getLocal } from '@/utils/local'
import router from '@/router/index'
import { useOverallStore, useGenerateStore } from '@/stores'
// 扩展 AxiosRequestConfig 接口
declare module 'axios' {
interface AxiosRequestConfig {
loading?: boolean
loadingDom?: any
repeatRequest?: boolean
meta?: {
responseAll?: boolean
}
}
}
// 创建axios实例
// console.log(import.meta.env,123)
const service = axios.create({
// baseURL: import.meta.env.VITE_APP_URL, // api的base_url
timeout: 20000, // 请求超时时间
// baseURL: import.meta.env.VITE_APP_URL, // api的base_url
timeout: 20000 // 请求超时时间
})
if(import.meta.env.MODE != 'development'){
if (import.meta.env.MODE != 'development') {
service.defaults.baseURL = import.meta.env.VITE_APP_URL
}
axios.defaults.headers.post["Content-Type"] = "application/json";
axios.defaults.headers.post['lang'] = 'en'; //配置语言请求头
axios.defaults.withCredentials = true; //跨域携带cookie
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers.post['lang'] = 'en' //配置语言请求头
axios.defaults.withCredentials = true //跨域携带cookie
// request拦截器
service.interceptors.request.use(
@@ -36,9 +47,9 @@ service.interceptors.request.use(
}
// 如果登录了有token则请求携带token
// Do something before request is sent
if (store.token) {
if (store.state.token) {
config.headers.Authorization = getLocal('token') // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
// config.headers['X-Token'] = getLocal('token') // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
// config.headers['X-Token'] = getLocal('token') // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
}
return config
},
@@ -65,14 +76,15 @@ service.interceptors.response.use(
}
const res = response.data
// 处理异常的情况
console.log(res)
console.log(res)
if (res.code != 0) {
showToast({
message: res.errMsg,
type: 'fail',
duration: 5000
message: res.errMsg || res.message,
// type: 'fail',
duration: 5000,
position:'top'
})
return Promise.reject(new Error('error'))
} else {
// 默认只返回data不返回状态码和message
@@ -165,14 +177,13 @@ const LoadingInstance: { _count: number } = {
_count: 0
}
function openLoading(loadingDom: any) {
useOverallStore().setLoading(true)
useOverallStore().setLoading(true)
}
function closeLoading() {
if (LoadingInstance._count > 0) LoadingInstance._count--
if (LoadingInstance._count === 0) {
useOverallStore().setLoading(false)
useOverallStore().setLoading(false)
}
}
export default service