returns-apply.js 11.3 KB
/**
 * 退换货申请
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/2/29
 */
var $ = require('yoho.jquery'),
    Addr = require('./common-address');

var $goodsTable = $('#goods-table'),
    $checkBox = $goodsTable.find('input[type="checkbox"]'),
    $reasons = $goodsTable.find('.return-reason'),
    $file = $goodsTable.find('input[type=file]'),
    $refundType = $('input[name="refund-type"]'),
    $province = $('#province'),
    $city = $('#city'),
    $areaCode = $('#area');

var $refundInfo = $('#refund-pay-info'),
    $payYoho = $refundInfo.find('.type-yoho'),
    $payBank = $refundInfo.find('.type-bank'),
    $payAli = $refundInfo.find('.type-alipay');

var $exchange = $('.exchange-detail'),
    $refundMode = $exchange.find('.refund-mode');

var pageType = 0,  // 0-换货  1-退货
    orderCode = $('#order-code').val(),
    defaultArea = $exchange.find('input[name="hide-area"]').val(),
    verifyTip = '',
    colorSize = {};

require('../../plugin/jquery.upload');

function syncRefundMode(code) {
    $.ajax({
        type: 'POST',
        url: '/home/returns/getDelivery',
        data: {
            areaCode: code
        }
    }).then(function(jsonData) {
        var _html = '';

        if (jsonData.code === 200) {
            $.each(jsonData.data, function(k, v) {
                var checked = '';

                if (v.is_default === 'Y') {
                    checked = 'checked="checked"';
                }
                _html += '<input type="radio" name="delivery-tpye" value="' +
                    v.id + '" ' + checked + '"">' + v.name;
            });
            $refundMode.html(_html);
        }
    });
}

if ($refundInfo.length) {
    pageType = 1;
} else {
    syncRefundMode(defaultArea);
}

defaultArea = defaultArea ? defaultArea : '';
Addr.loadAllData(defaultArea, {
    areaDomId: 'area'
});

// 页面获取size列表数据
$goodsTable.find('.sizeinfo').each(function() {
    var sizes = [];

    $(this).children().each(function() {
        sizes.push($(this).data());
    });
    colorSize[$(this).data().id] = sizes;
});

$reasons.change(function() {
    var $par = $(this).parent().parent(),
        type = $(this).val() * 1;

    if (type === 4 || type === 6 || type === 8) {
        $par.next().show();
    } else {
        $par.next().hide();
    }
});

$checkBox.change(function() {
    var $par = $(this).parent().parent();

    if ($(this).prop('checked')) {
        $par.find('select').prop('disabled', false);
    } else {
        $par.find('select').val(0).prop('disabled', true);
        $par.next().hide();
    }
});

$goodsTable.on('change', '.exchange-color', function() {
    var $warp = $(this).closest('.exchange-choose-wrap'),
        _html = '<option value="0">请选择尺码</option>',
        id = $(this).val();

    if (colorSize[id]) {
        $.each(colorSize[id], function(k, v) {
            _html += '<option value="' + v.id + '">' + v.name + '</option>';
        });
    }
    $warp.find('.exchange-size').html(_html).val(0);
});

$goodsTable.on('click', '.btn-del', function() {
    $(this).parent().empty();
});

$file.each(function(e) {
    $('#upload-img-' + e).upload({
        auto: true,
        buttonClass: 'upload-img-btn',
        fileType: 'image/*',
        uploadScript: '/home/returns/imgUpload',
        fileObjName: 'fileData',
        fileSizeLimit: 5000,
        buttonText: '上传图片',
        height: 34,
        width: 116,
        multi: false,
        onUploadComplete: function(file, data) {
            var $par = $(this).closest('.problem-description'),
                $imgList = $par.find('li'),
                isShow = false,
                img, _html;

            img = JSON.parse(data).imgList[0];
            _html = '<span class="btn-del" title="删除"></span>' +
                '<img src="' + img.imgUrl + '" width="126" height="126">' +
                '<input type="hidden" name="imgs" value="' + img.imgRelUrl + '">';
            $imgList.each(function() {
                if (!isShow && !$(this).find('img').length) {
                    isShow = true;
                    $(this).html(_html);
                }
            });
        },
        onError: function(info, fileType, data) {
            var msg = '图片上传失败!';

            if (info === 'FILE_SIZE_LIMIT_EXCEEDED') {
                msg = '图片大小超出限制!';
            }
            alert(msg);
        }
    });
});

$exchange.on('change', '#area', function() {
    var code = $(this).val() * 1;

    if (code) {
        syncRefundMode(code);
    }
});

$refundType.change(function() {
    var type = $(this).val() * 1;

    $payYoho.hide();
    if (type === 2) {
        $payBank.show();
        $payAli.hide();
        $payYoho.hide();
    } else if (type === 3) {
        $payAli.show();
        $payBank.hide();
        $payYoho.hide();
    } else if (type === 4) {
        $payYoho.show();
        $payBank.hide();
        $payAli.hide();
    }
});

