returns.js 3.1 KB
/**
 *  个人中心 退换货
 *  @author chenxuan <xuan.chen@yoho.cn>
 */

'use strict';

const path = require('path');
const Promise = require('bluebird');

// 使用 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 REFUND = 1; // 退货
const EXCHANGE = 2; // 换货
const TRUE = 'Y';
const RETURNS_EMPTY = '您没有退/换货订单';
const REFUND_URI = '/home/returns/refundDetail';
const EXCHANGE_URI = '/home/returns/exchangeDetail';

// 处理订单商品的数据
function getGoodsData(goods) {
    const arr = [];

    goods.forEach(good => {
        const obj = {};
        const cnAlphabet = good.cn_alphabet || '';

        obj.href = helpers.getUrlBySkc(good.product_id, good.goods_id, cnAlphabet);
        obj.thumb = helpers.image(good.goods_image, 60, 60);
        obj.name = good.product_name;
        obj.color = good.color_name;
        obj.size = good.size_name;

        arr.push(obj);
    });

    return arr;
}

/**
 * 退换货列表页数据
 * @param  {string} uid   用户 uid
 * @param  {Number} page  当前页数
 * @param  {Number} limit 每页最大条数
 * @return {Object}             Promise
 */
exports.getReturnsList = co(function*(uid, page, limit) {
    page = Number.parseInt(page, 10) || 1;
    limit = Number.parseInt(limit, 10) || 10;

    const obj = {
        orders: [],
        pager: {}
    };
    let response = yield api.post('', {
        method: 'app.refund.getList',
        data: { uid: uid, page: page, limit: limit }
    });
    let repData = response.data;


    // 处理数据
    if (response.code === 200 && repData && repData.list.length) {
        obj.pager = pager(repData.total, {
            page: page,
            limit: limit
        });

        repData.list.forEach(item => {
            const t = {};

            t.returnId = item.id;
            t.orderNum = item.order_code;
            t.orderTime = item.oreder_create_time.replace('-', '/');
            t.returnTime = item.create_time;
            t.returnStatus = item.status_name;


            const canCancel = item.canCancel === TRUE;
            let isChange, uri;

            switch (item.refund_type) {
                case REFUND:
                    isChange = false;
                    if (canCancel) {
                        uri = REFUND_URI;
                    }
                    break;
                case EXCHANGE:
                    isChange = true;
                    if (canCancel) {
                        uri = EXCHANGE_URI;
                    }
                    break;
                default:
            }

            t.isChange = isChange;
            t.canCancelUrl = helpers.urlFormat(uri);
            t.moreHref = helpers.urlFormat(uri, { id: item.id });
            t.goods = getGoodsData(item.goods);

            obj.orders.push(t);
        });
    } else {
        obj.empty = RETURNS_EMPTY;
    }

    return obj;
});

exports.getRefund = (orderCode, uid) =>{

};