Blame view

apps/home/models/comment.js 9.06 KB
陈轩 authored
1 2 3 4 5 6 7 8 9 10 11
/**
 *  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');
刘传洋 authored
12
const moment = require('moment');
陈轩 authored
13 14 15 16 17

// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
陈轩 authored
18 19 20 21 22
const co = Promise.coroutine;
const api = global.yoho.API;
const helpers = global.yoho.helpers;

陈轩 authored
23 24 25
const NO_COMMENTED_GOODS = '您还没有已评论的商品';
const NO_UNCOMMENT_GOODS = '您还没有未评论的商品';
刘传洋 authored
26 27 28 29 30 31 32 33
const sizeLabelMap = {
    SMALL: '偏小',
    MIDDLE: '合身',
    BIG: '偏大'
};

exports.sizeLabelMap = sizeLabelMap;
陈轩 authored
34 35 36 37
exports.getCommentList = (uid, isComment, page, limit) => {
    limit = limit || 10;

    const process = function*() {
yyq authored
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        let resData = {};

        // 临时解决个人中心-评论-用户头像缺失问题,待个人中心整体重构调整头像获取机制
        let proInfo = yield Promise.all([
            api.post('', {
                method: 'web.show.queryOrderProductCommentList',
                uid: uid
            }),
            api.get('', {
                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);
        }
陈轩 authored
59 60

        let commentList = {
yyq authored
61
            isComment: isComment,
陈轩 authored
62
            goodsNum: 0,
陈轩 authored
63 64
            orders: []
        };
陈轩 authored
65
        let pagerObj = {};
刘传洋 authored
66 67 68
        let begin = (page - 1) * limit;
        let end = page * limit;
        let total = 0;
陈轩 authored
69 70 71 72 73 74 75 76 77 78 79 80

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

                let order = {
                    orderNum: value.orderCode,
                    orderTime: value.createTime,
                    orderId: value.orderId,
                    goods: []
                };
陈轩 authored
81
                _.forEach(value.orderGoods, (v) => {
刘传洋 authored
82
                    let hasComments = false;
陈轩 authored
83
                    let good = {
m  
OF1706 authored
84
                        href: helpers.getUrlBySkc(v.productSkn),
刘传洋 authored
85
                        thumb: helpers.image(imgUtils.getImageUrl(v.imageUrl, 100, 100), 100, 100),
陈轩 authored
86 87 88 89
                        name: v.productName || '',
                        productSkn: v.productSkn,
                        productId: v.productId,
                        goodsId: v.goodsId,
刘传洋 authored
90 91
                        erpSkuId: v.erpSkuId,
                        orderId: value.orderId,
刘传洋 authored
92
                        orderCode: value.orderCode,
m  
刘传洋 authored
93
                        orderTime: value.createTime ? value.createTime.substr(0, 10) : ''
陈轩 authored
94
                    };
陈轩 authored
95
刘传洋 authored
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
                    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
                            };
                        });

                        hasComments = true;
                    }

                    if (isComment === hasComments) {
                        /* if (isComment) {
yyq authored
117
                            good.remark = v.comment;
刘传洋 authored
118 119 120 121
                        }*/

                        if (commentList.goodsNum >= begin && commentList.goodsNum < end) {
                            order.goods.push(good);
yyq authored
122 123
                        }
                        commentList.goodsNum++;
陈轩 authored
124 125
                    }
                });
陈轩 authored
126 127

                order.goods.length && commentList.orders.push(order);
陈轩 authored
128 129 130
            });

刘传洋 authored
131
            total = commentList.goodsNum;// commentList.orders.length;
陈轩 authored
132 133

            // let totalPage = Math.ceil(total / limit);
刘传洋 authored
134
            // commentList.orders = commentList.orders.slice(begin, begin + limit); // [begin, begin+limit)
陈轩 authored
135 136

            pagerObj = pager(total, {
yyq authored
137
                isComment: isComment ? 'Y' : 'N',
刘传洋 authored
138 139
                page: page,
                limit: limit
陈轩 authored
140
            });
陈轩 authored
141
        } else {
陈轩 authored
142
            commentList.empty = isComment ? NO_COMMENTED_GOODS : NO_UNCOMMENT_GOODS; // 空数据 提示文字
陈轩 authored
143 144
        }
