feat: reoede web to tailwindcss
This commit is contained in:
+20
-9
@@ -1,9 +1,11 @@
|
||||
// request.js
|
||||
import axios from 'axios';
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
import { usePageState } from '../store/page_sate';
|
||||
import { CONSTANT } from '../constant';
|
||||
import config from '../../config.js';
|
||||
import { toast } from "vue-sonner"
|
||||
|
||||
|
||||
const ERR_NETWORK = "ERR_NETWORK";
|
||||
|
||||
@@ -14,6 +16,7 @@ const request = axios.create({
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 请求拦截器
|
||||
request.interceptors.request.use(
|
||||
(config) => {
|
||||
@@ -27,7 +30,6 @@ request.interceptors.request.use(
|
||||
'm-token': pageState.Token,
|
||||
};
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
@@ -38,16 +40,20 @@ request.interceptors.request.use(
|
||||
// 响应拦截器
|
||||
request.interceptors.response.use(
|
||||
(response) => {
|
||||
|
||||
if (response && response.data.code != 200) {
|
||||
ElMessage({ message: response.data.msg, type: 'error' })
|
||||
toast.error(response.data.msg, {
|
||||
description: '接口逻辑错误'
|
||||
})
|
||||
// Promise.reject();
|
||||
}
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
|
||||
if (error.response && error.response.status == 401) {
|
||||
logout();
|
||||
} else if (20000 <= error.response.status && error.response.status <= 29999) {
|
||||
} else if (error.response && 20000 <= error.response.status && error.response.status <= 29999) {
|
||||
logout();
|
||||
} else {
|
||||
handleException(error);
|
||||
@@ -57,11 +63,16 @@ request.interceptors.response.use(
|
||||
|
||||
// 异常处理
|
||||
const handleException = (error) => {
|
||||
if (!error.response) {
|
||||
|
||||
return
|
||||
};
|
||||
|
||||
if (error.code == ERR_NETWORK) {
|
||||
ElMessage({ message: `网络错误!`, type: 'error' })
|
||||
toast(`网络错误!`)
|
||||
} else {
|
||||
let msg = `未知错误:${error.response.status}, ${error.response.data.msg}`;
|
||||
ElMessage({ message: msg, type: 'error' })
|
||||
toast(msg)
|
||||
};
|
||||
|
||||
};
|
||||
@@ -69,10 +80,10 @@ const handleException = (error) => {
|
||||
// 登出系统
|
||||
const logout = () => {
|
||||
const pageState = usePageState();
|
||||
pageState.setIsLogin(true);
|
||||
localStorage.setItem(CONSTANT.STORE_TOKEN_NAME, "");
|
||||
pageState.setIsLogin(false);
|
||||
localStorage.removeItem(CONSTANT.STORE_TOKEN_NAME);
|
||||
setTimeout(() => {
|
||||
window.location.href = '/';
|
||||
window.location.href = '/login';
|
||||
}, 500);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user