address.js 27.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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/**
 * @description: 地址管理
 * @author: chenglong.wang@yoho.cn
 */

var $ = require('yoho.jquery');
var dialog = require('../common/dialog');


var isProvinceChecked = false;

var $addressManage = $('.address-manage'),
    $province = $addressManage.find('.text-input[name="province"]'),
    $city = $addressManage.find('.text-input[name="city"]'),
    $county = $addressManage.find('.text-input[name="county"]'),
    $selectList = $addressManage.find('.text-input[name="province"], .text-input[name="city"]'),
    $addressList = $('.address-list'),
    $name = $addressManage.find('.text-input[name="name"]'),
    $phone = $addressManage.find('.text-input[name="phone"]'),
    $mail = $addressManage.find('.text-input[name="mail"]'),
    $address = $addressManage.find('.text-input[name="address"]'),
    $code = $addressManage.find('.text-input[name="code"]'),
    $saveBtn = $('.save-btn'),
    $telCode = $addressManage.find('.text-input[name="tel-code"]'),
    $tel = $addressManage.find('.text-input[name="tel"]'),
    $useNewAddress = $('.use-new-address'),
    $existAddressList = $('.exist-address-list'),
    $loading = $('.loading'),
    $orderEditMain = $('#order-edit-main'),
    $invoiceCheck = $('.invoice');

var cartType = $addressManage.attr('cart-type');

$loading.show();

// 构建select下拉选项
function structureOption($obj, data, selectId) {

    var i,
        optionHtml = '',
        defaultOption,
        isStar = '';

    for (i = 0; i < data.length; i++) {

        if (data[i].is_support_express === 'Y') {
            isStar = '*';
        } else {
            isStar = '';
        }

        if (data[i].id === selectId) {
            optionHtml += '<option selected value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
        } else {
            optionHtml += '<option value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
        }
    }


    if ($obj.attr('name') === 'province') {
        defaultOption = '<option value="0">请选择省份</option>';
    } else if ($obj.attr('name') === 'city') {
        defaultOption = '<option value="0">请选择城市</option>';

    } else if ($obj.attr('name') === 'county') {
        defaultOption = '<option value="0">请选择区县</option>';
    }

    $obj.html(defaultOption + optionHtml);
}
/**
 * @description: 改函数会返回地址信息
 * d {Object} type: 'getProvince'获取省 type: 'getCity'获取城市 type: 'getCounty'获取县
 * id: 0 && type: 'getProvince' 获取所有省,默认没有选中项
 * id !== 0 && type: 'getProvince' 获取所有省,默认选中用户所在的省
 */
function getAddress(d, callback) {

    var $obj;
    var selectId = d.selectId;

    if (d.type === 'getProvince') {

        //url = 'getProvince';
        $obj = $province;
    } else if (d.type === 'getCity') {

        //url = 'getCity';
        $obj = $city;
    } else if (d.type === 'getCounty') {

        //url = 'getCounty';
        $obj = $county;
    }

    $.ajax({
        type: 'GET',
        url: '/cart/index/getAreaList',
        dataType: 'json',
        data: {
            id: d.id * 1
        }
    }).then(function(d) {

        structureOption($obj, d.data, selectId);

        if (typeof callback === 'function') {
            callback();
        }

    });
}

//手机号码****处理
function setPhoneStar(pn) {
    return pn.replace(/^(\d{3}).*(\d{4})$/, '$1****$2');
}

// 修改地址时把用户信息带过来
function getUserInfo($obj) {

    $name.val($obj.attr('data-name'));
    $name.attr('data-id', $obj.attr('data-id'));
    $address.val($obj.attr('data-addressDesc'));
    $phone.val($obj.attr('data-mobile'));
    $mail.val($obj.attr('data-email'));
    $code.val($obj.attr('data-zipcode'));
    $telCode.val($obj.attr('data-phoneCode'));
    $tel.val($obj.attr('data-phoneNum'));
}

