96 lines
3.5 KiB
JavaScript
96 lines
3.5 KiB
JavaScript
|
|
// src/plugins/fetch.js
|
|||
|
|
import { createFetch } from "@vueuse/core";
|
|||
|
|
import router from "@/router"; // 假设你使用 Vue Router
|
|||
|
|
import { useUserStore } from "@/stores/userStore";
|
|||
|
|
import { useAgentStore } from "@/stores/agentStore";
|
|||
|
|
// 创建全局的 fetch 实例
|
|||
|
|
const useApiFetch = createFetch({
|
|||
|
|
baseUrl: "/api/v1", // 你的 API 基础路径
|
|||
|
|
options: {
|
|||
|
|
async beforeFetch({ url, options }) {
|
|||
|
|
showLoadingToast({
|
|||
|
|
message: "加载中...",
|
|||
|
|
forbidClick: true,
|
|||
|
|
duration: 0, // 设置为 0 表示不会自动关闭
|
|||
|
|
loadingType: "spinner",
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
const timestamp = Date.now();
|
|||
|
|
const separator = url.includes("?") ? "&" : "?"; // 判断是否已有参数
|
|||
|
|
url += `${separator}t=${timestamp}`; // 追加时间戳
|
|||
|
|
|
|||
|
|
// 在请求前添加通用的 Header,例如 Authorization
|
|||
|
|
const token = localStorage.getItem("token");
|
|||
|
|
let platform = "h5";
|
|||
|
|
const userAgent = navigator.userAgent.toLowerCase();
|
|||
|
|
const isWechat = /micromessenger/.test(userAgent);
|
|||
|
|
if (isWechat) {
|
|||
|
|
platform = "wxh5";
|
|||
|
|
}
|
|||
|
|
options.headers['X-Platform'] = platform
|
|||
|
|
|
|||
|
|
if (token) {
|
|||
|
|
options.headers = {
|
|||
|
|
...options.headers,
|
|||
|
|
Authorization: `${token}`,
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
return { url, options };
|
|||
|
|
},
|
|||
|
|
async afterFetch({ data, response }) {
|
|||
|
|
closeToast();
|
|||
|
|
// 全局处理响应
|
|||
|
|
if (response.status === 401) {
|
|||
|
|
// 清除本地存储的 token
|
|||
|
|
localStorage.removeItem("token");
|
|||
|
|
localStorage.removeItem('refreshAfter')
|
|||
|
|
localStorage.removeItem('accessExpire')
|
|||
|
|
// 跳转到登录页
|
|||
|
|
router.replace("/login");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (data.code !== 200) {
|
|||
|
|
if (data.code === 100009) {
|
|||
|
|
// 改进的存储管理
|
|||
|
|
localStorage.removeItem('token')
|
|||
|
|
localStorage.removeItem('refreshAfter')
|
|||
|
|
localStorage.removeItem('accessExpire')
|
|||
|
|
localStorage.removeItem('userInfo')
|
|||
|
|
localStorage.removeItem('agentInfo')
|
|||
|
|
|
|||
|
|
// 重置状态
|
|||
|
|
const userStore = useUserStore();
|
|||
|
|
const agentStore = useAgentStore();
|
|||
|
|
userStore.resetUser()
|
|||
|
|
agentStore.resetAgent()
|
|||
|
|
location.reload()
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
if (data.code !== 200002 && data.code !== 200003 && data.code !== 200004 && data.code !== 100009) {
|
|||
|
|
showToast({ message: data.msg });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return { data, response };
|
|||
|
|
},
|
|||
|
|
async onFetchError({ error, response }) {
|
|||
|
|
console.log("error", error);
|
|||
|
|
closeToast();
|
|||
|
|
if (response.status === 401) {
|
|||
|
|
// 清除本地存储的 token
|
|||
|
|
localStorage.removeItem("token");
|
|||
|
|
localStorage.removeItem('refreshAfter')
|
|||
|
|
localStorage.removeItem('accessExpire')
|
|||
|
|
// 跳转到登录页
|
|||
|
|
router.replace("/login");
|
|||
|
|
} else {
|
|||
|
|
if (typeof error === "string") {
|
|||
|
|
showToast({ message: error });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return { error };
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
export default useApiFetch;
|