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

'use strict';

const mRoot = '../models';

// 商品详情model
const detail = 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;
}

module.exports.show= (req, res, next) => {
    // TODO: 需要修改为正式取 UID 的方式
    // TODO: 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);

    detail.showAsync({
        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);
};

/**
 * 获得评论
 * @param req
 * @param res
 * @param next
 */
module.exports.comments = (req, res, next) => {
    let pid = req.params.productId || 0;
    let page = req.params.page || 1;
    let size = req.params.size || 10;

    detail.getCommentsAsync(pid, page, size).then(result => {
        res.json(result)
    }).catch(next);
}