comment.js 3.24 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');

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

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


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

exports.getCommentList = (uid, isComment, page, limit) => {
    limit = limit || 10;

    const process = function*() {

        let result = yield api.post('', {
            method: 'web.show.queryOrderProductCommentList',
            uid: uid
        });

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

        // 接口返回成功, 处理数据
        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 cnAlphabet = v.cnAlphabet || '';
                    let good = {
                        href: helpers.getUrlBySkc(v.productId, v.goodsId, cnAlphabet),
                        thumb: imgUtils.getImageUrl(v.imageUrl, 100, 100),
                        name: v.productName || '',
                        productSkn: v.productSkn,
                        productId: v.productId,
                        goodsId: v.goodsId,
                        erpSkuId: v.erpSkuId
                    };

                    if (isComment === v.hasOwnProperty('comment')) {
                        if (isComment) {
                            good.remark = v.comment;
                        }
                        order.goods.push(good);
                        commentList.goodsNum++;
                    }
                });

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


            let total = commentList.orders.length;

            // let totalPage = Math.ceil(total / limit);
            let begin = (page - 1) * limit;

            commentList.orders = commentList.orders.slice(begin, begin + limit); // [begin, begin+limit)

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

        return {
            comment: commentList,
            pager: pagerObj
        };
    };

    return co(process)();
};


exports.saveShareOrder = data => {
    let process = function*() {
        let res = api.post('', {
            method: 'show.saveShareOrder',
            parameters: JSON.stringify(data)
        });

        return res;
    };

    return co(process)();
};