returns-apply.js 19.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
/**
 * 退换货申请
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/2/29
 */
var $ = require('yoho-jquery'),
    Addr = require('./common-address'),
    _dialog = require('../common/dialog');

var reTipsHbs = require('hbs/home/orders/re-tips.hbs');

var stringHandle = require('../common/stringHandle');

var $goodsTable = $('#goods-table'),
    $checkBox = $goodsTable.find('input[type="checkbox"]'),
    $reasons = $goodsTable.find('.return-reason'),
    $refundType = $('input[name="refund-type"]'),
    $areaCode = $('#streets'),
    $bankNameSpan = $('#bank-name-span'),
    $bankId = $('#bank-id');

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

var $exchange = $('.exchange-detail'),
    $hidePhone = $exchange.find('input[name="hide-phone"]'),
    $refundMode = $exchange.find('.refund-mode');

var $submitBox = $('.submit-box'),
    $waitPoint = $submitBox.find('.wait-gif span');

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

var option = {
    className: 'refund-dialog',
    btns: [
        {
            id: 'refund-sure',
            btnClass: ['refund-sure'],
            name: '确定提交'
        },
        {
            id: 'refund-cancel close',
            btnClass: ['refund-cancel close'],
            name: '返回修改'
        }
    ]
};

var refundConfirm = {};

var Dialog = _dialog.Dialog,
    Confirm = _dialog.Confirm;

var resetImg = function($p) {
    $p.removeClass('selected');
    $p.find('input[name=imgs]').val('');
    $p.find('img').attr('src', '');
};

require('../plugins/jquery.qupload');
require('../common/ajaxfileupload');

if (defaultPhone) {
    $hidePhone.remove();
    disPhone = defaultPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    $exchange.find('input[name="phone"]').val(disPhone);
}

// 退货结算
function getRefundCompute() {
    var goods = [];

    if (!pageType) {
        return false;
    }

    $goodsTable.find('input[type="checkbox"]:checked').each(function() {
        var $par = $(this).closest('tr'),
            goodsInfo;

        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="lastPrice"]').val()
        };

        goods.push(goodsInfo);
    });

    if (goods.length <= 0) {
        $('.re-tips').html(reTipsHbs({}));
        return false;
    }

    $.ajax({
        type: 'POST',
        url: '/home/returns/api/refundCompute',
        data: {
            orderCode: orderCode,
            goods: JSON.stringify(goods)
        }
    }).then(function(d) {
        if (d.code === 200) {
            $('.re-tips').html(reTipsHbs(d.data));
        }
    });
}

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);
        }
    });
}

function setBackInput(type) {
    $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 formatExchangeOption($dom, type) {
    var sku = $dom.data('sku'),
        skc = $dom.data('skc');
    var $optItem,
        $colorDom,
        $colorItem;
    var cd;

    if (!$dom || !sku) {
        return;
    }

    $dom.children().each(function() {
        if (+sku === +$(this).attr('value')) {
            $optItem = $(this);
        }
    });

    $colorDom = $dom.closest('.exchange-choose-wrap').find('.exchange-color');

    $colorDom.children().each(function() {
        if (+skc === +$(this).attr('value')) {
            $colorItem = $(this);
        }
    });

    type = type * 1;
    if (!type || type === 4 || type === 6 || type === 8) {
        $optItem && $optItem.removeAttr('disabled');
        $colorItem && $colorItem.removeAttr('disabled');
    } else {
        if (+$dom.val() === +sku) {
            $dom.val(0);
            cd = new Confirm({
                content: '无理由换货不可选择换为相同颜色尺码',
                cb: function() {
                    cd.close();
                }
            }).show();
        }
        $optItem && $optItem.attr('disabled', 'disabled');

        if (colorSize[skc].length === 1 && +colorSize[skc][0].id === +sku) {
            if (+$colorDom.val() === +skc) {
                $colorDom.val(0).change();
            }
            $colorItem && $colorItem.attr('disabled', 'disabled');
        } else {
            $colorItem && $colorItem.removeAttr('disabled');
        }
    }
}

if ($refundInfo.length) {
    pageType = 1;

    $refundType.each(function() {
        if ($(this).prop('checked')) {
            setBackInput($(this).val() * 1);
        }
    });
} else if (defaultArea) {
    syncRefundMode(defaultArea);
}

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

// 页面获取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();
    }

    formatExchangeOption($par.find('.exchange-size'), type);
});

