comment.js 9.54 KB
/**
 *  comment model
 *  @author 陈轩 <xuan.chen@yoho.cn>
 */

'use strict';

const path = require('path');
const imgUtils = require(path.join(global.utils, 'images'));
const Promise = require('bluebird');
const _ = require('lodash');
const moment = require('moment');

// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;

const co = Promise.coroutine;
const helpers = global.yoho.helpers;


const NO_COMMENTED_GOODS = '您还没有已评论的商品';
const NO_UNCOMMENT_GOODS = '您还没有未评论的商品';

const sizeLabelMap = {
    SMALL: '偏小',
    MIDDLE: '合身',
    BIG: '偏大'
};

function getCommentList(uid, isComment, page, limit) {
    limit = limit || 10;

    let that = this;
    const process = function*() {
        let resData = {};

        // 临时解决个人中心-评论-用户头像缺失问题,待个人中心整体重构调整头像获取机制
        let proInfo = yield Promise.all([
            that.post({
                data: {
                    method: 'web.show.queryOrderProductCommentList',
                    uid: uid
                }
            }),
            that.get({
                data: {
                    method: 'app.passport.profile',
                    uid: uid
                }
            })
        ]);

        let result = proInfo[0];

        let headIco = _.get(proInfo, '[1].data.head_ico', '');

        if (headIco) {
            resData.userThumb = helpers.image(headIco, 100, 100);
        }

        let commentList = {
            isComment: isComment,
            goodsNum: 0,
            orders: []
        };
        let pagerObj = {};
        let begin = (page - 1) * limit;
        let end = page * limit;
        let total = 0;

        // 接口返回成功, 处理数据
        if (result.code === 200 && !_.isEmpty(result.data)) {
            _.forEach(result.data, (value) => {

                let order = {
                    orderNum: value.orderCode,
                    orderTime: value.createTime,
                    orderId: value.orderId,
                    goods: []
                };

                _.forEach(value.orderGoods, (v) => {
                    let hasComments = false;
                    let good = {
                        href: helpers.getUrlBySkc(v.productSkn),
                        thumb: helpers.image(imgUtils.getImageUrl(v.imageUrl, 100, 100), 100, 100),
                        name: v.productName || '',
                        productSkn: v.productSkn,
                        productId: v.productId,
                        goodsId: v.goodsId,
                        erpSkuId: v.erpSkuId,
                        orderId: value.orderId,
                        orderCode: value.orderCode,
                        orderTime: value.createTime ? value.createTime.substr(0, 10) : ''
                    };

                    if (v.commentDetailBoDetail && v.commentDetailBoDetail.length > 0) {

                        good.comments = v.commentDetailBoDetail.map((cm) => {
                            return {
                                content: cm.content,
                                createTime: moment(cm.createTime * 1000).format('YYYY-MM-DD HH:mm:ss'),
                                satisfied: cm.satisfied,
                                size: cm.size,
                                sizeLabel: sizeLabelMap[cm.size],
                                url: helpers.image(cm.url, 100, 100),
                                sourceUrl: helpers.image(cm.url, 400, 400),
                                height: cm.height,
                                weight: cm.weight,
                                reply: cm.reply,
                                replyTitle: cm.replyTitle
                            };
                        });

                        hasComments = true;
                    }

                    if (isComment === hasComments) {
                        /* if (isComment) {
                            good.remark = v.comment;
                        }*/

                        if (commentList.goodsNum >= begin && commentList.goodsNum < end) {
                            order.goods.push(good);
                        }
                        commentList.goodsNum++;
                    }
                });

                order.goods.length && commentList.orders.push(order);
            });


            total = commentList.goodsNum;// commentList.orders.length;

            // let totalPage = Math.ceil(total / limit);
            // commentList.orders = commentList.orders.slice(begin, begin + limit); // [begin, begin+limit)

            pagerObj = pager(total, {
                isComment: isComment ? 'Y' : 'N',
                page: page,
                limit: limit
            });
        } else {
            commentList.empty = isComment ? NO_COMMENTED_GOODS : NO_UNCOMMENT_GOODS; // 空数据 提示文字
        }

        return Object.assign(resData, {
            comment: commentList,
            pager: pagerObj
        });
    };

    return co(process)();
}

