Files
cz-web/src/views/login/register.vue
2026-10-18 07:59:07 +08:00

259 lines
6.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { PasswordStrength, SmsCodeInput } from '@/components'
import { useUserStore } from '@/stores/modules/user'
import { isPhone, isValidPassword } from '@/utils/validate'
const router = useRouter()
const userStore = useUserStore()
const phone = ref('')
const phoneCode = ref('')
const password = ref('')
const confirmPassword = ref('')
const agree = ref(false)
let redirectTimer: number | null = null
async function onGetCode(): Promise<void> {
if (!isPhone(phone.value)) {
ElMessage.warning('手机号格式不正确')
return
}
try {
await userStore.sendSmsCode(phone.value)
} catch {}
}
async function onRegister(): Promise<void> {
if (!isPhone(phone.value)) {
ElMessage.warning('手机号格式不正确')
return
}
if (!/^\d{6}$/.test(phoneCode.value)) {
ElMessage.warning('请输入6位验证码')
return
}
if (!isValidPassword(password.value)) {
ElMessage.warning('密码长度应为6-20位')
return
}
if (password.value !== confirmPassword.value) {
ElMessage.warning('两次输入的密码不一致')
return
}
if (!agree.value) {
ElMessage.warning('请先阅读并同意《用户协议》和《隐私政策》')
return
}
const ok = await userStore.register({
phoneNumber: phone.value,
smsCode: phoneCode.value,
password: password.value,
confirmPassword: confirmPassword.value,
})
if (ok) {
redirectTimer = window.setTimeout(() => {
void router.push('/login')
}, 3000)
}
}
onBeforeUnmount(() => {
if (redirectTimer !== null) window.clearTimeout(redirectTimer)
})
</script>
<template>
<div class="auth-page register">
<aside class="auth-page__brand">
<h1 class="auth-page__logo">虫子智造研发<br />一体化平台</h1>
<p class="auth-page__key">chongzizhizaoyanfayitihuaPINGTAI</p>
<p class="auth-page__slogan">安全 · 高效 · 可控</p>
</aside>
<RouterLink class="auth-page__guest" to="/"> 游客模式 </RouterLink>
<main class="auth-page__main">
<div class="auth-page__card">
<h2 class="auth-page__title">创建账号</h2>
<p class="auth-page__subtitle">注册后即可开始使用全部功能</p>
<form class="register__form" @submit.prevent="onRegister">
<div class="register__field">
<label class="register__label" for="reg-phone">手机号</label>
<input
id="reg-phone"
v-model="phone"
class="register__input"
type="text"
placeholder="请输入手机号"
maxlength="11"
autocomplete="tel"
/>
</div>
<div class="register__field">
<label class="register__label" for="reg-code">验证码</label>
<SmsCodeInput
id="reg-code"
v-model:code="phoneCode"
@get-code="onGetCode"
/>
</div>
<div class="register__field">
<label class="register__label" for="reg-password">设置密码</label>
<input
id="reg-password"
v-model="password"
class="register__input"
type="password"
placeholder="请设置6-20位密码"
autocomplete="new-password"
/>
<PasswordStrength :password="password" />
</div>
<div class="register__field">
<label class="register__label" for="reg-confirm">确认密码</label>
<input
id="reg-confirm"
v-model="confirmPassword"
class="register__input"
type="password"
placeholder="请设置6-20位密码"
autocomplete="new-password"
/>
</div>
<label class="register__agree">
<input v-model="agree" type="checkbox" />
<span
>已阅读并同意<em>《用户协议》</em>和<em>《隐私政策》</em></span
>
</label>
<button class="register__submit" type="submit">注册</button>
</form>
<p class="register__switch">
已有账号?<RouterLink class="auth-page__link" to="/login"
>立即登录</RouterLink
>
</p>
</div>
</main>
</div>
</template>
<style lang="less" scoped>
// 设计稿 #39(注册账号)纵向坐标:
// 手机号标签 368 → 验证码标签 450 → 设置密码标签 532 → 确认密码标签 658
// 前三个字段间距均为 82px;设置密码字段含密码强度条,故到确认密码为 126px。
.register {
&__form {
.flex(column, flex-start, stretch);
margin: 26px 0 0;
width: 100%;
}
&__field {
.flex(column, flex-start, stretch);
gap: 6px;
width: 100%;
}
// 字段盒 = 标签17 + gap6 + 输入框40 = 63px;63 + 19 = 82px 字段间距
&__field + &__field {
margin-top: 19px;
}
&__label {
font-size: 13px;
font-weight: 500;
line-height: 17px;
color: #1f2937;
text-align: center;
}
&__input {
width: 100%;
height: 40px;
padding: 0 12px;
border: 1px solid @border-color;
border-radius: @radius-sm;
background: @white;
font-size: 13px;
color: #1f2937;
outline: none;
transition: border-color 0.2s;
box-sizing: border-box;
&:focus {
border-color: @primary-strong;
}
&::placeholder {
color: #1f2937;
}
}
// 设计稿 #39:勾选框 (1002.2,749) 7.5×14
&__agree {
.flex(row, flex-start, center);
gap: 8px;
margin-top: 21px;
font-size: 12px;
font-weight: 400;
line-height: 16px;
color: #6b7280;
cursor: pointer;
input {
flex-shrink: 0;
width: 14px;
height: 14px;
margin: 0;
accent-color: @primary-strong;
}
em {
font-style: normal;
color: @primary-strong;
}
}
// 设计稿 #39:按钮文字 (1312,795) 54×20
&__submit {
width: 100%;
height: 46px;
margin-top: 17px;
border: none;
border-radius: @radius-sm;
background: @primary-strong;
color: @white;
font-size: 15px;
font-weight: 500;
cursor: pointer;
transition: background 0.2s;
&:hover {
background: @primary-hover;
}
}
// 设计稿 #39:「已有账号?」(1285,844)、「立即登录」(1344.5,844)
&__switch {
align-self: center;
margin: 17px 0 0;
font-size: 12px;
font-weight: 400;
line-height: 16px;
color: #6b7280;
}
}
</style>