feat(login): 增加记住账号功能

This commit is contained in:
chen.home 2023-01-13 23:41:29 +08:00
parent 310dc43830
commit bba1b16e1a
3 changed files with 42 additions and 20 deletions

View File

@ -1,44 +1,44 @@
import { loacl } from './storage'; import { local } from './storage';
import { EnumStorageKey } from '@/enum'; import { EnumStorageKey } from '@/enum';
const DURATION = 6 * 60 * 60; const DURATION = 6 * 60 * 60;
/* 获取当前token */ /* 获取当前token */
export function getToken() { export function getToken() {
return loacl.get(EnumStorageKey.token); return local.get(EnumStorageKey.token);
} }
/* 设置token */ /* 设置token */
export function setToken(data: string) { export function setToken(data: string) {
loacl.set(EnumStorageKey.token, data, DURATION); local.set(EnumStorageKey.token, data, DURATION);
} }
/* 移除token */ /* 移除token */
export function removeToken() { export function removeToken() {
loacl.remove(EnumStorageKey.token); local.remove(EnumStorageKey.token);
} }
/* 获取当前refreshToken */ /* 获取当前refreshToken */
export function getRefreshToken() { export function getRefreshToken() {
return loacl.get(EnumStorageKey.refreshToken); return local.get(EnumStorageKey.refreshToken);
} }
/* 设置refreshToken */ /* 设置refreshToken */
export function setRefreshToken(data: string) { export function setRefreshToken(data: string) {
loacl.set(EnumStorageKey.refreshToken, data, DURATION); local.set(EnumStorageKey.refreshToken, data, DURATION);
} }
/* 移除refreshToken */ /* 移除refreshToken */
export function removeRefreshToken() { export function removeRefreshToken() {
loacl.remove(EnumStorageKey.refreshToken); local.remove(EnumStorageKey.refreshToken);
} }
/* 获取用户详情 */ /* 获取用户详情 */
export function getUserInfo() { export function getUserInfo() {
return loacl.get(EnumStorageKey.userInfo); return local.get(EnumStorageKey.userInfo);
} }
/* 设置用户详情 */ /* 设置用户详情 */
export function setUserInfo(data: any) { export function setUserInfo(data: any) {
loacl.set(EnumStorageKey.userInfo, data); local.set(EnumStorageKey.userInfo, data);
} }
/* 移除用户详情 */ /* 移除用户详情 */
export function removeUserInfo() { export function removeUserInfo() {
loacl.remove(EnumStorageKey.userInfo); local.remove(EnumStorageKey.userInfo);
} }
/** 去除用户相关缓存 */ /** 去除用户相关缓存 */

View File

@ -34,7 +34,7 @@ function createLocalStorage() {
return value; return value;
} }
} }
loacl.remove(key); local.remove(key);
return null; return null;
} }
@ -81,5 +81,5 @@ function createSessionStorage() {
} }
} }
export const loacl = createLocalStorage(); export const local = createLocalStorage();
export const session = createSessionStorage(); export const session = createSessionStorage();

View File

@ -78,7 +78,9 @@
:size="24" :size="24"
> >
<div class="flex-y-center justify-between"> <div class="flex-y-center justify-between">
<n-checkbox>记住我</n-checkbox> <n-checkbox v-model:checked="isRemember">
记住我
</n-checkbox>
<n-button :text="true"> <n-button :text="true">
忘记密码 忘记密码
</n-button> </n-button>
@ -107,7 +109,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { FormInst } from 'naive-ui'; import { FormInst } from 'naive-ui';
import { useAuthStore } from '@/store'; import { useAuthStore } from '@/store';
import { ref } from 'vue'; import { ref, onMounted } from 'vue';
import { local } from '@/utils';
const authStore = useAuthStore(); const authStore = useAuthStore();
const swiperList = [ const swiperList = [
@ -118,10 +121,12 @@ const swiperList = [
]; ];
const formValue = ref({ const formValue = ref({
account: 'admin', account: '',
pwd: '123456', pwd: '',
code: '1234', code: '',
}); });
const isRemember = ref(false);
const rules = { const rules = {
account: { account: {
required: true, required: true,
@ -142,14 +147,31 @@ const rules = {
}; };
const formRef = ref<FormInst | null>(null); const formRef = ref<FormInst | null>(null);
const handleLogin = () => { function handleLogin() {
formRef.value?.validate((errors) => { formRef.value?.validate((errors) => {
if (errors) return console.error(errors); if (errors) return;
const { account, pwd } = formValue.value; const { account, pwd } = formValue.value;
if (isRemember.value) {
local.set('login_account', { account, pwd });
}
authStore.login(account, pwd); authStore.login(account, pwd);
}); });
}; }
function checkUserAccount() {
const loginAccount = local.get('login_account');
if (!loginAccount) return;
formValue.value = loginAccount;
isRemember.value = true;
}
onMounted(() => {
checkUserAccount();
});
</script> </script>
<style scoped></style> <style scoped></style>