Files
wer-admin/src/views/login.vue
2026-11-02 14:18:26 +08:00

561 lines
13 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.
<template>
<div class="login">
<!-- 左侧品牌区 -->
<section class="login-brand">
<header class="brand-head">
<span class="brand-mark">
<el-icon :size="26"><Drizzling /></el-icon>
</span>
<span class="brand-name">{{ title }}</span>
</header>
<div class="brand-copy">
<p class="brand-eyebrow">智慧计量 · 在线充值缴费 · 数据管控</p>
<h1 class="brand-headline">一体化水电缴费与计量管理后台</h1>
<p class="brand-desc">
面向物业、园区、公寓与宿舍场景,提供远程抄表、账单生成、在线缴费、欠费管控与用电用水量分析的一站式解决方案。
</p>
</div>
</section>
<!-- 右侧登录区 -->
<section class="login-panel">
<el-form
ref="loginRef"
:model="loginForm"
:rules="loginRules"
label-position="top"
hide-required-asterisk
class="login-form"
@submit.prevent
>
<div class="form-head">
<h2 class="form-title">管理员登录</h2>
<p class="form-subtitle">请输入您的管理员账号和密码登录系统</p>
</div>
<el-form-item prop="username" label="管理员账号">
<el-input v-model="loginForm.username" size="large" autocomplete="username">
<template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
</el-input>
</el-form-item>
<el-form-item prop="password" label="登录密码">
<el-input
v-model="loginForm.password"
type="password"
size="large"
show-password
autocomplete="current-password"
@keyup.enter="handleLogin"
>
<template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
</el-input>
</el-form-item>
<el-form-item v-if="captchaEnabled" prop="code" label="验证码" class="captcha-row">
<el-input v-model="loginForm.code" size="large" autocomplete="off" @keyup.enter="handleLogin">
<template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
</el-input>
<div class="login-code">
<img :src="codeUrl" class="login-code-img" alt="验证码" @click="getCode" />
</div>
</el-form-item>
<div class="form-meta">
<el-checkbox v-model="loginForm.rememberMe">记住账号</el-checkbox>
<router-link v-if="register" class="link-type" :to="'/register'">立即注册</router-link>
</div>
<el-form-item class="submit-row">
<el-button :loading="loading" size="large" type="primary" class="submit-button" @click.prevent="handleLogin">
<span v-if="!loading">登 录</span>
<span v-else>登 录 中...</span>
</el-button>
</el-form-item>
<div class="security">
<p class="security-label">安全提示</p>
<div class="security-card">
<p class="security-title">
<el-icon :size="16"><CircleCheckFilled /></el-icon>
<span>安全登录须知</span>
</p>
<ul class="security-list">
<li>仅限授权管理员登录,禁止共享账号</li>
<li>长时间无操作将自动退出登录</li>
</ul>
</div>
</div>
</el-form>
<p class="login-footer">Copyright © 2018-{{ currentYear }} 疯狂的狮子Li All Rights Reserved.</p>
</section>
</div>
</template>
<script setup lang="ts">
import { to } from 'await-to-js';
import { useI18n } from 'vue-i18n';
import { getCodeImg } from '@/api/login';
import { LoginData } from '@/api/types';
import { useUserStore } from '@/store/modules/user';
const title = import.meta.env.VITE_APP_TITLE;
const currentYear = new Date().getFullYear();
const userStore = useUserStore();
const router = useRouter();
const { t } = useI18n();
const loginForm = ref<LoginData>({
username: '',
password: '',
rememberMe: false,
code: '',
uuid: ''
} as LoginData);
const loginRules: ElFormRules = {
username: [
{
required: true,
trigger: 'blur',
message: t('login.rule.username.required')
}
],
password: [
{
required: true,
trigger: 'blur',
message: t('login.rule.password.required')
}
],
code: [
{
required: true,
trigger: 'change',
message: t('login.rule.code.required')
}
]
};
const codeUrl = ref('');
const loading = ref(false);
const captchaEnabled = ref(true);
const register = ref(false);
const redirect = ref('/');
const loginRef = ref<ElFormInstance>();
watch(
() => router.currentRoute.value,
(newRoute: any) => {
redirect.value = newRoute.query && newRoute.query.redirect && decodeURIComponent(newRoute.query.redirect);
},
{ immediate: true }
);
const handleLogin = () => {
loginRef.value?.validate(async (valid: boolean, fields: any) => {
if (valid) {
loading.value = true;
if (loginForm.value.rememberMe) {
localStorage.setItem('username', String(loginForm.value.username));
localStorage.setItem('rememberMe', String(loginForm.value.rememberMe));
} else {
localStorage.removeItem('username');
localStorage.removeItem('rememberMe');
}
localStorage.removeItem('password');
const [err] = await to(userStore.login(loginForm.value));
if (!err) {
const redirectUrl = redirect.value || '/';
await router.push(redirectUrl);
loading.value = false;
} else {
loading.value = false;
if (captchaEnabled.value) {
await getCode();
}
}
} else {
console.log('error submit!', fields);
}
});
};
const getCode = async () => {
const res = await getCodeImg();
const { data } = res;
captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
if (captchaEnabled.value) {
loginForm.value.code = '';
codeUrl.value = 'data:image/gif;base64,' + data.img;
loginForm.value.uuid = data.uuid;
}
};
const getLoginData = () => {
const username = localStorage.getItem('username');
const rememberMe = localStorage.getItem('rememberMe');
localStorage.removeItem('password');
loginForm.value = {
// 仅回填勾选“记住账号”时保存的用户名,密码一律不回填
username: username === null ? '' : username,
password: '',
rememberMe: rememberMe === 'true'
} as LoginData;
};
onMounted(() => {
getCode();
getLoginData();
});
</script>
<style lang="scss" scoped>
.login {
display: flex;
min-height: 100vh;
background: var(--app-surface-bg);
color: var(--app-text-title);
}
/* ---------- 左侧品牌区 ---------- */
.login-brand {
position: relative;
display: flex;
flex: 0 0 46%;
flex-direction: column;
max-width: 820px;
padding: 44px 56px 56px;
overflow: hidden;
color: #fff;
background: linear-gradient(160deg, #123a56 0%, #0f3350 46%, #0a2740 100%);
/* 右上角柔光,避免大面积纯色显得死板 */
&::after {
position: absolute;
top: -160px;
right: -140px;
width: 460px;
height: 460px;
content: '';
pointer-events: none;
background: radial-gradient(circle, rgba(96, 165, 250, 0.28), transparent 66%);
}
}
.brand-head {
position: relative;
z-index: 1;
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
display: inline-flex;
align-items: center;
justify-content: center;
width: 46px;
height: 46px;
color: #cfe2ff;
background: rgba(255, 255, 255, 0.12);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 12px;
}
.brand-name {
font-size: 22px;
font-weight: 700;
letter-spacing: 0.02em;
}
.brand-copy {
position: relative;
z-index: 1;
max-width: 520px;
margin-top: clamp(64px, 15vh, 150px);
}
.brand-eyebrow {
margin: 0 0 18px;
font-size: 13px;
font-weight: 700;
letter-spacing: 0.08em;
color: #a9c6e8;
}
.brand-headline {
margin: 0 0 20px;
font-size: clamp(26px, 2.6vw, 34px);
font-weight: 700;
line-height: 1.32;
letter-spacing: 0.01em;
}
.brand-desc {
margin: 0;
font-size: 14px;
line-height: 1.9;
color: rgba(226, 238, 250, 0.72);
}
/* ---------- 右侧登录区 ---------- */
.login-panel {
display: flex;
flex: 1 1 auto;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 24px;
padding: 48px 32px;
background: var(--app-surface-bg);
}
.login-form {
width: 100%;
max-width: 460px;
}
.form-head {
margin-bottom: 26px;
}
.form-title {
margin: 0;
font-size: 26px;
font-weight: 700;
letter-spacing: -0.01em;
color: var(--app-text-title);
}
.form-subtitle {
margin: 10px 0 0;
font-size: 13px;
line-height: 1.7;
color: var(--app-text-muted);
}
.login-form :deep(.el-form-item) {
margin-bottom: 20px;
}
.login-form :deep(.el-form-item__label) {
padding: 0 0 8px;
font-size: 13px;
font-weight: 600;
line-height: 1.4;
color: var(--app-text-title);
}
.login-form :deep(.el-input__wrapper) {
min-height: 48px;
padding: 0 14px;
background-color: var(--app-surface-bg);
border-radius: 10px;
box-shadow: 0 0 0 1px var(--app-surface-border) inset;
}
.login-form :deep(.el-input__wrapper:hover) {
box-shadow: 0 0 0 1px rgba(64, 158, 255, 0.45) inset;
}
.login-form :deep(.el-input__wrapper.is-focus) {
box-shadow:
0 0 0 1px rgba(64, 158, 255, 0.6) inset,
0 0 0 4px rgba(64, 158, 255, 0.1);
}
.login-form :deep(.input-icon) {
width: 16px;
height: 16px;
margin-right: 8px;
color: var(--app-text-muted);
fill: currentcolor;
}
/* 聚焦时图标跟随主色,弱化输入框里的黑白图标感 */
.login-form :deep(.el-input__wrapper.is-focus) .input-icon {
fill: var(--app-accent-strong);
}
.captcha-row :deep(.el-form-item__content) {
display: grid;
grid-template-columns: minmax(0, 1fr) 124px;
gap: 12px;
}
.login-code {
height: 48px;
overflow: hidden;
background: var(--app-surface-bg);
border: 1px solid var(--app-surface-border);
border-radius: 10px;
img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
cursor: pointer;
}
}
.form-meta {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: -4px 0 22px;
}
.login-form :deep(.el-checkbox__label) {
color: var(--app-text-muted);
}
.submit-row {
margin-bottom: 0;
}
.submit-button {
width: 100%;
height: 48px;
font-size: 15px;
font-weight: 600;
letter-spacing: 0.12em;
border-radius: 10px;
box-shadow: 0 14px 28px rgba(64, 158, 255, 0.22);
}
/* ---------- 安全提示 ---------- */
.security {
margin-top: 34px;
}
.security-label {
margin: 0 0 14px;
font-size: 13px;
text-align: center;
color: var(--app-text-muted);
}
.security-card {
padding: 16px 18px;
background: var(--app-accent-soft);
border-radius: 10px;
}
.security-title {
display: flex;
align-items: center;
gap: 6px;
margin: 0 0 12px;
font-size: 13px;
font-weight: 700;
color: var(--app-accent-strong);
}
.security-list {
margin: 0;
padding: 0;
list-style: none;
li {
position: relative;
padding-left: 16px;
margin-bottom: 8px;
font-size: 13px;
line-height: 1.6;
color: var(--app-accent-strong);
&::before {
position: absolute;
top: 8px;
left: 2px;
width: 5px;
height: 5px;
content: '';
background: currentcolor;
border-radius: 50%;
}
&:last-child {
margin-bottom: 0;
}
}
}
.login-footer {
margin: 0;
font-size: 12px;
text-align: center;
color: var(--app-text-muted);
letter-spacing: 0.04em;
}
/* 矮屏笔记本(约 1366×768 及以下)压缩纵向留白,避免表单溢出首屏 */
@media (max-height: 740px) and (min-width: 961px) {
.login-panel {
padding: 28px 32px;
gap: 16px;
}
.form-head {
margin-bottom: 20px;
}
.login-form :deep(.el-form-item) {
margin-bottom: 16px;
}
.security {
margin-top: 24px;
}
.brand-copy {
margin-top: clamp(40px, 8vh, 90px);
}
}
/* ---------- 响应式:窄屏下品牌区收成顶部条 ---------- */
@media (max-width: 960px) {
.login {
flex-direction: column;
}
.login-brand {
flex: 0 0 auto;
max-width: none;
padding: 24px 24px 28px;
}
.brand-copy {
margin-top: 24px;
}
.brand-headline {
font-size: 22px;
}
.brand-desc {
display: none;
}
.login-panel {
justify-content: flex-start;
padding: 32px 20px 40px;
}
}
@media (max-width: 640px) {
.brand-name {
font-size: 18px;
}
.captcha-row :deep(.el-form-item__content) {
grid-template-columns: 1fr;
}
}
</style>