Repository navigation
Expand file tree
/
Copy pathuser.js
More file actions
138 lines (130 loc) · 2.35 KB
/
Copy pathuser.js
File metadata and controls
138 lines (130 loc) · 2.35 KB
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
var mongoose = require('mongoose')
var bcrypt = require('bcrypt')
var SALT_WORK_FACTOR = 10
var UserSchema = new mongoose.Schema({
// 用户名
username: {
unique: true,
type: String
},
// 密码
password: String,
// 角色
// 0: nomal user
// 1: verified user
// 2: professonal user
// >10: admin
// >50: super admin
role: {
type: Number,
default: 0
},
phoneNumber: {
type: String,
unique: true
},
email: {
type: String,
unique: true
},
// 教育信息
eduInfo: {
// 学校
university: String,
// 学院
college: String,
// 专业
major: String,
// 学年
year: String,
// 校区
campus: String,
// 学号
card: String
},
// 微信相关的信息
weixin: {
openid: {
type: String,
unique: true
},
headimgurl: String,
nickname: String,
sex: String,
language: String,
city: String,
province: String,
country: String
},
// 身份相关信息
profile: {
// 身份证号
cardId: String,
// 名字
name: String,
// 性别
gender: String,
// 生日
birthday: String,
// 常住地
location: String
},
// 积分
score: { type: Number, default: 0 },
meta: {
createAt: {
type: Date,
default: Date.now()
},
updateAt: {
type: Date,
default: Date.now()
}
}
});
UserSchema.pre('save', function(next) {
var user = this;
if (this.isNew) {
this.meta.createAt = this.meta.updateAt = Date.now();
}
else {
this.meta.updateAt = Date.now();
}
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
});
UserSchema.methods = {
comparePassword: function(_password, cb) {
bcrypt.compare(_password, this.password, function(err, isMatch) {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
}
};
UserSchema.statics = {
fetch: function(cb) {
return this
.find({})
.sort('meta.updateAt')
.exec(cb);
},
findById: function(id, cb) {
return this
.findOne({_id: id})
.exec(cb);
}
};
var User = mongoose.model('User', UserSchema);
module.exports = User;