yyq authored
145
        return Object.assign(resData, {
陈轩 authored
146
            comment: commentList,
陈轩 authored
147
            pager: pagerObj
yyq authored
148
        });
陈轩 authored
149 150 151 152
    };

    return co(process)();
};
陈轩 authored
153
刘传洋 authored
154
exports.getCommentList4Order = (uid, orderCode) => {
刘传洋 authored
155 156 157 158

    const process = function*() {

        let result = yield api.post('', {
刘传洋 authored
159 160
            // method: 'show.toShareOrderList',
            method: 'web.show.queryCommentListByOrderCode',
刘传洋 authored
161
            uid: uid,
刘传洋 authored
162
            orderCode: orderCode
刘传洋 authored
163 164 165 166 167 168
        });

        let commentList = {
            goodsNum: 0,
            orders: []
        };
刘传洋 authored
169
刘传洋 authored
170 171 172
        // 接口返回成功, 处理数据
        if (result.code === 200 && !_.isEmpty(result.data)) {
刘传洋 authored
173
            /* let order = {
刘传洋 authored
174 175
                // orderNum: value.orderCode,
                // orderTime: value.createTime,
刘传洋 authored
176
                orderNum: orderId,
刘传洋 authored
177 178 179 180 181 182 183 184
                goods: []
            };

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

                let cnAlphabet = v.cnAlphabet || '';
                let good = {
                    href: helpers.getUrlBySkc(v.productId, v.goodsId, cnAlphabet),
刘传洋 authored
185
                    thumb: helpers.image(imgUtils.getImageUrl(v.imageUrl, 100, 100), 100, 100),
刘传洋 authored
186 187 188 189 190 191
                    name: v.productName || '',
                    productSkn: v.productSkn,
                    productId: v.productId,
                    goodsId: v.goodsId,
                    erpSkuId: v.erpSkuId,
                    orderCode: v.orderCode,
刘传洋 authored
192 193
                    orderId: v.orderId,
                    orderTime: _.get(v, 'orderCreateTime') ?
m  
刘传洋 authored
194
                              moment(_.get(v, 'orderCreateTime') * 1000).format('YYYY-MM-DD') : ''
刘传洋 authored
195 196
                };
刘传洋 authored
197 198
                order.goods.push(good);
                commentList.goodsNum++;
刘传洋 authored
199
            });
刘传洋 authored
200
刘传洋 authored
201
            order.goods.length && commentList.orders.push(order);*/
刘传洋 authored
202
刘传洋 authored
203
            _.forEach(result.data, (value) => {
刘传洋 authored
204
刘传洋 authored
205 206 207 208 209 210
                let order = {
                    orderNum: value.orderCode,
                    orderTime: value.createTime,
                    orderId: value.orderId,
                    goods: []
                };
刘传洋 authored
211
刘传洋 authored
212
                _.forEach(value.orderGoods, (v) => {
m  
OF1706 authored
213
                    // let cnAlphabet = v.cnAlphabet || '';
刘传洋 authored
214
                    let good = {
m  
OF1706 authored
215
                        href: helpers.getUrlBySkc(v.productSkn),
刘传洋 authored
216 217 218 219 220 221 222 223 224 225
                        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) : ''
                    };
刘传洋 authored
226
刘传洋 authored
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                    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);
            });
刘传洋 authored
250 251

        } else {
刘传洋 authored
252
            commentList.empty = NO_UNCOMMENT_GOODS; // 空数据 提示文字
刘传洋 authored
253
        }
刘传洋 authored
254
刘传洋 authored
255
        return {
刘传洋 authored
256
            comment: commentList,
刘传洋 authored
257
            isFromOrder: true
刘传洋 authored
258 259 260 261 262
        };
    };

    return co(process)();
};
陈轩 authored
263 264

exports.saveShareOrder = data => {
陈轩 authored
265
    let process = function*() {
刘传洋 authored
266
        return api.post('', {
陈轩 authored
267
            method: 'show.saveShareOrder',
yyq authored
268
            parameters: JSON.stringify(data)
陈轩 authored
269 270 271 272 273
        });
    };

    return co(process)();
};