auth.js
879 Bytes
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
/**
* 登录判断
* @author: leo<qi.li@yoho.cn>
* @date: 2017/6/23
*/
'use strict';
const _ = require('lodash');
const UserModel = require('../models/user');
module.exports = (req, res, next) => {
const userId = _.get(req.session, 'user.id', 0);
const userPhone = _.get(req.session, 'user.phone');
if (!userId) {
if (req.xhr) {
return res.json({
code: 403,
message: '抱歉,您暂未验证'
});
} else {
return res.render('error/403');
}
}
req.ctx(UserModel).isUserExisted(userPhone)
.then(result => {
if (!result.length) {
return res.json({
code: 403,
message: '抱歉,您暂未验证'
});
}
next();
})
.catch(next);
};