comment.js 5.86 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,
                        orderId: value.orderId,
                        orderCode: value.orderCode
                    };

                    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.getCommentList4Order = (uid, orderId) => {

    // limit = limit || 10;
    // http://api.yoho.cn/?app_version=5.0.0&client_secret=e8098ddd3129372e8c93daea6600eda4&client_type=android&gender=1%2C3&
        // method=show.toShareOrderList&
        // orderCode=1617595089&os_version=android6.0.1%3AMI_MAX&screen_size=1080x1920&session_key=
        // 720e95e42f9c1edc5940f1d49c34b68e&uid=9963947&v=7&yh_channel=1

    const process = function*() {

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

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

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

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

            _.forEach(result.data, (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,
                    orderCode: v.orderCode,
                    orderId: v.orderId
                };

                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 = NO_UNCOMMENT_GOODS; // 空数据 提示文字
        }
        console.log(JSON.stringify(commentList.orders.length));
        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)();
};