// 打包退换货商品信息
function packGoogsList() {
    var goods = [],
        type = '换货';

    if (pageType) {
        type = '退货';
    }
    if (!$checkBox.length) {
        verifyTip = '没有可以' + type + '的商品';
    }

    $checkBox.each(function() {
        var $par = $(this).parent().parent(),
            $next = $par.next(),
            goodsInfo;

        if (verifyTip.length) {
            return;
        }

        if ($(this).prop('checked')) {
            goodsInfo = {
                product_skn: $par.find('input[name="skn"]').val(),
                product_skc: $par.find('input[name="skc"]').val(),
                product_sku: $par.find('input[name="sku"]').val(),
                goods_type: $par.find('input[name="typeid"]').val(),
                last_price: $par.find('input[name="price"]').val(),
                reason: $par.find('.return-reason').val() * 1
            };

            if (pageType) {
                goodsInfo.returned_reason = goodsInfo.reason;
            } else {
                goodsInfo.exchange_reason = goodsInfo.reason;
                goodsInfo.new_goods_id = $par.find('.exchange-color').val() * 1;
                goodsInfo.new_product_sku = $par.find('.exchange-size').val() * 1;

                if (!verifyTip && !goodsInfo.new_goods_id) {
                    verifyTip = '请选择要换的颜色';
                }

                if (!verifyTip && !goodsInfo.new_product_sku) {
                    verifyTip = '请选择要换的尺码';
                }
            }
            if (!verifyTip && !goodsInfo.reason) {
                verifyTip = '请选择' + type + '原因';
            }

            if (goodsInfo.reason === 4 ||
                goodsInfo.reason === 6 ||
                goodsInfo.reason === 8) {
                goodsInfo.remark = $.trim($next.find('textarea').val());
                goodsInfo.evidence_images = [];
                $next.find('input[name="imgs"]').each(function() {
                    goodsInfo.evidence_images.push($(this).val());
                });

                if (!verifyTip && !goodsInfo.remark) {
                    verifyTip = '请填写' + type + '描述';
                }

                if (!verifyTip && !goodsInfo.evidence_images.length) {
                    verifyTip = '请上传图片';
                }
            }
            delete goodsInfo.reason;

            goods.push(goodsInfo);
        }
    });

    if (!verifyTip && !goods.length) {
        verifyTip = '请选择要' + type + '的商品';
    }

    return goods;
}

// 打包退货信息
function packRefundInfo() {
    var res = {};

    res.goods = packGoogsList();
    res.payment = {};

    $refundType.each(function() {
        if ($(this).prop('checked')) {
            res.payment.return_amount_mode = $(this).val() * 1;
        }
    });

    if (!verifyTip && !res.payment.return_amount_mode) {
        verifyTip = '请选择退款方式';
    }

    if (res.payment.return_amount_mode === 2) {
        res.payment.province = $province.val();
        res.payment.city = $city.val();
        res.payment.area_code = $areaCode.val();
        if (!verifyTip && !res.payment.area_code * 1) {
            verifyTip = '请选择银行所在地';
        }

        res.payment.bank_name = $.trim($refundInfo.find('input[name="bank-name"]').val());
        if (!verifyTip && !res.payment.bank_name) {
            verifyTip = '请填写银行名称';
        }

        res.payment.payee_name = $.trim($refundInfo.find('input[name="payee-name"]').val());
        if (!verifyTip && !res.payment.payee_name) {
            verifyTip = '请填写收款人姓名';
        }

        res.payment.bank_card = $.trim($refundInfo.find('input[name="bank-card"]').val());
        if (!verifyTip && !res.payment.bank_card) {
            verifyTip = '请填写收款账户';
        }

    } else if (res.payment.return_amount_mode === 3) {
        res.payment.alipay_name = $.trim($refundInfo.find('input[name="alipay-name"]').val());
        if (!verifyTip && !res.payment.alipay_name) {
            verifyTip = '请填写收款人姓名';
        }

        res.payment.alipay_account = $.trim($refundInfo.find('input[name="alipay-account"]').val());
        if (!verifyTip && !res.payment.alipay_account) {
            verifyTip = '请填写收款账户';
        }

    }
    return res;
}

// 打包换货信息
function packExchangeInfo() {
    var res = {};

    res.goods = packGoogsList();

    res.consigneeName = $.trim($exchange.find('input[name="name"]').val());
    if (!verifyTip && !res.consigneeName) {
        verifyTip = '请填写收货人姓名';
    }

    res.areaCode = $areaCode.val();
    if (!verifyTip && !res.areaCode * 1) {
        verifyTip = '请选择地区';
    }

    res.address = $.trim($exchange.find('input[name="address"]').val());
    if (!verifyTip && !res.address) {
        verifyTip = '请填写详细地址';
    }

    res.mobile = $.trim($exchange.find('input[name="phone"]').val());
    if (!verifyTip && !res.mobile) {
        verifyTip = '请填写手机号';
    }

    res.zipCode = $.trim($exchange.find('input[name="postcode"]').val());
    res.deliveryType = $exchange.find('input[name="delivery-tpye"]:checked').val();
    if (!verifyTip && !res.deliveryType) {
        verifyTip = '请选择商品返回方式';
    }

    return res;
}

// 提交申请
$('.save-btn').click(function() {
    var url, data;

    verifyTip = '';  // 初始化提示语
    if (pageType) {
        url = '/home/returns/saveRefund';
        data = packRefundInfo();
    } else {
        url = '/home/returns/saveExchange';
        data = packExchangeInfo();
    }
    data.orderCode = orderCode;

    if (verifyTip) {
        alert(verifyTip);
        return;
    }

    $.ajax({
        type: 'POST',
        url: url,
        data: data
    }).then(function(jsonData) {
        if (jsonData.code === 200) {
            if (jsonData.data && jsonData.data.refer) {
                window.location.href = jsonData.data.refer;
            }
        } else {
            alert(jsonData.message);
        }
    });
});