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

'use strict';

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

/**
 * 单个商品详情
 */
const showMain = (req, res, next) => {
    let vipLevel = 0;
    let pid = req.params[0];
    let gid = req.params[1];
    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({
        pid: pid,
        gid: gid,
        channel: channel,
        gender: gender,
        uid: uid,
        vipLevel: vipLevel
    })
        .then(result => {
            res.render('product/detail', Object.assign({
                module: 'product',
                page: 'detail'
            }, result));
        })
        .catch(next);
};

/**
 * 获取热区图
 */
const 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
 */
const 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
 */
const 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
 */
const 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 => {
                console.log(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')
            }
        });

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

};

module.exports = {
    showMain,
    indexHotArea,
    indexComment,
    indexConsult,
    createConsult
};