2024-08-26 03:40:38 +00:00
|
|
|
import Request from "./request/index.js";
|
|
|
|
import useToast from "@/hooks/toast/useToast.js";
|
|
|
|
const { showMessage } = useToast();
|
|
|
|
const request = new Request({
|
2024-09-11 07:06:36 +00:00
|
|
|
baseURL: import.meta.env.VITE_API_URL,
|
2024-08-26 03:40:38 +00:00
|
|
|
timeout: 1000 * 60 * 5,
|
|
|
|
interceptors: {
|
|
|
|
//实例的请求拦截器
|
|
|
|
requestInterceptors: (config) => {
|
|
|
|
config.headers["Content-Type"] =
|
|
|
|
config.method === "get"
|
|
|
|
? "application/x-www-form-urlencoded"
|
|
|
|
: "application/json";
|
|
|
|
|
|
|
|
const token = uni.getStorageSync("token") || "";
|
|
|
|
if (config.isFormData) {
|
|
|
|
config.headers["Content-Type"] = "multipart/form-data";
|
|
|
|
config.headers["Authorization"] = token;
|
|
|
|
} else {
|
|
|
|
config.headers["Authorization"] = token;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
//实例的响应拦截器
|
|
|
|
responseInterceptors: async (response) => {
|
|
|
|
if (response.data.status === 1) {
|
|
|
|
showMessage({ type: "error", message: response.data.msg });
|
|
|
|
}
|
2024-09-14 03:44:44 +00:00
|
|
|
// if(response.data.status === 0){
|
|
|
|
// uni.setStorageSync("token", response.data.data.Token);
|
|
|
|
// uni.setStorageSync("userInfo", response.data.data.AccountInfo);
|
|
|
|
// response.config.headers["Authorization"] = response.data.data.Token;
|
|
|
|
// }
|
2024-08-26 03:40:38 +00:00
|
|
|
if (response.data.code === 401) {
|
2024-10-17 02:59:23 +00:00
|
|
|
uni.clearStorageSync(); //清缓存
|
|
|
|
const params = new URLSearchParams(window.location.search); //属性包含当前 URL 的查询字符串部分
|
|
|
|
const id = params.get("id");
|
|
|
|
const pid = params.get("pid");
|
2024-08-26 03:40:38 +00:00
|
|
|
uni.navigateTo({
|
2024-10-17 02:59:23 +00:00
|
|
|
url: "/pages/login/index?id=" + id + "&pid=" + pid,
|
2024-08-26 03:40:38 +00:00
|
|
|
});
|
2024-10-17 02:59:23 +00:00
|
|
|
return false;
|
2024-08-26 03:40:38 +00:00
|
|
|
}
|
|
|
|
if ([200, 201, 204].includes(response.status)) {
|
|
|
|
return response.config.responseType === "blob" ? response : response;
|
|
|
|
} else {
|
2024-09-12 06:53:48 +00:00
|
|
|
// showMessage({ type: "error", message: response.data.msg });
|
2024-08-26 03:40:38 +00:00
|
|
|
return Promise.reject(
|
|
|
|
new Error(response.data.msg || "An error occurred.")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const fontRequest = (config) => {
|
|
|
|
console.log(import.meta);
|
|
|
|
|
|
|
|
if (["get", "GET"].includes(config.method)) {
|
|
|
|
config.params = config.data;
|
|
|
|
}
|
|
|
|
return request.request(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default fontRequest;
|