From c8021ef3253765059b0aacb955158642072640c5 Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 1 Nov 2020 14:58:27 +0800 Subject: [PATCH] chore: changed login cache from sessionStorage to LocalStorage --- CHANGELOG.zh_CN.md | 4 ++ src/setup/ant-design-vue/index.ts | 4 +- src/store/modules/user.ts | 16 +++--- src/utils/helper/persistent.ts | 12 +++- src/views/sys/login/Login.vue | 94 +++++++++++++++++++++---------- 5 files changed, 87 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.zh_CN.md b/CHANGELOG.zh_CN.md index a511d1de..d10f7e27 100644 --- a/CHANGELOG.zh_CN.md +++ b/CHANGELOG.zh_CN.md @@ -4,6 +4,10 @@ - 全局 loading 添加文本 +### 🎫 Chores + +- 登录缓存从 sessionStorage 改为 LocalStorage + ### ⚡ Performance Improvements - Layout 界面布局样式调整 diff --git a/src/setup/ant-design-vue/index.ts b/src/setup/ant-design-vue/index.ts index e6c7ead0..05212100 100644 --- a/src/setup/ant-design-vue/index.ts +++ b/src/setup/ant-design-vue/index.ts @@ -2,7 +2,7 @@ import type { App } from 'vue'; -import { Form, Input } from 'ant-design-vue'; +import { Form, Input, Row, Col } from 'ant-design-vue'; import 'ant-design-vue/dist/antd.less'; import './spin'; @@ -11,4 +11,6 @@ export function setupAntd(app: App) { // 这两个组件在登录也就用。全局注册 app.use(Form); app.use(Input); + app.use(Row); + app.use(Col); } diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 699e35fc..4f7e7f7a 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -21,7 +21,7 @@ import { tabStore } from './tab'; import { loginApi, getUserInfoById } from '/@/api/sys/user'; -import { setSession, getSession, clearSession, clearLocal } from '/@/utils/helper/persistent'; +import { setLocal, getLocal, clearSession, clearLocal } from '/@/utils/helper/persistent'; // import { FULL_PAGE_NOT_FOUND_ROUTE } from '/@/router/constant'; export type UserInfo = Omit; @@ -40,17 +40,15 @@ class User extends VuexModule { private roleListState: RoleEnum[] = []; get getUserInfoState(): UserInfo { - return this.userInfoState || (getSession(USER_INFO_KEY) as UserInfo) || {}; + return this.userInfoState || (getLocal(USER_INFO_KEY) as UserInfo) || {}; } get getTokenState(): string { - return this.tokenState || (getSession(TOKEN_KEY) as string); + return this.tokenState || (getLocal(TOKEN_KEY) as string); } get getRoleListState(): RoleEnum[] { - return this.roleListState.length > 0 - ? this.roleListState - : (getSession(ROLES_KEY) as RoleEnum[]); + return this.roleListState.length > 0 ? this.roleListState : (getLocal(ROLES_KEY) as RoleEnum[]); } @Mutation @@ -64,7 +62,7 @@ class User extends VuexModule { commitUserInfoState(info: UserInfo): void { this.userInfoState = info; if (info) { - setSession(USER_INFO_KEY, info); + setLocal(USER_INFO_KEY, info, true); } } @@ -72,7 +70,7 @@ class User extends VuexModule { commitRoleListState(roleList: RoleEnum[]): void { this.roleListState = roleList; if (roleList) { - setSession(ROLES_KEY, roleList); + setLocal(ROLES_KEY, roleList, true); } } @@ -80,7 +78,7 @@ class User extends VuexModule { commitTokenState(info: string): void { this.tokenState = info; if (info) { - setSession(TOKEN_KEY, info); + setLocal(TOKEN_KEY, info, true); } } diff --git a/src/utils/helper/persistent.ts b/src/utils/helper/persistent.ts index 0b5fef32..89bb5e8f 100644 --- a/src/utils/helper/persistent.ts +++ b/src/utils/helper/persistent.ts @@ -27,9 +27,13 @@ function initCache() { } initCache(); -export function setLocal(key: string, value: any) { +export function setLocal(key: string, value: any, immediate = false) { cacheStore.local[BASE_LOCAL_CACHE_KEY] = cacheStore.local[BASE_LOCAL_CACHE_KEY] || {}; cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value; + if (immediate) { + const localCache = cacheStore.local; + ls.set(BASE_LOCAL_CACHE_KEY, localCache); + } } export function getLocal(key: string): T | null { @@ -49,9 +53,13 @@ export function clearLocal() { cacheStore.local = {}; } -export function setSession(key: string, value: any) { +export function setSession(key: string, value: any, immediate = false) { cacheStore.session[BASE_SESSION_CACHE_KEY] = cacheStore.session[BASE_SESSION_CACHE_KEY] || {}; cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value; + if (immediate) { + const cache = cacheStore.session; + ls.set(BASE_SESSION_CACHE_KEY, cache); + } } export function removeSession(key: string) { diff --git a/src/views/sys/login/Login.vue b/src/views/sys/login/Login.vue index 5ab58be6..5b0f2c61 100644 --- a/src/views/sys/login/Login.vue +++ b/src/views/sys/login/Login.vue @@ -11,19 +11,34 @@ - + - + + + + + + + 自动登录 + + + + + + 忘记密码 + + +