// 更新地址列表状态
function refreshAddressStatus() {
    $useNewAddress.removeClass('active');
    $existAddressList.find('li:not(".active")').addClass('hide');
    $existAddressList.find('li.active').addClass('address_select');
    $addressManage.addClass('hide');
    $saveBtn.addClass('hide').removeAttr('data-id'); //清除data-id
    $existAddressList.removeClass('address-expand');
    $('.address-list h2 span').text('[修改]');
}

//获取用户已经存在的地址列表
exports.getUserAddressList = function() {

    var addressData,
        i,
        addressStr = '',
        isChecked = '',
        isHide = '',
        addressText = '',
        hasDefault = 0;

    $.ajax({
        type: 'GET',
        url: '/cart/index/getAddress',
        dataType: 'json'
    }).then(function(d) {

        $loading.hide();

        if (d.code === 200) {

            addressData = d.data.list;

            if (!addressData) {

                // 新用户引导
                if (!!$orderEditMain.attr('data-new')) {

                    $saveBtn.addClass('new-user-btn').html('下一步:选择支付及送货时间');

                    $('.pay-btn').addClass('new-user-pay').html('下一步:发票和备注');

                    $('.switch-pay-modify').html('');

                }

                $addressList.find('>h2').find('span').click();

                $('.exist-address-list li').click();

                $('.use-new-address').removeClass('active');

                return;

            }

            for (i = 0; i < addressData.length; i++) {

                if (addressData[i].checked) {

                    isChecked = 'checked';
                    isHide = 'active address_select';
                    hasDefault++;
                    addressText = '默认地址';
                    $('#recordMobileNumb').val(addressData[i].completeMobile);

                } else {
                    isChecked = '';
                    isHide = 'hide';
                    addressText = '设为默认地址';
                }

                addressStr +=
                    '<li data-id="' + addressData[i].id + '" class="' + isHide +
                    '" data-express="' + addressData[i].isSupport + '">' +
                    '<input data-name="' + addressData[i].user +
                    '" data-areaCode="' + addressData[i].areaCode +
                    '" data-addressDesc="' + addressData[i].addressDesc +
                    '" data-mobile="' + addressData[i].mobile +
                    '" data-completeMobile="' + addressData[i].completeMobile +
                    '" data-phoneNum="' + addressData[i].phoneNum +
                    '" data-phoneCode="' + addressData[i].phoneCode +
                    '" data-zipCode="' + addressData[i].zipCode +
                    '" data-email="' + addressData[i].email +
                    '" class="radio" type="radio" name="address" ' + isChecked + ' />' +
                    '<label for="">' +
                    '<strong>' + addressData[i].user + '</strong>' + addressData[i].address +
                    '<a href="javascript:void(0);" class="default-address">' + addressText + '</a>' +
                    '<div class="order-modify-btn">' +
                    '<span class="address-modify">[修改]</span>' +
                    '<span class="address-del">[删除]</span>' +
                    '</div>' +
                    '</label>' +
                    '</li>';
            }

            $('.use-new-address').before($(addressStr));

            //说明用户的地址列表没有默认地址
            if (hasDefault === 0) {

                //没有默认地址则把所有地址都显示出来
                //$('.exist-address-list li:not(".use-new-address")').removeClass('hide');
                $('.address-list h2 span').click();
            }

            if ($('.active.address_select').attr('data-express') === 'N') {
                $('.express-list input[name="carriagegroup"][value="2"]').attr('disabled', 'disabled');
                $('.express-list input[name="carriagegroup"][value="1"]').attr('checked', 'checked');
                $('.express-list.sf').addClass('hide');
                $('.is-sup').removeClass('hide');
            } else {
                $('.express-list input[name="carriagegroup"][value="2"]').removeAttr('disabled');
                $('.express-list.sf').removeClass('hide');
                $('.is-sup').addClass('hide');
            }

        }

    });
};

/*
 * 添加地址&修改地址
 * id: 根据id获取省市区列表
 * code: 根据code确定是否选中省市区
 */
