detail.js 1.65 KB
/**
 * 商品详情controller
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/5/6
 */

'use strict';

const mRoot = '../models';

// 商品详情model
const detailHandle = require(`${mRoot}/detail-handle`);
const moment = require('moment');

/* COOKIE标识访问的是男生频道 */
const COOKIE_NAME_BOYS = 'boys';
const COOKIE_DOMAIN = '.yohobuy.com';

function setSwitchToCookie(res) {
    res.cookie('_Channel', COOKIE_NAME_BOYS, {
        domain: COOKIE_DOMAIN,
        maxAge: moment.duration(300, 'days').seconds()
    });
}

function getGenderByCookie(req) {
    let gender = null;

    let channel = req.cookies._Channel || 'boys';

    switch (channel) {
        case 'boys':
            {
                gender = '1,3';
                break;
            }
        case 'girls':
            {
                gender = '2,3';
                break;
            }
        default:
            {
                gender = '1,2,3';
            }
    }

    return gender;
}

exports.index = (req, res, next) => {
    // var uid = null, // 需要修改为正式取 UID 的方式
    //    vipLevel = 0; // 用户等级

    let pid = 204503;

    let channel = COOKIE_NAME_BOYS;

    if (req.cookies._Channel) {
        channel = req.cookies._Channel;
    } else {
        setSwitchToCookie(res);
    }

    let gender = getGenderByCookie(req);

    detailHandle({
        id: pid,
        ua: req.get('user-agent') || '',
        channel: channel,
        gender: gender,
        uid: 0,
        vipLevel: 0
    }).then((result) => {
        res.render('detail', Object.assign({
            module: 'product',
            page: 'detail'
        }, result));
    }).catch(next);
};