完善登录页分角色密码重置流程并保留安全中心入口

This commit is contained in:
xuans committed 2026-09-18 18:20:53 +08:00
1 parent 5d21c71720
commit 8e5fd95ebd
14 files changed
+952 -50

No files matched your search

+4
View File
@@ -48,6 +48,10 @@ const router = createRouter({
{
path: "/forgot-password",
name: "forgot-password",
beforeEnter: () => {
window.location.assign('/public-password-recovery');
return false;
},
component: () => import("../views/PasswordRecovery/PasswordRecovery.vue")
},
{
@@ -55,7 +55,7 @@
color: #2563eb;
font-size: 13px;
font-weight: 700;
letter-spacing: 0.08em;
letter-spacing: 0;
}
h1,
@@ -65,7 +65,7 @@ h2 {
}
h1 {
font-size: clamp(23px, 4vw, 28px);
font-size: 28px;
line-height: 1.25;
}
@@ -74,6 +74,45 @@ h1 {
gap: 10px;
}
.public-recovery-form strong {
overflow-wrap: anywhere;
}
.recovery-methods {
display: flex;
flex-wrap: wrap;
gap: 16px;
margin: 0;
padding: 12px 0;
border: 0;
}
.recovery-methods label {
display: flex;
align-items: center;
gap: 6px;
}
.recovery-methods input {
width: 16px;
min-height: 16px;
}
.recovery-code-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.recovery-code-row input {
flex: 1 1 150px;
min-width: 0;
}
.recovery-code-row button {
flex: 0 0 auto;
}
.public-recovery-copy,
.public-recovery-complete p {
margin: 0 0 10px;
@@ -10,7 +10,6 @@
</header>
<form v-if="page === 'request'" class="public-recovery-form" @submit.prevent="requestReset">
<p class="public-recovery-copy">输入账号或完整邮箱。若该账号允许外部邮箱恢复,系统会向已验证的安全邮箱发送一次性恢复链接。</p>
<label for="public-recovery-identifier">账号或完整邮箱</label>
<input
id="public-recovery-identifier"
@@ -21,17 +20,60 @@
:disabled="loading"
required
/>
<p v-if="submitted" class="public-recovery-status success" role="status">请求已受理。若信息匹配,请检查已验证的外部安全邮箱。</p>
<p v-if="errorMessage" class="public-recovery-status error" role="alert">{{ errorMessage }}</p>
<div class="public-recovery-actions">
<button type="button" class="text-button" :disabled="loading" @click="goLogin">返回登录</button>
<button type="submit" :disabled="loading || !form.identifier.trim()">{{ loading ? '正在提交…' : '发送恢复链接' }}</button>
<button type="submit" :disabled="loading || !form.identifier.trim()">{{ loading ? '正在查询' : '下一步' }}</button>
</div>
</form>
<section v-else-if="page === 'reset'" class="public-recovery-form" aria-live="polite">
<p class="public-recovery-copy">请设置新的登录密码。为保护账号安全,此链接仅可使用一次。</p>
<p v-if="loading" class="public-recovery-status">正在验证恢复链接…</p>
<template v-else-if="available">
<section v-else-if="page === 'blocked'" class="public-recovery-form" aria-live="polite">
<strong>{{ roleLabel }}</strong>
<p class="public-recovery-status">超级管理员无法通过登录页通道重置密码。已登录时可在安全中心重置;无法登录时请使用部署端本机重置。</p>
<div class="public-recovery-actions">
<button type="button" class="text-button" @click="goRequest">更换账号</button>
<button type="button" @click="goLogin">返回登录</button>
</div>
</section>
<form v-else-if="page === 'verify'" class="public-recovery-form" @submit.prevent="verifyIdentity">
<strong>{{ form.identifier }} · {{ roleLabel }}</strong>
<p class="public-recovery-copy">绑定邮箱:{{ identity.email || '未绑定已验证的安全邮箱' }}</p>
<template v-if="identity.methods.length">
<fieldset v-if="identity.methods.length > 1" class="recovery-methods" :disabled="loading">
<legend>验证方式</legend>
<label><input v-model="form.method" type="radio" value="email" />邮箱验证码</label>
<label><input v-model="form.method" type="radio" value="security_code" />安全码</label>
</fieldset>
<label for="public-recovery-code">{{ form.method === 'email' ? '邮箱验证码' : '安全码' }}</label>
<div class="recovery-code-row">
<input
id="public-recovery-code"
v-model="form.code"
:type="form.method === 'email' ? 'text' : 'password'"
:inputmode="form.method === 'email' ? 'numeric' : 'text'"
:maxlength="form.method === 'email' ? 8 : 64"
:placeholder="form.method === 'email' ? '输入 8 位邮件验证码' : '输入未使用的安全码'"
autocomplete="one-time-code"
:disabled="loading"
required
/>
<button v-if="form.method === 'email'" type="button" :disabled="loading || countdown > 0" @click="sendCode">
{{ countdown ? `${countdown} 秒后重发` : '获取验证码' }}
</button>
</div>
</template>
<p v-else class="public-recovery-status">此账号尚未配置可用的重置方式,请登录安全中心或联系管理员处理。</p>
<p v-if="notice" class="public-recovery-status success" role="status">{{ notice }}</p>
<p v-if="errorMessage" class="public-recovery-status error" role="alert">{{ errorMessage }}</p>
<div class="public-recovery-actions">
<button type="button" class="text-button" :disabled="loading" @click="goRequest">更换账号</button>
<button v-if="identity.methods.length" type="submit" :disabled="loading || !canVerify">{{ loading ? '正在验证' : '验证并继续' }}</button>
</div>
</form>
<form v-else-if="page === 'reset'" class="public-recovery-form" @submit.prevent="completeReset">
<p class="public-recovery-status success">身份验证已通过</p>
<label for="public-recovery-password">新密码</label>
<input
id="public-recovery-password"
@@ -53,30 +95,12 @@
placeholder="再次输入新密码"
/>
<p v-if="passwordMismatch" class="public-recovery-status error">两次输入的新密码不一致。</p>
<template v-if="requiresRecoveryCode">
<label for="public-recovery-code">离线恢复码</label>
<input
id="public-recovery-code"
v-model="form.recoveryCode"
type="password"
autocomplete="one-time-code"
maxlength="64"
placeholder="输入未使用的离线恢复码"
/>
</template>
<p v-if="errorMessage" class="public-recovery-status error">{{ errorMessage }}</p>
<div class="public-recovery-actions">
<button type="button" class="text-button" :disabled="loading" @click="goRequest">重新申请</button>
<button type="button" :disabled="loading || !canSubmitReset" @click="completeReset">{{ loading ? '正在重置…' : '确认重置' }}</button>
<button type="submit" :disabled="loading || !canSubmitReset">{{ loading ? '正在重置' : '确认重置' }}</button>
</div>
</template>
<template v-else>
<p class="public-recovery-status error">{{ errorMessage || '恢复链接无效或已失效,请重新申请。' }}</p>
<div class="public-recovery-actions single-action">
<button type="button" @click="goRequest">重新申请恢复链接</button>
</div>
</template>
</section>
</form>
<section v-else class="public-recovery-complete" aria-live="polite">
<div class="public-recovery-success-icon" aria-hidden="true">✓</div>
@@ -89,14 +113,18 @@
</template>
<script setup>
import { usePage } from './PublicPasswordReset.js'
import { usePage } from './useRecoveryFlow.js'
const {
page,
loading,
submitted,
available,
requiresRecoveryCode,
identity,
roleLabel,
notice,
countdown,
canVerify,
sendCode,
verifyIdentity,
errorMessage,
form,
passwordMismatch,
@@ -0,0 +1,110 @@
import { computed, onBeforeUnmount, reactive, ref, watch } from 'vue'
import { isValidPasswordLength } from '@/utils/password'
import { postPublicRecovery } from '@/utils/publicRecoveryHttp'
export function usePage() {
const page = ref('request')
const loading = ref(false)
const errorMessage = ref('')
const notice = ref('')
const identity = ref(null)
const token = ref('')
const countdown = ref(0)
let timer
let version = 0
const form = reactive({ identifier: '', method: 'email', code: '', newPassword: '', confirmPassword: '' })
const roleLabel = computed(() => ({ super_admin: '超级管理员', admin: '管理员', user: '普通用户' })[identity.value?.role] || '')
const passwordMismatch = computed(() => !!form.confirmPassword && form.newPassword !== form.confirmPassword)
const canSubmitReset = computed(() => isValidPasswordLength(form.newPassword) && form.newPassword === form.confirmPassword)
const canVerify = computed(() => form.method === 'email' ? /^\d{8}$/.test(form.code.trim()) : !!form.code.trim())
watch(() => form.method, () => { form.code = ''; errorMessage.value = '' })
const request = async (endpoint, payload) => {
const result = await postPublicRecovery(endpoint, payload)
if (!result.ok) throw new Error(result.data?.message || (result.status >= 500 ? '服务暂时不可用,请稍后重试' : '操作失败,请重试'))
return result.data
}
const run = async action => {
if (loading.value) return
loading.value = true
errorMessage.value = ''
const current = version
try { await action(() => current === version) } catch (error) {
if (current === version) errorMessage.value = error?.message || '网络异常,请稍后重试'
} finally { if (current === version) loading.value = false }
}
const requestReset = () => {
if (!form.identifier.trim()) return
return run(async active => {
const data = await request('begin', { identifier: form.identifier.trim() })
if (!active()) return
identity.value = data
token.value = data.token || ''
form.method = data.methods?.[0] || 'email'
page.value = data.role === 'super_admin' ? 'blocked' : 'verify'
})
}
const sendCode = () => {
if (countdown.value || !token.value) return
return run(async active => {
const data = await request('send-code', { token: token.value })
if (!active() || !data.sent) return
notice.value = '验证码已发送,请查看绑定邮箱'
form.code = ''
const deadline = Date.now() + 60000
countdown.value = 60
clearInterval(timer)
timer = setInterval(() => {
countdown.value = Math.max(0, Math.ceil((deadline - Date.now()) / 1000))
if (!countdown.value) clearInterval(timer)
}, 1000)
})
}
const verifyIdentity = () => {
if (!canVerify.value) return
return run(async active => {
const data = await request('verify', { token: token.value, method: form.method, code: form.code.trim() })
if (!active()) return
if (!data.token) throw new Error('未取得重置凭证,请重新验证')
token.value = data.token
form.code = ''
notice.value = ''
page.value = 'reset'
})
}
const completeReset = () => {
if (!canSubmitReset.value || page.value !== 'reset') return
return run(async active => {
const data = await request('reset', { token: token.value, new_password: form.newPassword })
if (!active()) return
if (!data.reset) throw new Error('无法确认重置结果,请重试')
token.value = ''
form.newPassword = ''
form.confirmPassword = ''
page.value = 'success'
})
}
const goRequest = () => {
version++
clearInterval(timer)
countdown.value = 0
loading.value = false
token.value = ''
identity.value = null
errorMessage.value = ''
notice.value = ''
form.code = ''
form.newPassword = ''
form.confirmPassword = ''
page.value = 'request'
}
const goLogin = () => window.location.assign(document.querySelector('#app')?.dataset.loginUrl || '/#/login')
onBeforeUnmount(goRequest)
return { page, loading, errorMessage, notice, identity, countdown, form, roleLabel, passwordMismatch, canSubmitReset, canVerify, requestReset, sendCode, verifyIdentity, completeReset, goRequest, goLogin }
}
+6 -6
View File
@@ -2,11 +2,11 @@
<AuthLayout
brand-caption="超级管理员密码重置"
intro-title="重置超级管理员密码"
intro-description="超级管理员密码不支持通过网页恢复,只能由部署端本机安全通道完成。"
intro-description="超级管理员无法通过登录页通道重置密码。已登录时可在安全中心重置,无法登录时请使用部署端本机重置。"
>
<template #intro-extra>
<el-alert
title="为避免远程接管风险,网页端不会收集或验证超级管理员重置凭据。"
title="登录页找回通道不收集或验证超级管理员重置凭据。"
type="warning"
:closable="false"
show-icon
@@ -18,10 +18,10 @@
<div class="security-notice">
<span class="notice-icon" aria-hidden="true">!</span>
<div>
<h2 id="recovery-title">请使用本机部署端工具</h2>
<h2 id="recovery-title">安全中心或本机部署重置</h2>
<p>
超级管理员重置必须在部署服务器本机发起,并由部署管理员进行交互式确认。
普通登录页、忘记密码页和其他 Web API 均不能开启或完成该流程。
已登录时,请在安全中心通过已绑定安全邮箱验证后设置新密码。
无法登录时,请在部署服务器本机发起重置,并进行交互式确认。
</p>
</div>
</div>
@@ -69,4 +69,4 @@ import { usePage } from './Recovery.js'
const { goLogin } = usePage()
</script>
<style scoped src="./Recovery.css"></style>
<style scoped src="./Recovery.css"></style>