exports.newAddress = function(id, code) {

    var pId = id || 0; //如果没有传id则获取所有省列表

    var addressCodeReg = /[0-9]{2}/gi;

    var codeId = !!code && code.match(addressCodeReg);

    //获取省
    getAddress({
        id: pId,
        type: 'getProvince',
        selectId: codeId[0]
    }, function() {

        var provinceId = $province.val();

        if (typeof provinceId !== 'undefined' && provinceId !== '0') {

            //如果获取的省有默认选中项则获取市
            getAddress({
                id: provinceId,
                type: 'getCity',
                selectId: '' + codeId[0] + codeId[1]
            }, function() {

                var cityId = $city.val();

                //如果获取的市有默认选中项则获取县
                if (cityId !== '0') {
                    getAddress({
                        id: cityId,
                        type: 'getCounty',
                        selectId: code
                    });
                }
            });
        } else {
            $addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
            $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
        }
    });

    // 发生change事件时获取下一级地址
    $selectList.change(function() {

        var $this = $(this);

        if ($this.attr('name') === 'province') {

            if ($this.val() === '0') {
                $addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
                $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
            } else {
                getAddress({
                    id: $this.val(),
                    type: 'getCity'
                }, function() {
                    isProvinceChecked = true;
                });
                $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
            }
        }

        if ($this.attr('name') === 'city' && isProvinceChecked) {

            if ($this.val() === '0') {
                $addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
            } else {
                getAddress({
                    id: $this.val(),
                    type: 'getCounty'
                });
            }
        }
    });
};

//修改地址
exports.modifyAddress = function() {

    $addressList.click(function(event) {

        var $this = $(event.target),
            id = $this.closest('li').attr('data-id'),
            areaCode = $this.closest('li').find('.radio').attr('data-areaCode');

        if ($this.hasClass('address-modify')) {
            $addressManage.removeClass('hide');
            exports.newAddress(0, areaCode);
            getUserInfo($this.closest('li').find('.radio'));
            $('.address-list-inner .save-btn').removeClass('hide');
            $saveBtn.attr('data-id', id);
            isProvinceChecked = true;
        } else if ($this.hasClass('address-del')) {

            $loading.show();

            $.ajax({
                type: 'POST',
                url: '/cart/index/delAddress',
                data: {
                    id: id
                }
            }).then(function(d) {

                $loading.hide();

                if (d.code === 200) {

                    $this.closest('li').remove();
                } else {
                    new dialog.Alert(d.message).show();
                }
            }).fail(function() {

                //todo
            });
        } else if ($this.hasClass('default-address') && !$this.parents('li').hasClass('address_select')) {

            $loading.show();

            // 设为默认地址
            $.ajax({
                type: 'post',
                url: '/cart/index/setDefaultAddress',
                data: {
                    id: id
                }
            }).then(function(d) {

                $loading.hide();

                if (d.code === 200) {

                    $('.exist-address-list li').removeClass('address_select')
                        .find('.default-address').text('设为默认地址');
                    $this.parents('li').addClass('address_select');
                    $this.text('默认地址');

                } else {
                    new dialog.Alert(d.message).show();
                }
            });
        }
    });
};

