Files
emsfront/src/views/login.vue

283 lines
6.7 KiB
Vue
Raw Normal View History

2025-06-14 18:03:55 +08:00
<template>
<div class="login">
2025-09-27 15:45:56 +08:00
<img :src="loginBg" alt="" srcset="" class="login-bg" />
2025-09-27 15:19:15 +08:00
<el-form
ref="loginForm"
:model="loginForm"
:rules="loginRules"
class="login-form"
>
<img
src="./../assets/images/ems/logo.png"
alt=""
srcset=""
class="login-logo"
/>
2025-06-14 18:03:55 +08:00
<el-form-item prop="username">
<el-input
v-model="loginForm.username"
type="text"
auto-complete="off"
placeholder="账号"
>
2025-09-27 15:19:15 +08:00
<svg-icon
slot="prefix"
icon-class="user"
class="el-input__icon input-icon"
/>
2025-06-14 18:03:55 +08:00
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
v-model="loginForm.password"
type="password"
auto-complete="off"
placeholder="密码"
@keyup.enter.native="handleLogin"
>
2025-09-27 15:19:15 +08:00
<svg-icon
slot="prefix"
icon-class="password"
class="el-input__icon input-icon"
/>
2025-06-14 18:03:55 +08:00
</el-input>
</el-form-item>
<el-form-item prop="code" v-if="captchaEnabled">
<el-input
v-model="loginForm.code"
auto-complete="off"
placeholder="验证码"
style="width: 63%"
@keyup.enter.native="handleLogin"
>
2025-09-27 15:19:15 +08:00
<svg-icon
slot="prefix"
icon-class="validCode"
class="el-input__icon input-icon"
/>
2025-06-14 18:03:55 +08:00
</el-input>
<div class="login-code">
2025-09-27 15:19:15 +08:00
<img :src="codeUrl" @click="getCode" class="login-code-img" />
2025-06-14 18:03:55 +08:00
</div>
</el-form-item>
2025-09-27 15:19:15 +08:00
<el-checkbox
v-model="loginForm.rememberMe"
style="margin: 0px 0px 25px 0px"
>记住密码</el-checkbox
>
<el-form-item style="width: 100%">
2025-06-14 18:03:55 +08:00
<el-button
:loading="loading"
size="medium"
type="primary"
2025-09-27 15:19:15 +08:00
style="width: 100%"
2025-06-14 18:03:55 +08:00
@click.native.prevent="handleLogin"
>
<span v-if="!loading"> </span>
<span v-else> 中...</span>
</el-button>
2025-09-27 15:19:15 +08:00
<div style="float: right" v-if="register">
<router-link class="link-type" :to="'/register'"
>立即注册</router-link
>
2025-06-14 18:03:55 +08:00
</div>
</el-form-item>
</el-form>
<!-- 底部 -->
<div class="el-login-footer">
2025-06-30 14:18:32 +08:00
<span>Copyright © 2025 上海电动工具研究所集团有限公司.</span>
2025-06-14 18:03:55 +08:00
</div>
</div>
</template>
<script>
2025-09-27 15:19:15 +08:00
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
2025-09-27 15:45:56 +08:00
import intervalUpdate from "@/mixins/ems/intervalUpdate";
2025-06-14 18:03:55 +08:00
export default {
name: "Login",
2025-09-27 15:45:56 +08:00
mixins: [intervalUpdate],
computed: {
loginBg() {
return require(`./../assets/images/ems/loginBg/${this.bgNum}.png`);
},
},
2025-06-14 18:03:55 +08:00
data() {
return {
2025-09-27 15:45:56 +08:00
bgNum: 1,
2025-06-14 18:03:55 +08:00
codeUrl: "",
loginForm: {
username: "admin",
password: "admin123",
rememberMe: false,
code: "",
2025-09-27 15:19:15 +08:00
uuid: "",
2025-06-14 18:03:55 +08:00
},
loginRules: {
username: [
2025-09-27 15:19:15 +08:00
{ required: true, trigger: "blur", message: "请输入您的账号" },
2025-06-14 18:03:55 +08:00
],
password: [
2025-09-27 15:19:15 +08:00
{ required: true, trigger: "blur", message: "请输入您的密码" },
2025-06-14 18:03:55 +08:00
],
2025-09-27 15:19:15 +08:00
code: [{ required: true, trigger: "change", message: "请输入验证码" }],
2025-06-14 18:03:55 +08:00
},
loading: false,
// 验证码开关
captchaEnabled: true,
// 注册开关
register: false,
2025-09-27 15:19:15 +08:00
redirect: undefined,
};
2025-06-14 18:03:55 +08:00
},
watch: {
$route: {
2025-09-27 15:19:15 +08:00
handler: function (route) {
this.redirect = route.query && route.query.redirect;
2025-06-14 18:03:55 +08:00
},
2025-09-27 15:19:15 +08:00
immediate: true,
},
2025-06-14 18:03:55 +08:00
},
created() {
2025-09-27 15:19:15 +08:00
this.getCode();
this.getCookie();
2025-06-14 18:03:55 +08:00
},
2025-09-27 15:45:56 +08:00
mounted() {
this.updateInterval(this.updateBgNum, 5000);
},
2025-06-14 18:03:55 +08:00
methods: {
2025-09-27 15:45:56 +08:00
updateBgNum() {
if (this.bgNum >= 4) this.bgNum = 0;
this.bgNum += 1;
},
2025-06-14 18:03:55 +08:00
getCode() {
2025-09-27 15:19:15 +08:00
getCodeImg().then((res) => {
this.captchaEnabled =
res.captchaEnabled === undefined ? true : res.captchaEnabled;
2025-06-14 18:03:55 +08:00
if (this.captchaEnabled) {
2025-09-27 15:19:15 +08:00
this.codeUrl = "data:image/gif;base64," + res.img;
this.loginForm.uuid = res.uuid;
2025-06-14 18:03:55 +08:00
}
2025-09-27 15:19:15 +08:00
});
2025-06-14 18:03:55 +08:00
},
getCookie() {
2025-09-27 15:19:15 +08:00
const username = Cookies.get("username");
const password = Cookies.get("password");
const rememberMe = Cookies.get("rememberMe");
2025-06-14 18:03:55 +08:00
this.loginForm = {
username: username === undefined ? this.loginForm.username : username,
2025-09-27 15:19:15 +08:00
password:
password === undefined ? this.loginForm.password : decrypt(password),
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
};
2025-06-14 18:03:55 +08:00
},
handleLogin() {
2025-09-27 15:19:15 +08:00
this.$refs.loginForm.validate((valid) => {
2025-06-14 18:03:55 +08:00
if (valid) {
2025-09-27 15:19:15 +08:00
this.loading = true;
2025-06-14 18:03:55 +08:00
if (this.loginForm.rememberMe) {
2025-09-27 15:19:15 +08:00
Cookies.set("username", this.loginForm.username, { expires: 30 });
Cookies.set("password", encrypt(this.loginForm.password), {
expires: 30,
});
Cookies.set("rememberMe", this.loginForm.rememberMe, {
expires: 30,
});
2025-06-14 18:03:55 +08:00
} else {
2025-09-27 15:19:15 +08:00
Cookies.remove("username");
Cookies.remove("password");
Cookies.remove("rememberMe");
2025-06-14 18:03:55 +08:00
}
2025-09-27 15:19:15 +08:00
this.$store
.dispatch("Login", this.loginForm)
.then(() => {
this.$router.push({ path: this.redirect || "/" }).catch(() => {});
})
.catch(() => {
this.loading = false;
if (this.captchaEnabled) {
this.getCode();
}
});
2025-06-14 18:03:55 +08:00
}
2025-09-27 15:19:15 +08:00
});
},
},
};
2025-06-14 18:03:55 +08:00
</script>
<style rel="stylesheet/scss" lang="scss">
.login {
padding-left: 180px;
2025-06-14 18:03:55 +08:00
display: flex;
2025-09-27 15:19:15 +08:00
justify-content: left;
2025-06-14 18:03:55 +08:00
align-items: center;
height: 100%;
2025-09-27 15:45:56 +08:00
}
.login-bg {
position: absolute;
top: 0;
left: 0;
z-index: 1;
display: block;
height: 100%;
width: 100%;
2025-06-14 18:03:55 +08:00
}
2025-09-27 15:19:15 +08:00
.login-logo {
display: block;
width: 70%;
height: auto;
margin: 0 auto;
}
2025-06-14 18:03:55 +08:00
.login-form {
border-radius: 6px;
background: #ffffff;
width: 400px;
2025-09-27 15:19:15 +08:00
padding: 0 25px 5px 25px;
2025-09-27 15:45:56 +08:00
z-index: 2;
2025-06-14 18:03:55 +08:00
.el-input {
height: 38px;
input {
height: 38px;
}
}
.input-icon {
height: 39px;
width: 14px;
margin-left: 2px;
}
}
.login-tip {
font-size: 13px;
text-align: center;
color: #bfbfbf;
}
.login-code {
width: 33%;
height: 38px;
float: right;
img {
cursor: pointer;
vertical-align: middle;
}
}
.el-login-footer {
height: 40px;
line-height: 40px;
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
color: #fff;
font-family: Arial;
font-size: 12px;
letter-spacing: 1px;
}
.login-code-img {
height: 38px;
}
</style>