2024-11-21 09:48:47 -05:00
|
|
|
import axios, {Canceler, CancelToken, CancelTokenSource} from "axios";
|
|
|
|
|
|
|
|
|
|
|
|
export const httpReq = axios.create({
|
|
|
|
// cancelToken: new CancelToken(function (cancel:Canceler) {
|
|
|
|
// },),
|
|
|
|
// validateStatus: function (status) {
|
|
|
|
// return status >= 200 && status < 300; // default
|
|
|
|
// },
|
|
|
|
withCredentials: false,
|
|
|
|
})
|
|
|
|
// 从cookie获取token
|
|
|
|
|
|
|
|
// 创建一个取消令牌
|
|
|
|
const cancelTokenSource: CancelTokenSource = axios.CancelToken.source();
|
|
|
|
|
|
|
|
httpReq.defaults.headers.common['Accept'] = 'application/json';
|
2024-11-25 05:28:24 -05:00
|
|
|
httpReq.interceptors.request.use((config) => {
|
|
|
|
const token = localStorage.getItem('platform-security'); // 从本地存储中获取 token
|
|
|
|
if (token) {
|
|
|
|
// config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
config.headers.Authorization = `${token}`;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
return Promise.reject(error);
|
|
|
|
})
|
|
|
|
httpReq.interceptors.response.use(
|
|
|
|
(response) => {
|
|
|
|
// http请求最外层
|
|
|
|
if (response.status >= 500) {
|
|
|
|
console.log("系统异常")
|
|
|
|
} else if (response.status >= 400 && response.status <= 500) {
|
|
|
|
console.log("无权限")
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
if (error.response && error.response.status === 401) {
|
|
|
|
localStorage.removeItem('authToken');
|
|
|
|
window.location.href = '/login';
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|