From c6c6df9c570ef8bd7963687790c2e194a6cac616 Mon Sep 17 00:00:00 2001 From: Kunagisa <1549184870@qq.com> Date: Sat, 14 Jun 2025 22:08:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B7=E6=96=B0=E5=A4=9A=E6=AD=A4=E4=BC=9A?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E5=88=B0=E7=99=BB=E9=99=86=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E5=86=B3=E4=BA=86=EF=BC=8C=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E4=BC=9A=E5=87=BA=E7=8E=B0=E6=9B=B4=E9=80=86?= =?UTF-8?q?=E5=A4=A9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/axiosConfig.js | 77 +++++++++++++++++---------------- src/components/login_module.vue | 8 ++-- src/main.js | 6 +++ src/router/index.js | 64 ++++++++++++++------------- src/utils/jwt.js | 40 +++++++++++------ 5 files changed, 110 insertions(+), 85 deletions(-) diff --git a/src/api/axiosConfig.js b/src/api/axiosConfig.js index 9cea5f0..850087c 100644 --- a/src/api/axiosConfig.js +++ b/src/api/axiosConfig.js @@ -13,48 +13,49 @@ const axiosInstance = axios.create({ timeout: 10000 }); -/** - * 请求拦截器 - * - 对需要认证的请求,在请求头中添加Authorization - * - 对登录、注册和获取列表的请求,不添加Authorization - */ -axiosInstance.interceptors.request.use( - config => { - const token = localStorage.getItem('access_token'); - const url = config.url; +export function setupInterceptors() { + /** + * 请求拦截器 + * - 对需要认证的请求,在请求头中添加Authorization + * - 对登录、注册和获取列表的请求,不添加Authorization + */ + axiosInstance.interceptors.request.use( + config => { + const token = localStorage.getItem('access_token'); + const url = config.url; - // 定义不需要Token的接口条件 - const noAuthRequired = - url === '/user/login' || - url === '/user/register' || // 明确添加注册接口 - url.endsWith('/getlist'); + // 定义不需要Token的接口条件 + const noAuthRequired = + url === '/user/login' || + url === '/user/register'; - if (token && !noAuthRequired) { - config.headers.Authorization = `Bearer ${token}`; + if (token && !noAuthRequired) { + config.headers.Authorization = `Bearer ${token}`; + } + return config; + }, + error => { + return Promise.reject(error); } - return config; - }, - error => { - return Promise.reject(error); - } -); + ); -/** - * 响应拦截器 - * - 如果收到401错误(未授权),并且不是来自登录请求,则调用logoutUser函数清除用户凭证 - */ -axiosInstance.interceptors.response.use( - response => response, - error => { - const originalRequest = error.config; - - // 如果收到401错误,并且不是来自登录请求本身 - if (error.response && error.response.status === 401 && originalRequest.url !== '/user/login') { - logoutUser(); // 调用简化的logoutUser,它只清除token,不导航 + /** + * 响应拦截器 + * - 如果收到401错误(未授权),并且不是来自登录请求,则调用logoutUser函数清除用户凭证 + */ + axiosInstance.interceptors.response.use( + response => response, + error => { + const originalRequest = error.config; + + // 如果收到401错误,并且不是来自登录请求本身 + if (error.response && error.response.status === 401 && originalRequest.url !== '/user/login') { + logoutUser(); // 调用简化的logoutUser,它只清除token,不导航 + } + // 不需要额外的console.error,错误会自然地在调用处被捕获或显示在网络请求中 + return Promise.reject(error); } - // 不需要额外的console.error,错误会自然地在调用处被捕获或显示在网络请求中 - return Promise.reject(error); - } -); + ); +} export default axiosInstance; \ No newline at end of file diff --git a/src/components/login_module.vue b/src/components/login_module.vue index 4db10e3..138b52a 100644 --- a/src/components/login_module.vue +++ b/src/components/login_module.vue @@ -112,9 +112,6 @@ const refreshCaptcha = async () => { } } -onMounted(() => { - refreshCaptcha() -}) const handleLogin = async () => { try { @@ -203,6 +200,11 @@ const validateForm = () => { return true } + +onMounted(() => { + refreshCaptcha() +}) +