returns-refund.page.js 4.54 KB
/**
 * 退货申请页
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/7/15
 */
var $ = require('yoho-jquery'),
    lazyload = require('yoho-jquery-lazyload');

var $refundTable = $('.refund-goods'),
    $check = $('.check'),
    $refundType = $('.refund-type'),
    $typeInfo = $refundType.children('dl');

var $applyBtn = $('#apply-btn');

var orderCode = $('#order-code').val() || 0,
    specialReason = $refundTable.data() || {},
    payInfo = {  // 退款信息
        type: 3, // 2--银行卡 3--支付宝 4--yoho币
        dom: $typeInfo.eq(0)
    };

var checked;

require('../plugins/check');

lazyload($('img.lazy'));

function restApplyBtn() {
    if (checked.documents && checked.documents.length) {
        $applyBtn.removeClass('disable');
    } else {
        $applyBtn.addClass('disable');
    }
}

function packApplyInfo() {
    var dom,
        isLack = false,
        payment = {},
        resData = {
            order_code: orderCode
        };

    if (checked && checked.documents) {
        dom = checked.documents;
        resData.goods = [];

        // 打包商品信息
        $.each(dom, function(key) {
            var data = $(dom[key]).data(),
                good = {
                    last_price: data.price,
                    goods_type: data.type,
                    product_skn: data.skn,
                    product_skc: data.skc,
                    product_sku: data.sku
                };

            if (isLack || !data.reason) {
                isLack = true;
                return;
            } else {
                good.returned_reason = data.reason;

                // if (specialReason.hasOwnProperty(data.reason)) {
                    // remark: ,
                    // returned_reason: 3,
                    // evidence_images: ,
                // }
            }
            resData.goods.push(good);
        });

        // 处理退款信息
        if (payInfo) {
            payment = {
                return_amount_mode: payInfo.type
            };
            switch (payInfo.type) {
                case 2:
                    payment.bank_name = payInfo.dom.find('.bank').val();
                    payment.bank_card = payInfo.dom.find('.account').val();
                    payment.payee_name = payInfo.dom.find('.name').val();
                    break;
                case 3:
                    payment.alipay_account = payInfo.dom.find('.account').val();
                    payment.alipay_name = payInfo.dom.find('.name').val();
                    break;
                default:
                    isLack = true;
                    break;
            }

            // 检查信息完整性
            $.each(payment, function(key) {
                if (!payment[key]) {
                    isLack = true;
                }
            });
            resData.payment = payment;
        }
    }

    if (isLack) {
        return false;
    }

    return resData;
}

$check.check({
    type: 'checkbox',
    onChange: function() {
        checked = $check.check('getChecked');
        restApplyBtn();
    }
});

$refundTable.on('change', '.refund-reason', function() {
    var $this = $(this),
        $specialDom = $this.parent().siblings('.special-reason'),
        val = $this.val();

    if (specialReason.hasOwnProperty(val)) {
        $specialDom.slideDown();
    } else {
        $specialDom.slideUp();
    }
    $specialDom.siblings('.check').data({reason: val});
});

$refundType.on('click', '.type-item', function() {
    var $this = $(this),
        index;

    if (!$this.hasClass('cur')) {
        if ($this.hasClass('ali-item')) {
            payInfo.type = 3;
        } else {
            payInfo.type = 2;
        }
        index = $this.index();

        // 切换退款卡片
        $this.addClass('cur').siblings('.cur').removeClass('cur');

        // 切换退款信息输入容器
        payInfo.dom = $typeInfo.eq(index);
        $typeInfo.addClass('hide');
        payInfo.dom.removeClass('hide');
    }
});

$applyBtn.click(function() {
    var $tip = $applyBtn.siblings('.apply-tip'),
        param = packApplyInfo();

    if (!param) {
        $tip.removeClass('hide');
        return;
    } else {
        $tip.addClass('hide');
    }

    $.ajax({
        type: 'POST',
        url: '/me/return/refund/apply',
        data: param,
        success: function(data) {
            if (data.code === 200) {
                if (data.data && data.data.refer) {
                    location.href = data.data.refer;
                }
                $tip.text('申请成功').removeClass('hide');
            }
        }
    });
});