From 06bb4e35aaea288edbde763a3642a5a6d9631698 Mon Sep 17 00:00:00 2001 From: shijing Date: Wed, 27 Apr 2022 15:23:17 +0800 Subject: [PATCH] functionBrtton --- hb_client/src/layout/index.vue | 14 ------ hb_client/src/store/modules/user.js | 12 ++--- hb_client/src/utils/auth.js | 12 ----- hb_client/src/utils/request.js | 47 ++++++++++++++--- hb_client/src/views/em/record.vue | 4 +- hb_client/src/views/inm/fifo.vue | 2 +- hb_client/src/views/mtm/material.vue | 56 +++++++++++++++++++-- hb_client/src/views/mtm/materialDetail.vue | 41 ++++++--------- hb_client/src/views/mtm/productprocess.vue | 3 ++ hb_client/src/views/procurement/puorder.vue | 2 +- 10 files changed, 120 insertions(+), 73 deletions(-) diff --git a/hb_client/src/layout/index.vue b/hb_client/src/layout/index.vue index bc9f667..bc167a8 100644 --- a/hb_client/src/layout/index.vue +++ b/hb_client/src/layout/index.vue @@ -66,12 +66,10 @@ export default { let hasToken = getToken(); if (hasToken) { this.refreshCountData(); - this.refreshtoken(); this.$store.dispatch("user/getCount", {}); }else{ this.$router.push({name:'login'}) } - // console.log('$store.state.user.refresh:'+this.$store.state.user.refresh); }, methods: { refreshCountData(){ @@ -84,18 +82,6 @@ export default { },0) },50000) }, - refreshtoken(){ - let refresh = getRefresh(); - /* debugger; - console.log('refresh:'+refresh); - debugger;*/ - // let refresh = this.$store.state.user.refresh; - this.timer = window.setInterval(() => { - if (refresh) { - this.$store.dispatch("user/refreshToken", refresh); - } - },3540000) - }, handleClickOutside() { this.$store.dispatch('app/closeSideBar', { withoutAnimation: false }) }, diff --git a/hb_client/src/store/modules/user.js b/hb_client/src/store/modules/user.js index 68d4b60..5c72760 100644 --- a/hb_client/src/store/modules/user.js +++ b/hb_client/src/store/modules/user.js @@ -1,14 +1,14 @@ import { login, logout, getInfo } from '@/api/user' import { getCount } from '@/api/workflow' import { refreshToken } from '@/api/user' -import { getToken, setToken, removeToken,getRefresh, setRefresh } from '@/utils/auth' +import { getToken, setToken, removeToken} from '@/utils/auth' import { resetRouter } from '@/router' const getDefaultState = () => { return { token: getToken(), name: '', - refresh: getRefresh(), + refresh: '', avatar: '', count: {}, perms: [] @@ -50,8 +50,8 @@ const actions = { const { data } = response; commit('SET_TOKEN', data.access); commit('SET_REFRESH', data.refresh); + sessionStorage.setItem('refresh',data.refresh); setToken(data.access); - setRefresh(data.refresh); resolve() }).catch(error => { reject(error) @@ -69,14 +69,11 @@ const actions = { this.$router.push({name:'login',params:{}}); reject('验证失败,重新登陆.'); } - const { perms, name, avatar } = data; - // perms must be a non-empty array if (!perms || perms.length <= 0) { reject('没有任何权限!') } - commit('SET_PERMS', perms); commit('SET_NAME', name); commit('SET_AVATAR', avatar); @@ -129,10 +126,9 @@ const actions = { return new Promise((resolve, reject) => { refreshToken({refresh:ref}).then((res) => { const { data } = res; + removeToken(); commit('SET_TOKEN', data.access); - // commit('SET_REFRESH', data.refresh); setToken(data.access); - // setRefresh(data.refresh); resolve() }).catch(error => { reject(error) diff --git a/hb_client/src/utils/auth.js b/hb_client/src/utils/auth.js index f03597d..b63b921 100644 --- a/hb_client/src/utils/auth.js +++ b/hb_client/src/utils/auth.js @@ -1,7 +1,6 @@ import Cookies from 'js-cookie' const TokenKey = 'token'; -const refreshKey = 'refresh'; export function getToken() { return Cookies.get(TokenKey) @@ -14,15 +13,4 @@ export function setToken(token) { export function removeToken() { return Cookies.remove(TokenKey) } -export function getRefresh() { - return Cookies.get(refreshKey) -} - -export function setRefresh(refresh) { - return Cookies.set(refreshKey, refresh) -} - -export function removeRefresh() { - return Cookies.remove(refreshKey) -} diff --git a/hb_client/src/utils/request.js b/hb_client/src/utils/request.js index a8e9cbb..f046f8b 100644 --- a/hb_client/src/utils/request.js +++ b/hb_client/src/utils/request.js @@ -1,8 +1,11 @@ import axios from 'axios' import { MessageBox, Message } from 'element-ui' import store from '@/store' -import { getToken } from '@/utils/auth' - +import { refreshToken } from '@/api/user' +import { getToken,setToken,removeToken } from '@/utils/auth' +let isRefreshing = false; +//重试队列 +let requests = []; // create an axios instance const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url @@ -56,7 +59,7 @@ service.interceptors.response.use( * You can also judge the status by HTTP Status Code */ response => { - const res = response.data + const res = response.data; if(res.code>=200 && res.code<400){ return res } @@ -68,7 +71,40 @@ service.interceptors.response.use( duration: 3 * 1000 }) }else{ - MessageBox.confirm('认证失败,请重新登陆.', '确认退出', { + if (!isRefreshing) { + isRefreshing = true; + //调用刷新token的接口 + return refreshToken({refresh: sessionStorage.getItem('refresh')}).then(res => { + const token = res.data.access; + // 替换token + removeToken(); + setToken(token); + response.headers.Authorization = 'Bearer ' + token; + // token 刷新后将数组的方法重新执行 + requests.forEach((cb) => cb(token)); + requests = []; // 重新请求完清空 + return service(response.config) + }).catch(err => { + //跳到登录页 + removeToken(); + store.dispatch('user/logout').then(() => { + location.reload() + }); + return Promise.reject(err) + }).finally(() => { + isRefreshing = false + }) + }else { + // 返回未执行 resolve 的 Promise + return new Promise(resolve => { + // 用函数形式将 resolve 存入,等待刷新后再执行 + requests.push(token => { + response.headers.Authorization = 'Bearer ' + token; + resolve(service(response.config)) + }) + }) + } + /* MessageBox.confirm('认证失败,请重新登陆.', '确认退出', { confirmButtonText: '重新登陆', cancelButtonText: '取消', type: 'warning' @@ -76,9 +112,8 @@ service.interceptors.response.use( store.dispatch('user/logout').then(() => { location.reload() }) - }) + })*/ } - } else if (res.code >= 400) { if(res.msg.indexOf('请调整位置')>-1){ return; diff --git a/hb_client/src/views/em/record.vue b/hb_client/src/views/em/record.vue index 5dcd17c..52367a8 100644 --- a/hb_client/src/views/em/record.vue +++ b/hb_client/src/views/em/record.vue @@ -78,13 +78,13 @@ width="120px" > - + - + - - + - + - + diff --git a/hb_client/src/views/mtm/productprocess.vue b/hb_client/src/views/mtm/productprocess.vue index bd0e314..90bdf09 100644 --- a/hb_client/src/views/mtm/productprocess.vue +++ b/hb_client/src/views/mtm/productprocess.vue @@ -175,6 +175,9 @@ + + + diff --git a/hb_client/src/views/procurement/puorder.vue b/hb_client/src/views/procurement/puorder.vue index 602e2a4..bc77435 100644 --- a/hb_client/src/views/procurement/puorder.vue +++ b/hb_client/src/views/procurement/puorder.vue @@ -72,7 +72,7 @@ 订单项