214 lines
4.3 KiB
Vue
214 lines
4.3 KiB
Vue
|
|
<!-- src/views/Register.vue -->
|
|||
|
|
<script setup lang="ts">
|
|||
|
|
import { ref } from 'vue'
|
|||
|
|
import { useRouter } from 'vue-router'
|
|||
|
|
import { useUserStore } from '@/stores/user'
|
|||
|
|
import { ElMessage } from 'element-plus'
|
|||
|
|
import PasswordInput from '@/components/common/PasswordInput.vue'
|
|||
|
|
|
|||
|
|
const router = useRouter()
|
|||
|
|
const userStore = useUserStore()
|
|||
|
|
|
|||
|
|
const username = ref('')
|
|||
|
|
const email = ref('')
|
|||
|
|
const password = ref('')
|
|||
|
|
const passwordConfirm = ref('')
|
|||
|
|
const loading = ref(false)
|
|||
|
|
|
|||
|
|
async function handleRegister() {
|
|||
|
|
if (!username.value || !email.value || !password.value) {
|
|||
|
|
ElMessage.warning('请填写所有必填项')
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (password.value !== passwordConfirm.value) {
|
|||
|
|
ElMessage.warning('两次输入的密码不一致')
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (password.value.length < 6) {
|
|||
|
|
ElMessage.warning('密码长度至少为 6 位')
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
loading.value = true
|
|||
|
|
await userStore.register({
|
|||
|
|
username: username.value,
|
|||
|
|
email: email.value,
|
|||
|
|
password: password.value,
|
|||
|
|
passwordConfirm: passwordConfirm.value
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
ElMessage.success('注册成功')
|
|||
|
|
router.push({ name: 'Home' })
|
|||
|
|
} catch (error: any) {
|
|||
|
|
ElMessage.error(error.message || '注册失败')
|
|||
|
|
} finally {
|
|||
|
|
loading.value = false
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function goToLogin() {
|
|||
|
|
router.push({ name: 'Login' })
|
|||
|
|
}
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
<template>
|
|||
|
|
<div class="register-page">
|
|||
|
|
<div class="register-card">
|
|||
|
|
<h1 class="title">注册 Game Group</h1>
|
|||
|
|
|
|||
|
|
<form class="register-form" @submit.prevent="handleRegister">
|
|||
|
|
<div class="form-group">
|
|||
|
|
<label for="username">用户名</label>
|
|||
|
|
<input
|
|||
|
|
id="username"
|
|||
|
|
v-model="username"
|
|||
|
|
type="text"
|
|||
|
|
placeholder="请输入用户名"
|
|||
|
|
required
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="form-group">
|
|||
|
|
<label for="email">邮箱</label>
|
|||
|
|
<input
|
|||
|
|
id="email"
|
|||
|
|
v-model="email"
|
|||
|
|
type="email"
|
|||
|
|
placeholder="请输入邮箱"
|
|||
|
|
required
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="form-group">
|
|||
|
|
<label for="password">密码</label>
|
|||
|
|
<PasswordInput
|
|||
|
|
id="password"
|
|||
|
|
v-model="password"
|
|||
|
|
placeholder="请输入密码(至少6位)"
|
|||
|
|
required
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="form-group">
|
|||
|
|
<label for="passwordConfirm">确认密码</label>
|
|||
|
|
<PasswordInput
|
|||
|
|
id="passwordConfirm"
|
|||
|
|
v-model="passwordConfirm"
|
|||
|
|
placeholder="请再次输入密码"
|
|||
|
|
required
|
|||
|
|
/>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<button type="submit" class="submit-btn" :disabled="loading">
|
|||
|
|
{{ loading ? '注册中...' : '注册' }}
|
|||
|
|
</button>
|
|||
|
|
</form>
|
|||
|
|
|
|||
|
|
<div class="footer-links">
|
|||
|
|
<span>已有账号?</span>
|
|||
|
|
<a @click="goToLogin">立即登录</a>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</template>
|
|||
|
|
|
|||
|
|
<style scoped>
|
|||
|
|
.register-page {
|
|||
|
|
display: flex;
|
|||
|
|
align-items: center;
|
|||
|
|
justify-content: center;
|
|||
|
|
min-height: 100vh;
|
|||
|
|
padding: 20px;
|
|||
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.register-card {
|
|||
|
|
width: 100%;
|
|||
|
|
max-width: 400px;
|
|||
|
|
padding: 40px;
|
|||
|
|
background: white;
|
|||
|
|
border-radius: 16px;
|
|||
|
|
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.title {
|
|||
|
|
font-size: 28px;
|
|||
|
|
font-weight: 700;
|
|||
|
|
text-align: center;
|
|||
|
|
margin: 0 0 32px;
|
|||
|
|
color: #333;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.register-form {
|
|||
|
|
display: flex;
|
|||
|
|
flex-direction: column;
|
|||
|
|
gap: 20px;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.form-group {
|
|||
|
|
display: flex;
|
|||
|
|
flex-direction: column;
|
|||
|
|
gap: 8px;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.form-group label {
|
|||
|
|
font-size: 14px;
|
|||
|
|
font-weight: 500;
|
|||
|
|
color: #555;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.form-group input {
|
|||
|
|
padding: 12px 16px;
|
|||
|
|
border: 1px solid #ddd;
|
|||
|
|
border-radius: 8px;
|
|||
|
|
font-size: 14px;
|
|||
|
|
transition: border-color 0.2s;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.form-group input:focus {
|
|||
|
|
outline: none;
|
|||
|
|
border-color: #667eea;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.submit-btn {
|
|||
|
|
padding: 14px;
|
|||
|
|
border: none;
|
|||
|
|
border-radius: 8px;
|
|||
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|||
|
|
color: white;
|
|||
|
|
font-size: 16px;
|
|||
|
|
font-weight: 600;
|
|||
|
|
cursor: pointer;
|
|||
|
|
transition: opacity 0.2s;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.submit-btn:hover:not(:disabled) {
|
|||
|
|
opacity: 0.9;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.submit-btn:disabled {
|
|||
|
|
opacity: 0.6;
|
|||
|
|
cursor: not-allowed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.footer-links {
|
|||
|
|
margin-top: 24px;
|
|||
|
|
text-align: center;
|
|||
|
|
font-size: 14px;
|
|||
|
|
color: #666;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.footer-links a {
|
|||
|
|
color: #667eea;
|
|||
|
|
cursor: pointer;
|
|||
|
|
font-weight: 500;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.footer-links a:hover {
|
|||
|
|
text-decoration: underline;
|
|||
|
|
}
|
|||
|
|
</style>
|