returns.js 3.82 KB
/**
 * 退换货
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/19
 */
'use strict';

const api = global.yoho.API;
const camelCase = global.yoho.camelCase;

const _ = require('lodash');
const mcHandler = require('./menu-crumb-handler');
const returnsAPI = require('./returns-api');

const helpers = global.yoho.helpers;

const pageSize = 10;

const getUserReturn = (uid, page) => {

    return api.get('', {
        method: 'app.refund.getList',
        uid: uid,
        page: page || 1,
        limit: 10
    }).then(result => {
        const basicData = {
            title: '我的退/换货'
        };

        const refundStr = {
            1: '退货',
            2: '换货'
        };

        const data = camelCase(result.data);

        const paginationOpts = data.total > pageSize ? {
            paginationOpts: {
                total: data.total,
                page: data.page,
                limit: pageSize
            }
        } : false;

        data.list.forEach(item => {
            item.orderGoods = item.goods;
            item.createTime = item.orderCreateTime;
            item.hidePrice = true;
            item.showStatus = true;
            item.refundStr = refundStr[item.refundType];

            item.orderGoods.forEach(it => {
                it.hidePrice = true;
            });
        });

        return {
            returnsList: Object.assign(data, paginationOpts, basicData)
        };
    });
};

const _setSideMenu = (type) => {
    return {
        nav: mcHandler.getMeCrumb(type),
        navigation: mcHandler.getSideMenu(type)
    };
};

const _setRefundGoodList = (data) => {
    let resData = {};

    if (data.goods_list) {
        let goods = [];

        _.forEach(data.goods_list, value => {
            let cnAlphabet = value.cn_alphabet ? value.cn_alphabet : '';

            goods.push({
                href: helpers.urlFormat(`/product/pro_${value.product_id}_${value.goods_id}/${cnAlphabet}.html`),
                img: value.goods_image,
                name: value.product_name,
                size: value.size_name,
                color: value.color_name,
                num: 1, // 接口目前不支持
                price: value.last_price,
                skn: value.product_skn,
                skc: value.product_skc,
                sku: value.product_sku,
                type: value.goods_type,
                typeId: value.goods_type_id,
                reasonList: data.return_reason
            });

            if (value.hasShoes) {
                resData.hasShoes = true;
            }
        });
        resData.goods = goods;
    }
    resData.speclialReason = data.special_return_reason;

    return resData;
};

const getRefundGoodsData = (orderCode, uid) => {
    return returnsAPI.getRefundGoodsAsync(orderCode, uid).then(result => {
        let resData = {};

        Object.assign(resData, _setSideMenu('我的退/换货'));
        resData.returns = {
            title: '退货申请',
            refund: {}
        };

        if (result.data) {
            Object.assign(resData.returns.refund, _setRefundGoodList(result.data), {
                orderCode: orderCode
            });
        }

        return resData;
    });
};

const saveRefund = (orderCode, uid, goods, payment) => {
    return returnsAPI.getOrderInfoAsync(orderCode, uid, '').then(result => {
        if (!_.isEmpty(result)) {
            return returnsAPI.refundSubmitAsync(orderCode, uid, goods, payment).then(subRes => {
                if (subRes && subRes.data) {
                    return {code: 200, data: {refer: subRes.data.apply_id}};
                }
                return {code: 400, message: '申请失败'};
            });
        } else {
            return {code: 403, message: '没有找到该订单'};
        }
    });
};

module.exports = {
    getUserReturn,
    getRefundGoodsData,
    saveRefund
};