zhiqi/src/views/login.vue

520 lines
11 KiB
Vue

<template>
<div class="login">
<img src="../assets/images/logo.png" class="logo" alt="logo" />
<div class="login-container">
<!-- 左侧插图区域 -->
<!-- <div class="login-left">
<img
src="../assets/images/login-box-bg.svg"
alt="登录插图"
class="login-illustration"
/>
</div> -->
<!-- 右侧登录表单区域 -->
<div class="login-right">
<el-form
ref="loginRef"
:model="loginForm"
:rules="loginRules"
class="login-form"
>
<h3 class="title-text">品号高效检索系统</h3>
<el-form-item prop="username">
<el-input
v-model="loginForm.username"
type="text"
size="large"
auto-complete="off"
placeholder="请输入工号"
>
<template #prefix
><svg-icon icon-class="user" class="el-input__icon input-icon"
/></template>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
v-model="loginForm.password"
:type="showPassword ? 'text' : 'password'"
size="large"
auto-complete="off"
placeholder="请输入密码"
@keyup.enter="handleLogin"
>
<template #prefix
><svg-icon
icon-class="password"
class="el-input__icon input-icon"
/></template>
<template #suffix>
<el-icon
class="password-eye-icon"
@click="togglePasswordVisibility"
style="cursor: pointer"
>
<View v-if="!showPassword" />
<Hide v-else />
</el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item prop="code" v-if="captchaEnabled">
<el-input
v-model="loginForm.code"
size="large"
auto-complete="off"
placeholder="验证码"
style="width: 63%"
@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" @click="getCode" class="login-code-img" />
</div>
</el-form-item>
<el-checkbox
v-model="loginForm.rememberMe"
style="margin: 0px 0px 25px 0px"
>记住密码</el-checkbox
>
<el-form-item style="width: 100%">
<el-button
:loading="loading"
size="large"
type="primary"
style="width: 100%"
@click.prevent="handleLogin"
>
<span v-if="!loading">登 录</span>
<span v-else>登 录 中...</span>
</el-button>
<div style="float: right" v-if="register">
<router-link class="link-type" :to="'/register'"
>立即注册</router-link
>
</div>
</el-form-item>
</el-form>
</div>
</div>
<!-- 底部 -->
<div class="el-login-footer">
<span>Copyright © 2015-2025</span>
</div>
</div>
</template>
<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();
const loginForm = ref({
username: "",
password: "",
rememberMe: false,
code: "",
uuid: "",
});
const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);
// 密码显示/隐藏
const showPassword = ref(false);
// 动态验证规则
const loginRules = computed(() => {
const rules = {
username: [{ required: true, trigger: "blur", message: "请输入您的工号" }],
password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
};
if (captchaEnabled.value) {
rules.code = [
{ required: true, trigger: "change", message: "请输入验证码" },
];
}
return rules;
});
watch(
route,
(newRoute) => {
redirect.value = newRoute.query && newRoute.query.redirect;
},
{ immediate: true }
);
function handleLogin() {
proxy.$refs.loginRef.validate((valid) => {
if (valid) {
loading.value = true;
// 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
if (loginForm.value.rememberMe) {
Cookies.set("username", loginForm.value.username, { expires: 30 });
Cookies.set("password", encrypt(loginForm.value.password), {
expires: 30,
});
Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
} else {
// 否则移除
Cookies.remove("username");
Cookies.remove("password");
Cookies.remove("rememberMe");
}
// 调用action的登录方法
userStore
.login(loginForm.value)
.then(() => {
const query = route.query;
const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
if (cur !== "redirect") {
acc[cur] = query[cur];
}
return acc;
}, {});
router.push({ path: redirect.value || "/", query: otherQueryParams });
})
.catch(() => {
loading.value = false;
// 重新获取验证码
if (captchaEnabled.value) {
getCode();
}
});
}
});
}
function getCode() {
getCodeImg().then((res) => {
captchaEnabled.value =
res.captchaEnabled === undefined ? true : res.captchaEnabled;
if (captchaEnabled.value) {
codeUrl.value = "data:image/gif;base64," + res.img;
loginForm.value.uuid = res.uuid;
}
});
}
function getCookie() {
const username = Cookies.get("username");
const password = Cookies.get("password");
const rememberMe = Cookies.get("rememberMe");
loginForm.value = {
username: username === undefined ? loginForm.value.username : username,
password:
password === undefined ? loginForm.value.password : decrypt(password),
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
};
}
function togglePasswordVisibility() {
showPassword.value = !showPassword.value;
}
getCode();
getCookie();
</script>
<style lang='scss' scoped>
.login {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
width: 100%;
position: relative;
background-image: url("../assets/images/bg.jpg");
background-repeat: no-repeat;
background-size: 100% 100%;
background-clip: padding-box;
overflow: visible;
background-position: center;
background-attachment: fixed;
box-sizing: border-box;
}
.logo {
position: absolute;
top: 70px;
left: 80px;
width: 30%;
}
.login-container {
display: flex;
width: 100%;
max-width: 1400px;
min-height: 100vh;
position: relative;
z-index: 1;
box-sizing: border-box;
justify-content: flex-end;
align-items: center;
padding: 40px 60px;
}
.login-right {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: relative;
box-sizing: border-box;
width: 100%;
max-width: 500px;
}
.title-text {
font-family: "PingFang SC", SimHei, sans-serif;
margin: 0 auto 35px;
text-align: center;
color: #017d9c;
font-size: 34px;
font-weight: 600;
letter-spacing: 0.5px;
line-height: 1.4;
width: 100%;
box-sizing: border-box;
}
.login-form {
border-radius: 16px;
background: #ffffff;
width: 420px;
min-width: 420px;
max-width: 420px;
padding: 50px 40px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
box-sizing: border-box;
flex-shrink: 0;
:deep(.el-form-item) {
margin-bottom: 15px;
&:last-child {
margin-bottom: 0;
}
}
:deep(.el-input__wrapper) {
border-radius: 8px;
box-shadow: 0 0 0 1px #dcdfe6 inset;
transition: all 0.3s;
&:hover {
box-shadow: 0 0 0 1px #c0c4cc inset;
}
&.is-focus {
box-shadow: 0 0 0 1px #409eff inset;
}
}
:deep(.el-input__inner) {
font-size: 14px;
color: #333;
&::placeholder {
color: #a8abb2;
font-size: 14px;
}
}
.el-input {
height: 48px;
input {
height: 48px;
line-height: 48px;
}
}
.input-icon {
height: 48px;
width: 16px;
margin-left: 0px;
color: #909399;
}
.password-eye-icon {
color: #909399;
font-size: 18px;
transition: color 0.2s;
cursor: pointer;
&:hover {
color: #409eff;
}
}
:deep(.el-button--primary) {
background-color: #017d9c;
border-color: #017d9c;
height: 48px;
font-size: 16px;
font-weight: 500;
letter-spacing: 2px;
border-radius: 8px;
transition: all 0.3s;
&:hover {
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(32, 178, 170, 0.3);
}
&:active {
transform: translateY(0);
}
}
:deep(.el-checkbox) {
margin-bottom: 20px;
.el-checkbox__label {
color: #666;
font-size: 14px;
padding-left: 8px;
}
.el-checkbox__input.is-checked .el-checkbox__inner {
background-color: #017d9c;
border-color: #017d9c;
}
}
}
.login-tip {
font-size: 13px;
text-align: center;
color: #bfbfbf;
}
.login-code {
width: 36%;
height: 48px;
float: right;
display: flex;
align-items: center;
justify-content: center;
img {
cursor: pointer;
vertical-align: middle;
height: 48px;
border-radius: 8px;
border: 1px solid #dcdfe6;
transition: all 0.2s;
&:hover {
border-color: #409eff;
}
}
}
.el-login-footer {
height: 40px;
line-height: 40px;
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
color: #999;
font-family: Arial, sans-serif;
font-size: 12px;
letter-spacing: 0.5px;
z-index: 10;
background: transparent;
pointer-events: none;
span {
display: inline-block;
opacity: 0.8;
}
}
.login-code-img {
height: 48px;
padding-left: 0;
width: 100%;
}
// 响应式设计
@media (max-width: 1024px) {
.login-container {
justify-content: center;
padding: 40px 40px;
}
.login-right {
max-width: 100%;
width: 100%;
}
.login-form {
width: 100%;
max-width: 420px;
min-width: 320px;
padding: 45px 35px;
}
}
@media (max-width: 768px) {
.login-container {
padding: 30px 20px;
justify-content: center;
align-items: center;
}
.login-right {
width: 100%;
max-width: 100%;
}
.login-form {
width: 100%;
max-width: 100%;
min-width: 280px;
padding: 35px 28px;
}
.title-text {
font-size: 28px;
margin-bottom: 30px;
}
}
@media (max-width: 480px) {
.login-container {
padding: 20px 15px;
}
.login-form {
padding: 30px 20px;
border-radius: 12px;
}
.title-text {
font-size: 24px;
margin-bottom: 25px;
}
}
</style>