// 切换支行银行信息
function switchSubBank() {
    needSubbranch = $bankId.children('option:selected').attr('data');
    if (needSubbranch === 'Y') {
        $bankNameSpan.removeClass('hide');
    } else {
        $bankNameSpan.addClass('hide');
    }
    $refundInfo.find('input[name="bank-name"]').val('');
}

// 选择退款银行
$bankId.change(function() {
    switchSubBank();
});

$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();
    }

    getRefundCompute();
});

$goodsTable.on('change', '.exchange-color', function() {
    var $warp = $(this).closest('.exchange-choose-wrap').parent(),
        $warpSize = $warp.find('.exchange-size'),
        _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>';
        });
    }
    $warpSize.html(_html).val(0);

    formatExchangeOption($warpSize, $warp.find('.return-reason').val());
});

$goodsTable.on('click', '.btn-del', function(e) {
    var $p = $(this).closest('li');

    e.stopPropagation();
    resetImg($p);
});

// 上传图片事件
function fileChangeEvent() {

    // 图片上传
    $goodsTable.on('change', 'input[type=file]', function() {

        var $f = $(this);
        var $p = $f.parent().siblings('.up-image-list');

        var $unSelectLi = $p.find('li:not(.selected)');

        if ($f.val()) {
            $.ajaxFileUpload({
                url: '/common/upload/image',
                secureuri: false,
                fileElementId: $f,
                data: {
                    bucket: 'evidenceImages'
                },
                dataType: 'json',
                success: function(data /* , status */) {

                    var resData = data.data,
                        len = resData.images.length > $unSelectLi.length ?
                            $unSelectLi.length : resData.images.length;

                    var url,
                        relaUrl,
                        i,
                        $li;

                    if (data && data.code === 200 && data.data && data.data.images) {

                        for (i = 0; i < len; i++) {
                            $li = $($unSelectLi[i]);

                            url = resData.images[i] +
                                '?imageMogr2/thumbnail/130x130/extent/130x130/background' +
                                '/d2hpdGU=/position/center/quality/90';
                            relaUrl = resData.imagesList[i];
                            $li.find('img').attr('src', url).attr('source-url', url);
                            $li.find('input[name=imgs]').val(relaUrl);
                            $li.addClass('selected');
                        }


                    } else if (data && data.message) {
                        alert(data.message);//eslint-disable-line
                    }
                },
                error: function() {    /** data, status, e **/
                    alert('上传失败,请稍后再试!');//eslint-disable-line
                },
                complete: function() {
                    $f.clone().replaceAll($f);
                }
            });
        }
    });
}


fileChangeEvent();// 初始化上传图片事件

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

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

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

    setBackInput(type);
});

$('#bank-card').keyup(function() {
    var $this = $(this),
        v = $this.val();

    /(\S{5}|\D)/.test(v) && $this.val($.trim(v.replace(/\D/g, '').replace(/(.{4})/g, '$1 ')));
});


// 打包退换货商品信息
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, cskc;

        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="lastPrice"]').val(),
                reason: $par.find('.return-reason').val() * 1
            };

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

                cskc = $par.find('.exchange-color').val() * 1;

                $par.find('.exchange-color').children().each(function() {
                    if (cskc === +$(this).attr('value')) {
                        goodsInfo.new_goods_id = $(this).data('goodsid');
                    }
                });

                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(returnMode) {
    var res = {};

    res.goods = packGoogsList();
    res.payment = {return_amount_mode: returnMode};

    if (res.payment.return_amount_mode === 2) {
        res.payment.bank_id = $bankId.val() * 1;
        res.payment.head_bank_name = $bankId.children('option:selected').text();

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

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

        res.payment.bank_card = $refundInfo.find('input[name="bank-card"]').val().replace(/\s/g, '');
        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 = '请填写收款账户';
        }

    }

    res.goods = JSON.stringify(res.goods);
    res.payment = JSON.stringify(res.payment);
    return res;
}

