Skip to content

Commit

Permalink
perf: 优化免登录功能,用户可选择免登录的天数
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxian521 committed Oct 9, 2023
1 parent 7e7b6fe commit fc2d905
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
4 changes: 2 additions & 2 deletions locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ login:
username: Username
password: Password
verifyCode: VerifyCode
remember: No need to login for 7 days
rememberInfo: After checking and logging in, you will automatically log in to the system without entering your username and password within 7 days
remember: days no need to login
rememberInfo: After checking and logging in, will automatically log in to the system without entering your username and password within the specified number of days.
sure: Sure Password
forget: Forget Password?
login: Login
Expand Down
4 changes: 2 additions & 2 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ login:
username: 账号
password: 密码
verifyCode: 验证码
remember: 7天内免登录
rememberInfo: 勾选并登录后,7天内无需输入用户名和密码会自动登入系统
remember: 天内免登录
rememberInfo: 勾选并登录后,规定天数内无需输入用户名和密码会自动登入系统
sure: 确认密码
forget: 忘记密码?
login: 登录
Expand Down
1 change: 1 addition & 0 deletions src/store/modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export type userType = {
verifyCode?: string;
currentPage?: number;
isRemembered?: boolean;
loginDay?: number;
};
12 changes: 9 additions & 3 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export const useUserStore = defineStore({
verifyCode: "",
// 判断登录页面显示哪个组件(0:登录(默认)、1:手机登录、2:二维码登录、3:注册、4:忘记密码)
currentPage: 0,
// 是否勾选了7天内免登录
isRemembered: false
// 是否勾选了登录页的免登录
isRemembered: false,
// 登录页的免登录存储几天,默认7天
loginDay: 7
}),
actions: {
/** 存储用户名 */
Expand All @@ -40,10 +42,14 @@ export const useUserStore = defineStore({
SET_CURRENTPAGE(value: number) {
this.currentPage = value;
},
/** 存储是否勾选了7天内免登录 */
/** 存储是否勾选了登录页的免登录 */
SET_ISREMEMBERED(bool: boolean) {
this.isRemembered = bool;
},
/** 设置登录页的免登录存储几天 */
SET_LOGINDAY(value: number) {
this.loginDay = Number(value);
},
/** 登入 */
async loginByUsername(data) {
return new Promise<UserResult>((resolve, reject) => {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function getToken(): DataInfo<number> {
export function setToken(data: DataInfo<Date>) {
let expires = 0;
const { accessToken, refreshToken } = data;
const { isRemembered, loginDay } = useUserStoreHook();
expires = new Date(data.expires).getTime(); // 如果后端直接设置时间戳,将此处代码改为expires = data.expires,然后把上面的DataInfo<Date>改成DataInfo<number>即可
const cookieString = JSON.stringify({ accessToken, expires });

Expand All @@ -54,9 +55,9 @@ export function setToken(data: DataInfo<Date>) {
Cookies.set(
multipleTabsKey,
"true",
useUserStoreHook().isRemembered
isRemembered
? {
expires: 7
expires: loginDay
}
: {}
);
Expand Down
17 changes: 17 additions & 0 deletions src/views/login/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defineOptions({
});
const imgCode = ref("");
const loginDay = ref(7);
const router = useRouter();
const loading = ref(false);
const checked = ref(false);
Expand Down Expand Up @@ -111,6 +112,9 @@ watch(imgCode, value => {
watch(checked, bool => {
useUserStoreHook().SET_ISREMEMBERED(bool);
});
watch(loginDay, value => {
useUserStoreHook().SET_LOGINDAY(value);
});
</script>

<template>
Expand Down Expand Up @@ -230,6 +234,19 @@ watch(checked, bool => {
<div class="w-full h-[20px] flex justify-between items-center">
<el-checkbox v-model="checked">
<span class="flex">
<select
v-model="loginDay"
:style="{
width: loginDay < 10 ? '10px' : '16px',
outline: 'none',
background: 'none',
appearance: 'none'
}"
>
<option value="1">1</option>
<option value="7">7</option>
<option value="30">30</option>
</select>
{{ t("login.remember") }}
<el-tooltip
effect="dark"
Expand Down

1 comment on commit fc2d905

@xiaoxian521
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Please sign in to comment.