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

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

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

var orderCode = $('#order-code').val() || 0,
    specialReason = $refundTable.data() || {},
    payInfo = {  // 退款信息
        type: $refundType.find('.type-item.cur').data('id') // 1--原卡返回 2--银行卡 3--支付宝 4--yoho币
    };

var imgBoxTpl = require('../../tpl/me/thumbnail.hbs');

var checked;

lazyload($('.banner-img'));

$('.blk-footer .return-top').remove(); // 移除通用的返回顶部组件

require('yoho-jquery-dotdotdot');

require('../plugins/check');
require('../common/header'); // header
require('../common/return-top'); // return-top

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

// dot
$('.goods-item .title').dotdotdot({
    wrap: 'letter'
});

function setUnionList(list) {
    var i, html = '';

    if (list && list.length) {
        for (i = 0; i < list.length; i++) {
            html += '<option>' + list[i].title + '</option>';
        }
        $bankSelect.html(html);
    }
}

function getUnionList() {
    var that = this;

    if (!this.geted) {
        $.ajax({
            type: 'GET',
            url: '/me/return/unionInfo',
            data: {},
            success: function(data) {
                if (data.code === 200) {
                    that.geted = true;
                    setUnionList(data.data);
                }
            }
        });
    }
}

function changePayMode(id) {
    payInfo.type = id;

    // 切换退款信息输入容器
    payInfo.dom = $refundType.children('.mode' + payInfo.type);
    $typeInfo.addClass('hide');
    payInfo.dom.removeClass('hide');

    // 银行卡退款,获取银行列表
    if (+id === 3) {
        getUnionList();
    }
}

changePayMode(payInfo.type);

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

function packApplyInfo() {
    var dom,
        isLack = false,
        payment = {},
        resData = {
            orderCode: 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
                };
            var $special = $(dom[key]).siblings('.special-reason');

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

                if (specialReason.hasOwnProperty(data.reason)) {
                    good.remark = $special.find('.mark-text').val();
                    if (!good.remark) {
                        isLack = true;
                    }

                    good.evidence_images = [];
                    $special.find('.thumb-box').each(function() {
                        var img = $(this).data('img');

                        if (img) {
                            good.evidence_images.push(img);
                        }
                    });
                    if (!good.evidence_images.length) {
                        isLack = true;
                    }
                }
            }
            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_name += '-' + payInfo.dom.find('.open-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:
                    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});
}).on('keyup', '.mark-text', function() {
    var $this = $(this),
        str = $.trim($this.val());

    if (str.length > 100) {
        str = str.substring(0, 100);
    }
    $this.val(str);
}).on('click', '.img-upload', function() {
    var $this = $(this),
        num = $this.data('num') || 0;

    if (num > 3) {
        $this.addClass('hide');
        return;
    }

    upload.up({
        callback: function(result) {
            var data = {};

            if (result && result.code === 200) {
                // 避免异步上传导致数量不统一
                num = $this.data('num') || 0;

                if (num > 3) {
                    $this.addClass('hide');
                    return;
                } else if (num === 3) {
                    $this.addClass('hide');
                }
                data.src = result.data;
                if (result.imgs && result.imgs.length) {
                    data.url = result.imgs[0];
                }
                $this.siblings('.img-up-tip').text(++num + '/4');
                $this.data('num', num);
                $this.before(imgBoxTpl(data));
            }
        }
    });
}).on('click', '.thumb-box .delete', function() {
    var $this = $(this),
        $wrap = $this.closest('.thumb-box'),
        $upBtn = $wrap.siblings('.img-upload'),
        num = $upBtn.data('num') || 0;

    $wrap.siblings('.img-up-tip').text(--num + '/4');
    $wrap.remove();
    $upBtn.removeClass('hide').data('num', num);
});

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

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

        changePayMode(id);
    }
});

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

    if ($(this).hasClass('disable')) {
        return;
    }
    
    if (!param || !param.goods) {
        $tip.removeClass('hide');
        return;
    } else {
        $tip.addClass('hide');
    }

    $.ajax({
        type: 'POST',
        url: '/me/return/refund/apply',
        data: param,
        success: function(data) {
            var message;

            if (data.code === 200) {
                if (data.data && data.data.refer) {
                    location.href = data.data.refer;
                }
                $tip.text('申请成功').removeClass('hide');
            } else {
                message = data.message || '申请提交失败';
                new Alert('<h1>' + message + '</h1>').show();
            }
        }
    });
});