// 打包换货信息
function packExchangeInfo() {
    var nameReg = /^([\u4e00-\u9fa5\w*]{1,20})$/;
    var res = {};

    res.goods = packGoogsList();

    res.consigneeName = $.trim($exchange.find('input[name="name"]').val());
    if (!verifyTip) {
        if (!res.consigneeName) {
            verifyTip = '请填写收货人姓名';
        } else if (stringHandle.chinese2(res.consigneeName) > 20) {
            verifyTip = '收货人姓名最多支持20个字符';
        } else if (!nameReg.test(res.consigneeName)) {
            verifyTip = '收货人姓名不支持特殊符号';
        }
    }
    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 (res.mobile === disPhone) {
        res.mobile = defaultPhone;
    } else {
        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 = '请选择商品返回方式';
    }

    res.goods = JSON.stringify(res.goods);

    return res;
}

function loadWaiting(status) {
    var text = '○';

    if (status) {
        $submitBox.addClass('applying');
        timer = setInterval(function() {
            $waitPoint.text(text);
            if (text.length > 3) {
                text = '';
            }
            text += '○';
        }, 600);
    } else {
        $submitBox.removeClass('applying');
        clearInterval(timer);
    }
}

function saveRefundExchange(url, data) {
    loadWaiting(true);
    $.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); //eslint-disable-line
            loadWaiting(false);
        }
    });
}

// 提交申请
$('.save-btn').click(function() {
    var url,
        data = {
            orderCode: orderCode
        },
        returnMode = 0,
        tip = '',
        content = '';

    verifyTip = ''; // 初始化提示语
    if (pageType) {
        url = '/home/returns/saveRefund';

        // 付款方式
        $refundType.each(function() {
            if ($(this).prop('checked')) {
                returnMode = $(this).val() * 1;
            }
        });

        if (!verifyTip && !returnMode) {
            verifyTip = '请选择退款方式';
        } else {
            $.extend(data, packRefundInfo(returnMode));
        }

        if (returnMode === 2) {
            tip = '请确保账户名与账户一致,如果账号与名称不符或填写错误,会影响退款时效或退到他人账户,请谨慎填写!';
            content = '<h2 class="title">确认提交</h2><div class="refund-tips">' + tip + '</div>';
        } else if (returnMode === 3) {
            tip = '支付宝账户需要经过实名认证,账户与真实姓名必须相符,可以在支付宝个人中心查看,' +
                '如果账户与名称不符或填写错误,会影响退款时效或退到他人账户,请谨慎填写!';
            content = '<h2 class="title">确认提交</h2><div class="refund-tips">' + tip + '</div>';
        }

    } else {
        url = '/home/returns/saveExchange';
        $.extend(data, packExchangeInfo());
    }

    if (verifyTip) {
        alert(verifyTip); //eslint-disable-line
        return;
    }

    if (pageType && returnMode !== 1) {
        refundConfirm = new Dialog($.extend(true, {}, option, {
            content: content,
            btns: [{
                cb: function() {
                    refundConfirm.close();
                    saveRefundExchange(url, data);
                }
            }, {
                cb: function() {
                    loadWaiting(false);
                }
            }]
        })).show();
    } else {
        saveRefundExchange(url, data);
    }

});

// 退货结算
getRefundCompute();

$('.re-tips').on('click', '.icon-tip-help', function() {
    var tipHelpDialog, content;

    content = '<div class="tip-title">退款金额说明</div>';
    content += '<div class="tip-content">';
    content += '订单中包含的促销活动、优惠券、有货币、红包、礼品卡金额均按比例分摊至各件商品。';
    content += '</div>';

    tipHelpDialog = new Dialog({
        content: content,
        className: 'icon-tip-help-dialog',
        btns: [
            {
                id: 'tip-btn-sure',
                btnClass: ['tip-btn'],
                name: '确定',
                cb: function() {
                    tipHelpDialog.close();
                }
            }
        ]
    }).show();
});