From e287df2393782f771fe6571b9cd8cb3bc5721231 Mon Sep 17 00:00:00 2001 From: shijing Date: Fri, 3 Feb 2023 09:01:23 +0800 Subject: [PATCH] xinbiaoqian --- src/config/route.js | 5 ++- src/utils/request.js | 48 ++++++++++++++++++--- src/views/login/components/passwordForm.vue | 2 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/config/route.js b/src/config/route.js index 623b2783..61e2297d 100644 --- a/src/config/route.js +++ b/src/config/route.js @@ -25,6 +25,8 @@ // }] // } // ] +// import sysConfig from "@/config"; +// const linkUrl = sysConfig.API_URL.slice(0,--3)+"#/bigScreen"; const routes = [ { @@ -50,11 +52,12 @@ const routes = [ }, { "name": "bigScreen", - "path": "/bigScreen", + "path": "http://222.222.144.147:6013/#/bigScreen", "meta": { "title": "驾驶舱", "icon": "el-icon-position", "fullpage": true, + "type": "link", "perms": ["bigScreen"] }, "component": "bigScreen" diff --git a/src/utils/request.js b/src/utils/request.js index 9dab396f..b58948e3 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -6,8 +6,12 @@ import router from '@/router'; axios.defaults.baseURL = sysConfig.API_URL; -axios.defaults.timeout = sysConfig.TIMEOUT +axios.defaults.timeout = sysConfig.TIMEOUT; +// 是否正在刷新的标记 +let isRefreshing = false; +// 重试队列,每一项将是一个待执行的函数形式 +let requests = []; // HTTP request 拦截器 axios.interceptors.request.use( (config) => { @@ -36,10 +40,44 @@ axios.interceptors.response.use( if (error.response) { if (error.response.status == 401) { if(error.response.data.err_code == 'no_active_account'){ - ElNotification.warning({ - title: '请求失败', - message: "账户密码错误或已禁用" - }); + // ElNotification.warning({ + // title: '请求失败', + // message: "账户密码错误或已禁用" + // }); + if (!isRefreshing) { + isRefreshing = true; + axios({ + method: 'post', + url: sysConfig.API_URL+'/auth/token/refresh/', + headers: { + Authorization: `Bearer ${tool.cookie.get("TOKEN")}`, + }, + data:{refresh:tool.cookie.get("TOKEN_REFRESH")} + }).then(res=>{ + debugger; + console.log(res.data.access); + // 替换本地的token + tool.cookie.set("TOKEN", res.data.access); + tool.data.set("TOKEN_TIME", new Date().getTime()); + error.config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + res.data.access; + isRefreshing = false; + requests.forEach(cb => cb()) + //发起后将requests置空 + requests=[]; + return axios(error.config) + }).catch(()=>{ + // refresh_token 有值 , 但是过期了, 请求失败 + router.push('/login') + }) + + }else{ + //如果正在刷新token,将发起的请求暂存在requests中 + return new Promise((resolve)=>{ + requests.push(()=>{ + resolve(axios(error.config)); + }) + }) + } }else{ // ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。', '无权限访问', { // type: 'error', diff --git a/src/views/login/components/passwordForm.vue b/src/views/login/components/passwordForm.vue index d9b4c781..d055b930 100644 --- a/src/views/login/components/passwordForm.vue +++ b/src/views/login/components/passwordForm.vue @@ -157,6 +157,8 @@ export default { that.$TOOL.cookie.set("TOKEN", res.access, { expires: that.form.autologin ? 24 * 60 * 60 : 0, }); + that.$TOOL.cookie.set("TOKEN_REFRESH", res.refresh); + that.$TOOL.data.set("TOKEN_TIME", new Date().getTime()); try { var res1 = await that.$API.auth.info.get(); that.$TOOL.data.set("USER_INFO", res1);