// 保存地址并送到这个地址
exports.saveAddress = function() {
    $saveBtn.click(function() {

        var $this = $(this);

        var name = $name.val(),
            province = $province.val(),
            city = $city.val(),
            county = $county.val(),
            address = $address.val(),
            phone = $phone.val(),
            telCode = $telCode.val(),
            tel = $tel.val(),
            mail = $mail.val(),
            code = $code.val();

        var provinceText = $province.find('option[value="' + province + '"]').text().replace(/\*/, ''),
            cityText = $city.find('option[value="' + city + '"]').text().replace(/\*/, ''),
            countyText = $county.find('option[value="' + county + '"]').text().replace(/\*/, '');


        var postData = {
            consignee: name,
            areaCode: county,
            address: address,
            email: mail,
            mobile: phone,
            zipCode: code,
            phoneCode: telCode,
            phoneNum: tel
        };

        var addressStr;
        var $modifyLi;
        var hasCheckedAddress;

        var nameReg = /^[\u4e00-\u9fa5]{2,5}$/;
        var addressReg = /^[\s\S]{2,100}$/;
        var phoneReg = /^\d+$/;
        var codeReg = /^[0-9]{6}$/;
        var emailReg = /^[^@]+\@[^@]+$/;   // /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var telCodeReg = /^[0-9]{3,5}$/;
        var telReg = /^[0-9]{5,10}$/;

        function hasText(val) {

            if (val === telCode) {

                if (!!val) {
                    return val + '-';
                } else {
                    return ' ';
                }

            } else {
                if (!!val) {
                    return val + ' ';
                } else {
                    return ' ';
                }
            }
        }

        hasCheckedAddress = $('.exist-address-list li:not(".use-new-address")')
            .find('input[name="address"]:checked').length > 0;

        // 说明是送货到这个地址
        if (!$('.add-address').attr('checked') && hasCheckedAddress && !$('.save-btn').attr('data-id')) {

            $('.exist-address-list li').removeClass('address_select');
            $('.exist-address-list li.active').addClass('address_select')
                .find('input[name="address"]').attr('checked', 'checked');

            $('.address-list span').removeAttr('expand');
            $('#recordMobileNumb').val($('.exist-address-list li.active input').attr('data-completemobile'));
            refreshAddressStatus();

        }

        //if (!!$('.add-address').attr('checked')) {
        if (!$('.address-manage').hasClass('hide')) {

            if (!nameReg.test(name)) {
                new dialog.Alert('真实姓名至少2个中文,最多5个中文').show();
                return;
            }
            if (!addressReg.test(address)) {
                new dialog.Alert('你输入的地址格式不正确').show();
                return;
            }
            if (!phoneReg.test(phone)) {
                new dialog.Alert('你输入的联系电话格式不正确').show();
                return;
            }

            if ((!!telCode && !telCodeReg.test(telCode)) || (!!tel && !telReg.test(tel)) ||
                (telCodeReg.test(telCode) && !telReg.test(tel)) ||
                (!telCodeReg.test(telCode) && telReg.test(tel))) {
                new dialog.Alert('你输入的电话格式不正确').show();
                return;
            }

            if (!!mail && !emailReg.test(mail)) {
                new dialog.Alert('你输入的邮箱格式不正确').show();
                return;
            }

            if (!!code && !codeReg.test(code)) {
                new dialog.Alert('你输入的邮编格式不正确').show();
                return;
            }

            // 如果存在data-id说明是修改,否则是新增
            if (!!$saveBtn.attr('data-id')) {
                postData.id = $saveBtn.attr('data-id');
            }
            $('#recordMobileNumb').val(phone);

            $.ajax({
                type: 'post',
                url: '/cart/index/saveAddress',
                data: postData
            }).then(function(d) {

                var addressData;

                if (d.code === 200) {

                    addressData = d.data;

                    if (addressData.is_support === 'Y') {
                        $('.express-list input[name="carriagegroup"][value="2"]').removeAttr('disabled');
                        $('.express-list.sf').removeClass('hide');
                        $('.is-sup').addClass('hide');

                    } else {

                        $('.express-list input[name="carriagegroup"][value="2"]').attr('disabled', 'disabled');
                        $('.express-list input[name="carriagegroup"][value="1"]').attr('checked', 'checked');
                        $('.express-list.sf').addClass('hide');
                        $('.is-sup').removeClass('hide');
                    }

                    if (!!postData.id) {

                        // 修改操作接口没有返回id
                        addressStr = '<li class="active" data-id="' + postData.id +
                        '" data-express="' + addressData.is_support + '">' +
                        '<input data-name="' + postData.consignee +
                        '" data-areaCode="' + postData.areaCode +
                        '" data-addressDesc="' + postData.address +
                        '" data-mobile="' + postData.mobile +
                        '" data-phoneNum="' + postData.phoneNum +
                        '" data-phoneCode="' + postData.phoneCode +
                        '" data-zipCode="' + postData.zipCode +
                        '" data-email="' + postData.email +
                        '" checked="checked" class="radio" type="radio" name="address" />' +
                        '<label>' +
                        '<strong>' + name + '</strong>' +
                        '<span>' + provinceText + ' ' + cityText + ' ' + countyText + ' ' + address +
                        ' ' + hasText(code) + setPhoneStar(phone) + hasText(telCode) + hasText(tel) + '</span>' +
                        '<a href="javascript:void(0);" class="default-address">设为默认地址</a>' +
                        '<div class="order-modify-btn">' +
                        '<span class="address-modify">[修改]</span>' +
                        '<span class="address-del">[删除]</span>' +
                        '</div>' +
                        '</label>' +
                        '</li>';
                    } else {

                        //添加操作接口有返回id
                        addressStr = '<li class="active" data-id="' + addressData.id +
                        '" data-express="' + addressData.is_support + '">' +
                        '<input data-name="' + addressData.consignee +
                        '" data-areaCode="' + addressData.area_code +
                        '" data-addressDesc="' + addressData.address +
                        '" data-mobile="' + addressData.mobile +
                        '" data-phoneNum="' + postData.phoneNum +
                        '" data-phoneCode="' + postData.phoneCode +
                        '" data-zipCode="' + addressData.zip_code +
                        '" data-email="' + addressData.email +
                        '" checked="checked" class="radio" type="radio" name="address" />' +
                        '<label>' +
                        '<strong>' + name + '</strong>' +
                        '<span>' + provinceText + ' ' + cityText + ' ' + countyText + ' ' + address +
                        ' ' + hasText(code) + setPhoneStar(phone) + hasText(telCode) + hasText(tel) + '</span>' +
                        '<a href="javascript:void(0);" class="default-address">设为默认地址</a>' +
                        '<div class="order-modify-btn">' +
                        '<span class="address-modify">[修改]</span>' +
                        '<span class="address-del">[删除]</span>' +
                        '</div>' +
                        '</label>' +
                        '</li>';
                    }



                    // 如果dataID存在说明是修改操作
                    if (!!$this.attr('data-id')) {

                        $modifyLi = $('.exist-address-list li[data-id="' + $this.attr('data-id') + '"]');

                        $modifyLi.after($(addressStr)).remove();
                    } else {
                        $('.use-new-address').before($(addressStr));
                    }

                    $('.address-list span').removeAttr('expand');

                    refreshAddressStatus();
                } else {
                    new dialog.Alert(d.message).show();
                }
            });
        }

        if ($('.exist-address-list li').find('input[name="address"]:checked').length === 0) {
            new dialog.Alert('请选择一个地址').show();
            return;
        } else {

            if ($('.exist-address-list li').find('input[name="address"]:checked')
                    .closest('li').attr('data-express') === 'N') {
                $('.express-list input[name="carriagegroup"][value="2"]').attr('disabled', 'disabled');
                $('.express-list input[name="carriagegroup"][value="1"]').attr('checked', 'checked');
                $('.express-list.sf').addClass('hide');
                $('.is-sup').removeClass('hide');
            } else {
                $('.express-list input[name="carriagegroup"][value="2"]').removeAttr('disabled');
                $('.express-list.sf').removeClass('hide');
                $('.is-sup').addClass('hide');
            }
        }


        // 说明需要新用户引导,展开支付及送货时间
        if ($this.hasClass('new-user-btn')) {

            $('.switch-pay-modify').click();
        }

    });
};


