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

'use strict';


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

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

/**
 * 单个商品详情
 */
module.exports.showMain = (req, res, next) => {
    // TODO: vipLevel = 0; // 用户等级

    let pid = 204503;

    let uid = req.user.uid || '';

    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: uid,
        vipLevel: 0
    }).then((result) => {
        res.render('product/detail', Object.assign({
            module: 'product',
            page: 'detail'
        }, result));
    }).catch(next);
};

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

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

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

    let page = req.query.page || 1;

    let size = req.query.size || 10;

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

/**
 * 获得咨询列表
 * json
 */
module.exports.indexConsult = (req, res, next) => {
    let uid = req.user.uid || '';

    let pid = req.query.productId || 0;

    let page = req.query.page || 1;

    let size = req.query.size || 10;

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

/**
 * 新建咨询
 * json
 */
module.exports.createConsult = (req, res, next) => {
    let uid = req.user.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: ''
        });
    }

};