function getCommentList4Order(uid, orderCode) {
    let that = this;

    const process = function*() {

        let result = yield that.post({
            data: {
                // method: 'show.toShareOrderList',
                method: 'web.show.queryCommentListByOrderCode',
                uid: uid,
                orderCode: orderCode
            }
        });

        let commentList = {
            goodsNum: 0,
            orders: []
        };

        // 接口返回成功, 处理数据
        if (result.code === 200 && !_.isEmpty(result.data)) {

            /* let order = {
                // orderNum: value.orderCode,
                // orderTime: value.createTime,
                orderNum: orderId,
                goods: []
            };

            _.forEach(result.data, (v) => {

                let cnAlphabet = v.cnAlphabet || '';
                let good = {
                    href: helpers.getUrlBySkc(v.productId, v.goodsId, cnAlphabet),
                    thumb: helpers.image(imgUtils.getImageUrl(v.imageUrl, 100, 100), 100, 100),
                    name: v.productName || '',
                    productSkn: v.productSkn,
                    productId: v.productId,
                    goodsId: v.goodsId,
                    erpSkuId: v.erpSkuId,
                    orderCode: v.orderCode,
                    orderId: v.orderId,
                    orderTime: _.get(v, 'orderCreateTime') ?
                              moment(_.get(v, 'orderCreateTime') * 1000).format('YYYY-MM-DD') : ''
                };

                order.goods.push(good);
                commentList.goodsNum++;
            });

            order.goods.length && commentList.orders.push(order);*/

            _.forEach(result.data, (value) => {

                let order = {
                    orderNum: value.orderCode,
                    orderTime: value.createTime,
                    orderId: value.orderId,
                    goods: []
                };

                _.forEach(value.orderGoods, (v) => {
                    // let cnAlphabet = v.cnAlphabet || '';
                    let good = {
                        href: helpers.getUrlBySkc(v.productSkn),
                        thumb: helpers.image(imgUtils.getImageUrl(v.imageUrl, 100, 100), 100, 100),
                        name: v.productName || '',
                        productSkn: v.productSkn,
                        productId: v.productId,
                        goodsId: v.goodsId,
                        erpSkuId: v.erpSkuId,
                        orderId: value.orderId,
                        orderCode: value.orderCode,
                        orderTime: value.createTime ? value.createTime.substr(0, 10) : ''
                    };

                    if (v.commentDetailBoDetail && v.commentDetailBoDetail.length > 0) {

                        good.comments = v.commentDetailBoDetail.map((cm) => {
                            return {
                                content: cm.content,
                                createTime: moment(cm.createTime * 1000).format('YYYY-MM-DD HH:mm:ss'),
                                satisfied: cm.satisfied,
                                size: cm.size,
                                sizeLabel: sizeLabelMap[cm.size],
                                url: helpers.image(cm.url, 100, 100),
                                sourceUrl: helpers.image(cm.url, 400, 400),
                                height: cm.height,
                                weight: cm.weight
                            };
                        });
                    }

                    order.goods.push(good);
                    commentList.goodsNum++;
                });

                order.goods.length && commentList.orders.push(order);
            });

        } else {
            commentList.empty = NO_UNCOMMENT_GOODS; // 空数据 提示文字
        }

        return {
            comment: commentList,
            isFromOrder: true
        };
    };

    return co(process)();
}

function saveShareOrder(data) {
    return this.post({
        data: {
            method: 'show.saveShareOrder',
            parameters: JSON.stringify(data)
        }
    });
}


module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.sizeLabelMap = sizeLabelMap;

        this.getCommentList = getCommentList.bind(this);
        this.getCommentList4Order = getCommentList4Order.bind(this);
        this.saveShareOrder = saveShareOrder.bind(this);
    }
};