Skip to content

Commit

Permalink
feat: 添加自动登录可选项
Browse files Browse the repository at this point in the history
  • Loading branch information
welkin-sky committed Oct 21, 2023
1 parent dc2c3f7 commit bf8c77d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules
.nuxt
.nitro
.cache
.vscode
.output
.env
dist
Expand Down
47 changes: 47 additions & 0 deletions components/z-checkbox.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<template>
<div id="checkbox" class="form-item">
<input :id="id ?? label" v-model="selected" type="checkbox" />
<label :for="id ?? label">{{ label }}</label>
<slot v-if="modelValue" name="active" />
</div>
</template>

<script lang="ts" setup>
const props = defineProps({

Check warning on line 10 in components/z-checkbox.vue

View workflow job for this annotation

GitHub Actions / build-and-deploy

'props' is assigned a value but never used
label: {
type: String,
required: true,
default: '',
},
id: {
type: String,
required: false,
default: undefined,
},
modelValue: {
type: Boolean,
required: true,
default: false,
},
});
const emit = defineEmits<{
(e: 'update:modelValue', value: boolean): void;
}>();
const selected = ref(false);
watch(selected, (value) => {
emit('update:modelValue', value);
});
</script>

<style>

This comment has been minimized.

Copy link
@Saltro

Saltro Oct 21, 2023

Contributor

建议都用scoped

#checkbox {
display: flex;
gap: 20px;
align-items: center;
* {
margin: 0;
}
}
</style>
48 changes: 48 additions & 0 deletions components/z-radio.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<template>
<div id="radio" class="form-item">
<input :id="id ?? label" type="radio" name="radio" @change="onChange" />
<label :for="id ?? label">{{ label }}</label>
</div>
</template>

<script lang="ts" setup>
const props = defineProps({
label: {
type: String,
required: true,
default: '',
},
id: {
type: String,
required: false,
default: undefined,
},
modelValue: {
type: null,
required: true,
default: '',
},
value: {
type: null,
required: true,
},
});
const emit = defineEmits<{
(e: 'update:modelValue', value: unknown): void;
}>();
function onChange() {
emit('update:modelValue', props.value);
}
</script>

<style>
#radio {
display: flex;
gap: 20px;
align-items: center;
* {
margin: 0;
}
}
</style>
19 changes: 18 additions & 1 deletion pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<nuxt-link to="/reset">忘记密码?</nuxt-link>
</template>
</z-input>
<z-checkbox id="auto-login" v-model="isAutoLogin" label="自动登录">
<template #active>
<z-radio v-model="expireTimeLocal" label="7天" :value="7" />
<z-radio v-model="expireTimeLocal" label="14天" :value="14" />
</template>
</z-checkbox>
<button type="submit">
<z-loading v-if="isLoading" />
登录<template v-if="isLoading">中...</template>
Expand All @@ -57,6 +63,8 @@ import { API_PREFIX } from '@/utils/request';
const username = ref('');
const password = ref('');
const isAutoLogin = ref(false);
const expireTimeLocal = ref(7); // 以天为单位
const router = useRouter();
const route = useRoute();
Expand Down Expand Up @@ -137,7 +145,13 @@ onMounted(() => {
/**
* 自动登录,当仅验证学生时,获取用户信息并返回
*/
if (!manually && typeof localStorage.getItem('access') === 'string' && !isLoading.value) {
if (
!manually &&
isAutoLogin &&

This comment has been minimized.

Copy link
@Saltro

Saltro Oct 21, 2023

Contributor

用户刚进入页面的时候isAutoLogin就是false,这里就不会走自动登录

typeof localStorage.getItem('access') === 'string' &&
Date.parse(localStorage.getItem('exp-local') ?? Date()) > Date.now() &&

This comment has been minimized.

Copy link
@Saltro

Saltro Oct 21, 2023

Contributor

?? Date()这样子写不太好,虽然意思也是“当不存在exp-local”时不自动登录,但是可读性差很多

!isLoading.value
) {
isLoading.value = true;
if (isCertifyOnly) {
Expand All @@ -146,6 +160,7 @@ onMounted(() => {
console.warn('自动登录失败', err);
localStorage.removeItem('access');
localStorage.removeItem('exp');
localStorage.removeItem('exp-local');
localStorage.removeItem('refresh');
})
.finally(() => {
Expand All @@ -157,6 +172,7 @@ onMounted(() => {
console.warn('自动登录失败', err);
localStorage.removeItem('access');
localStorage.removeItem('exp');
localStorage.removeItem('exp-local');
localStorage.removeItem('refresh');
})
.finally(() => {
Expand Down Expand Up @@ -190,6 +206,7 @@ function submit(e: Event) {
localStorage.setItem('access', res.data.access);
localStorage.setItem('exp', res.data.expire_time);
localStorage.setItem('refresh', res.data.refresh);
localStorage.setItem('exp-local', new Date(Date.now() + expireTimeLocal.value * 24 * 60 * 60 * 1000).toString());

This comment has been minimized.

Copy link
@Saltro

Saltro Oct 21, 2023

Contributor

这里为什么要tostring,为什么不直接存一个timestamp

This comment has been minimized.

Copy link
@Saltro

Saltro Oct 21, 2023

Contributor

顺便,这样写的话,不选isautologin也会设置exp-local的,逻辑上不对,只有选了isautologin才设置exp-local

if (isCertifyOnly) {
if (res.data.is_certified) {
Expand Down

0 comments on commit bf8c77d

Please sign in to comment.