yohoAuth.js 1.38 KB
/**
 * YOHO授权登录
 * @author: lq
 * @date: 2017/9/1
 */
'use strict';
const _ = require('lodash');
const moment = require('moment');
const authYoho = require('../../utils/authYoho');
const activityModel = require('../models/activity');
const queryString = require('queryString');
const isProd = process.env.NODE_ENV === 'production';

module.exports = (req, res, next) => {
    const actId = req.query.actId;

    req.ctx(activityModel).authType(actId)
        .then(type => {
            if (type === 2) {
                const refer = req.get('Referer') || '';
                const yhAuthId = _.get(req.session, 'yh_auth_id', 0);

                if (!yhAuthId) {
                    let params = {
                        yh_type: 'activity',
                        yh_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                        yh_backurl: `${isProd ? 'https' : 'http'}://action.yoho.cn/passport/yohoAuth`
                    };

                    params.yh_sign = authYoho.sign(params);
                    _.set(req.session, 'auth_refer', refer);
                    return res.json({
                        code: 401,
                        message: '抱歉,您暂未登录!',
                        redirect: `//m.yohobuy.com/signin.html?${queryString.stringify(params)}`
                    });
                }
            }

            next();
        });
};