mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
perf: 简化login代码
This commit is contained in:
parent
6a9244a707
commit
2dd1e01183
@ -165,7 +165,7 @@ const filterSearchSchema = (crudSchema: VxeCrudSchema): VxeFormItemProps[] => {
|
|||||||
// 添加搜索按钮
|
// 添加搜索按钮
|
||||||
const buttons: VxeFormItemProps = {
|
const buttons: VxeFormItemProps = {
|
||||||
span: 24,
|
span: 24,
|
||||||
align: 'center',
|
align: 'right',
|
||||||
collapseNode: searchSchema.length > spanLength,
|
collapseNode: searchSchema.length > spanLength,
|
||||||
itemRender: {
|
itemRender: {
|
||||||
name: '$buttons',
|
name: '$buttons',
|
||||||
|
@ -36,45 +36,28 @@ export const formatToken = (token: string): string => {
|
|||||||
}
|
}
|
||||||
// ========== 账号相关 ==========
|
// ========== 账号相关 ==========
|
||||||
|
|
||||||
const UsernameKey = 'USERNAME'
|
const LoginFormKey = 'LOGINFORM'
|
||||||
const PasswordKey = 'PASSWORD'
|
|
||||||
const RememberMeKey = 'REMEMBER_ME'
|
|
||||||
|
|
||||||
export const getUsername = () => {
|
export type LoginFormType = {
|
||||||
return wsCache.get(UsernameKey)
|
tenantName: string
|
||||||
|
username: string
|
||||||
|
password: string
|
||||||
|
rememberMe: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const setUsername = (username: string) => {
|
export const getLoginForm = () => {
|
||||||
wsCache.set(UsernameKey, username, { exp: 30 * 24 * 60 * 60 })
|
const loginForm: LoginFormType = wsCache.get(LoginFormKey)
|
||||||
|
loginForm.password = decrypt(loginForm.password) as string
|
||||||
|
return loginForm
|
||||||
}
|
}
|
||||||
|
|
||||||
export const removeUsername = () => {
|
export const setLoginForm = (loginForm: LoginFormType) => {
|
||||||
wsCache.delete(UsernameKey)
|
loginForm.password = encrypt(loginForm.password) as string
|
||||||
|
wsCache.set(LoginFormKey, loginForm, { exp: 30 * 24 * 60 * 60 })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getPassword = () => {
|
export const removeLoginForm = () => {
|
||||||
const password = wsCache.get(PasswordKey)
|
wsCache.delete(LoginFormKey)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== 租户相关 ==========
|
// ========== 租户相关 ==========
|
||||||
|
@ -148,7 +148,6 @@ import { useIcon } from '@/hooks/web/useIcon'
|
|||||||
import { useMessage } from '@/hooks/web/useMessage'
|
import { useMessage } from '@/hooks/web/useMessage'
|
||||||
import { required } from '@/utils/formRules'
|
import { required } from '@/utils/formRules'
|
||||||
import * as authUtil from '@/utils/auth'
|
import * as authUtil from '@/utils/auth'
|
||||||
import { decrypt } from '@/utils/jsencrypt'
|
|
||||||
import { Verify } from '@/components/Verifition'
|
import { Verify } from '@/components/Verifition'
|
||||||
import { usePermissionStore } from '@/store/modules/permission'
|
import { usePermissionStore } from '@/store/modules/permission'
|
||||||
import * as LoginApi from '@/api/login'
|
import * as LoginApi from '@/api/login'
|
||||||
@ -180,10 +179,6 @@ const loginData = reactive({
|
|||||||
isShowPassword: false,
|
isShowPassword: false,
|
||||||
captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
|
captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
|
||||||
tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
|
tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
|
||||||
token: '',
|
|
||||||
loading: {
|
|
||||||
signIn: false
|
|
||||||
},
|
|
||||||
loginForm: {
|
loginForm: {
|
||||||
tenantName: '芋道源码',
|
tenantName: '芋道源码',
|
||||||
username: 'admin',
|
username: 'admin',
|
||||||
@ -194,22 +189,10 @@ const loginData = reactive({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const socialList = [
|
const socialList = [
|
||||||
{
|
{ icon: 'ant-design:github-filled', type: 0 },
|
||||||
icon: 'ant-design:github-filled',
|
{ icon: 'ant-design:wechat-filled', type: 30 },
|
||||||
type: 0
|
{ icon: 'ant-design:alipay-circle-filled', type: 0 },
|
||||||
},
|
{ icon: 'ant-design:dingtalk-circle-filled', type: 20 }
|
||||||
{
|
|
||||||
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 getCookie = () => {
|
||||||
const username = authUtil.getUsername()
|
const { username, password, rememberMe, tenantName } = authUtil.getLoginForm()
|
||||||
const password = authUtil.getPassword()
|
|
||||||
? decrypt(authUtil.getPassword() as unknown as string)
|
|
||||||
: undefined
|
|
||||||
const rememberMe = authUtil.getRememberMe()
|
|
||||||
const tenantName = authUtil.getTenantName()
|
|
||||||
loginData.loginForm = {
|
loginData.loginForm = {
|
||||||
...loginData.loginForm,
|
...loginData.loginForm,
|
||||||
username: username ? username : loginData.loginForm.username,
|
username: username ? username : loginData.loginForm.username,
|
||||||
@ -266,15 +244,9 @@ const handleLogin = async (params) => {
|
|||||||
background: 'rgba(0, 0, 0, 0.7)'
|
background: 'rgba(0, 0, 0, 0.7)'
|
||||||
})
|
})
|
||||||
if (loginData.loginForm.rememberMe) {
|
if (loginData.loginForm.rememberMe) {
|
||||||
authUtil.setUsername(loginData.loginForm.username)
|
authUtil.setLoginForm(loginData.loginForm)
|
||||||
authUtil.setPassword(loginData.loginForm.password)
|
|
||||||
authUtil.setRememberMe(loginData.loginForm.rememberMe)
|
|
||||||
authUtil.setTenantName(loginData.loginForm.tenantName)
|
|
||||||
} else {
|
} else {
|
||||||
authUtil.removeUsername()
|
authUtil.removeLoginForm()
|
||||||
authUtil.removePassword()
|
|
||||||
authUtil.removeRememberMe()
|
|
||||||
authUtil.removeTenantName()
|
|
||||||
}
|
}
|
||||||
authUtil.setToken(res)
|
authUtil.setToken(res)
|
||||||
if (!redirect.value) {
|
if (!redirect.value) {
|
||||||
|
Loading…
Reference in New Issue
Block a user