之前一直想自己独立做个项目,但总是纠结于没有后端数据,使用模拟的假数据,总觉得不是那么好,测不了接口请求等等之类的。反正就是各种原因吧(也可能是自己太懒找的各种接口),一直都是开始10%,就中断了,也就没有后续了。每次都是信心满满的开始,没有结果的结束。
哈哈哈哈哈,你们会不会像我这个小菜鸟这样。不说了(;′⌒`)😭桑心。
--------------------------------------------------进入正题---------------------------------------------------------------------
注:我会一模块一模块更新的。
项目需求:搭建一个后台管理系统。
前端:vue + vue-router + vuex + axios + elementUI等。
后端:nodejs + express + mongodb 等。
项目的部分截图展示:【项目还在进行中,暂完整实现了登录】

登录这块的代码如下:
<template>
<div id="login" class="login_container">
<div class="login_box">
<div class="avatar_box">
后台管理系统
</div>
<el-form
:model="loginForm"
:rules="loginFornRules"
ref="loginFormRef"
label-width="0px"
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 type="password" v-model="loginForm.password" prefix-icon="el-icon-goods"></el-input>
</el-form-item>
<!-- button -->
<el-form-item class="btns">
<el-button type="primary" @click="toLogin()">登录</el-button>
<el-button type="info" @click="resetForm()">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
import {reqLogin} from '../api/index'
import {DURATION} from '../utils/constants'
var that;
export default {
data () {
return {
loginForm: {
username: 'admin',
password: 'admin'
},
loginFornRules: {
username: [
{ required: true, message: '请输入登录名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 2, max: 8, message: '密码必须在2-8之间', trigger: 'blur' }
]
}
}
},
methods: {
// 重置按钮
resetForm: function () {
this.$refs.loginFormRef.resetFields()
},
toLogin: function () {
that = this;
that.$refs.loginFormRef.validate(async valid => {
// 表单与验证
if (!valid) {
return
}
const {message,token} = await reqLogin(that.loginForm)
if(message != 'ok') return that.$message.error('登录失败')
that.$message({
message:'登录成功',
duration:DURATION,
type:'success'
})
window.sessionStorage.setItem('token',token);
that.$router.push('/home');
})
}
}
}
</script>
<style lang="less" scoped>
.login_container {
background: url('../assets/image/bg.jpeg');
height: 100%;
}
.login_box {
width: 450px;
height: 300px;
background: #ffffff;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
.avatar_box {
position: absolute;
left: 50%;
top: 40px;
font-size: 28px;
transform: translate(-50%, -50%);
img {
width: 100%;
height: 100%;
border-radius: 50%;
background: #eeeeee;
}
}
.login_form {
position: absolute;
bottom: 24px;
width: 100%;
padding: 0 20px;
box-sizing: border-box;
.btns {
display: flex;
justify-content: flex-end;
}
}
}
</style>
/**
* 登录接口
* status: 0 成功、 1 失败
*
*/
router.post('/api/login.do',async (req,res)=>{
// 判断用户名
const user = await LoginModel.findOne({
username:req.body.username
})
if(!user){
return res.status(422).send({
status:1,
message:'用户名不存在'
})
}
// 判断密码
const isPasswordValid = require('bcrypt').compareSync(
req.body.password,
user.password
)
if(!isPasswordValid){
return res.status(422).send({
status:1,
message:'密码无效'
})
}
const token = jwt.sign({
id:String(user._id)
},SECRET)
// 返回结果
res.send({
status:0,
data:user,
token,
message:'ok'
})
res.end();
})
ღ( ´・ᴗ・` )❤暂完。
本文分享了使用Vue、Node.js等技术搭建后台管理系统的经验,详细介绍了登录模块的实现过程,包括前端代码示例和后端接口设计。
2375

被折叠的 条评论
为什么被折叠?



