Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 3x 3x 3x 3x 3x 3x 3x 11x 11x 11x 3x 3x 3x 3x 11x 11x 11x 11x 3x 11x 11x 11x 11x 3x 30x 30x 3x 3x 3x 1x 1x 3x 3x 11x 14x 14x 14x 3x 3x | 'use strict'
const mongoose = require('mongoose')
const crypto = require('crypto')
const paginate = require('../../libs/plugins/paginate')
const Schema = mongoose.Schema
const ObjectId = mongoose.Schema.Types.ObjectId
const schema = Schema({
email: {
type: String,
lowercase: true,
unique: true,
},
username: {
type: String,
unique: true,
},
hashedPassword: String,
salt: String,
role: { // 角色,跟config中对应上;
type: String,
default: 'user',
},
partner: { // 经销商合作伙伴
type: ObjectId,
ref: 'Partner',
default: null,
},
avatar: String,
status: { // 用户状态;//0、注册(待激活);1、正常使用;2、停用;3、(伪)删除
type: Number,
default: 0,
},
phone: {
type: String,
// validate: {
// validator: function(v) {
// return /(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}/.test(v);///^1[0-9]{10}$/
// },
// message: '{VALUE} is not a valid phone number!'
// },
// required: [true, 'User phone number required']
},
address: String,
gender: String,
provider: {
type: String,
default: 'local',
},
qq: {
id: String,
token: String,
email: String,
name: String,
},
wechat: {
id: String,
token: String,
email: String,
name: String,
},
}, {
timestamps: {},
toObject: { virtuals: true },
id: false,
})
schema
.virtual('password')
.set(function (password) {
this._password = password
this.salt = this.makeSalt()
this.hashedPassword = this.encryptPassword(password)
})
.get(function () {
return this._password
})
schema
.virtual('userInfo')
.get(function () {
return {
email: this.email,
username: this.username,
role: this.role,
provider: this.provider,
status: this.status,
avatar: this.avatar,
partner: this.partner,
}
})
schema
.virtual('providerInfo')
.get(function () {
return {
qq: this.qq,
wechat: this.wechat,
}
})
schema
.virtual('token')
.get(function () {
return {
_id: this._id,
role: this.role,
}
})
schema
.path('email')
.validate({
isAsync: true,
validator: function (v, cb) {
const self = this
// let msg = v + ' is not a valid email!';
// let regex = /\d{3}-\d{3}-\d{4}/;
self.constructor.findOne({ email: v }, function (err, user) {
Iif (user && self.id !== user.id) {
// cb(regex.test(v), msg)
cb(false)
}
cb(true)
})
},
message: '这个email已经被使用!',
})
schema
.path('username')
.validate({
isAsync: true,
validator: function (v, cb) {
const self = this
self.constructor.findOne({ username: v }, function (err, user) {
Iif (user && self.id !== user.id) {
cb(false)
}
cb(true)
})
},
message: '这个用户名已经被使用!',
})
const autoPopulate = function (next) {
this.populate([{
path: 'partner',
select: 'name',
}])
next()
};
schema.pre('find', autoPopulate);
schema.pre('findOne', autoPopulate);
// schema.post('find', async function(docs) {
// for (let doc of docs) {
// if (doc.role === config.userRoles[1]) {
// await doc.populate({path: 'partner', select:'name status'}).execPopulate();
// }
// }
// });
schema.post('save', function (error, doc, next) {
Eif (error.name === 'MongoError' && error.code === 11000) {
next(new Error('这个邮箱或用户名已经被使用!'));
} else {
next(error);
}
});
schema.methods = {
hasRole: function (role) {
var selfRoles = this.role
return (selfRoles.indexOf('admin') !== -1 || selfRoles.indexOf(role) !== -1)
},
authenticate: function (plainText) {
return this.encryptPassword(plainText) === this.hashedPassword
},
makeSalt: function () {
return crypto.randomBytes(16).toString('base64')
},
encryptPassword: function (password) {
Iif (!password || !this.salt) { return '' }
var salt = new Buffer(this.salt, 'base64')
return crypto.pbkdf2Sync(password, salt, 10000, 64, 'sha1').toString('base64')
},
}
schema.plugin(paginate);
module.exports = mongoose.model('User', schema) |