Skip to content

Commit

Permalink
feat: 增加message错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
Saltro committed Apr 20, 2023
1 parent 2db0e7b commit d859926
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 40 deletions.
16 changes: 11 additions & 5 deletions pages/certify-pending.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<p class="action"><nuxt-link to="/register">无法访问武大邮箱?</nuxt-link></p>
</template>
<script lang="ts" setup>
import MMessage from 'vue-m-message';
const route = useRoute();
const id = route.params.id?.toString() ?? '';
Expand Down Expand Up @@ -38,11 +40,15 @@ function submit(e: Event) {
e.preventDefault();
request<CertifyRes>(`/api/users/${id}/certify/`, {
method: 'GET',
}).then((res) => {
if (res.data.is_certified) {
location.href = '/';
}
});
})
.then((res) => {
if (res.data.is_certified) {
location.href = '/';
}
})
.catch((err) => {
MMessage.error(err.data.msg);
});
}
interface SendRes {
Expand Down
4 changes: 4 additions & 0 deletions pages/certify.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<script lang="ts" setup>
import User from 'assets/icon/user.svg?component';
import Message from 'assets/icon/message.svg?component';
import MMessage from 'vue-m-message';
const image = isDark ? '/image/certify-dark.svg' : '/image/certify.svg';
Expand Down Expand Up @@ -64,6 +65,9 @@ function submit(e: Event) {
})
.then(() => {
router.push(`/certify-pending?id=${id}`);
})
.catch((err) => {
MMessage.error(err.data.msg);
});
}
</script>
Expand Down
47 changes: 28 additions & 19 deletions pages/register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
required
maxlength="16"
minlength="4"
pattern="^[a-zA-Z0-9_-]{4,16}$"
pattern="[a-zA-Z0-9_\-]+"
placeholder="请输入用户名"
:custom-rule="usernameRule"
>
Expand All @@ -24,7 +24,7 @@
minlength="11"
maxlength="11"
type="tel"
pattern="[0-9]+"
pattern="1[3-9]\d+"
placeholder="请输入手机号"
>
<template #prefix>
Expand Down Expand Up @@ -57,7 +57,7 @@
type="password"
maxlength="18"
minlength="6"
pattern="^[a-zA-Z0-9_-]{6,18}$"
pattern="[a-zA-Z0-9_\-]+"
placeholder="请输入密码"
>
<template #prefix>
Expand All @@ -71,7 +71,7 @@
type="password"
maxlength="18"
minlength="6"
pattern="^[a-zA-Z0-9_-]{6,18}$"
pattern="[a-zA-Z0-9_\-]+"
placeholder="请再次输入密码"
:custom-rule="confirmedPasswordRule"
>
Expand All @@ -92,6 +92,7 @@ import Lock from 'assets/icon/lock.svg?component';
import Message from 'assets/icon/message.svg?component';
import User from 'assets/icon/user.svg?component';
import Phone from 'assets/icon/phone.svg?component';
import MMessage from 'vue-m-message';
const username = ref('');
const phone = ref('');
Expand Down Expand Up @@ -126,12 +127,16 @@ function submit(e: Event) {
code: sms.value,
password: password.value,
},
}).then((res) => {
localStorage.setItem('access', res.data.access);
localStorage.setItem('exp', res.data.expire_time);
localStorage.setItem('refresh', res.data.refresh);
router.push(`/certify?id=${res.data.id}`);
});
})
.then((res) => {
localStorage.setItem('access', res.data.access);
localStorage.setItem('exp', res.data.expire_time);
localStorage.setItem('refresh', res.data.refresh);
router.push(`/certify?id=${res.data.id}`);
})
.catch((err) => {
MMessage.error(err.data.msg);
});
}
const phoneInputRef = ref<InstanceType<typeof ZInput> | null>(null);
Expand All @@ -147,15 +152,19 @@ function sendMessage(e: Event) {
body: {
phone: phone.value,
},
}).then((res) => {
messageTimeout.value = 60;
intervalId = setInterval(() => {
messageTimeout.value -= 1;
if (messageTimeout.value === 0) {
clearInterval(intervalId);
}
}, 1000);
});
})
.then((res) => {
messageTimeout.value = 60;
intervalId = setInterval(() => {
messageTimeout.value -= 1;
if (messageTimeout.value === 0) {
clearInterval(intervalId);
}
}, 1000);
})
.catch((err) => {
MMessage.error(err.data.msg);
});
}
function usernameRule(value: string) {
Expand Down
37 changes: 23 additions & 14 deletions pages/reset.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
v-model="password"
maxlength="18"
minlength="6"
pattern="^[a-zA-Z0-9_-]{6,18}$"
pattern="[a-zA-Z0-9_\-]+"
type="password"
label="新密码"
required
Expand All @@ -48,7 +48,7 @@
minlength="6"
required
type="password"
pattern="^[a-zA-Z0-9_-]{6,18}$"
pattern="[a-zA-Z0-9_\-]+"
placeholder="请再次输入新密码"
:custom-rule="confirmedPasswordRule"
>
Expand All @@ -63,6 +63,7 @@
import Lock from 'assets/icon/lock.svg?component';
import Message from 'assets/icon/message.svg?component';
import Phone from 'assets/icon/phone.svg?component';
import MMessage from 'vue-m-message';
const router = useRouter();
Expand All @@ -82,9 +83,13 @@ function submit(e: Event) {
code: sms.value,
password: password.value,
},
}).then(() => {
router.back();
});
})
.then(() => {
router.back();
})
.catch((err) => {
MMessage.error(err.data.msg);
});
}
const phoneInputRef = ref<HTMLInputElement | null>(null);
Expand All @@ -100,15 +105,19 @@ function sendMessage(e: Event) {
body: {
phone: phone.value,
},
}).then(() => {
messageTimeout.value = 60;
intervalId = setInterval(() => {
messageTimeout.value -= 1;
if (messageTimeout.value === 0) {
clearInterval(intervalId);
}
}, 1000);
});
})
.then(() => {
messageTimeout.value = 60;
intervalId = setInterval(() => {
messageTimeout.value -= 1;
if (messageTimeout.value === 0) {
clearInterval(intervalId);
}
}, 1000);
})
.catch((err) => {
MMessage.error(err.data.msg);
});
}
function confirmedPasswordRule(value: string) {
Expand Down
10 changes: 8 additions & 2 deletions utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import MMessage from 'vue-m-message';

interface RefreshRes {
access: string;
expire_time: string;
Expand All @@ -14,9 +16,10 @@ export async function request<T>(url: string, options: RequestOptions) {
const refresh = localStorage.getItem('refresh');
const exp = localStorage.getItem('exp');
if (!access || !exp) return;
const router = useRouter();
if (Date.now() > new Date(exp).getTime()) {
try {
const res = await $fetch<ResBody<RefreshRes>>('/auth/refresh/', {
const res = await $fetch<ResBody<RefreshRes>>('/api/auth/refresh/', {
method: 'POST',
body: {
refresh,
Expand All @@ -26,7 +29,10 @@ export async function request<T>(url: string, options: RequestOptions) {
localStorage.setItem('exp', res.data.expire_time);
access = res.data.access;
} catch (err) {
throw new Error('Refresh expired');
MMessage.error('登录凭证过期,请重新登录');
setTimeout(() => {
router.push('/login');
}, 1500);
}
}
return $fetch<ResBody<T>>(url, {
Expand Down

0 comments on commit d859926

Please sign in to comment.