Skip to content

Commit 1bc4eef

Browse files
committed
修复记住密码失效的问题
1 parent 560a336 commit 1bc4eef

File tree

4 files changed

+19
-47
lines changed

4 files changed

+19
-47
lines changed

src/hooks/web/useCache.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import WebStorageCache from 'web-storage-cache'
77
type CacheType = 'localStorage' | 'sessionStorage'
88

99
export const CACHE_KEY = {
10-
IS_DARK: 'isDark',
10+
// 用户相关
11+
ROLE_ROUTERS: 'roleRouters',
1112
USER: 'user',
13+
// 系统设置
14+
IS_DARK: 'isDark',
1215
LANG: 'lang',
1316
THEME: 'theme',
1417
LAYOUT: 'layout',
15-
ROLE_ROUTERS: 'roleRouters',
16-
DICT_CACHE: 'dictCache'
18+
DICT_CACHE: 'dictCache',
19+
// 登录表单
20+
LoginForm: 'loginForm',
21+
TenantId: 'tenantId'
1722
}
1823

1924
export const useCache = (type: CacheType = 'localStorage') => {
@@ -30,4 +35,5 @@ export const deleteUserCache = () => {
3035
const { wsCache } = useCache()
3136
wsCache.delete(CACHE_KEY.USER)
3237
wsCache.delete(CACHE_KEY.ROLE_ROUTERS)
38+
// 注意,不要清理 LoginForm 登录表单
3339
}

src/store/modules/user.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,12 @@ interface UserVO {
1313
deptId: number
1414
}
1515

16-
interface RememberMeInfo {
17-
enable: boolean // 是否记住我
18-
username: string
19-
password: string
20-
}
21-
2216
interface UserInfoVO {
2317
// USER 缓存
2418
permissions: string[]
2519
roles: string[]
2620
isSetUser: boolean
2721
user: UserVO
28-
// REMEMBER_ME 缓存
29-
rememberMe: RememberMeInfo
3022
}
3123

3224
export const useUserStore = defineStore('admin-user', {
@@ -39,11 +31,6 @@ export const useUserStore = defineStore('admin-user', {
3931
avatar: '',
4032
nickname: '',
4133
deptId: 0
42-
},
43-
rememberMe: {
44-
enable: true,
45-
username: '',
46-
password: ''
4734
}
4835
}),
4936
getters: {

src/utils/auth.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCache } from '@/hooks/web/useCache'
1+
import { useCache, CACHE_KEY } from '@/hooks/web/useCache'
22
import { TokenType } from '@/api/login/types'
33
import { decrypt, encrypt } from '@/utils/jsencrypt'
44

@@ -36,8 +36,6 @@ export const formatToken = (token: string): string => {
3636
}
3737
// ========== 账号相关 ==========
3838

39-
const LoginFormKey = 'LOGINFORM'
40-
4139
export type LoginFormType = {
4240
tenantName: string
4341
username: string
@@ -46,7 +44,7 @@ export type LoginFormType = {
4644
}
4745

4846
export const getLoginForm = () => {
49-
const loginForm: LoginFormType = wsCache.get(LoginFormKey)
47+
const loginForm: LoginFormType = wsCache.get(CACHE_KEY.LoginForm)
5048
if (loginForm) {
5149
loginForm.password = decrypt(loginForm.password) as string
5250
}
@@ -55,38 +53,19 @@ export const getLoginForm = () => {
5553

5654
export const setLoginForm = (loginForm: LoginFormType) => {
5755
loginForm.password = encrypt(loginForm.password) as string
58-
wsCache.set(LoginFormKey, loginForm, { exp: 30 * 24 * 60 * 60 })
56+
wsCache.set(CACHE_KEY.LoginForm, loginForm, { exp: 30 * 24 * 60 * 60 })
5957
}
6058

6159
export const removeLoginForm = () => {
62-
wsCache.delete(LoginFormKey)
60+
wsCache.delete(CACHE_KEY.LoginForm)
6361
}
6462

6563
// ========== 租户相关 ==========
6664

67-
const TenantIdKey = 'TENANT_ID'
68-
const TenantNameKey = 'TENANT_NAME'
69-
70-
export const getTenantName = () => {
71-
return wsCache.get(TenantNameKey)
72-
}
73-
74-
export const setTenantName = (username: string) => {
75-
wsCache.set(TenantNameKey, username, { exp: 30 * 24 * 60 * 60 })
76-
}
77-
78-
export const removeTenantName = () => {
79-
wsCache.delete(TenantNameKey)
80-
}
81-
8265
export const getTenantId = () => {
83-
return wsCache.get(TenantIdKey)
66+
return wsCache.get(CACHE_KEY.TenantId)
8467
}
8568

8669
export const setTenantId = (username: string) => {
87-
wsCache.set(TenantIdKey, username)
88-
}
89-
90-
export const removeTenantId = () => {
91-
wsCache.delete(TenantIdKey)
70+
wsCache.set(CACHE_KEY.TenantId, username)
9271
}

src/views/Login/components/LoginForm.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const loginData = reactive({
188188
username: 'admin',
189189
password: 'admin123',
190190
captchaVerification: '',
191-
rememberMe: false
191+
rememberMe: true // 默认记录我。如果不需要,可手动修改
192192
}
193193
})
194194
@@ -218,14 +218,14 @@ const getTenantId = async () => {
218218
}
219219
}
220220
// 记住我
221-
const getCookie = () => {
221+
const getLoginFormCache = () => {
222222
const loginForm = authUtil.getLoginForm()
223223
if (loginForm) {
224224
loginData.loginForm = {
225225
...loginData.loginForm,
226226
username: loginForm.username ? loginForm.username : loginData.loginForm.username,
227227
password: loginForm.password ? loginForm.password : loginData.loginForm.password,
228-
rememberMe: loginForm.rememberMe ? true : false,
228+
rememberMe: loginForm.rememberMe,
229229
tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
230230
}
231231
}
@@ -320,7 +320,7 @@ watch(
320320
}
321321
)
322322
onMounted(() => {
323-
getCookie()
323+
getLoginFormCache()
324324
getTenantByWebsite()
325325
})
326326
</script>

0 commit comments

Comments
 (0)