Files
SpectraElement/Hardware-Vue/src/components/AdminPasswordModal.vue
T
2026-11-19 16:42:26 +08:00

318 lines
7.2 KiB
Vue

<script setup lang="ts">
import { nextTick, onBeforeUnmount, ref, watch } from 'vue'
import { verifyAdminPassword } from '@/api/systemConfig'
const props = defineProps<{
open: boolean
}>()
const emit = defineEmits<{
close: []
success: []
}>()
/** 默认填入测试密码,开发与正式环境均生效;上线前需改回空字符串 */
const DEFAULT_PASSWORD = '123456'
const password = ref(DEFAULT_PASSWORD)
const error = ref('')
const submitting = ref(false)
const inputElement = ref<HTMLInputElement | null>(null)
function close() {
if (submitting.value) return
emit('close')
}
function onKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') close()
}
async function submit() {
if (submitting.value) return
if (!password.value) {
error.value = '请输入密码'
return
}
submitting.value = true
error.value = ''
try {
await verifyAdminPassword(password.value)
password.value = ''
emit('success')
} catch (err) {
error.value = err instanceof Error ? err.message : '密码校验失败'
} finally {
submitting.value = false
}
}
watch(
() => props.open,
async (open) => {
if (open) {
password.value = DEFAULT_PASSWORD
error.value = ''
window.addEventListener('keydown', onKeydown)
await nextTick()
inputElement.value?.focus()
} else {
window.removeEventListener('keydown', onKeydown)
}
},
)
onBeforeUnmount(() => {
window.removeEventListener('keydown', onKeydown)
})
</script>
<template>
<Teleport to="body">
<Transition name="auth-modal">
<div v-if="open" class="auth-backdrop" @click.self="close">
<section class="auth-card" role="dialog" aria-modal="true" aria-label="管理员验证">
<div class="auth-card__icon" aria-hidden="true">
<svg viewBox="0 0 64 64">
<rect
x="14"
y="28"
width="36"
height="26"
rx="6"
fill="none"
stroke="currentColor"
stroke-width="3"
/>
<path
d="M22 28v-8a10 10 0 0 1 20 0v8"
fill="none"
stroke="currentColor"
stroke-width="3"
stroke-linecap="round"
/>
<circle cx="32" cy="41" r="3.2" fill="currentColor" />
</svg>
</div>
<h2>管理员验证</h2>
<p>请输入管理员密码以进入系统参数管理</p>
<form class="auth-card__form" @submit.prevent="submit">
<input
ref="inputElement"
v-model="password"
class="auth-card__input"
type="password"
autocomplete="off"
placeholder="请输入密码"
:disabled="submitting"
/>
<p v-if="error" class="auth-card__error">{{ error }}</p>
<div class="auth-card__actions">
<button
class="auth-card__button auth-card__button--ghost"
type="button"
:disabled="submitting"
@click="close"
>
取消
</button>
<button
class="auth-card__button auth-card__button--primary"
type="submit"
:disabled="submitting"
>
{{ submitting ? '验证中...' : '确定' }}
</button>
</div>
</form>
</section>
</div>
</Transition>
</Teleport>
</template>
<style scoped>
.auth-backdrop {
position: fixed;
inset: 0;
z-index: 70;
display: grid;
place-items: center;
padding: 24px;
background: rgba(3, 8, 18, 0.68);
backdrop-filter: blur(8px);
}
.auth-card {
width: 560px;
padding: 42px 44px 38px;
border: 1px solid rgba(130, 175, 255, 0.2);
border-radius: 26px;
background: linear-gradient(158deg, rgba(22, 42, 78, 0.98) 0%, rgba(8, 16, 32, 0.99) 100%);
box-shadow:
0 40px 90px rgba(0, 6, 20, 0.6),
0 0 50px rgba(48, 118, 255, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.08);
color-scheme: dark;
text-align: center;
}
.auth-card__icon {
display: grid;
place-items: center;
width: 86px;
height: 86px;
margin: 0 auto 24px;
border: 1px solid rgba(120, 200, 255, 0.3);
border-radius: 50%;
background: rgba(70, 140, 255, 0.14);
box-shadow:
0 0 32px rgba(70, 140, 255, 0.22),
inset 0 1px 0 rgba(255, 255, 255, 0.1);
color: #7fe0ff;
}
.auth-card__icon svg {
width: 46px;
height: 46px;
}
.auth-card h2 {
color: #eef5ff;
font-size: 30px;
font-weight: 600;
letter-spacing: 1px;
text-shadow: 0 0 24px rgba(80, 150, 255, 0.28);
}
.auth-card p {
margin-top: 14px;
color: rgba(158, 190, 226, 0.7);
font-size: 19px;
letter-spacing: 0.5px;
}
.auth-card__form {
margin-top: 26px;
}
.auth-card__input {
width: 100%;
height: 62px;
padding: 0 22px;
border: 1px solid rgba(130, 175, 255, 0.22);
border-radius: 12px;
background: rgba(6, 14, 28, 0.7);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
color: #e8f2ff;
font-family: inherit;
font-size: 22px;
letter-spacing: 2px;
outline: none;
transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.auth-card__input::placeholder {
color: rgba(158, 190, 226, 0.4);
}
.auth-card__input:focus {
border-color: rgba(120, 200, 255, 0.55);
box-shadow:
0 0 0 4px rgba(70, 140, 255, 0.15),
0 0 24px rgba(70, 140, 255, 0.12);
}
.auth-card__input:disabled {
background: rgba(4, 10, 22, 0.8);
}
.auth-card__error {
margin-top: 12px;
color: #ff9d9d;
font-size: 17px;
letter-spacing: 0.5px;
}
.auth-card__actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
margin-top: 30px;
}
.auth-card__button {
height: 56px;
border-radius: 12px;
font-size: 20px;
font-weight: 600;
letter-spacing: 3px;
transition:
transform 0.2s ease,
box-shadow 0.2s ease,
background 0.2s ease,
border-color 0.2s ease;
}
.auth-card__button:disabled {
cursor: default;
opacity: 0.5;
}
.auth-card__button--ghost {
border: 1px solid rgba(130, 175, 255, 0.28);
background: rgba(18, 40, 78, 0.5);
color: #bcd9ff;
}
.auth-card__button--ghost:hover:not(:disabled) {
border-color: rgba(150, 205, 255, 0.5);
background: rgba(30, 66, 128, 0.6);
}
.auth-card__button--primary {
background: linear-gradient(180deg, #ffd166 0%, #f0ac0b 100%);
box-shadow:
0 14px 30px rgba(240, 172, 11, 0.28),
0 0 26px rgba(255, 190, 60, 0.16),
inset 0 1px 0 rgba(255, 255, 255, 0.6);
color: #241500;
text-shadow: 0 1px 1px rgba(255, 255, 255, 0.25);
}
.auth-card__button--primary:hover:not(:disabled) {
box-shadow:
0 18px 38px rgba(240, 172, 11, 0.36),
0 0 34px rgba(255, 190, 60, 0.26),
inset 0 1px 0 rgba(255, 255, 255, 0.6);
transform: translateY(-2px);
}
.auth-modal-enter-active,
.auth-modal-leave-active {
transition: opacity 0.24s ease;
}
.auth-modal-enter-active .auth-card,
.auth-modal-leave-active .auth-card {
transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.24s ease;
}
.auth-modal-enter-from,
.auth-modal-leave-to {
opacity: 0;
}
.auth-modal-enter-from .auth-card,
.auth-modal-leave-to .auth-card {
opacity: 0;
transform: translateY(16px) scale(0.97);
}
</style>