im.js 1.05 KB
'use strict';

const _ = require('lodash');

/**
 *  im 接口数据的 处理
 */

/**
 *  function: 处理 订单返回的数据
 */
exports.handleOrderList = (data, w, h) => {
    if (_.isEmpty(data)) {
        return;
    }

    function replaceWH(img) {
        return img.replace(/(\{width\}|\{height\})/g, function($0) {
            const dict = {
                '{width}': w,
                '{height}': h,
            };

            return dict[$0];
        });
    }

    data.forEach(order => {
        order.goods = order.ordersGoodsBoList.map(good => {
            return {
                id: good.productSku,
                name: good.productName,
                thumb: replaceWH(good.imgUrl),
                color: good.colorName,
                size: good.sizeName,
                count: good.buyNumber,
                price: good.lastPrice,
                tariffPrice: good.tariffPrice
            };
        });
        order.count = order.ordersGoodsBoList.reduce((sum, good) => {
            return sum + good.buyNumber;
        }, 0);
    });
};