Commit 2dd1e01183c6e2c356c8e253683f88579e2d4417

Authored by xingyu
1 parent 6a9244a7

perf: 简化login代码

yudao-ui-admin-vue3/src/hooks/web/useVxeCrudSchemas.ts
... ... @@ -165,7 +165,7 @@ const filterSearchSchema = (crudSchema: VxeCrudSchema): VxeFormItemProps[] => {
165 165 // 添加搜索按钮
166 166 const buttons: VxeFormItemProps = {
167 167 span: 24,
168   - align: 'center',
  168 + align: 'right',
169 169 collapseNode: searchSchema.length > spanLength,
170 170 itemRender: {
171 171 name: '$buttons',
... ...
yudao-ui-admin-vue3/src/utils/auth.ts
... ... @@ -36,45 +36,28 @@ export const formatToken = (token: string): string => {
36 36 }
37 37 // ========== 账号相关 ==========
38 38  
39   -const UsernameKey = 'USERNAME'
40   -const PasswordKey = 'PASSWORD'
41   -const RememberMeKey = 'REMEMBER_ME'
  39 +const LoginFormKey = 'LOGINFORM'
42 40  
43   -export const getUsername = () => {
44   - return wsCache.get(UsernameKey)
  41 +export type LoginFormType = {
  42 + tenantName: string
  43 + username: string
  44 + password: string
  45 + rememberMe: boolean
45 46 }
46 47  
47   -export const setUsername = (username: string) => {
48   - wsCache.set(UsernameKey, username, { exp: 30 * 24 * 60 * 60 })
  48 +export const getLoginForm = () => {
  49 + const loginForm: LoginFormType = wsCache.get(LoginFormKey)
  50 + loginForm.password = decrypt(loginForm.password) as string
  51 + return loginForm
49 52 }
50 53  
51   -export const removeUsername = () => {
52   - wsCache.delete(UsernameKey)
  54 +export const setLoginForm = (loginForm: LoginFormType) => {
  55 + loginForm.password = encrypt(loginForm.password) as string
  56 + wsCache.set(LoginFormKey, loginForm, { exp: 30 * 24 * 60 * 60 })
53 57 }
54 58  
55   -export const getPassword = () => {
56   - const password = wsCache.get(PasswordKey)
57   - return password ? decrypt(password) : undefined
58   -}
59   -
60   -export const setPassword = (password: string) => {
61   - wsCache.set(PasswordKey, encrypt(password), { exp: 30 * 24 * 60 * 60 })
62   -}
63   -
64   -export const removePassword = () => {
65   - wsCache.delete(PasswordKey)
66   -}
67   -
68   -export const getRememberMe = () => {
69   - return wsCache.get(RememberMeKey) === true
70   -}
71   -
72   -export const setRememberMe = (rememberMe: boolean) => {
73   - wsCache.set(RememberMeKey, rememberMe, { exp: 30 * 24 * 60 * 60 })
74   -}
75   -
76   -export const removeRememberMe = () => {
77   - wsCache.delete(RememberMeKey)
  59 +export const removeLoginForm = () => {
  60 + wsCache.delete(LoginFormKey)
78 61 }
79 62  
80 63 // ========== 租户相关 ==========
... ...
yudao-ui-admin-vue3/src/views/Login/components/LoginForm.vue
... ... @@ -148,7 +148,6 @@ import { useIcon } from '@/hooks/web/useIcon'
148 148 import { useMessage } from '@/hooks/web/useMessage'
149 149 import { required } from '@/utils/formRules'
150 150 import * as authUtil from '@/utils/auth'
151   -import { decrypt } from '@/utils/jsencrypt'
152 151 import { Verify } from '@/components/Verifition'
153 152 import { usePermissionStore } from '@/store/modules/permission'
154 153 import * as LoginApi from '@/api/login'
... ... @@ -180,10 +179,6 @@ const loginData = reactive({
180 179 isShowPassword: false,
181 180 captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
182 181 tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
183   - token: '',
184   - loading: {
185   - signIn: false
186   - },
187 182 loginForm: {
188 183 tenantName: '芋道源码',
189 184 username: 'admin',
... ... @@ -194,22 +189,10 @@ const loginData = reactive({
194 189 })
195 190  
196 191 const socialList = [
197   - {
198   - icon: 'ant-design:github-filled',
199   - type: 0
200   - },
201   - {
202   - icon: 'ant-design:wechat-filled',
203   - type: 30
204   - },
205   - {
206   - icon: 'ant-design:alipay-circle-filled',
207   - type: 0
208   - },
209   - {
210   - icon: 'ant-design:dingtalk-circle-filled',
211   - type: 20
212   - }
  192 + { icon: 'ant-design:github-filled', type: 0 },
  193 + { icon: 'ant-design:wechat-filled', type: 30 },
  194 + { icon: 'ant-design:alipay-circle-filled', type: 0 },
  195 + { icon: 'ant-design:dingtalk-circle-filled', type: 20 }
213 196 ]
214 197  
215 198 // 获取验证码
... ... @@ -232,12 +215,7 @@ const getTenantId = async () => {
232 215 }
233 216 // 记住我
234 217 const getCookie = () => {
235   - const username = authUtil.getUsername()
236   - const password = authUtil.getPassword()
237   - ? decrypt(authUtil.getPassword() as unknown as string)
238   - : undefined
239   - const rememberMe = authUtil.getRememberMe()
240   - const tenantName = authUtil.getTenantName()
  218 + const { username, password, rememberMe, tenantName } = authUtil.getLoginForm()
241 219 loginData.loginForm = {
242 220 ...loginData.loginForm,
243 221 username: username ? username : loginData.loginForm.username,
... ... @@ -266,15 +244,9 @@ const handleLogin = async (params) => {
266 244 background: 'rgba(0, 0, 0, 0.7)'
267 245 })
268 246 if (loginData.loginForm.rememberMe) {
269   - authUtil.setUsername(loginData.loginForm.username)
270   - authUtil.setPassword(loginData.loginForm.password)
271   - authUtil.setRememberMe(loginData.loginForm.rememberMe)
272   - authUtil.setTenantName(loginData.loginForm.tenantName)
  247 + authUtil.setLoginForm(loginData.loginForm)
273 248 } else {
274   - authUtil.removeUsername()
275   - authUtil.removePassword()
276   - authUtil.removeRememberMe()
277   - authUtil.removeTenantName()
  249 + authUtil.removeLoginForm()
278 250 }
279 251 authUtil.setToken(res)
280 252 if (!redirect.value) {
... ...