auth.js 879 Bytes
/**
 * 登录判断
 * @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);
};