From acd16891059c81ff2d7e49c53612ad12056773ec Mon Sep 17 00:00:00 2001 From: wangxiang <1827945911@qq.com> Date: Fri, 15 Apr 2022 21:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=9F=20=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kicc-ui/src/enums/cacheEnum.ts | 8 +++++- kicc-ui/src/store/modules/user.ts | 28 +++++++++++---------- kicc-ui/src/utils/cache/persistent.ts | 4 ++- kicc-ui/src/utils/http/axios/checkStatus.ts | 1 + 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/kicc-ui/src/enums/cacheEnum.ts b/kicc-ui/src/enums/cacheEnum.ts index e96b4bd9..29e58f4a 100644 --- a/kicc-ui/src/enums/cacheEnum.ts +++ b/kicc-ui/src/enums/cacheEnum.ts @@ -1,6 +1,6 @@ /** * @program: kicc-ui - * @description: 缓存枚举 + * @description: 缓存枚举键 * @author: entfrm开发团队-王翔 * @create: 2022/4/9 */ @@ -10,6 +10,12 @@ export const ACCESS_TOKEN_KEY = 'ACCESS__TOKEN__'; // 刷新令牌密钥 export const REFRESH_TOKEN_KEY = 'REFRESH__TOKEN__'; +// 按钮权限键 +export const PERMISSIONS_KEY = 'PERMISSIONS__'; + +// 角色id键 +export const ROLE_IDS_KEY = 'ROLE__IDS__'; + // 国际化键 export const LOCALE_KEY = 'LOCALE__'; diff --git a/kicc-ui/src/store/modules/user.ts b/kicc-ui/src/store/modules/user.ts index a4d35101..9df51080 100644 --- a/kicc-ui/src/store/modules/user.ts +++ b/kicc-ui/src/store/modules/user.ts @@ -9,8 +9,8 @@ import type {UserInfo} from '/#/store'; import {defineStore} from 'pinia'; import {store} from '/@/store'; import {PageEnum} from '/@/enums/pageEnum'; -import {ACCESS_TOKEN_KEY, REFRESH_TOKEN_KEY, USER_INFO_KEY} from '/@/enums/cacheEnum'; -import {setAuthCache} from '/@/utils/auth'; +import {ACCESS_TOKEN_KEY, PERMISSIONS_KEY, REFRESH_TOKEN_KEY, ROLE_IDS_KEY, USER_INFO_KEY} from '/@/enums/cacheEnum'; +import {getAuthCache, setAuthCache} from '/@/utils/auth'; import {GetUserInfoModel, LoginParams} from '/@/api/sys/model/userModel'; import {doLogout, getUserInfo, loginApi} from '/@/api/sys/user'; import {useI18n} from '/@/hooks/web/useI18n'; @@ -24,35 +24,35 @@ interface UserState { sessionTimeout: boolean; roleIds: Recordable; permissions: Recordable; - access_token: string; - refresh_token: string; + access_token?: string; + refresh_token?: string; } export const useUserStore = defineStore({ id: 'app-user', state: (): UserState => ({ // 用户信息 - userInfo: null, + userInfo: getAuthCache(USER_INFO_KEY), // 登录是否过期 sessionTimeout: false, // 角色ID用于权限校验 - roleIds: [], + roleIds: getAuthCache(ROLE_IDS_KEY), // 按钮权限标识用于权限校验 - permissions: [], + permissions: getAuthCache(PERMISSIONS_KEY), // 访问令牌 - access_token: '', + access_token: getAuthCache(ACCESS_TOKEN_KEY), // 刷新令牌 - refresh_token: '', + refresh_token: getAuthCache(REFRESH_TOKEN_KEY), }), getters: { - getUserInfo(): UserInfo | {[key: string]: any} { - return this.userInfo || {}; + getUserInfo(): UserInfo { + return this.userInfo! || {}; }, getAccessToken(): string { - return this.access_token; + return this.access_token!; }, getRefreshToken(): string { - return this.refresh_token; + return this.refresh_token!; }, getSessionTimeout(): boolean { return !!this.sessionTimeout; @@ -67,6 +67,7 @@ export const useUserStore = defineStore({ actions: { setRoleIds(roleIds: Recordable) { this.roleIds = roleIds; + setAuthCache(ROLE_IDS_KEY, roleIds); }, setPermissions(permissions: Recordable) { this.permissions = permissions; @@ -141,6 +142,7 @@ export const useUserStore = defineStore({ } catch { console.log('注销Token失败'); } + debugger this.setAccessToken(''); this.setRefreshToken(''); this.setSessionTimeout(false); diff --git a/kicc-ui/src/utils/cache/persistent.ts b/kicc-ui/src/utils/cache/persistent.ts index bd29629a..39f66c5d 100644 --- a/kicc-ui/src/utils/cache/persistent.ts +++ b/kicc-ui/src/utils/cache/persistent.ts @@ -19,7 +19,7 @@ import { PROJ_CFG_KEY, APP_LOCAL_CACHE_KEY, APP_SESSION_CACHE_KEY, - MULTIPLE_TABS_KEY, REFRESH_TOKEN_KEY, + MULTIPLE_TABS_KEY, REFRESH_TOKEN_KEY, ROLE_IDS_KEY, PERMISSIONS_KEY, } from '/@/enums/cacheEnum'; import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; import { toRaw } from 'vue'; @@ -28,6 +28,8 @@ import { pick, omit } from 'lodash-es'; interface BasicStore { [ACCESS_TOKEN_KEY]: string | number | null | undefined; [REFRESH_TOKEN_KEY]: string | number | null | undefined; + [ROLE_IDS_KEY]: Recordable; + [PERMISSIONS_KEY]: Recordable; [USER_INFO_KEY]: UserInfo; [LOCK_INFO_KEY]: LockInfo; [PROJ_CFG_KEY]: ProjectConfig; diff --git a/kicc-ui/src/utils/http/axios/checkStatus.ts b/kicc-ui/src/utils/http/axios/checkStatus.ts index 0d4df929..2ba12101 100644 --- a/kicc-ui/src/utils/http/axios/checkStatus.ts +++ b/kicc-ui/src/utils/http/axios/checkStatus.ts @@ -31,6 +31,7 @@ export function checkStatus(status: number, msg: string, errorMessageMode: Error case 401: errMessage = t('sys.api.errMsg401'); if (stp === SessionTimeoutProcessingEnum.PAGE_COVERAGE) { + debugger userStore.setAccessToken(''); userStore.setRefreshToken(''); userStore.setSessionTimeout(true);