perf: 优化登录记住我流程
This commit is contained in:
parent
1f951d18c2
commit
2009594f08
|
@ -1,6 +1,6 @@
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { store } from '../index'
|
import { store } from '../index'
|
||||||
import { UserType } from '@/api/login/types'
|
import { UserLoginType, UserType } from '@/api/login/types'
|
||||||
import { ElMessageBox } from 'element-plus'
|
import { ElMessageBox } from 'element-plus'
|
||||||
import { useI18n } from '@/hooks/web/useI18n'
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
import { loginOutApi } from '@/api/login'
|
import { loginOutApi } from '@/api/login'
|
||||||
|
@ -12,6 +12,8 @@ interface UserState {
|
||||||
tokenKey: string
|
tokenKey: string
|
||||||
token: string
|
token: string
|
||||||
roleRouters?: string[] | AppCustomRouteRecordRaw[]
|
roleRouters?: string[] | AppCustomRouteRecordRaw[]
|
||||||
|
rememberMe: boolean
|
||||||
|
loginInfo?: UserLoginType
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', {
|
export const useUserStore = defineStore('user', {
|
||||||
|
@ -20,7 +22,10 @@ export const useUserStore = defineStore('user', {
|
||||||
userInfo: undefined,
|
userInfo: undefined,
|
||||||
tokenKey: 'Authorization',
|
tokenKey: 'Authorization',
|
||||||
token: '',
|
token: '',
|
||||||
roleRouters: undefined
|
roleRouters: undefined,
|
||||||
|
// 记住我
|
||||||
|
rememberMe: true,
|
||||||
|
loginInfo: undefined
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
|
@ -35,6 +40,12 @@ export const useUserStore = defineStore('user', {
|
||||||
},
|
},
|
||||||
getRoleRouters(): string[] | AppCustomRouteRecordRaw[] | undefined {
|
getRoleRouters(): string[] | AppCustomRouteRecordRaw[] | undefined {
|
||||||
return this.roleRouters
|
return this.roleRouters
|
||||||
|
},
|
||||||
|
getRememberMe(): boolean {
|
||||||
|
return this.rememberMe
|
||||||
|
},
|
||||||
|
getLoginInfo(): UserLoginType | undefined {
|
||||||
|
return this.loginInfo
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
|
@ -75,6 +86,12 @@ export const useUserStore = defineStore('user', {
|
||||||
},
|
},
|
||||||
logout() {
|
logout() {
|
||||||
this.reset()
|
this.reset()
|
||||||
|
},
|
||||||
|
setRememberMe(rememberMe: boolean) {
|
||||||
|
this.rememberMe = rememberMe
|
||||||
|
},
|
||||||
|
setLoginInfo(loginInfo: UserLoginType | undefined) {
|
||||||
|
this.loginInfo = loginInfo
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
persist: true
|
persist: true
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<script setup lang="tsx">
|
<script setup lang="tsx">
|
||||||
import { reactive, ref, watch } from 'vue'
|
import { reactive, ref, watch, onMounted, unref } from 'vue'
|
||||||
import { Form, FormSchema } from '@/components/Form'
|
import { Form, FormSchema } from '@/components/Form'
|
||||||
import { useI18n } from '@/hooks/web/useI18n'
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
import { ElCheckbox, ElLink } from 'element-plus'
|
import { ElCheckbox, ElLink } from 'element-plus'
|
||||||
|
@ -51,19 +51,19 @@ const schema = reactive<FormSchema[]>([
|
||||||
{
|
{
|
||||||
field: 'username',
|
field: 'username',
|
||||||
label: t('login.username'),
|
label: t('login.username'),
|
||||||
value: 'admin',
|
// value: 'admin',
|
||||||
component: 'Input',
|
component: 'Input',
|
||||||
colProps: {
|
colProps: {
|
||||||
span: 24
|
span: 24
|
||||||
},
|
},
|
||||||
componentProps: {
|
componentProps: {
|
||||||
placeholder: t('login.usernamePlaceholder')
|
placeholder: 'admin or test'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'password',
|
field: 'password',
|
||||||
label: t('login.password'),
|
label: t('login.password'),
|
||||||
value: 'admin',
|
// value: 'admin',
|
||||||
component: 'InputPassword',
|
component: 'InputPassword',
|
||||||
colProps: {
|
colProps: {
|
||||||
span: 24
|
span: 24
|
||||||
|
@ -72,7 +72,7 @@ const schema = reactive<FormSchema[]>([
|
||||||
style: {
|
style: {
|
||||||
width: '100%'
|
width: '100%'
|
||||||
},
|
},
|
||||||
placeholder: t('login.passwordPlaceholder')
|
placeholder: 'admin or test'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -186,10 +186,21 @@ const schema = reactive<FormSchema[]>([
|
||||||
|
|
||||||
const iconSize = 30
|
const iconSize = 30
|
||||||
|
|
||||||
const remember = ref(false)
|
const remember = ref(userStore.getRememberMe)
|
||||||
|
|
||||||
|
const initLoginInfo = () => {
|
||||||
|
const loginInfo = userStore.getLoginInfo
|
||||||
|
if (loginInfo) {
|
||||||
|
const { username, password } = loginInfo
|
||||||
|
setValues({ username, password })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onMounted(() => {
|
||||||
|
initLoginInfo()
|
||||||
|
})
|
||||||
|
|
||||||
const { formRegister, formMethods } = useForm()
|
const { formRegister, formMethods } = useForm()
|
||||||
const { getFormData, getElFormExpose } = formMethods
|
const { getFormData, getElFormExpose, setValues } = formMethods
|
||||||
|
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
|
|
||||||
|
@ -221,6 +232,16 @@ const signIn = async () => {
|
||||||
const res = await loginApi(formData)
|
const res = await loginApi(formData)
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
|
// 是否记住我
|
||||||
|
if (unref(remember)) {
|
||||||
|
userStore.setLoginInfo({
|
||||||
|
username: formData.username,
|
||||||
|
password: formData.password
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
userStore.setLoginInfo(undefined)
|
||||||
|
}
|
||||||
|
userStore.setRememberMe(unref(remember))
|
||||||
userStore.setUserInfo(res.data)
|
userStore.setUserInfo(res.data)
|
||||||
// 是否使用动态路由
|
// 是否使用动态路由
|
||||||
if (appStore.getDynamicRouter) {
|
if (appStore.getDynamicRouter) {
|
||||||
|
|
Loading…
Reference in New Issue