数据库
config/confgi.default.js
exports.mongoose = {
client: {
url: 'mongodb://admin:123456@127.0.0.1/eggxiaomi',
options: {},
}
};
config/plugin.js
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
};
model/admin.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
var d = new Date();
const AdminSchema = new Schema({
username: {
type: String
},
password: {
type: String
},
mobile: {
type: String
},
email: {
type: String
},
status: {
type: Number,
default: 1
},
role_id: {
type: Schema.Types.ObjectId
},
add_time: {
type: Number,
default: d.getTime()
},
is_super: {
type: Number
}
});
return mongoose.model('Admin', AdminSchema, 'admin');
}
登录
controller
controller/admin/login.js
async doLogin() {
console.log(this.ctx.request.body)
var username = this.ctx.request.body.username;
var password = this.ctx.request.body.password;
//密码md5加密
// var password = await this.service.tools.md5(this.ctx.request.body.password);
var code = this.ctx.request.body.verify;
//不区分验证码大小写
// if(code.toUpperCase() == this.ctx.session.code.toUpperCase()){
if(code.toUpperCase()==this.ctx.session.code.toUpperCase()){
var result = await this.ctx.model.Admin.find({"username":username,"password":password});
console.log("查询数据库的用户密码")
console.log(result)
if(result.length>0){
console.log("登录成功")
//登录成功
// 1.保存用户信息
this.ctx.session.userinfo = result[0]
// 2.跳转到用户中心
this.ctx.redirect('/admin/manager')
}else{
console.log("用户或者密码,登录失败")
this.ctx.redirect('/admin/login','用户或者密码不对')
}
}else{
console.log('验证码错误')
await this.error('/admin/login','验证码错误')
}
}
server
service/tools.js
const md5 = require('md5');
async md5(str){
return md5(str);
}
退出
view
view/admin/public/page_header.html
<ul class="nav navbar-nav navbar-right">
<li><a>欢迎您,<%= userinfo.username %></a>
</li>
<li><a href="/admin/loginOut">安全退出</a>
</li>
</ul>
router.js
router.get('/admin/loginOut', controller.admin.login.loginOut);
controller
controller/admin/login.js
async loginOut(){
this.ctx.session.userinfo= null;
this.ctx.redirect('/admin/login')
}