Commit 04a9ba8e699a4e342f15944579544e8ac3b3ba4d

Authored by ww
1 parent 76e6810a

perf: 优化分享页面token过期后能刷新token

... ... @@ -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 264 * @description: Confirm before logging out
253 265 */
... ...
... ... @@ -17,6 +17,8 @@ import {
17 17 APP_SESSION_CACHE_KEY,
18 18 MULTIPLE_TABS_KEY,
19 19 MENU_LIST,
  20 + SHARE_JWT_TOKEN_KEY,
  21 + SHARE_REFRESH_TOKEN_KEY,
20 22 } from '/@/enums/cacheEnum';
21 23 import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
22 24 import { toRaw } from 'vue';
... ... @@ -33,6 +35,8 @@ interface BasicStore {
33 35 [PROJ_CFG_KEY]: ProjectConfig;
34 36 [MULTIPLE_TABS_KEY]: RouteLocationNormalized[];
35 37 [MENU_LIST]: any[];
  38 + [SHARE_JWT_TOKEN_KEY]: string;
  39 + [SHARE_REFRESH_TOKEN_KEY]: string;
36 40 }
37 41
38 42 type LocalStore = BasicStore;
... ...
... ... @@ -115,12 +115,19 @@ export class VAxios {
115 115 this.axiosInstance.interceptors.request.use(async (config: AxiosRequestConfig) => {
116 116 // If cancel repeat request is turned on, then cancel repeat request is prohibited
117 117 const userStore = useUserStore();
118   - if (userStore && userStore.jwtToken) {
  118 + if (userStore) {
119 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 132 } catch (error) {
126 133 userStore.logout();
... ...
1 1 /**
2 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 5 import type { RequestOptions, Result } from '/#/axios';
6 6
7 7 export interface CreateAxiosOptions extends AxiosRequestConfig {
... ... @@ -10,6 +10,7 @@ export interface CreateAxiosOptions extends AxiosRequestConfig {
10 10 transform?: AxiosTransform;
11 11 requestOptions?: RequestOptions;
12 12 }
  13 +export type AxiosRequestConfig = OriginalAxiosRequestConfig & { requestOptions?: RequestOptions };
13 14
14 15 export abstract class AxiosTransform {
15 16 /**
... ...
1 1 export const isShareMode = () => {
2   - const sharePageReg = /\/share\/[^/]+\/[^/]+\/[^/]+/;
  2 + const sharePageReg = /^\/share\/[^/]+\/[^/]+\/[^/]+$/;
3 3 const { pathname } = location;
4 4 return sharePageReg.test(pathname);
5 5 };
... ...