perf: 简化login代码

This commit is contained in:
xingyu 2023-01-05 14:21:07 +08:00
parent 6a9244a707
commit 2dd1e01183
3 changed files with 23 additions and 68 deletions

View File

@ -165,7 +165,7 @@ const filterSearchSchema = (crudSchema: VxeCrudSchema): VxeFormItemProps[] => {
// 添加搜索按钮
const buttons: VxeFormItemProps = {
span: 24,
align: 'center',
align: 'right',
collapseNode: searchSchema.length > spanLength,
itemRender: {
name: '$buttons',

View File

@ -36,45 +36,28 @@ export const formatToken = (token: string): string => {
}
// ========== 账号相关 ==========
const UsernameKey = 'USERNAME'
const PasswordKey = 'PASSWORD'
const RememberMeKey = 'REMEMBER_ME'
const LoginFormKey = 'LOGINFORM'
export const getUsername = () => {
return wsCache.get(UsernameKey)
export type LoginFormType = {
tenantName: string
username: string
password: string
rememberMe: boolean
}
export const setUsername = (username: string) => {
wsCache.set(UsernameKey, username, { exp: 30 * 24 * 60 * 60 })
export const getLoginForm = () => {
const loginForm: LoginFormType = wsCache.get(LoginFormKey)
loginForm.password = decrypt(loginForm.password) as string
return loginForm
}
export const removeUsername = () => {
wsCache.delete(UsernameKey)
export const setLoginForm = (loginForm: LoginFormType) => {
loginForm.password = encrypt(loginForm.password) as string
wsCache.set(LoginFormKey, loginForm, { exp: 30 * 24 * 60 * 60 })
}
export const getPassword = () => {
const password = wsCache.get(PasswordKey)
return password ? decrypt(password) : undefined
}
export const setPassword = (password: string) => {
wsCache.set(PasswordKey, encrypt(password), { exp: 30 * 24 * 60 * 60 })
}
export const removePassword = () => {
wsCache.delete(PasswordKey)
}
export const getRememberMe = () => {
return wsCache.get(RememberMeKey) === true
}
export const setRememberMe = (rememberMe: boolean) => {
wsCache.set(RememberMeKey, rememberMe, { exp: 30 * 24 * 60 * 60 })
}
export const removeRememberMe = () => {
wsCache.delete(RememberMeKey)
export const removeLoginForm = () => {
wsCache.delete(LoginFormKey)
}
// ========== 租户相关 ==========

View File

@ -148,7 +148,6 @@ import { useIcon } from '@/hooks/web/useIcon'
import { useMessage } from '@/hooks/web/useMessage'
import { required } from '@/utils/formRules'
import * as authUtil from '@/utils/auth'
import { decrypt } from '@/utils/jsencrypt'
import { Verify } from '@/components/Verifition'
import { usePermissionStore } from '@/store/modules/permission'
import * as LoginApi from '@/api/login'
@ -180,10 +179,6 @@ const loginData = reactive({
isShowPassword: false,
captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
token: '',
loading: {
signIn: false
},
loginForm: {
tenantName: '芋道源码',
username: 'admin',
@ -194,22 +189,10 @@ const loginData = reactive({
})
const socialList = [
{
icon: 'ant-design:github-filled',
type: 0
},
{
icon: 'ant-design:wechat-filled',
type: 30
},
{
icon: 'ant-design:alipay-circle-filled',
type: 0
},
{
icon: 'ant-design:dingtalk-circle-filled',
type: 20
}
{ icon: 'ant-design:github-filled', type: 0 },
{ icon: 'ant-design:wechat-filled', type: 30 },
{ icon: 'ant-design:alipay-circle-filled', type: 0 },
{ icon: 'ant-design:dingtalk-circle-filled', type: 20 }
]
//
@ -232,12 +215,7 @@ const getTenantId = async () => {
}
//
const getCookie = () => {
const username = authUtil.getUsername()
const password = authUtil.getPassword()
? decrypt(authUtil.getPassword() as unknown as string)
: undefined
const rememberMe = authUtil.getRememberMe()
const tenantName = authUtil.getTenantName()
const { username, password, rememberMe, tenantName } = authUtil.getLoginForm()
loginData.loginForm = {
...loginData.loginForm,
username: username ? username : loginData.loginForm.username,
@ -266,15 +244,9 @@ const handleLogin = async (params) => {
background: 'rgba(0, 0, 0, 0.7)'
})
if (loginData.loginForm.rememberMe) {
authUtil.setUsername(loginData.loginForm.username)
authUtil.setPassword(loginData.loginForm.password)
authUtil.setRememberMe(loginData.loginForm.rememberMe)
authUtil.setTenantName(loginData.loginForm.tenantName)
authUtil.setLoginForm(loginData.loginForm)
} else {
authUtil.removeUsername()
authUtil.removePassword()
authUtil.removeRememberMe()
authUtil.removeTenantName()
authUtil.removeLoginForm()
}
authUtil.setToken(res)
if (!redirect.value) {