detail.js 2.84 KB
/**
 * 商品详情-controller
 * @author: Tao.Huang
 * @date: 2016/6/5
 */

'use strict';

const moment = require('moment');

const helpers = require(`${library}/helpers`);

const mRoot = '../models';
const service = require(`${mRoot}/detail-service`);
const detailHelper = require(`${mRoot}/detail-helper`);

/**
 * 单个商品详情
 */
module.exports.showMain = (req, res, next) => {
    // TODO: 需要修改为正式取 UID 的方式
    // TODO: vipLevel = 0; // 用户等级

    let pid = 204503;

    let channel = detailHelper.COOKIE_NAME_BOYS;

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

    let gender = detailHelper.getGenderByCookie(req);

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

/**
 * 获取热区图
 */
module.exports.indexHotArea = (req, res, next) => {
    let pid = req.params.productId || 0;

    service.indexHotAreaAsync(pid).then(result => {
        res.render('hotarea', {
            hotArea: result,
            layout:false
        });
    }).catch(next);
};

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

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

/**
 * 获得咨询列表
 * json
 */
module.exports.indexConsult = (req, res, next) => {
    // TODO: uid
    let uid = '';
    let pid = req.params.productId || 0;
    let page = req.params.page || 1;
    let size = req.params.size || 10;

    service.indexConsultAsync(uid, pid, page, size).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 新建咨询
 * json
 */
module.exports.createConsult = (req, res, next) => {

    //TODO: uid
    let uid = '';
    let pid = req.body.productId || 0;
    let content = req.body.content;

    if (content && uid) {
        service.createConsultAsync(uid, pid, content).then(result => {
            res.json(result);
        }).catch(next);

    } else if (!content) {
        res.json({
            code: 400,
            message: '请输入咨询内容'
        });

    } else if (!uid) {
        res.json({
            code: 403,
            message: "用户没有登录",
            data: {
                url: helpers.urlFormat('/signin.html')
            }
        });

    } else {
        res.json({
            code: 400,
            message: '请输入咨询内容',
            data: ''
        });
    }

};