91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
|
|
import axios from "axios";
|
||
|
|
import type { AxiosResponse } from "axios";
|
||
|
|
// import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
|
||
|
|
import { Session } from "@/utils/storage";
|
||
|
|
import qs from "qs";
|
||
|
|
|
||
|
|
// 配置新建一个 axios 实例
|
||
|
|
const service = axios.create({
|
||
|
|
baseURL: import.meta.env.VITE_API_URL,
|
||
|
|
timeout: 50000,
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
paramsSerializer: {
|
||
|
|
serialize(params) {
|
||
|
|
return qs.stringify(params, { allowDots: true });
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
// 添加请求拦截器
|
||
|
|
service.interceptors.request.use(
|
||
|
|
(config) => {
|
||
|
|
// 在发送请求之前做些什么 token
|
||
|
|
if (Session.get("token")) {
|
||
|
|
config.headers!["auth-token"] = `${Session.get("token")}`;
|
||
|
|
config.headers!["phone"] = `${Session.get("userPhone")}`;
|
||
|
|
}
|
||
|
|
return config;
|
||
|
|
},
|
||
|
|
(error) => {
|
||
|
|
// 对请求错误做些什么
|
||
|
|
return Promise.reject(error);
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 添加响应拦截器
|
||
|
|
service.interceptors.response.use(
|
||
|
|
(response) => {
|
||
|
|
// 对响应数据做点什么
|
||
|
|
const res: AxiosResponse = response.data;
|
||
|
|
if (res.code && res.code !== 200) {
|
||
|
|
console.log("提示");
|
||
|
|
uni.showToast({
|
||
|
|
title: res.msg,
|
||
|
|
icon: "error",
|
||
|
|
duration: 2000,
|
||
|
|
});
|
||
|
|
// ElNotification({
|
||
|
|
// title: '提示',
|
||
|
|
// message: 'res.msg',
|
||
|
|
// type: 'error',
|
||
|
|
// });
|
||
|
|
|
||
|
|
// `token` 过期或者账号已在别处登录
|
||
|
|
if (res.code === 401 || res.code === 4001) {
|
||
|
|
Session.clear(); // 清除浏览器全部临时缓存
|
||
|
|
window.location.href = "/"; // 去登录页
|
||
|
|
console.log("你已被登出,请重新登录");
|
||
|
|
// ElMessageBox.alert('你已被登出,请重新登录', '提示', {})
|
||
|
|
// .then(() => {})
|
||
|
|
// .catch(() => {});
|
||
|
|
}
|
||
|
|
// return Promise.reject(service.interceptors.response);
|
||
|
|
return res;
|
||
|
|
} else {
|
||
|
|
return res;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
(error) => {
|
||
|
|
// 对响应错误做点什么
|
||
|
|
if (error.message.indexOf("timeout") != -1) {
|
||
|
|
// ElMessage.error('网络超时');
|
||
|
|
console.log("网络超时");
|
||
|
|
} else if (error.message == "Network Error") {
|
||
|
|
// ElMessage.error('网络连接错误');
|
||
|
|
console.log("网络连接错误");
|
||
|
|
} else {
|
||
|
|
if (error.response.data) {
|
||
|
|
// ElMessage.error(error.response.statusText)
|
||
|
|
console.log("error.response.statusText ===>", error.response.statusText);
|
||
|
|
} else {
|
||
|
|
// ElMessage.error('接口路径找不到')
|
||
|
|
console.log("接口路径找不到");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return Promise.reject(error);
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 导出 axios 实例
|
||
|
|
export default service;
|