returns.js 8.21 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 _reduceArrByProductSku = data => {
//     const d = [];
//
//     d.push(data[0]);
//
//     data.reduce((p, c) => { //eslint-disable-line
//         if (p && c && p.productSku !== c.productSku) {
//             d.push[c];
//         }
//     });
//
//     return d;
// };

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 _calcStatusRate = (num, total) => {
    let rate = 0;

    if (total) {
        rate = `${_.toInteger(num / total * 100)}%`;
    }

    return rate;
};

const _setDefaultStatus = (type) => {
    const list = ['提交申请', '审核通过', '商品寄回', '商品入库', `${type}完成`];
    let statusList = [];
    let resData = {};

    _.forEach(list, (value, key) => {
        statusList.push({
            step: _.toInteger(key) + 1,
            name: value
        });
    });
    statusList[0].act = true;

    resData.returnStstus = {
        rate: _calcStatusRate(1, statusList.length),
        statusList: statusList
    };

    return resData;
};

const _setReturnStatus = (list, half) => {
    let resData = {};

    if (list && list.length) {
        let statusList = [],
            step = 0;
        let rate;

        // 遍历进度列表
        _.forEach(list, (value, key) => {
            let state = {
                act: value.act === 'Y',
                step: _.toInteger(key) + 1,
                name: value.name
            };

            if (state.act) {
                step++;
            }
            statusList.push(state);
        });

        // 多半格进度特殊处理
        if (half) {
            rate = _calcStatusRate(step * 2 + 1, statusList.length * 2);
        } else {
            rate = _calcStatusRate(step, statusList.length);
        }

        resData.returnStstus = {
            rate: rate,
            statusList: statusList
        };
    }

    return resData;
};

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 _setRefundDetailData = (data) => {
    let resData = {};

    switch (data.status) {
        case 10:
            resData.orderReview = {pass: true};
            resData.refundExpress = true;
            resData.refundAddress = true;
            break;
        case 20:
            resData.refundExpress = true;
            break;
        case 30:
            resData.backStorage = true;
            break;
        case 40:
            resData.refundSure = true;
            break;
        default:
            resData.orderReview = true;
            break;
    }

    return resData;
};

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

        Object.assign(resData, _setSideMenu('我的退/换货'));


        resData.returns = {
            title: '退货申请',
            refund: _setDefaultStatus('退货') || {}
        };

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

        return resData;
    });
};

const getRefundDetailData = (applyId, uid) => {
    return returnsAPI.getRefundDetailAsync(applyId, uid).then(result => {
        let resData = {};

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

        if (result.data) {
            // console.log(_setRefundDetailData(result.data));

            Object.assign(resData.refundDetail, _setReturnStatus(result.data.statusList));

            Object.assign(resData.refundDetail, _setRefundDetailData(result.data));
        }

        return {returns: 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: helpers.urlFormat(`/return/refund/detail/${subRes.data.apply_id}`)
                        }
                    };
                }
                return {code: 400, message: '申请失败'};
            });
        } else {
            return {code: 403, message: '没有找到该订单'};
        }
    });
};

const getProductInfo = (productId, productSkn) => {
    return returnsAPI.getProductInfoAsync(productId, productSkn).then(result => {
        if (result.code === 200) {
            camelCase(result);
            result.data.goodsList.forEach(good => {
                good.colorImage = helpers.image(good.colorImage, 20, 20);
            });
        }
        return result;
    });
};

const getChangeGoodsList = (orderCode, uid) => {
    return returnsAPI.getChangeGoodsListAsync(orderCode, uid).then(result => {
        const basicData = {
            title: '申请换货'
        };
        let data;


        if (result && result.data) {
            data = camelCase(result.data);


            // data.goodsList = _reduceArrByProductSku(data.goodsList);
            data.hidePrice = true;

            data.goodsList.forEach(good => {
                good.showCheckbox = true;
                good.hidePrice = true;

                // good.buyNumber = good.num;
                good.buyNumber = 1;
                data.goodsList[2].colorId = 1;
            });
        }


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

module.exports = {
    getUserReturn,
    getRefundGoodsData,
    getChangeGoodsList,
    getProductInfo,
    getRefundDetailData,
    saveRefund
};