2017-07-26 22:03:18 +08:00
|
|
|
import baseModel from './base.js';
|
2017-07-05 17:59:53 +08:00
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
class userModel extends baseModel {
|
|
|
|
getName() {
|
|
|
|
return 'user';
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-05 17:59:53 +08:00
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
getSchema() {
|
|
|
|
return {
|
|
|
|
username: {
|
2017-07-18 15:35:32 +08:00
|
|
|
type: String,
|
2017-07-26 22:03:18 +08:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
passsalt: String,
|
|
|
|
role: String,
|
|
|
|
add_time: Number,
|
2017-08-10 15:37:20 +08:00
|
|
|
up_time: Number,
|
|
|
|
type: {type: String, enum: ['site', 'third'], default: "site"} //site用户是网站注册用户, third是第三方登录过来的用户
|
2017-07-26 22:03:18 +08:00
|
|
|
};
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
save(data) {
|
2017-07-06 18:25:53 +08:00
|
|
|
let user = new this.model(data);
|
2017-07-05 17:59:53 +08:00
|
|
|
return user.save();
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
checkRepeat(email) {
|
2017-07-06 18:25:53 +08:00
|
|
|
return this.model.count({
|
2017-07-11 12:12:43 +08:00
|
|
|
email: email
|
2017-07-26 22:03:18 +08:00
|
|
|
});
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
list() {
|
|
|
|
return this.model.find().select('_id username email role add_time up_time').exec(); //显示id name email role
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
findByUids(uids) {
|
2017-07-20 15:26:26 +08:00
|
|
|
return this.model.find({
|
2017-07-26 22:03:18 +08:00
|
|
|
_id: { $in: uids }
|
|
|
|
}).select('_id username email role add_time up_time').exec();
|
2017-07-20 15:26:26 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-18 16:37:28 +08:00
|
|
|
listWithPaging(page, limit) {
|
|
|
|
page = parseInt(page);
|
|
|
|
limit = parseInt(limit);
|
2017-07-26 22:03:18 +08:00
|
|
|
return this.model.find().sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).select('_id username email role add_time up_time').exec();
|
2017-07-18 16:37:28 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-18 16:37:28 +08:00
|
|
|
listCount() {
|
|
|
|
return this.model.count();
|
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
findByEmail(email) {
|
|
|
|
return this.model.findOne({ email: email });
|
2017-07-07 12:04:14 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
findById(id) {
|
2017-08-08 14:45:19 +08:00
|
|
|
return this.model.findOne({
|
2017-07-06 18:25:53 +08:00
|
|
|
_id: id
|
2017-07-26 22:03:18 +08:00
|
|
|
});
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
del(id) {
|
2017-07-06 18:25:53 +08:00
|
|
|
return this.model.deleteOne({
|
2017-07-26 22:03:18 +08:00
|
|
|
_id: id
|
|
|
|
});
|
2017-07-06 18:25:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
|
|
update(id, data) {
|
2017-07-06 18:25:53 +08:00
|
|
|
return this.model.update({
|
2017-07-11 10:17:44 +08:00
|
|
|
_id: id
|
2017-07-26 22:03:18 +08:00
|
|
|
}, data);
|
2017-07-05 17:59:53 +08:00
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-14 16:41:14 +08:00
|
|
|
search(keyword) {
|
|
|
|
return this.model.find({
|
|
|
|
$or: [
|
|
|
|
{ email: new RegExp(keyword, 'i') },
|
2017-07-26 22:03:18 +08:00
|
|
|
{ username: new RegExp(keyword, 'i') }
|
2017-07-14 16:41:14 +08:00
|
|
|
]
|
2017-07-18 15:31:41 +08:00
|
|
|
}, {
|
|
|
|
passsalt: 0,
|
|
|
|
password: 0
|
2017-07-26 22:03:18 +08:00
|
|
|
}).limit(10);
|
2017-07-14 16:41:14 +08:00
|
|
|
}
|
2017-07-05 17:59:53 +08:00
|
|
|
}
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
module.exports = userModel;
|