Showing
5 changed files
with
31 additions
and
7 deletions
@@ -248,6 +248,18 @@ export const useUserStore = defineStore({ | @@ -248,6 +248,18 @@ export const useUserStore = defineStore({ | ||
248 | } | 248 | } |
249 | }, | 249 | }, |
250 | 250 | ||
251 | + async doShareRefresh() { | ||
252 | + try { | ||
253 | + const req = { refreshToken: this.shareRefreshToken } as RefreshTokenParams; | ||
254 | + const data = await doRefreshToken(req); | ||
255 | + const { token, refreshToken } = data; | ||
256 | + this.storeToken(token, refreshToken); | ||
257 | + } catch (error) { | ||
258 | + window.location.reload(); | ||
259 | + throw error; | ||
260 | + } | ||
261 | + }, | ||
262 | + | ||
251 | /** | 263 | /** |
252 | * @description: Confirm before logging out | 264 | * @description: Confirm before logging out |
253 | */ | 265 | */ |
@@ -17,6 +17,8 @@ import { | @@ -17,6 +17,8 @@ import { | ||
17 | APP_SESSION_CACHE_KEY, | 17 | APP_SESSION_CACHE_KEY, |
18 | MULTIPLE_TABS_KEY, | 18 | MULTIPLE_TABS_KEY, |
19 | MENU_LIST, | 19 | MENU_LIST, |
20 | + SHARE_JWT_TOKEN_KEY, | ||
21 | + SHARE_REFRESH_TOKEN_KEY, | ||
20 | } from '/@/enums/cacheEnum'; | 22 | } from '/@/enums/cacheEnum'; |
21 | import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; | 23 | import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; |
22 | import { toRaw } from 'vue'; | 24 | import { toRaw } from 'vue'; |
@@ -33,6 +35,8 @@ interface BasicStore { | @@ -33,6 +35,8 @@ interface BasicStore { | ||
33 | [PROJ_CFG_KEY]: ProjectConfig; | 35 | [PROJ_CFG_KEY]: ProjectConfig; |
34 | [MULTIPLE_TABS_KEY]: RouteLocationNormalized[]; | 36 | [MULTIPLE_TABS_KEY]: RouteLocationNormalized[]; |
35 | [MENU_LIST]: any[]; | 37 | [MENU_LIST]: any[]; |
38 | + [SHARE_JWT_TOKEN_KEY]: string; | ||
39 | + [SHARE_REFRESH_TOKEN_KEY]: string; | ||
36 | } | 40 | } |
37 | 41 | ||
38 | type LocalStore = BasicStore; | 42 | type LocalStore = BasicStore; |
@@ -115,12 +115,19 @@ export class VAxios { | @@ -115,12 +115,19 @@ export class VAxios { | ||
115 | this.axiosInstance.interceptors.request.use(async (config: AxiosRequestConfig) => { | 115 | this.axiosInstance.interceptors.request.use(async (config: AxiosRequestConfig) => { |
116 | // If cancel repeat request is turned on, then cancel repeat request is prohibited | 116 | // If cancel repeat request is turned on, then cancel repeat request is prohibited |
117 | const userStore = useUserStore(); | 117 | const userStore = useUserStore(); |
118 | - if (userStore && userStore.jwtToken) { | 118 | + if (userStore) { |
119 | try { | 119 | try { |
120 | - const res = jwt_decode(userStore.jwtToken) as JwtModel; | ||
121 | - const currentTime = (new Date().getTime() + (config.timeout || 0)) / 1000; | ||
122 | - if (currentTime >= res.exp && this.isNeedTokenURL(config.url)) { | ||
123 | - await this.refreshTokenBeforeReq(userStore.doRefresh); | 120 | + const { requestOptions = {} } = config; |
121 | + const { withShareToken, withToken } = requestOptions; | ||
122 | + const token = withShareToken && withToken ? userStore.shareJwtToken : userStore.jwtToken; | ||
123 | + const doRefresh = | ||
124 | + withShareToken && withToken ? userStore.doShareRefresh : userStore.doRefresh; | ||
125 | + if (token) { | ||
126 | + const res = jwt_decode(token) as JwtModel; | ||
127 | + const currentTime = (new Date().getTime() + (config.timeout || 0)) / 1000; | ||
128 | + if (currentTime >= res.exp && this.isNeedTokenURL(config.url!)) { | ||
129 | + await this.refreshTokenBeforeReq(doRefresh); | ||
130 | + } | ||
124 | } | 131 | } |
125 | } catch (error) { | 132 | } catch (error) { |
126 | userStore.logout(); | 133 | userStore.logout(); |
1 | /** | 1 | /** |
2 | * Data processing class, can be configured according to the project | 2 | * Data processing class, can be configured according to the project |
3 | */ | 3 | */ |
4 | -import type { AxiosRequestConfig, AxiosResponse } from 'axios'; | 4 | +import type { AxiosRequestConfig as OriginalAxiosRequestConfig, AxiosResponse } from 'axios'; |
5 | import type { RequestOptions, Result } from '/#/axios'; | 5 | import type { RequestOptions, Result } from '/#/axios'; |
6 | 6 | ||
7 | export interface CreateAxiosOptions extends AxiosRequestConfig { | 7 | export interface CreateAxiosOptions extends AxiosRequestConfig { |
@@ -10,6 +10,7 @@ export interface CreateAxiosOptions extends AxiosRequestConfig { | @@ -10,6 +10,7 @@ export interface CreateAxiosOptions extends AxiosRequestConfig { | ||
10 | transform?: AxiosTransform; | 10 | transform?: AxiosTransform; |
11 | requestOptions?: RequestOptions; | 11 | requestOptions?: RequestOptions; |
12 | } | 12 | } |
13 | +export type AxiosRequestConfig = OriginalAxiosRequestConfig & { requestOptions?: RequestOptions }; | ||
13 | 14 | ||
14 | export abstract class AxiosTransform { | 15 | export abstract class AxiosTransform { |
15 | /** | 16 | /** |
1 | export const isShareMode = () => { | 1 | export const isShareMode = () => { |
2 | - const sharePageReg = /\/share\/[^/]+\/[^/]+\/[^/]+/; | 2 | + const sharePageReg = /^\/share\/[^/]+\/[^/]+\/[^/]+$/; |
3 | const { pathname } = location; | 3 | const { pathname } = location; |
4 | return sharePageReg.test(pathname); | 4 | return sharePageReg.test(pathname); |
5 | }; | 5 | }; |