2025-08-12 12:16:47 +08:00
|
|
|
|
import axios from "axios"
|
2025-08-14 16:05:58 +08:00
|
|
|
|
import cache from "./cache"
|
2025-08-12 12:16:47 +08:00
|
|
|
|
import { ElMessage } from "element-plus"
|
2025-08-14 16:05:58 +08:00
|
|
|
|
import router from "@/router"
|
2025-07-17 10:28:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建axios实例
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
|
baseURL: import.meta.env.VITE_APP_BASE_API, // 从环境变量获取基础URL
|
|
|
|
|
|
timeout: 10000 // 请求超时时间
|
2025-08-12 12:16:47 +08:00
|
|
|
|
})
|
2025-07-17 10:28:56 +08:00
|
|
|
|
|
2025-08-14 16:05:58 +08:00
|
|
|
|
//白名单
|
|
|
|
|
|
const excludePath = new Set(["/login"])
|
2025-07-17 10:28:56 +08:00
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
|
(config) => {
|
2025-08-14 16:05:58 +08:00
|
|
|
|
const token = cache.getItem("token")
|
|
|
|
|
|
if (!excludePath.has(config.url) && token && config.headers) {
|
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
|
|
|
}
|
2025-08-12 12:16:47 +08:00
|
|
|
|
return config
|
2025-07-17 10:28:56 +08:00
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
// 对请求错误做些什么
|
2025-08-12 12:16:47 +08:00
|
|
|
|
return Promise.reject(error)
|
2025-07-17 10:28:56 +08:00
|
|
|
|
}
|
2025-08-12 12:16:47 +08:00
|
|
|
|
)
|
2025-07-17 10:28:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(response) => {
|
2025-08-12 12:16:47 +08:00
|
|
|
|
const res = response.data
|
2025-07-17 10:28:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据业务状态码处理
|
|
|
|
|
|
if (res.code !== 200) {
|
|
|
|
|
|
ElMessage({
|
|
|
|
|
|
message: res.message || "Error",
|
|
|
|
|
|
type: "error",
|
|
|
|
|
|
duration: 5 * 1000
|
2025-08-12 12:16:47 +08:00
|
|
|
|
})
|
2025-07-17 10:28:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 特殊状态码处理
|
|
|
|
|
|
if (res.code === 401 || res.code === 403) {
|
2025-08-14 16:05:58 +08:00
|
|
|
|
// 若token过期,或者token无效,或者未找到token,对页面中需要认证的接口都有效,使得重新登录
|
|
|
|
|
|
import("@/store/authentication/index").then(({ useLoginStore }) => {
|
|
|
|
|
|
useLoginStore().token = ""
|
|
|
|
|
|
})
|
|
|
|
|
|
cache.removeItem("token")
|
|
|
|
|
|
router.push("/login")
|
2025-07-17 10:28:56 +08:00
|
|
|
|
}
|
2025-08-14 16:05:58 +08:00
|
|
|
|
return res
|
|
|
|
|
|
// return Promise.reject(new Error(res.message || "Error"))
|
2025-07-17 10:28:56 +08:00
|
|
|
|
} else {
|
2025-08-12 12:16:47 +08:00
|
|
|
|
return res
|
2025-07-17 10:28:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
2025-08-14 16:05:58 +08:00
|
|
|
|
if (error.status == 401 || error.status == 403) {
|
|
|
|
|
|
cache.removeItem("token")
|
|
|
|
|
|
import("@/store/authentication/index").then(({ useLoginStore }) => {
|
|
|
|
|
|
useLoginStore().token = ""
|
|
|
|
|
|
})
|
|
|
|
|
|
router.push("/login")
|
|
|
|
|
|
}
|
2025-07-17 10:28:56 +08:00
|
|
|
|
ElMessage({
|
|
|
|
|
|
message: error.message,
|
|
|
|
|
|
type: "error",
|
|
|
|
|
|
duration: 5 * 1000
|
2025-08-12 12:16:47 +08:00
|
|
|
|
})
|
|
|
|
|
|
return Promise.reject(error)
|
2025-07-17 10:28:56 +08:00
|
|
|
|
}
|
2025-08-12 12:16:47 +08:00
|
|
|
|
)
|
2025-07-17 10:28:56 +08:00
|
|
|
|
|
2025-08-12 12:16:47 +08:00
|
|
|
|
export default service
|