-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuser_controller.js
238 lines (217 loc) · 5.68 KB
/
user_controller.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var express = require('express');
var router = express.Router();
var async = require('async');
var redis = require('redis');
var moment = require('moment');
var Redis = require('../library/redis');
var UserService = require('../service/user_service');
var UserModel = require('../model/user_model');
var Msg91u = require('../library/msg91u');
// 登陆
router.post('/login2', checkLogin);
router.post('/login2', function (req, res) {
async.waterfall([
function (cb) { // 检查员工号+密码
UserModel
.find({
where: {
worker_num: req.body.name
}
})
.then(function (user) {
cb(null, user);
});
},
function (user, cb) { // 检查手机号+密码
if (user) { // 已经找到,则直接跳过手机号查找
return cb(null, user);
}
UserModel
.find({
where: {
mobile: req.body.name
}
})
.then(function (user) {
cb(null, user);
});
},
], function (err, user) {
if (user === null) {
res.status(403);
res.json({msg: '用户不存在'});
return;
}
if (!user.isPasswordValid(req.body.password)) {
res.status(403);
res.json({msg: '密码错误'});
return;
}
UserService.setSession(user, req);
res.json({id: user.id, sid: req.session.id});
});
});
// 登出
router.post('/logout', UserService.checkSession);
router.post('/logout', function (req, res) {
UserService.destroySession(req);
res.json({msg: '登出成功'});
});
// 列表
router.get('/list', function (req, res, next) {
UserModel
.findAll({
offset: req.query.offset || 0,
limit: req.query.size || 10
})
.then(function (users) {
res.json(users);
});
});
// 详情
router.get('/show/:id', UserService.checkSession);
router.get('/show/:id', checkUserId);
router.get('/show/:id', function (req, res, next) {
res.json(req.user);
});
// 修改
router.put('/modi/:id', UserService.checkSession);
router.put('/modi/:id', checkUserId);
router.put('/modi/:id', function (req, res) {
req.user
.update(req.body)
.then(function (user) {
res.json(user);
});
});
// 用户删除
router.delete('/del/:id', UserService.checkSession);
router.delete('/del/:id', checkUserId);
router.delete('/del/:id', function (req, res) {
req.user.destroy();
res.json({msg: '操作成功'});
});
// 用户注册 步骤1
router.post('/reg_step1', checkReg);
router.post('/reg_step1', checkWorkerNum);
router.post('/reg_step1', function (req, res) {
// 发送91u信息
var msg91u = new Msg91u(req.body.worker_num);
var number = moment().format('x').substr(-6);
var msg = '您正在注册看板工具,校验码:' + number;
msg91u.send(msg);
// 将key保存
var redisClient = Redis.create();
var key = getKey(req.body.worker_num);
redisClient.hset(key, 'is_used', 0);
redisClient.hset(key, 'worker_num', req.body.worker_num);
redisClient.hset(key, 'password', req.body.password);
redisClient.hset(key, 'num', number);
redisClient.expire(key, 60);
res.json({msg: '请在91u中查收验证码'});
});
// 用户注册 步骤2
router.post('/reg_step2', checkKey);
router.post('/reg_step2', function (req, res) {
var salt = UserModel.generateSalt();
var password = UserModel.generatePassword(salt, req.password);
UserModel
.create({
worker_num: req.body.worker_num,
password: password,
salt: salt,
create_time: moment().unix()
})
.then(function (user) {
if (user) {
res.json({id: user.id});
} else {
res.status(500);
res.json({msg: '注册失败'});
}
});
});
function checkUserId(req, res, next) {
UserModel
.findById(req.params.id)
.then(function (user) {
if (user === null) {
res.status(400);
res.json({msg: '用户不存在'});
} else {
req.user = user;
next();
}
});
}
function checkWorkerNum(req, res, next) {
UserModel
.find({
where: {
worker_num: req.body.worker_num
}
})
.then(function (user) {
if (user) {
res.status(400);
res.json({msg: '工号已经注册'});
} else {
next();
}
});
}
function checkLogin(req, res, next) { // 检查登陆状况
// name
req.checkBody('name', '请提供用户名').notEmpty();
// password
req.checkBody('password', '请提供密码').notEmpty();
var errors = req.validationErrors(true);
if (errors) {
res.status(400);
res.json(errors);
} else {
next();
}
}
function checkReg(req, res, next) {
// name
req.checkBody('worker_num', '请提供91U员工号').notEmpty();
// password
req.checkBody('password', '请提供密码').notEmpty();
var errors = req.validationErrors(true);
if (errors) {
res.status(400);
res.json(errors);
} else {
next();
}
}
function checkKey(req, res, next) { // 校验验证码
var redisClient = Redis.create();
var key = getKey(req.body.worker_num);
redisClient.hgetall(key, function (err, obj) {
if (obj === null) {
res.status(403);
res.json({msg: '请核实验证码'});
return;
}
console.log('验证码' + req.body.number + '对应的值为' + JSON.stringify(obj));
if (parseInt(obj.is_used)) {
res.status(403);
res.json({msg: '验证码已使用'});
return;
}
if (parseInt(obj.num) !== parseInt(req.body.number)) {
res.status(403);
res.json({msg: '验证码错误'});
return;
}
req.password = obj.password;
redisClient.hset(key, 'is_used', 1);
next();
});
}
function getKey(number) { // redis中key
return 'kb_login_' + number;
}
module.exports = router;