feat(auth): 新增密码登录注册与改密功能并加固安全
- 后端:新增密码登录/注册/重置/改密接口,users 表新增 password_hash 字段 - 安全加固:注册改为冲突即失败防止"注册即改密",登录用户不存在统一返回密码错误并计失败次数防枚举,改密需校验旧密码,清理登录失败计数中的死代码 - 前端:登录页重构为"登录/注册"两个 tab,登录内可切换密码/短信方式,默认密码登录 - 个人中心新增修改密码入口(PC 弹窗 + 移动端 popup),PC 个人资料页移除买家/卖家服务面板
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
import { readError } from '@/shared/utils/error'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { ChatLineRound, Iphone, Key } from '@element-plus/icons-vue'
|
||||
import { onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||
import { ChatLineRound, Iphone, Key, Lock } from '@element-plus/icons-vue'
|
||||
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
|
||||
import { fetchAuthCaptcha, sendSmsCode, type AuthCaptcha } from '@/features/auth/api/auth'
|
||||
@@ -15,12 +15,19 @@ const sending = ref(false)
|
||||
const captchaLoading = ref(false)
|
||||
const captcha = ref<AuthCaptcha | null>(null)
|
||||
const countDown = ref(0)
|
||||
const topTab = ref<'login' | 'register'>('login')
|
||||
const loginMode = ref<'password' | 'sms'>('password')
|
||||
const form = reactive({
|
||||
phone: '',
|
||||
captchaCode: '',
|
||||
code: '',
|
||||
password: '',
|
||||
confirmPassword: '',
|
||||
})
|
||||
|
||||
const passwordValid = computed(() => form.password.length >= 8 && form.password.length <= 20)
|
||||
const confirmValid = computed(() => form.confirmPassword && form.confirmPassword === form.password)
|
||||
|
||||
let timer: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
function startCountDown() {
|
||||
@@ -43,6 +50,20 @@ onUnmounted(() => {
|
||||
|
||||
onMounted(loadCaptcha)
|
||||
|
||||
function switchTab(tab: 'login' | 'register') {
|
||||
topTab.value = tab
|
||||
form.code = ''
|
||||
form.captchaCode = ''
|
||||
form.password = ''
|
||||
form.confirmPassword = ''
|
||||
}
|
||||
|
||||
function switchLoginMode(mode: 'password' | 'sms') {
|
||||
loginMode.value = mode
|
||||
form.code = ''
|
||||
form.captchaCode = ''
|
||||
}
|
||||
|
||||
async function loadCaptcha() {
|
||||
captchaLoading.value = true
|
||||
try {
|
||||
@@ -78,9 +99,7 @@ async function handleSendCode() {
|
||||
}
|
||||
|
||||
async function refreshCaptcha() {
|
||||
if (captchaLoading.value) {
|
||||
return
|
||||
}
|
||||
if (captchaLoading.value) return
|
||||
await loadCaptcha()
|
||||
}
|
||||
|
||||
@@ -96,6 +115,59 @@ async function handleLogin() {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handlePasswordLogin() {
|
||||
if (!form.phone.trim()) {
|
||||
ElMessage.warning('请输入手机号')
|
||||
return
|
||||
}
|
||||
if (!form.password) {
|
||||
ElMessage.warning('请输入密码')
|
||||
return
|
||||
}
|
||||
loading.value = true
|
||||
try {
|
||||
await session.loginByPassword(form.phone, form.password)
|
||||
ElMessage.success('登录成功')
|
||||
await router.push('/')
|
||||
} catch (error) {
|
||||
ElMessage.error(readError(error, '登录失败'))
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handleRegister() {
|
||||
if (!form.phone.trim()) {
|
||||
ElMessage.warning('请输入手机号')
|
||||
return
|
||||
}
|
||||
if (!form.code) {
|
||||
ElMessage.warning('请输入验证码')
|
||||
return
|
||||
}
|
||||
if (!passwordValid.value) {
|
||||
ElMessage.warning('密码长度应为 8-20 位')
|
||||
return
|
||||
}
|
||||
if (!confirmValid.value) {
|
||||
ElMessage.warning('两次输入的密码不一致')
|
||||
return
|
||||
}
|
||||
loading.value = true
|
||||
try {
|
||||
await session.register(form.phone, form.code, form.password)
|
||||
ElMessage.success('注册成功')
|
||||
await router.push('/')
|
||||
} catch (error) {
|
||||
ElMessage.error(readError(error, '注册失败'))
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
const modeEyebrow = computed(() => (topTab.value === 'register' ? 'Register' : 'Login'))
|
||||
const modeTitle = computed(() => (topTab.value === 'register' ? '创建账号' : '欢迎回来'))
|
||||
const modeSubtitle = computed(() => (topTab.value === 'register' ? '注册后即可发布或租赁账号' : '登录后即可发布或租赁账号'))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -120,78 +192,104 @@ async function handleLogin() {
|
||||
|
||||
<div class="login-right">
|
||||
<div class="login-header">
|
||||
<p class="eyebrow">SMS Login</p>
|
||||
<h1>欢迎回来</h1>
|
||||
<p class="subtitle">登录后即可发布或租赁账号</p>
|
||||
<p class="eyebrow">{{ modeEyebrow }}</p>
|
||||
<h1>{{ modeTitle }}</h1>
|
||||
<p class="subtitle">{{ modeSubtitle }}</p>
|
||||
</div>
|
||||
|
||||
<el-form class="user-form" label-position="top" @submit.prevent>
|
||||
<el-form-item label="手机号">
|
||||
<el-input
|
||||
v-model="form.phone"
|
||||
maxlength="11"
|
||||
placeholder="请输入手机号"
|
||||
size="large"
|
||||
:prefix-icon="Iphone"
|
||||
/>
|
||||
</el-form-item>
|
||||
<div class="login-tabs">
|
||||
<button
|
||||
:class="{ active: topTab === 'login' }"
|
||||
@click="switchTab('login')"
|
||||
>登录</button>
|
||||
<button
|
||||
:class="{ active: topTab === 'register' }"
|
||||
@click="switchTab('register')"
|
||||
>注册</button>
|
||||
</div>
|
||||
|
||||
<!-- 登录模式下的方式切换 -->
|
||||
<div v-if="topTab === 'login'" class="login-sub-tabs">
|
||||
<button
|
||||
:class="{ active: loginMode === 'password' }"
|
||||
@click="switchLoginMode('password')"
|
||||
>密码登录</button>
|
||||
<span class="sub-divider">|</span>
|
||||
<button
|
||||
:class="{ active: loginMode === 'sms' }"
|
||||
@click="switchLoginMode('sms')"
|
||||
>短信登录</button>
|
||||
</div>
|
||||
|
||||
<!-- 短信登录 -->
|
||||
<el-form v-if="topTab === 'login' && loginMode === 'sms'" class="user-form" label-position="top" @submit.prevent>
|
||||
<el-form-item label="手机号">
|
||||
<el-input v-model="form.phone" maxlength="11" placeholder="请输入手机号" size="large" :prefix-icon="Iphone" />
|
||||
</el-form-item>
|
||||
<el-form-item label="图形验证码">
|
||||
<div class="user-captcha-row">
|
||||
<el-input
|
||||
v-model="form.captchaCode"
|
||||
maxlength="4"
|
||||
placeholder="请输入图形验证码"
|
||||
size="large"
|
||||
:prefix-icon="Key"
|
||||
@keyup.enter="handleSendCode"
|
||||
/>
|
||||
<button
|
||||
class="user-captcha-image"
|
||||
type="button"
|
||||
:disabled="captchaLoading"
|
||||
aria-label="刷新图形验证码"
|
||||
@click="refreshCaptcha"
|
||||
>
|
||||
<el-input v-model="form.captchaCode" maxlength="4" placeholder="请输入图形验证码" size="large" :prefix-icon="Key" @keyup.enter="handleSendCode" />
|
||||
<button class="user-captcha-image" type="button" :disabled="captchaLoading" aria-label="刷新图形验证码" @click="refreshCaptcha">
|
||||
<img v-if="captcha" :src="captcha.image" alt="图形验证码" />
|
||||
<span v-else>刷新</span>
|
||||
</button>
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="验证码">
|
||||
<div class="user-code-row">
|
||||
<el-input
|
||||
v-model="form.code"
|
||||
maxlength="6"
|
||||
placeholder="6 位验证码"
|
||||
size="large"
|
||||
:prefix-icon="ChatLineRound"
|
||||
@keyup.enter="handleLogin"
|
||||
/>
|
||||
<el-button
|
||||
size="large"
|
||||
:disabled="countDown > 0 || sending"
|
||||
:loading="sending"
|
||||
@click="handleSendCode"
|
||||
>
|
||||
<el-input v-model="form.code" maxlength="6" placeholder="6 位验证码" size="large" :prefix-icon="ChatLineRound" @keyup.enter="handleLogin" />
|
||||
<el-button size="large" :disabled="countDown > 0 || sending" :loading="sending" @click="handleSendCode">
|
||||
{{ countDown > 0 ? `${countDown}s` : sending ? '发送中' : '发送验证码' }}
|
||||
</el-button>
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
<el-button
|
||||
class="user-login-btn"
|
||||
type="primary"
|
||||
size="large"
|
||||
:loading="loading"
|
||||
@click="handleLogin"
|
||||
>
|
||||
登录
|
||||
</el-button>
|
||||
|
||||
<el-button class="user-login-btn" type="primary" size="large" :loading="loading" @click="handleLogin">登录</el-button>
|
||||
<p class="login-notice">未收到验证码时,请稍后重试或联系客服处理</p>
|
||||
</el-form>
|
||||
|
||||
<!-- 密码登录 -->
|
||||
<el-form v-if="topTab === 'login' && loginMode === 'password'" class="user-form" label-position="top" @submit.prevent>
|
||||
<el-form-item label="手机号">
|
||||
<el-input v-model="form.phone" maxlength="11" placeholder="请输入手机号" size="large" :prefix-icon="Iphone" />
|
||||
</el-form-item>
|
||||
<el-form-item label="密码">
|
||||
<el-input v-model="form.password" type="password" maxlength="20" placeholder="请输入密码" size="large" :prefix-icon="Lock" show-password @keyup.enter="handlePasswordLogin" />
|
||||
</el-form-item>
|
||||
<el-button class="user-login-btn" type="primary" size="large" :loading="loading" @click="handlePasswordLogin">登录</el-button>
|
||||
<p class="login-notice">未设置密码?请使用短信登录后前往个人中心设置密码</p>
|
||||
</el-form>
|
||||
|
||||
<!-- 注册 -->
|
||||
<el-form v-if="topTab === 'register'" class="user-form" label-position="top" @submit.prevent>
|
||||
<el-form-item label="手机号">
|
||||
<el-input v-model="form.phone" maxlength="11" placeholder="请输入手机号" size="large" :prefix-icon="Iphone" />
|
||||
</el-form-item>
|
||||
<el-form-item label="图形验证码">
|
||||
<div class="user-captcha-row">
|
||||
<el-input v-model="form.captchaCode" maxlength="4" placeholder="请输入图形验证码" size="large" :prefix-icon="Key" @keyup.enter="handleSendCode" />
|
||||
<button class="user-captcha-image" type="button" :disabled="captchaLoading" aria-label="刷新图形验证码" @click="refreshCaptcha">
|
||||
<img v-if="captcha" :src="captcha.image" alt="图形验证码" />
|
||||
<span v-else>刷新</span>
|
||||
</button>
|
||||
</div>
|
||||
</el-form-item>
|
||||
<el-form-item label="验证码">
|
||||
<div class="user-code-row">
|
||||
<el-input v-model="form.code" maxlength="6" placeholder="6 位验证码" size="large" :prefix-icon="ChatLineRound" @keyup.enter="handleRegister" />
|
||||
<el-button size="large" :disabled="countDown > 0 || sending" :loading="sending" @click="handleSendCode">
|
||||
{{ countDown > 0 ? `${countDown}s` : sending ? '发送中' : '发送验证码' }}
|
||||
</el-button>
|
||||
</div>
|
||||
</el-form-item>
|
||||
<el-form-item label="设置密码" :error="form.password && !passwordValid ? '密码长度 8-20 位' : ''">
|
||||
<el-input v-model="form.password" type="password" maxlength="20" placeholder="请输入密码(8-20 位)" size="large" :prefix-icon="Lock" show-password :class="{ 'is-error': form.password && !passwordValid }" />
|
||||
</el-form-item>
|
||||
<el-form-item label="确认密码" :error="form.confirmPassword && !confirmValid ? '两次密码不一致' : ''">
|
||||
<el-input v-model="form.confirmPassword" type="password" maxlength="20" placeholder="请再次输入密码" size="large" :prefix-icon="Lock" show-password :class="{ 'is-error': form.confirmPassword && !confirmValid }" />
|
||||
</el-form-item>
|
||||
<el-button class="user-login-btn" type="primary" size="large" :loading="loading" @click="handleRegister">注册</el-button>
|
||||
<p class="login-notice">注册即表示同意《用户协议》和《隐私政策》</p>
|
||||
</el-form>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
@@ -287,16 +385,16 @@ async function handleLogin() {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
padding: 36px 32px;
|
||||
padding: 32px 32px;
|
||||
background: rgba(255, 255, 255, 0.45);
|
||||
}
|
||||
|
||||
.login-header {
|
||||
margin-bottom: 24px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.login-header .eyebrow {
|
||||
margin: 0 0 8px;
|
||||
margin: 0 0 6px;
|
||||
color: #ff6a00;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
@@ -313,11 +411,79 @@ async function handleLogin() {
|
||||
}
|
||||
|
||||
.login-header .subtitle {
|
||||
margin: 8px 0 0;
|
||||
margin: 6px 0 0;
|
||||
color: #64748b;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.login-tabs {
|
||||
display: flex;
|
||||
gap: 0;
|
||||
margin-bottom: 20px;
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.login-tabs button {
|
||||
flex: 1;
|
||||
height: 38px;
|
||||
border: none;
|
||||
background: #f8fafc;
|
||||
color: #64748b;
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.login-tabs button + button {
|
||||
border-left: 1px solid #e2e8f0;
|
||||
}
|
||||
|
||||
.login-tabs button.active {
|
||||
background: #ff6a00;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.login-tabs button:hover:not(.active) {
|
||||
background: #f1f5f9;
|
||||
color: #334155;
|
||||
}
|
||||
|
||||
.login-sub-tabs {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.login-sub-tabs button {
|
||||
border: none;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
padding: 0;
|
||||
color: #94a3b8;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.login-sub-tabs button.active {
|
||||
color: #ff6a00;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.login-sub-tabs button:hover:not(.active) {
|
||||
color: #475569;
|
||||
}
|
||||
|
||||
.login-sub-tabs .sub-divider {
|
||||
color: #cbd5e1;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.user-form :deep(.el-form-item__label) {
|
||||
color: #334155;
|
||||
font-size: 13px;
|
||||
@@ -425,7 +591,7 @@ async function handleLogin() {
|
||||
border-radius: 10px;
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
margin-top: 8px;
|
||||
margin-top: 4px;
|
||||
letter-spacing: 0.5px;
|
||||
background: linear-gradient(135deg, #ff6a00, #ff8a1f);
|
||||
border: none;
|
||||
@@ -440,13 +606,17 @@ async function handleLogin() {
|
||||
}
|
||||
|
||||
.login-notice {
|
||||
margin: 18px 0 0;
|
||||
margin: 14px 0 0;
|
||||
color: #94a3b8;
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.is-error :deep(.el-input__wrapper) {
|
||||
box-shadow: 0 0 0 1px #ef4444 inset !important;
|
||||
}
|
||||
|
||||
/* ========== 响应式 ========== */
|
||||
@media (max-width: 860px) {
|
||||
.login-card {
|
||||
@@ -457,7 +627,7 @@ async function handleLogin() {
|
||||
display: none;
|
||||
}
|
||||
.login-right {
|
||||
padding: 32px 28px;
|
||||
padding: 28px 24px;
|
||||
}
|
||||
.login-shell {
|
||||
padding: 24px 16px;
|
||||
|
||||
Reference in New Issue
Block a user