detail.js 4.57 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 Actions = require('./lib/actions');
const YohoAction = require('./lib/yoho-action');
const moment = require('moment');
const camelCase = global.yoho.camelCase;
const DEFAULT_AVATAR_ICO = 'http://static.yohobuy.com/images/v3/boy.jpg';

class DetailAction extends YohoAction {
    /**
     * 渲染商品详情
     */
    render() {
        const req = this.request;

        let pid = req.params[0];
        let gid = req.params[1];
        let uid = req.user.uid || '';

        return service.showMainAsync({
            pid: pid,
            gid: gid,
            channel: this.getSessionChannel(),
            gender: this.guessUserGender(),
            uid: uid
        }).then(result=> {
            const seo = result.seo;

            this.setTitle(seo.title, true, '|');
            this.setKeywords(seo.keywords);
            this.setDescription(seo.description, true);

            this.setEntry('product', 'detail');
            this.renderTemplate('product/detail', Object.assign({
                headerData: result.headerData
            }, result));
        }).catch(err => {
            if (err.code === 404) {
                return this.next();
            }

            return this.next(err);
        });
    }
}

/**
 * 获取热区图
 */
class HotAreaAction extends YohoAction {
    render() {
        let pid = this.request.query.productId || 0;

        return service.indexHotAreaAsync(pid).then(result => {
            this.renderTemplate('product/hotarea', {
                hotArea: result,
                layout: false
            });
        });
    }
}

/**
 * 获得评论列表
 */
class CommentAction extends YohoAction {
    render() {
        const req = this.request;
        let pid = req.query.productId || 0;
        let page = req.query.page || 1;
        let size = req.query.size || 10;

        return service.getShareOrderListAsync(pid, page, size).then(result => {
            this.response.json({
                code: result.code,
                data: camelCase(result.data.pageResponse.list).map((item)=> {

                    return {
                        avatar: item.userInfo.headIco ?
                            helpers.image(item.userInfo.headIco, 30, 30) :
                            DEFAULT_AVATAR_ICO,
                        userName: item.userInfo.nickName,
                        date: moment(item.createTime, 'X').format('YYYY-MM-DD HH:mm:ss'),
                        color: item.goods.colorName,
                        size: item.goods.sizeName,
                        comment: item.content,
                        total: result.data.pageResponse.totalCount
                    };
                })
            });
        });
    }
}

/**
 * 获得咨询列表
 */
class ConsultAction extends YohoAction {
    render() {
        const req = this.request;
        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 => {
            this.response.json({
                code: 200,
                data: result
            });
        });
    }
}

/**
 * 新建咨询
 */
class CreateConsultAction extends YohoAction {
    render() {
        const req = this.request, res = this.response;
        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);
            });
        } 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: '请输入咨询内容'
            });
        }
    }
}

module.exports = {
    showMain: Actions.createAction(DetailAction),
    indexHotArea: Actions.createAction(HotAreaAction),
    indexComment: Actions.createAction(CommentAction),
    indexConsult: Actions.createAction(ConsultAction),
    createConsult: Actions.createAction(CreateConsultAction)
};