Commit c8021ef3253765059b0aacb955158642072640c5

Authored by vben
1 parent 4f98978e

chore: changed login cache from sessionStorage to LocalStorage

CHANGELOG.zh_CN.md
@@ -4,6 +4,10 @@ @@ -4,6 +4,10 @@
4 4
5 - 全局 loading 添加文本 5 - 全局 loading 添加文本
6 6
  7 +### 🎫 Chores
  8 +
  9 +- 登录缓存从 sessionStorage 改为 LocalStorage
  10 +
7 ### ⚡ Performance Improvements 11 ### ⚡ Performance Improvements
8 12
9 - Layout 界面布局样式调整 13 - Layout 界面布局样式调整
src/setup/ant-design-vue/index.ts
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 2
3 import type { App } from 'vue'; 3 import type { App } from 'vue';
4 4
5 -import { Form, Input } from 'ant-design-vue'; 5 +import { Form, Input, Row, Col } from 'ant-design-vue';
6 import 'ant-design-vue/dist/antd.less'; 6 import 'ant-design-vue/dist/antd.less';
7 7
8 import './spin'; 8 import './spin';
@@ -11,4 +11,6 @@ export function setupAntd(app: App<Element>) { @@ -11,4 +11,6 @@ export function setupAntd(app: App<Element>) {
11 // 这两个组件在登录也就用。全局注册 11 // 这两个组件在登录也就用。全局注册
12 app.use(Form); 12 app.use(Form);
13 app.use(Input); 13 app.use(Input);
  14 + app.use(Row);
  15 + app.use(Col);
14 } 16 }
src/store/modules/user.ts
@@ -21,7 +21,7 @@ import { tabStore } from './tab'; @@ -21,7 +21,7 @@ import { tabStore } from './tab';
21 21
22 import { loginApi, getUserInfoById } from '/@/api/sys/user'; 22 import { loginApi, getUserInfoById } from '/@/api/sys/user';
23 23
24 -import { setSession, getSession, clearSession, clearLocal } from '/@/utils/helper/persistent'; 24 +import { setLocal, getLocal, clearSession, clearLocal } from '/@/utils/helper/persistent';
25 // import { FULL_PAGE_NOT_FOUND_ROUTE } from '/@/router/constant'; 25 // import { FULL_PAGE_NOT_FOUND_ROUTE } from '/@/router/constant';
26 26
27 export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>; 27 export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>;
@@ -40,17 +40,15 @@ class User extends VuexModule { @@ -40,17 +40,15 @@ class User extends VuexModule {
40 private roleListState: RoleEnum[] = []; 40 private roleListState: RoleEnum[] = [];
41 41
42 get getUserInfoState(): UserInfo { 42 get getUserInfoState(): UserInfo {
43 - return this.userInfoState || (getSession(USER_INFO_KEY) as UserInfo) || {}; 43 + return this.userInfoState || (getLocal(USER_INFO_KEY) as UserInfo) || {};
44 } 44 }
45 45
46 get getTokenState(): string { 46 get getTokenState(): string {
47 - return this.tokenState || (getSession(TOKEN_KEY) as string); 47 + return this.tokenState || (getLocal(TOKEN_KEY) as string);
48 } 48 }
49 49
50 get getRoleListState(): RoleEnum[] { 50 get getRoleListState(): RoleEnum[] {
51 - return this.roleListState.length > 0  
52 - ? this.roleListState  
53 - : (getSession(ROLES_KEY) as RoleEnum[]); 51 + return this.roleListState.length > 0 ? this.roleListState : (getLocal(ROLES_KEY) as RoleEnum[]);
54 } 52 }
55 53
56 @Mutation 54 @Mutation
@@ -64,7 +62,7 @@ class User extends VuexModule { @@ -64,7 +62,7 @@ class User extends VuexModule {
64 commitUserInfoState(info: UserInfo): void { 62 commitUserInfoState(info: UserInfo): void {
65 this.userInfoState = info; 63 this.userInfoState = info;
66 if (info) { 64 if (info) {
67 - setSession(USER_INFO_KEY, info); 65 + setLocal(USER_INFO_KEY, info, true);
68 } 66 }
69 } 67 }
70 68
@@ -72,7 +70,7 @@ class User extends VuexModule { @@ -72,7 +70,7 @@ class User extends VuexModule {
72 commitRoleListState(roleList: RoleEnum[]): void { 70 commitRoleListState(roleList: RoleEnum[]): void {
73 this.roleListState = roleList; 71 this.roleListState = roleList;
74 if (roleList) { 72 if (roleList) {
75 - setSession(ROLES_KEY, roleList); 73 + setLocal(ROLES_KEY, roleList, true);
76 } 74 }
77 } 75 }
78 76
@@ -80,7 +78,7 @@ class User extends VuexModule { @@ -80,7 +78,7 @@ class User extends VuexModule {
80 commitTokenState(info: string): void { 78 commitTokenState(info: string): void {
81 this.tokenState = info; 79 this.tokenState = info;
82 if (info) { 80 if (info) {
83 - setSession(TOKEN_KEY, info); 81 + setLocal(TOKEN_KEY, info, true);
84 } 82 }
85 } 83 }
86 84
src/utils/helper/persistent.ts
@@ -27,9 +27,13 @@ function initCache() { @@ -27,9 +27,13 @@ function initCache() {
27 } 27 }
28 initCache(); 28 initCache();
29 29
30 -export function setLocal(key: string, value: any) { 30 +export function setLocal(key: string, value: any, immediate = false) {
31 cacheStore.local[BASE_LOCAL_CACHE_KEY] = cacheStore.local[BASE_LOCAL_CACHE_KEY] || {}; 31 cacheStore.local[BASE_LOCAL_CACHE_KEY] = cacheStore.local[BASE_LOCAL_CACHE_KEY] || {};
32 cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value; 32 cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
  33 + if (immediate) {
  34 + const localCache = cacheStore.local;
  35 + ls.set(BASE_LOCAL_CACHE_KEY, localCache);
  36 + }
33 } 37 }
34 38
35 export function getLocal<T>(key: string): T | null { 39 export function getLocal<T>(key: string): T | null {
@@ -49,9 +53,13 @@ export function clearLocal() { @@ -49,9 +53,13 @@ export function clearLocal() {
49 cacheStore.local = {}; 53 cacheStore.local = {};
50 } 54 }
51 55
52 -export function setSession(key: string, value: any) { 56 +export function setSession(key: string, value: any, immediate = false) {
53 cacheStore.session[BASE_SESSION_CACHE_KEY] = cacheStore.session[BASE_SESSION_CACHE_KEY] || {}; 57 cacheStore.session[BASE_SESSION_CACHE_KEY] = cacheStore.session[BASE_SESSION_CACHE_KEY] || {};
54 cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value; 58 cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
  59 + if (immediate) {
  60 + const cache = cacheStore.session;
  61 + ls.set(BASE_SESSION_CACHE_KEY, cache);
  62 + }
55 } 63 }
56 64
57 export function removeSession(key: string) { 65 export function removeSession(key: string) {
src/views/sys/login/Login.vue
@@ -11,19 +11,34 @@ @@ -11,19 +11,34 @@
11 11
12 <a-form class="mx-auto mt-10" :model="formData" :rules="formRules" ref="formRef"> 12 <a-form class="mx-auto mt-10" :model="formData" :rules="formRules" ref="formRef">
13 <a-form-item name="account"> 13 <a-form-item name="account">
14 - <a-input size="large" v-model:value="formData.account" placeholder="vben" /> 14 + <a-input size="large" v-model:value="formData.account" placeholder="Username: vben" />
15 </a-form-item> 15 </a-form-item>
16 <a-form-item name="password"> 16 <a-form-item name="password">
17 <a-input-password 17 <a-input-password
18 size="large" 18 size="large"
19 visibilityToggle 19 visibilityToggle
20 v-model:value="formData.password" 20 v-model:value="formData.password"
21 - placeholder="123456" 21 + placeholder="Password: 123456"
22 /> 22 />
23 </a-form-item> 23 </a-form-item>
24 - <a-form-item name="verify" v-if="openLoginVerify"> 24 +
  25 + <!-- <a-form-item name="verify" v-if="openLoginVerify">
25 <BasicDragVerify v-model:value="formData.verify" ref="verifyRef" /> 26 <BasicDragVerify v-model:value="formData.verify" ref="verifyRef" />
26 - </a-form-item> 27 + </a-form-item> -->
  28 + <a-row>
  29 + <a-col :span="12">
  30 + <a-form-item>
  31 + <!-- 未做逻辑,需要自行处理 -->
  32 + <a-checkbox v-model:checked="autoLogin" size="small">自动登录</a-checkbox>
  33 + </a-form-item>
  34 + </a-col>
  35 + <a-col :span="12">
  36 + <a-form-item :style="{ 'text-align': 'right' }">
  37 + <!-- 未做逻辑,需要自行处理 -->
  38 + <a-button type="link" size="small">忘记密码</a-button>
  39 + </a-form-item>
  40 + </a-col>
  41 + </a-row>
27 <a-form-item> 42 <a-form-item>
28 <a-button 43 <a-button
29 type="primary" 44 type="primary"
@@ -42,28 +57,44 @@ @@ -42,28 +57,44 @@
42 </div> 57 </div>
43 </template> 58 </template>
44 <script lang="ts"> 59 <script lang="ts">
45 - import { defineComponent, reactive, ref, unref, toRaw, computed } from 'vue';  
46 - import { BasicDragVerify, DragVerifyActionType } from '/@/components/Verify/index'; 60 + import {
  61 + defineComponent,
  62 + reactive,
  63 + ref,
  64 + unref,
  65 + toRaw,
  66 + // computed
  67 + } from 'vue';
  68 + import { Checkbox } from 'ant-design-vue';
  69 +
  70 + import Button from '/@/components/Button/index.vue';
  71 + // import { BasicDragVerify, DragVerifyActionType } from '/@/components/Verify/index';
  72 +
47 import { userStore } from '/@/store/modules/user'; 73 import { userStore } from '/@/store/modules/user';
48 - import { appStore } from '/@/store/modules/app'; 74 + // import { appStore } from '/@/store/modules/app';
49 import { useMessage } from '/@/hooks/web/useMessage'; 75 import { useMessage } from '/@/hooks/web/useMessage';
50 import { useSetting } from '/@/hooks/core/useSetting'; 76 import { useSetting } from '/@/hooks/core/useSetting';
51 - import Button from '/@/components/Button/index.vue';  
52 77
53 export default defineComponent({ 78 export default defineComponent({
54 - components: { BasicDragVerify, AButton: Button }, 79 + components: {
  80 + // BasicDragVerify,
  81 + AButton: Button,
  82 + ACheckbox: Checkbox,
  83 + },
55 setup() { 84 setup() {
  85 + const formRef = ref<any>(null);
  86 + const autoLoginRef = ref(false);
  87 + // const verifyRef = ref<RefInstanceType<DragVerifyActionType>>(null);
  88 +
56 const { globSetting } = useSetting(); 89 const { globSetting } = useSetting();
57 const { notification } = useMessage(); 90 const { notification } = useMessage();
58 - const formRef = ref<any>(null);  
59 - const verifyRef = ref<RefInstanceType<DragVerifyActionType>>(null);  
60 91
61 - const openLoginVerifyRef = computed(() => appStore.getProjectConfig.openLoginVerify); 92 + // const openLoginVerifyRef = computed(() => appStore.getProjectConfig.openLoginVerify);
62 93
63 const formData = reactive({ 94 const formData = reactive({
64 account: 'vben', 95 account: 'vben',
65 password: '123456', 96 password: '123456',
66 - verify: undefined, 97 + // verify: undefined,
67 }); 98 });
68 const formState = reactive({ 99 const formState = reactive({
69 loading: false, 100 loading: false,
@@ -72,15 +103,15 @@ @@ -72,15 +103,15 @@
72 const formRules = reactive({ 103 const formRules = reactive({
73 account: [{ required: true, message: '请输入账号', trigger: 'blur' }], 104 account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
74 password: [{ required: true, message: '请输入密码', trigger: 'blur' }], 105 password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
75 - verify: unref(openLoginVerifyRef) ? [{ required: true, message: '请通过验证码校验' }] : [], 106 + // verify: unref(openLoginVerifyRef) ? [{ required: true, message: '请通过验证码校验' }] : [],
76 }); 107 });
77 108
78 - function resetVerify() {  
79 - const verify = unref(verifyRef);  
80 - if (!verify) return;  
81 - formData.verify && verify.$.resume();  
82 - formData.verify = undefined;  
83 - } 109 + // function resetVerify() {
  110 + // const verify = unref(verifyRef);
  111 + // if (!verify) return;
  112 + // formData.verify && verify.$.resume();
  113 + // formData.verify = undefined;
  114 + // }
84 115
85 async function handleLogin() { 116 async function handleLogin() {
86 const form = unref(formRef); 117 const form = unref(formRef);
@@ -103,19 +134,20 @@ @@ -103,19 +134,20 @@
103 } 134 }
104 } catch (error) { 135 } catch (error) {
105 } finally { 136 } finally {
106 - resetVerify(); 137 + // resetVerify();
107 formState.loading = false; 138 formState.loading = false;
108 } 139 }
109 } 140 }
110 141
111 return { 142 return {
112 formRef, 143 formRef,
113 - verifyRef, 144 + // verifyRef,
114 formData, 145 formData,
115 formState, 146 formState,
116 formRules, 147 formRules,
117 login: handleLogin, 148 login: handleLogin,
118 - openLoginVerify: openLoginVerifyRef, 149 + autoLogin: autoLoginRef,
  150 + // openLoginVerify: openLoginVerifyRef,
119 title: globSetting && globSetting.title, 151 title: globSetting && globSetting.title,
120 }; 152 };
121 }, 153 },
@@ -141,13 +173,13 @@ @@ -141,13 +173,13 @@
141 } 173 }
142 174
143 &-form { 175 &-form {
144 - width: 100%; 176 + width: 520px;
145 background: @white; 177 background: @white;
146 border: 10px solid rgba(255, 255, 255, 0.5); 178 border: 10px solid rgba(255, 255, 255, 0.5);
147 border-width: 8px; 179 border-width: 8px;
148 border-radius: 4px; 180 border-radius: 4px;
149 background-clip: padding-box; 181 background-clip: padding-box;
150 - .respond-to(xlarge, { margin: 0 56px}); 182 + .respond-to(xlarge, { margin: 0 120px 0 50px});
151 183
152 &-wrap { 184 &-wrap {
153 position: absolute; 185 position: absolute;
@@ -155,14 +187,14 @@ @@ -155,14 +187,14 @@
155 right: 0; 187 right: 0;
156 display: flex; 188 display: flex;
157 width: 100%; 189 width: 100%;
158 - height: 100%; 190 + height: 90%;
159 justify-content: center; 191 justify-content: center;
160 align-items: center; 192 align-items: center;
161 .respond-to(large, { 193 .respond-to(large, {
162 - width: 520px;  
163 - right: calc(50% - 260px); 194 + width: 600px;
  195 + right: calc(50% - 300px);
164 }); 196 });
165 - .respond-to(xlarge, { width: 520px; right:0}); 197 + .respond-to(xlarge, { width: 600px; right:0});
166 } 198 }
167 199
168 &__content { 200 &__content {
@@ -178,13 +210,13 @@ @@ -178,13 +210,13 @@
178 210
179 img { 211 img {
180 display: inline-block; 212 display: inline-block;
181 - width: 64px; 213 + width: 48px;
182 } 214 }
183 215
184 h1 { 216 h1 {
185 margin-bottom: 0; 217 margin-bottom: 0;
186 font-size: 24px; 218 font-size: 24px;
187 - color: @primary-color; 219 + // color: @primary-color;
188 text-align: center; 220 text-align: center;
189 } 221 }
190 } 222 }