detail.js 7.71 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(`${mRoot}/detail-helper`);

const moment = require('moment');
const DEFAULT_AVATAR_ICO = 'http://static.yohobuy.com/images/v3/boy.jpg';
const SEO_SLOGAN = 'YOHO!BUY 有货';
const _ = require('lodash');

const CHANNEL = {
    boys: 'boys',
    girls: 'girls',
    kids: 'kids'
};

const _getChannel = (req, res) => {
    let channel = req.cookies._Channel;

    if (!channel) {
        res.cookie('_Channel', CHANNEL.boys, {
            domain: '.yohobuy.com',
            maxAge: moment.duration(300, 'days').seconds()
        });

        channel = 'boys'; // 设置默认值
    }

    return channel;
};

const _getGender = (channel) => {
    switch (channel) {
        case 'boys':
            return '1,3';
        case 'girls':
            return '2,3';
        default:
            return '1,2,3';
    }
};

/**
 * 首屏渲染商品详情
 */
const showMain = (req, res, next) => {
    let pid = req.params[0];
    let channel = _getChannel(req, res);
    let gender = _getGender(channel);

    const nullUserInfo = {
        uid: null,
        isStudent: null,
        vipLevel: null,
        saveInCookies: null
    };

    return service.showMainAsync(Object.assign({
        pid: pid,
        channel: channel,
        gender: gender
    }, nullUserInfo)).then((result)=> {
        return res.render('product/detail', Object.assign({
            module: 'product',
            page: 'detail',
            title_more: true,
            title: _.get(result, 'seo.title', '') + ' | ' + SEO_SLOGAN,
            keywords: _.get(result, 'seo.keywords', '').replace(/~+/, ''),
            description_more: true,
            description: result.description
        }, result));
    }).catch(next);

};

/**
 * 商品价格相关的数据
 */
const detailHeader = (req, res, next) => {
    let pid = req.query.productId || 0;
    let uid = req.user.uid || 0;
    let vipLevel = detailHelper.vipLevel(req.user.vip);
    let dataMd5 = req.query.md5 || 0;

    let saveCurrentGoodsInCookies = _.partial(service.saveRecentGoodInCookies,
        req.cookies._browseskn,
        res
    );

    return service.getDetailHeader(pid, uid, req.user.isStudent, vipLevel, dataMd5, saveCurrentGoodsInCookies)
        .then((result) => {
            if (result.code === 200) {
                return res.render('product/detail-header', Object.assign({layout: false}, result.data));
            } else {
                return res.status(204).end();
            }
        }).catch(next);
};

/**
 * 特殊商品退换货
 */
const detailReturn = (req, res, next) => {
    let skn = req.query.skn || 0;

    if (!skn) {
        return {
            code: 400,
            message: '商品数据出错'
        };
    }

    return service.saleReturn(skn).then(result => {
        return res.json({
            code: 200,
            data: {
                result: result
            }
        });
    }).catch(next);
};

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

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

/**
 * 获得评论列表
 */

const indexComment = (req, res, next) => {
    let pid = req.query.productId || 0;
    let page = req.query.page || 1;
    let size = req.query.size || 10;
    let filterId = req.query.filterId || 7; // 7全部,6有图

    return service.getShareOrderListAsync(pid, page, size, filterId).then((result) => {
        let pageResponse = _.get(result, 'data.pageResponse', {});

        return res.json({
            code: result.code,
            filter: _.get(result, 'data.fileter', []),
            data: _.get(pageResponse, 'list', []).map((item)=> {

                return {
                    avatar: _.get(item, 'userInfo.headIco', '') ?
                        helpers.image(item.userInfo.headIco, 30, 30) :
                        DEFAULT_AVATAR_ICO,
                    userName: _.get(item, 'userInfo.nickName', ''),
                    date: moment(item.createTime, 'X').format('YYYY-MM-DD HH:mm:ss'),
                    color: _.get(item, 'goods.color_name', ''),
                    size: _.get(item, 'goods.size_name', ''),
                    comment: item.content,
                    total: pageResponse.totalCount,
                    url: _.get(item, 'url', ''),
                    star: _.get(item, 'satisfied', 1),
                    comfort: _.get(item, 'size', 'MIDDLE'),
                    height: _.get(item, 'height', ''),
                    weight: _.get(item, 'weight', '')
                };
            })
        });
    }).catch(next);
};

/**
 * 获得咨询列表
 */
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;

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

/**
 * 新建咨询
 */
const createConsult = (req, res, next) => {
    let uid = req.user.uid || '';
    let pid = req.body.productId || 0;
    let content = req.body.content;

    if (content && uid) {
        return 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: '请输入咨询内容'
        });
    }
};

const likeConsult = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.params.id || 0;

    if (!id) {
        return res.json({
            code: 401,
            message: '咨询无效'
        });
    }

    service.likeAsync(uid, id).then((result) => {
        return res.json(result);
    }).catch(next);
};

const usefulConsult = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.params.id || 0;

    if (!id) {
        return res.json({
            code: 401,
            message: '咨询无效'
        });
    }

    service.usefulAsync(uid, id).then((result) => {
        return res.json(result);
    }).catch(next);
};

const recommend = (req, res, next) => {
    let skn = req.query.skn || '';
    let pageSize = req.query.size || 5;
    let pageNum = req.query.num || 1;

    if (!skn) {
        return res.json({
            code: 400,
            message: '服务器错误'
        });
    }

    service.recommendAsync(skn, pageNum, pageSize)
        .then(result => res.json(result))
        .catch(next);
};

/**
 *  获取套餐
 */
const getPackage = (req, res, next) => {
    let skn = req.query.skn || '';

    if (!skn) {
        return res.json({
            code: 400,
            message: '服务器错误'
        });
    }
    service.getPackage(skn)
        .then(result => res.json(result))
        .catch(next);
};

const notifyStatus = (req, res, next) => {

};

const notify = (req, res, next) => {

};

module.exports = {
    showMain,
    indexHotArea,
    indexComment,
    indexConsult,
    createConsult,
    productHeader: detailHeader,
    detailReturn,
    recommend,
    getPackage,
    likeConsult,
    usefulConsult,
    notifyStatus,
    notify
};