// 支付及送货时间选择
$('.pay-time-modify .pay-btn').click(function() {

    var $this = $(this);

    var $payInfoBox = $('.modity-pay-info'),
        $payTime = $('.pay-time-modify'),
        payTypeVal = $payTime.find('input[name="pay-type"]:checked').val(),
        payTypeText = $payTime.find('input[name="pay-type"]:checked').next('label').text(),
        payTimeText = $payTime.find('input[name="pay-time-radio"]:checked').next('label').text(),
        payTimeVal = $payTime.find('input[name="pay-time-radio"]:checked').val(),
        isCall = $payTime.find('input[name="call-me"]:checked').next('label').text(),
        callMeVal = $payTime.find('input[name="call-me"]:checked').val();


    $loading.show();

    $.ajax({
        type: 'POST',
        url: '/cart/index/orderCompute',
        dataType: 'json',
        data: {
            cartType: cartType,
            paymentType: payTypeVal
        }
    }).then(function(d) {

        $loading.hide();

        if (d.code === 200) {

            $payInfoBox.find('li').eq(0).find('span').text(payTypeText);
            $payInfoBox.find('li').eq(1).find('span').text(payTimeText);
            $payInfoBox.find('li').eq(2).find('span').text(isCall);

            $('.pay-time-modify').addClass('hide')
                .find('input[name="pay-type"][value=' + payTypeVal + ']').addClass('checked')
                .end().find('input[name="pay-time-radio"][value=' + payTimeVal + ']').addClass('checked')
                .end().find('input[name="call-me"][value=' + callMeVal + ']').addClass('checked');

            $('.modity-pay-info').removeClass('hide');

            $('.switch-pay-modify').text('[修改]');

            if ($this.hasClass('new-user-pay')) {

                $('.play-piao-pan dd').css({

                    display: 'block'
                });
                $('.play-piao-pan dt').addClass('active');
            }

        } else {
            new dialog.Alert(d.message).show();
        }
    });
});

