You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.5 KiB
69 lines
1.5 KiB
const mongoose = require('mongoose');
|
|
const bcrypt = require('bcrypt-nodejs');
|
|
const SALT_WORK_FACTOR = 10;
|
|
const Schema = mongoose.Schema;
|
|
const AdminSchema = new Schema({
|
|
code: {
|
|
type: String,
|
|
unique: true,
|
|
required: true
|
|
},
|
|
password: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
isAdmin:{
|
|
type:Boolean,
|
|
default:true
|
|
},
|
|
meta: {
|
|
createAt: {
|
|
type: Date,
|
|
default: Date.now()
|
|
},
|
|
updateAt: {
|
|
type: Date,
|
|
default: Date.now()
|
|
}
|
|
}
|
|
});
|
|
|
|
//新增用户
|
|
AdminSchema.pre('save', function (next) {
|
|
let user = this;
|
|
if (this.isNew) {
|
|
this.meta.createAt = this.meta.updateAt = Date.now()
|
|
} else {
|
|
this.meta.updateAt = Date.now()
|
|
}
|
|
bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt) => {
|
|
if (err) {
|
|
return next(err)
|
|
} else {
|
|
//密码加盐
|
|
bcrypt.hash(user.password, salt, null, (err, hash) => {
|
|
if (err) {
|
|
return next(err)
|
|
} else {
|
|
user.password = hash;
|
|
next()
|
|
}
|
|
})
|
|
}
|
|
})
|
|
});
|
|
|
|
//匹配密码
|
|
AdminSchema.methods = {
|
|
comparePassword(_password, cb) {
|
|
bcrypt.compare(_password, this.password, (err, isMatch) => {
|
|
if (err) {
|
|
console.log(err);
|
|
return cb(err)
|
|
} else {
|
|
cb(null, isMatch)
|
|
}
|
|
})
|
|
}
|
|
};
|
|
module.exports = AdminSchema; |