本文主要是介绍vue3+node.js 高校设备管理系统设计5-前端登录界面,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- Login.vue组件制作
- 1.在components文件夹中新建Login.vue,并创建vue默认样式
- 2.在router文件夹中的index.js修改路由
- 3.Login代码
- 4.部分代码解释及组件介绍
Login.vue组件制作
1.在components文件夹中新建Login.vue,并创建vue默认样式
2.在router文件夹中的index.js修改路由
- 首先导入Login组件
import Login from '../components/Login.vue'
- 修改路由重定向至Login界面
const routes = [{
path: '/',
redirect: '/login',
},{
path: '/login',
name: '/login',
component: Login,
}
]
3.Login代码
<template>
<div class="login_bg">
<div class="login_box">
<!-- LOGO -->
<div class="avatar_box">
<img src="../assets/logo.png" alt="" />
</div>
<!-- 表单 -->
<el-form
ref="loginFormRef"
:model="loginForm"
:rules="loginFormRules"
class="login_form"
>
<!-- 用户名 -->
<el-form-item prop="username">
<el-input
v-model="loginForm.username"
prefix-icon="el-icon-user"
></el-input>
</el-form-item>
<!-- 密码 -->
<el-form-item prop="password">
<el-input
v-model="loginForm.password"
prefix-icon="el-icon-lock"
type="password"
></el-input>
</el-form-item>
<!-- 按钮 -->
<el-form-item class="btns">
<el-button type="primary" plain @click="login">登录</el-button>
<el-button type="primary" plain @click="resetLoginForm"
>重置</el-button
>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: "admin",
password: "admin",
msg: "", // 接收数据
},
loginList: {},
loginFormRules: {
// 验证用户名是否合法
username: [
{ required: true, message: "请输入账号", trigger: "blur" },
{ min: 3, max: 5, message: "长度在 3 到 10 个字符", trigger: "blur" },
],
// 验证密码是否合法
password: [
{ required: true, message: "请输入密码", trigger: "blur" },
{ min: 3, max: 5, message: "长度在 3 到 10 个字符", trigger: "blur" },
],
},
};
},
created() {},
methods: {
// 点击重置按钮
resetLoginForm() {
this.$refs.loginFormRef.resetFields();
},
login() {
// 重置Cookie
this.$cookies.remove("user_id");
this.$cookies.remove("username");
this.$cookies.remove("token");
this.$cookies.remove("role_name");
this.$cookies.remove("rightsList");
this.$axios
.post(this.HOST + "/api/login", {
username: this.loginForm.username,
password: this.loginForm.password,
})
.then((result) => {
this.status = result.data.status;
if (this.status === 200) {
this.$message.success("登录成功!");
this.loginList = result.data.data;
this.$cookies.set("user_id", result.data.user_id, {
expires: 7,
});
this.$cookies.set("username", result.data.username, {
expires: 7,
});
this.$cookies.set("token", result.data.token, {
expires: 7,
});
this.$cookies.set("role_name", result.data.role_name, {
expires: 7,
});
this.addUserLogin();
this.$router.push("/home");
} else if(this.status === 404){
this.$message.error("无此用户!");
} else if(this.status === 403){
this.$message.error("该用户被禁用!");
} else {
this.$message.error("密码错误!");
}
})
.catch((err) => {
console.log(err);
});
},
// 记录用户登录时间
addUserLogin() {
this.user_id = this.$cookies.get("user_id");
this.$axios
.post(this.HOST + "/api/addUserLogin", {
user_id: this.user_id,
})
.then((result) => {
this.status = result.data.status;
if (this.status === 200) {
this.$message.success("记录用户登录时间成功");
} else {
this.$message.error("记录用户登录时间失败");
}
})
.catch((error) => {
console.log(error);
});
},
},
};
</script>
<style lang="less" scoped>
.login_bg {
background: url('../assets/bg.jpg') fixed 100%;
height: 100%;
}
.login_box {
width: 450px;
height: 300px;
background-color: #fff;
border-radius: 3px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.avatar_box {
height: 130px;
width: 130px;
border: 1px solid #eee;
border-radius: 50%;
padding: 10px;
box-shadow: 0 0 10px #ddd;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
img {
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #eee;
}
}
.login_form {
position: absolute;
bottom: 0;
width: 100%;
padding: 0 20px;
box-sizing: border-box;
}
.btns {
display: flex;
justify-content: flex-end;
}
</style>
4.部分代码解释及组件介绍
el-from
<el-form
ref="loginFormRef"
:model="loginForm"
:rules="loginFormRules"
class="login_form"
>
</el-form>
- ref 绑定控件,$refs 获取控件,methods 里就可以用
this.refs.loginFormRef.validate((valid) => {}
- :model 动态绑定loginForm表单
- :rules 动态绑定验证规则,通过loginFormRules属性传入约定的验证规则
- class 绑定样式
这篇关于vue3+node.js 高校设备管理系统设计5-前端登录界面的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!