1
0
mirror of https://github.com/FatttSnake/Pinnacle-OA.git synced 2026-04-05 23:11:24 +08:00

Added token automatic renew

This commit is contained in:
2023-06-02 02:33:05 +08:00
parent 1b186a8e9e
commit 1b6ac6ade8
9 changed files with 58 additions and 11 deletions

View File

@@ -19,6 +19,7 @@ const LOGOUT_SUCCESS = 20015
const LOGOUT_FAILED = 20016
const TOKEN_IS_ILLEGAL = 20017
const TOKEN_HAS_EXPIRED = 20018
const TOKEN_RENEW_SUCCESS = 20019
const DATABASE_SELECT_OK = 20021
const DATABASE_SAVE_OK = 20022
const DATABASE_UPDATE_OK = 20023
@@ -59,6 +60,7 @@ export {
LOGOUT_FAILED,
TOKEN_IS_ILLEGAL,
TOKEN_HAS_EXPIRED,
TOKEN_RENEW_SUCCESS,
DATABASE_SELECT_OK,
DATABASE_SAVE_OK,
DATABASE_UPDATE_OK,

View File

@@ -155,10 +155,10 @@ export default {
return
}
login(this.userName, this.password).then((res) => {
const data = res.data
switch (data.code) {
const response = res.data
switch (response.code) {
case LOGIN_SUCCESS:
setToken(data.data.token)
setToken(response.data.token)
ElMessage.success({
dangerouslyUseHTMLString: true,
message: '<strong>登录成功</strong>'

View File

@@ -1,5 +1,6 @@
import axios, { type AxiosError } from 'axios'
import { clearLocalStorage, getToken } from '@/utils/common'
import jwtDecode from 'jwt-decode'
import { clearLocalStorage, getToken, setToken } from '@/utils/common'
import router from '@/router'
import {
ACCESS_DENIED,
@@ -7,6 +8,7 @@ import {
DATABASE_DATA_VALIDATION_FAILED,
TOKEN_HAS_EXPIRED,
TOKEN_IS_ILLEGAL,
TOKEN_RENEW_SUCCESS,
UNAUTHORIZED
} from '@/constants/Common.constants'
import { ElMessage } from 'element-plus'
@@ -18,9 +20,27 @@ const service = axios.create({
})
service.interceptors.request.use(
(config) => {
const token = getToken()
async (config) => {
let token = getToken()
if (token != null) {
const jwt = jwtDecode(token)
if (
(jwt as any).exp * 1000 - new Date().getTime() < 1200000 &&
(jwt as any).exp * 1000 - new Date().getTime() > 0
) {
await axios
.get('http://localhost:8621/token', {
headers: { token }
})
.then((res) => {
const response = res.data
if (response.code === TOKEN_RENEW_SUCCESS) {
setToken(response.data.token)
}
})
}
token = getToken()
config.headers.set('token', token)
}
return config