function getYohoBiList() {

    var $juanUl = $('.play-juan ul'),
        juanStr = '',
        dLen,
        data,
        i;

    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/cart/index/getCouponList'
    }).then(function(d) {

        if (d.code === 200) {
            data = d.data;
            dLen = data.length;

            for (i = 0; i < dLen; i++) {
                juanStr += '<li>' +
                '<input name="juangroup" type="radio" value="' +
                data[i].code + '" ' + (data[i].valid ? '' : 'disabled="disabled"') + '>' +
                '<label>' + data[i].desc + '</label>' +
                '</li>';
            }

            $juanUl.prepend($(juanStr));

        }
    });
}
getYohoBiList();

// 把Y、N转换成bool

function ytoTrue(val) {

    if (val === 'N') {
        return false;
    } else {
        return true;
    }
}


// 去支付
$('.to-play input.submit').click(function() {

    var $this = $(this);

    var addressId = $('.exist-address-list input[name="address"]:checked').closest('li').attr('data-id'),
        deliveryTimeId = $('.pay-time-modify input[name="pay-time-radio"]:checked').val(),
        deliveryWayId = $('.select-express input[name="carriagegroup"]:checked').val(),
        invoiceType,
        invoiceContent,
        receiverMobile,
        invoiceTitle,
        paymentType = $('.pay-time-modify input[name="pay-type"]:checked').val(),
        paymentId = $('.pay-time-modify input[name="pay-type"]:checked').data('pay'),
        remark = $('#notedesc').val(),
        yohoCoin = $('#biprice').val(),
        isPreContact = $('.pay-time-modify input[name="call-me"]:checked').val(),
        isPrintPrice = $('.play-pan input[name="isPP"]:checked').val(),
        redEnvelopes = $('.use-envelopes input:checked').attr('data-use'),
        $juangroupInput = $('.play-juan.is-select input[name="juangroup"]:checked'),
        codeVal = $juangroupInput.val(),
        juanCode = !!codeVal ? codeVal : $juangroupInput.closest('li').find('#juancode').val();

    if ($invoiceCheck.hasClass('active')) {
        invoiceType = $('.invoice-upload-type').html();
        invoiceContent = $('.invoice-upload-content').html();
        receiverMobile = $('.invoice-upload-mobile').html();
        invoiceTitle = $('.invoice-upload-title').html();
    }

    $loading.show();

    if (!!$this.attr('disabled')) {
        return;
    }

    $this.attr('disabled', 'disabled');

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/cart/index/orderSub',
        data: {
            addressId: addressId,
            cartType: cartType,
            deliveryTimeId: deliveryTimeId,
            deliveryWayId: deliveryWayId,
            invoiceType: invoiceType,
            invoiceContent: invoiceContent,
            receiverMobile: receiverMobile,
            invoiceTitle: invoiceTitle,
            paymentId: paymentId,
            paymentType: paymentType,
            remark: remark,
            couponCode: juanCode,
            yohoCoin: yohoCoin,
            isPreContact: ytoTrue(isPreContact),
            isPrintPrice: ytoTrue(isPrintPrice),
            redEnvelopes: redEnvelopes
        }
    }).then(function(d) {

        $loading.hide();

        if (d.code === 200) {
            window.location.href = d.data.payUrl;
        } else {
            new dialog.Alert(d.message).show();
        }
    }).always(function() {
        $this.removeAttr('disabled');
    });
});