From e5ee1481de5fd96d0c6665fe4c9f9c15cbc92e9e Mon Sep 17 00:00:00 2001 From: shijing Date: Thu, 31 Mar 2022 15:18:51 +0800 Subject: [PATCH] refreshToken --- hb_client/src/api/user.js | 8 ++++++++ hb_client/src/layout/index.vue | 25 +++++++++++++++++++------ hb_client/src/store/getters.js | 1 + hb_client/src/store/modules/user.js | 25 +++++++++++++++++++++++-- hb_client/src/utils/auth.js | 23 +++++++++++++---------- hb_client/src/utils/request.js | 2 +- 6 files changed, 65 insertions(+), 19 deletions(-) diff --git a/hb_client/src/api/user.js b/hb_client/src/api/user.js index df7dac1..8343375 100644 --- a/hb_client/src/api/user.js +++ b/hb_client/src/api/user.js @@ -8,6 +8,14 @@ export function login(data) { }) } +export function refreshToken(data) { + return request({ + url: '/auth/token/refresh/', + method: 'post', + data + }) +} + export function logout() { return request({ url: '/auth/token/black/', diff --git a/hb_client/src/layout/index.vue b/hb_client/src/layout/index.vue index ef11a7b..bc9f667 100644 --- a/hb_client/src/layout/index.vue +++ b/hb_client/src/layout/index.vue @@ -25,7 +25,7 @@ import { Navbar, Sidebar, AppMain,TagsView } from './components' import ResizeMixin from './mixin/ResizeHandler' import { mapGetters } from 'vuex' -import { getToken } from '@/utils/auth' // get token from cookie +import { getToken,getRefresh } from '@/utils/auth' // get token from cookie export default { name: 'Layout', components: { @@ -64,12 +64,14 @@ export default { }, mounted() { let hasToken = getToken(); - /*debugger; - console.log(hasToken);*/ if (hasToken) { + this.refreshCountData(); + this.refreshtoken(); this.$store.dispatch("user/getCount", {}); + }else{ + this.$router.push({name:'login'}) } - // this.refreshCountData(); + // console.log('$store.state.user.refresh:'+this.$store.state.user.refresh); }, methods: { refreshCountData(){ @@ -80,12 +82,23 @@ export default { this.$store.dispatch("user/getCount", {}); } },0) - },5000) + },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 }) }, - gotoTicketPage(){ let path = this.$route.path; if(path==='/workflow/ticket'){ diff --git a/hb_client/src/store/getters.js b/hb_client/src/store/getters.js index 3ff47b0..82d3611 100644 --- a/hb_client/src/store/getters.js +++ b/hb_client/src/store/getters.js @@ -2,6 +2,7 @@ const getters = { sidebar: state => state.app.sidebar, device: state => state.app.device, token: state => state.user.token, + refresh: state => state.user.refresh, avatar: state => state.user.avatar, name: state => state.user.name, perms: state => state.user.perms, diff --git a/hb_client/src/store/modules/user.js b/hb_client/src/store/modules/user.js index 6458db4..68d4b60 100644 --- a/hb_client/src/store/modules/user.js +++ b/hb_client/src/store/modules/user.js @@ -1,12 +1,14 @@ import { login, logout, getInfo } from '@/api/user' import { getCount } from '@/api/workflow' -import { getToken, setToken, removeToken } from '@/utils/auth' +import { refreshToken } from '@/api/user' +import { getToken, setToken, removeToken,getRefresh, setRefresh } from '@/utils/auth' import { resetRouter } from '@/router' const getDefaultState = () => { return { token: getToken(), name: '', + refresh: getRefresh(), avatar: '', count: {}, perms: [] @@ -22,6 +24,9 @@ const mutations = { SET_TOKEN: (state, token) => { state.token = token }, + SET_REFRESH: (state, refresh) => { + state.refresh = refresh + }, SET_NAME: (state, name) => { state.name = name }, @@ -44,9 +49,10 @@ const actions = { login({ username: username.trim(), password: password }).then(response => { const { data } = response; commit('SET_TOKEN', data.access); + commit('SET_REFRESH', data.refresh); setToken(data.access); + setRefresh(data.refresh); resolve() - }).catch(error => { reject(error) }) @@ -118,6 +124,21 @@ const actions = { }) }) } + , + refreshToken({ commit },ref) { + return new Promise((resolve, reject) => { + refreshToken({refresh:ref}).then((res) => { + const { data } = res; + commit('SET_TOKEN', data.access); + // commit('SET_REFRESH', data.refresh); + setToken(data.access); + // setRefresh(data.refresh); + resolve() + }).catch(error => { + reject(error) + }) + }) + } } export default { diff --git a/hb_client/src/utils/auth.js b/hb_client/src/utils/auth.js index 392db62..f03597d 100644 --- a/hb_client/src/utils/auth.js +++ b/hb_client/src/utils/auth.js @@ -1,6 +1,7 @@ import Cookies from 'js-cookie' -const TokenKey = 'token' +const TokenKey = 'token'; +const refreshKey = 'refresh'; export function getToken() { return Cookies.get(TokenKey) @@ -13,13 +14,15 @@ 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) +} -// export function refreshToken() { -// let token = getToken() -// let data = {"token": token} -// return request({ -// url: '/token/refresh/', -// method: 'post', -// data -// }) -// } diff --git a/hb_client/src/utils/request.js b/hb_client/src/utils/request.js index 6183457..f89e07c 100644 --- a/hb_client/src/utils/request.js +++ b/hb_client/src/utils/request.js @@ -73,7 +73,7 @@ service.interceptors.response.use( cancelButtonText: '取消', type: 'warning' }).then(() => { - store.dispatch('user/resetToken').then(() => { + store.dispatch('user/logout').then(() => { location.reload() }) })