ensure.js 33.2 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 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
/**
 * @description: 订单保存
 * @time: 2015/12/21
 */

var $ = require('yoho.jquery'),
    $invoiceCheck = $('.invoice'),
    $receiptInfo = $('.receipt-info'),
    $receiptInfoSaveBtn = $('.receipt-info .save-receipt'),
    $receiptInfoCancelBtn = $('.receipt-info .cancel-receipt'),
    $eReceiptBtn = $('.receipt-type .e-receipt'),
    $paperReceiptBtn = $('.receipt-type .paper-receipt'),
    $yohoCoinHelp = $('.yoho-coin-help');


var address = require('./address'),
    receiptTitle = "个人",
    receiptMobile = $('#recordMobileNumb').val(),
    receiptMobileStar;

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

var accMul = function(arg1, arg2) {
    var m = 0,
    s1 = arg1.toString(),
    s2 = arg2.toString();

    try {
        m += s1.split('.')[1].length;
    } catch (e) {
    }
    try {
        m += s2.split('.')[1].length;
    } catch (e) {
    }
    return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
};

var Order = {
    /*业务数据(人民币)*/
    Data: {
        product: 0, //商品价格
        activity: 0, //活动
        carriage: 0, //邮费
        juan: 0, //优惠卷
        yohocoin: 0 //有货币
    },
    /*view 交互*/
    UI: {
        e: {
            $pan: $('.pan'),
            $jc: $('#juancode'),
            $bp: $('#biprice'),
            $statistics: $('li', '.play-total'),
            $tobi: $('p em', '.play-bi-pan .play-pan'),
            $cancel: $('.cancel', '.btn-group'),
            $total: $('em', '.to-play'),
            $tbody: $('table tbody', '.pay-wapper'),

            //请填写并核对一下信息
            $orderSelectionSave: $('.address-list h2 span'),
            $addAddress: $('.add-address'),
            $addressManage: $('.address-manage'),
            $spanPayTypeLegend: $('span.pay-type-legend'),
            $supportType: $('.support-type'),
            $existAddressListWrap: $('.exist-address-list'),
            $existAddressList: $('.exist-address-list').find('li'),
            $existAddress: $('.exist-address-list').find('input[name="exist-address"]'),
            $payTimeModify: $('.pay-time-modify'),
            $switchPayModify: $('.switch-pay-modify'),
            $modityPayInfo: $('.modity-pay-info'),
            $payTotal: $('.play-total'),
            $toPay: $('.to-play em'),
            $redEnvelopes: $('.red-envelopes'),
            $useEnvelopes: $('.use-envelopes'),
            $hasEnvelopes: $('.has-envelopes span'),
            $useEnvelopesInput: $('.use-envelopes').find('input'),
            $expressFee: $('.express-fee')
        },
        init: function() {
            var e = this.e,
                    d = Order.Data,
                    c = Order.Common;

            //初始化产品价格(表格中的价格)
            e.$tbody.find('tr').each(function(index) {

                var td = $(this).find('td');
                var len = e.$tbody.find('tr').length;
                var subtotal = parseFloat(td.eq(1).html()) * parseInt(td.eq(3).html());

                td.eq(1).html('¥' + td.eq(1).html());

                if (td.hasClass('xforone')) {
                    td.eq(4).find('del').html('¥' + c.strFixed(subtotal));
                    subtotal = 0;
                } else {
                    td.eq(4).html('¥' + c.strFixed(subtotal));
                }
                d.product += subtotal;
                if (!index) {
                    td.last().attr('rowspan', len);
                } else {
                    td.last().remove();
                }
                if (index === len - 1) {
                    e.$tbody.find('tr').eq(0).find('td').last().html('<strong>¥' + c.strFixed(d.product) + '</strong>');
                }
            });

            // 使用优惠券、使用有货币点击展开
            e.$pan.find('dt').click(function() {

                var pan = $(this).parent('dl.pan');

                if ($(this).hasClass('not-btn')) {
                    return false;
                }

                if (pan.find('dd').is(':hidden')) {
                    c.winShow(pan);

                    $(this).addClass('active');
                } else {
                    c.winHide(pan);

                    $(this).removeClass('active');
                }

            });

            //请填写并核对一下信息
            e.$orderSelectionSave.click(function() {
                var i = 0,
                        $addressLi = $('.exist-address-list li'),
                        liLen = $addressLi.length,
                        notSelect = 0;

                if (!$(this).attr('expand')) {

                    $(this).attr('expand', 'expand').text('[不保存并关闭]');

                    $('.exist-address-list li').removeClass('hide');

                    $('.save-btn').removeClass('hide');

                    e.$existAddressListWrap.addClass('address-expand');

                    if (liLen === 1) {
                        e.$addressManage.removeClass('hide');
                    }

                } else {

                    for (i = 0; i < liLen; i++) {

                        if ($addressLi.eq(i).hasClass('address_select')) {
                            notSelect++;
                        }

                        if (!$addressLi.eq(i).hasClass('address_select')) {
                            $addressLi.eq(i).addClass('hide');
                        }
                    }

                    if (notSelect === 0) {
                        $('.exist-address-list li:not(".use-new-address")').removeClass('hide');

                        $('.use-new-address input').removeAttr('checked');
                    }

                    $('.address-list-inner li.address_select input').attr('checked', 'checked');
                    $('.address-list-inner li').removeClass('active');
                    $('.address-list-inner li.address_select').addClass('active');

                    $(this).removeAttr('expand').text('[修改]');

                    $('.save-btn').addClass('hide');

                    e.$existAddressListWrap.removeClass('address-expand');

                    e.$addressManage.addClass('hide');
                }

            });


            //支付及送货时间
            e.$switchPayModify.click(function() {

                if (e.$payTimeModify.hasClass('hide')) {
                    e.$payTimeModify.removeClass('hide');
                    e.$modityPayInfo.addClass('hide');
                    $(this).text('[不保存并关闭]');
                } else {
                    e.$payTimeModify.addClass('hide');
                    e.$modityPayInfo.removeClass('hide');
                    $(this).text('[修改]');

                    e.$payTimeModify.find('input[name="pay-type"]').filter('.checked').attr('checked', 'checked');
                    e.$payTimeModify.find('input[name="pay-time-radio"]').filter('.checked').attr('checked', 'checked');
                    e.$payTimeModify.find('input[name="call-me"]').filter('.checked').attr('checked', 'checked');
                }
            });

            e.$spanPayTypeLegend.click(function() {
                $(this).toggleClass('active');

                e.$supportType.toggleClass('hide');
            });

            $(document).on('click', '.exist-address-list li', function() {

                $('.exist-address-list li').removeClass('active');

                $(this).addClass('active');

                $(this).find('input').attr('checked', 'checked');

                if ($(this).hasClass('use-new-address')) {
                    e.$addressManage.removeClass('hide');

                    $('.address-manage input').val('');

                    $('.save-btn').removeAttr('data-id');

                    // 0表示获取所有省的列表,'000000'表示无匹配的区域码
                    address.newAddress(0, '000000');
                }
            });
        },

        //渲染价格明细
        render: function(data, tips) {

            // 下面这段是在服务端算价格
            var e = this.e,
                    i,
                    dArr = data.promotion_formula_list,
                    packageList = data.package_list,
                    str = '',
                    isExpress = '';

            var tipsStr;

            var envelopesVal;

            if (!!tips) {
                tipsStr = tips;
            } else {
                tipsStr = '';
            }

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

                if (dArr[i].promotion === '优惠券:') {
                    dArr[i].promotion = (tipsStr ? tipsStr : '优惠券') + '<span class="juan-modify">[修改]</span>:';
                }

                if (dArr[i].promotion === '有货币:') {
                    dArr[i].promotion = '有货币<span class="bi-modify">[修改]</span>:';
                }

                if (dArr[i].isExpress) {
                    isExpress = 'total-express-w';
                } else {
                    isExpress = '';
                }

                str += '<li class="' + isExpress + '">' +
                        '<label>' + dArr[i].promotion + '</label>' +
                        '<em>' + dArr[i].promotion_amount + '</em>' +
                        '</li>';
            }

            e.$payTotal.html(str);
            e.$toPay.html((data.last_order_amount).toFixed(2));

            //有货币清零状态更新
            $('dl.play-bi-pan').find('dt').removeClass('not-btn');
            $('.not-btn-tip').addClass('hide');
            if (!data.yohoCoinCompute.yohoCoinClick) {
                //不可以使用状态
                $('dl.play-bi-pan').find('dt').addClass('not-btn');
                $('.not-btn-tip').removeClass('hide');
            }
            $('#biprice').data('.yoho-coin-click', data.yohoCoinCompute.yohoCoinClick);
            $('#biprice').find('.num-limit-coin').html(data.yohoCoinCompute.yoho_coin_pay_rule.num_limit);
            $('#biprice').find('.totalYohoCoinNum').html(data.yohoCoinCompute.totalYohoCoinNum);
            $('#biprice').find('.yohoCoinNum').html(data.yohoCoinCompute.yohoCoinNum);
            $('#biprice').find('.yohoCoin').html(data.yohoCoinCompute.yohoCoin);

            //使用的红包
            envelopesVal = e.$useEnvelopesInput.data('all') - data.use_red_envelopes;
            if (~~data.use_red_envelopes > 0) {
                e.$useEnvelopes.find('span').html('-¥' + data.use_red_envelopes.toFixed(2));
                e.$useEnvelopes.find('input').attr('data-use', data.use_red_envelopes);
            } else {
                e.$useEnvelopes.find('span').html('-¥0.00');
                e.$hasEnvelopes.html('¥' + (e.$useEnvelopesInput.attr('data-all') * 1).toFixed(2));
                e.$redEnvelopes.removeClass('active');
                e.$useEnvelopesInput.prop('checked', false);
            }
            e.$hasEnvelopes.html('¥' + envelopesVal.toFixed(2));

            if (packageList) {
                e.$expressFee.each(function(index, expressCost) {
                    var orignCost = packageList[index].shopping_orig_cost,
                        disCount = packageList[index].shopping_cut_cost,
                        realCost = packageList[index].shopping_cost;

                    var cost = parseInt(orignCost) === 0 ?
                        '运费:¥0.00元' :
                        '运费:¥' + realCost + '元(原价' + orignCost + '元,优惠' + disCount + ')元';

                    $(expressCost).text(cost);
                });
            }
        }
    },
    /*公共方法*/
    Common: {
        enterNUM: function(str1, str2) {
            str1 = str1.replace(/[^\d]/g, '');
            if (str1 && str2) {
                str1 = parseInt(str1);
                str2 = parseInt(str2);
                if (str1 > str2) {
                    str1 = str2;
                }
            }
            return str1;
        },
        calucate: function() {
            var p = Order.Data;

            $.each(Order.Data, function(key, index) {
                p[key] = parseFloat(Order.Data[key]);
            });
            return p.product - p.activity + p.carriage - p.juan - p.yohocoin;
        },
        winShow: function(pan) {
            pan.find('dt').show();
            pan.find('dd').show();
            if (pan.attr('class').match(/(-bi-)|(-juan-)/g)) {
                pan.find('dt').hide();
            }
        },
        winHide: function(pan) {
            pan.find('dt').show();
            pan.find('dd').hide();

            /*if (pan.hasClass('play-juan-pan') && $('.juan-modify').length > 0) {
             pan.find('dt').hide();
             }

             if (pan.hasClass('play-bi-pan') && $('.bi-modify').length > 0) {
             pan.find('dt').hide();
             }*/
        },
        strFixed: function(str) {
            return parseFloat(str).toFixed(2);
        }
    },
    /*业务逻辑*/
    Bll: {
        e: {
            $juanok: $('#juansubmit'),
            $juantip: $('.errtip', '.play-juan-pan'),
            $biok: $('#bisubmit'),
            $carriageGroup: $('input[name="carriagegroup"]'),
            $selectExpress: $('.express-list input'),
            $bp: $('#biprice'),
            $errorTip: $('.errbitip'),
            $useEnvelopes: $('.use-envelopes'),
            $hasEnvelopes: $('.has-envelopes span'),
            $useEnvelopesInput: $('.use-envelopes').find('input'),
            $redEnvelopes: $('.red-envelopes'),
            $loading: $('.loading')
        },
        init: function() {
            var e = this.e,
                    u = Order.UI,
                    options,
                    c = Order.Common,
                    envelopesVal;

            function getSelectData(callback) {

                var $juangroupInput = $('.play-juan.is-select input[name="juangroup"]:checked');
                var expressVal = $('.express-list input[name="carriagegroup"]:checked').val();
                var biVal = $('#biprice.is-select').data('yoho-coin-num') * 1;
                var codeVal = $juangroupInput.val();
                var cartType = $('.address-manage').attr('cart-type');
                var redEnvelopes = $('.use-envelopes input:checked').attr('data-use');
                var resultData;

                codeVal = codeVal ? codeVal : $juangroupInput.closest('li').find('#juancode').val();

                resultData = {
                    cartType: cartType,
                    deliveryWay: expressVal,
                    couponCode: codeVal,
                    yohoCoin: biVal

                    //redEnvelopes: e.$useEnvelopesInput.length > 0 ? ~~e.$hasEnvelopes.text().substring(1) : '0'
                };

                if (!!redEnvelopes) {
                    resultData.redEnvelopes = redEnvelopes;
                }

                callback(resultData);

            }

            // 选择快递
            e.$selectExpress.click(function() {

                var expressId = $(this).val();

                e.$loading.show();

                getSelectData(function(d) {

                    d.deliveryWay = expressId;

                    options = {
                        type: 'POST',
                        url: '/cart/index/orderCompute',
                        data: d,
                        success: function(data) {
                            e.$loading.hide();

                            if (data.code === 200) {
                                u.render(data.data);

                                if (expressId === '1') {

                                    $('.express-list.sf').addClass('hide');
                                } else if (expressId === '2') {
                                    $('.express-list.sf').removeClass('hide');
                                }
                            } else {
                                new dialog.Alert(data.message).show();
                            }
                        }
                    };

                    $.ajax(options);
                });
            });

            //点击 优惠卷支付 确认按钮
            e.$juanok.click(function() {

                var pan = $(this).parents('dl.pan');

                var juanText = '';

                $('.play-juan').addClass('is-select');

                getSelectData(function(d) {

                    var code = d.couponCode;

                    if (!code) {

                        e.$juantip.html('请选择或填写一个优惠券');
                        $('.play-juan').removeClass('is-select');

                    } else {

                        e.$loading.show();


                        options = {
                            type: 'POST',
                            url: '/cart/index/orderCompute',

                            data: d,
                            success: function(data) {

                                e.$loading.hide();

                                if (data.code === 200) {

                                    juanText = $('.play-juan input[value="' + code + '"]').next().text();
                                    u.render(data.data, juanText);

                                    //$('#juancode').val(code);

                                    c.winHide(pan);
                                    e.$juantip.html('');

                                    //$('.play-juan-pan dt').hide();

                                } else {
                                    e.$juantip.html(data.message);
                                    $('.play-juan').removeClass('is-select');
                                }
                            }
                        };

                        $.ajax(options);
                    }
                });

            });

            // 优惠券修改
            $(document).on('click', '.juan-modify', function() {
                c.winShow($('.play-juan-pan'));
            });

            e.$biok.click(function() {
                var pan = $(this).parents('dl.pan');
                var juanText;

                if (!$('#biprice').data('yoho-coin-click')) {
                    $('.errbitip').html('抱歉,您的订单实付款不满足有货币使用条件');
                    return true;
                }

                $('#biprice').addClass('is-select');

                getSelectData(function(d) {

                    if (!!d.couponCode) {
                        juanText = $('.play-juan input[value="' + d.couponCode + '"]').next().text();
                    }

                    if (d.yohoCoin * 1 <= 0) {
                        $('.errbitip').html('请输入一个正整数');
                        $('#biprice').removeClass('is-select');
                        return false;
                    }

                    e.$loading.show();
                    options = {
                        type: 'POST',
                        url: '/cart/index/orderCompute',
                        data: d,
                        success: function(data) {

                            e.$loading.hide();

                            if (data.code === 200) {

                                u.render(data.data, juanText);
                                c.winHide(pan);

                            } else {
                                $('.errbitip').html(data.message);
                                $('#biprice').removeClass('is-select');
                            }
                        }
                    };
                    $.ajax(options);

                });
            });

            // 修改有货币
            $(document).on('click', '.bi-modify', function() {


                var $pan = $('.play-bi-pan');

                c.winShow($pan);
            });

            // 取消yoho 币
            $('.cancel-bi').click(function() {


                var $pan = $('.play-bi-pan');
                var juanText;

                $('.errbitip').html('');

                if ($('#biprice').hasClass('is-select')) {

                    e.$loading.show();

                    getSelectData(function(d) {

                        d.yohoCoin = 0;

                        if (!!d.couponCode) {
                            juanText = $('.play-juan input[value="' + d.couponCode + '"]').next().text();
                        }

                        options = {
                            type: 'POST',
                            url: '/cart/index/orderCompute',
                            data: d,
                            success: function(data) {

                                e.$loading.hide();

                                if (data.code === 200) {
                                    u.render(data.data, juanText);
                                    c.winHide($pan);

                                    $('#biprice').val('0');

                                    //$('.play-bi-pan dt').hide();
                                    $('#biprice').removeClass('is-select');

                                    $('.errbitip').html('');

                                    // $('.use-envelopes input').attr('checked', 'checked');
                                    // $('.red-envelopes').addClass('active');

                                } else {
                                    $('.errbitip').html(data.message);
                                }
                            }
                        };

                        $.ajax(options);
                    });

                } else {
                    c.winHide($pan);
                    $('#biprice').val('0');
                }

            });

            // 取消优惠券
            $('.cancel-code').click(function() {

                var $pan = $('.play-juan-pan');

                if ($('.play-juan').hasClass('is-select')) {

                    e.$loading.show();

                    getSelectData(function(d) {

                        d.couponCode = '';

                        options = {
                            type: 'POST',
                            url: '/cart/index/orderCompute',
                            data: d,
                            success: function(data) {

                                e.$loading.hide();

                                if (data.code === 200) {
                                    u.render(data.data);

                                    c.winHide($pan);
                                    $('.play-juan').removeClass('is-select');

                                    $('#juancode').val('')
                                            .siblings('input[name="juangroup"]').attr('checked', 'checked');

                                    // $('.use-envelopes input').attr('checked', 'checked');
                                    // $('.red-envelopes').addClass('active');

                                } else {
                                    e.$juantip.html(data.message);
                                }
                            }
                        };

                        $.ajax(options);
                    });
                } else {
                    c.winHide($pan);
                }
            });


            // 红包
            envelopesVal = e.$useEnvelopesInput.data('all') - e.$useEnvelopesInput.data('use'); // 剩余的红包
            e.$hasEnvelopes.text('¥' + envelopesVal.toFixed(2));
            e.$useEnvelopes.find('input').click(function() {

                var $this = $(this),
                        juanText;

                e.$loading.show();

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

                getSelectData(function(d) {

                    if (!!$this.attr('checked')) {

                        //d.redEnvelopes = $this.val();
                        d.redEnvelopes = $this.data('use');

                    } else {

                        d.redEnvelopes = 0;
                    }

                    juanText = $('.play-juan input[value="' + d.couponCode + '"]').next().text();

                    options = {
                        type: 'POST',
                        url: '/cart/index/orderCompute',
                        data: d,
                        success: function(data) {
                            e.$loading.hide();

                            if (data.code === 200) {
                                u.render(data.data, juanText);

                                $this.removeAttr('disabled');

                                if (!!$this.attr('checked')) {
                                    e.$redEnvelopes.addClass('active');
                                } else {
                                    e.$useEnvelopes.find('span').html('-¥0.00');
                                    e.$hasEnvelopes.html('¥' + ($this.attr('data-all') * 1).toFixed(2));
                                    e.$redEnvelopes.removeClass('active');
                                }
                            } else {

                                new dialog.Alert(data.message).show();
                            }
                        }
                    };

                    $.ajax(options);
                });

            });


            //发票抬头和备注字数限制
            function limitStrLen($obj, len) {

                var $this = $obj;

                $this.keyup(function() {

                    if ($this.val().length > len) {

                        $this.val($this.val().substr(0, len));
                    }
                });
            }

            limitStrLen($('#piaodesc'), 30);
            limitStrLen($('#notedesc'), 100);
        }
    }
};

Order.Data.activity = 0;
Order.Data.carriage = 0;

Order.UI.init();
Order.Bll.init();

address.getUserAddressList();

// 0表示获取所有省的列表,'000000'表示无匹配的区域码
address.newAddress(0, '000000');
address.modifyAddress();
address.saveAddress();

//不同订单包裹提示

$('.why').click(function() {
    $('#differentBag').show();
    return false;

});
$('#differentBag').click(function() {
    return false;
});

$(document).click(function() {
    $('#differentBag').hide();
});

//订单包裹左右切换
function lunBo($pre, $next, $ul, $iWidth) {
    var i = 1,
            j = 1;

    var iSpeed = 0,
            isMoving = false;

    $pre.click(function() {
        if (isMoving) {
            return;
        }

        if ($ul.css('left') === '0px') {
            iSpeed = 0;

        } else {
            iSpeed += j * $iWidth;
            isMoving = true;
        }
        $ul.stop().animate({
            left: iSpeed
        }, 300, function() {
            isMoving = false;
        });
    });
    $next.click(function() {
        var $largeLeft = $ul.parent().width() - $ul.width() + 'px';

        if (isMoving) {
            return;
        }

        if ($ul.css('left') === $largeLeft) {
            $ul.css('left', $largeLeft);
        } else {
            iSpeed += -i * $iWidth;
            isMoving = true;
        }

        $ul.stop().animate({
            left: iSpeed
        }, 300, function() {
            isMoving = false;
        });
    });
}



//订单分类
$('.bag').find('ul').each(function() {
    var $pre = $(this).parent().parent().find('.pre');
    var $next = $(this).parent().parent().find('.next');

    var $iWidth = $(this).find('li').eq(0).width();
    var $length = $(this).find('li').size();
    var $ulWidth = $iWidth * $length;

    $(this).css('width', $ulWidth);
    if ($length > 5) {
        $pre.show();
        $next.show();
        lunBo($pre, $next, $(this), $iWidth);
    } else {
        $pre.hide();
        $next.hide();
    }
});

$invoiceCheck.click(function(){

    receiptMobile = $('#recordMobileNumb').val(),
    (function receiptMobileToStarString(numb) {
        receiptMobileStar = numb.substr(0, 3) + '****' + numb.substr(7);
        $('#input-mobile').val(receiptMobileStar);
    }(receiptMobile));
    if ($invoiceCheck.hasClass('active')) {
        $invoiceCheck.removeAttr('checked');
        $invoiceCheck.removeClass('active');
        $('.section-play .invoice-content-outter').addClass('hide');
    }

    if ($invoiceCheck.attr('checked')) {
        $invoiceCheck.addClass('active');
        if ($('.invoice-content-outter').hasClass('hide')) {
            $receiptInfo.removeClass('hide');
        }
    }
});


//电子发票

function saveReceiptInfo() {
    var passInfoVerify = true,
        receiptType,
        receiptContent,
        verifymobile,
        receiptTypeID,
        invoiceTypeInt,
        finalReceiptMobile;

    if ($eReceiptBtn.hasClass('receipt-type-selected')) {
        receiptType = "电子发票";
        invoiceTypeInt = '2';
    } else {
        receiptType = "纸质发票";
        invoiceTypeInt = '1';
    }

    if (receiptTitle === "单位") {
        $('.input-line').each(function(){
            if ($(this).find('input').val() === '') {
                $(this).find('.enpty-input').removeClass('hide');
                passInfoVerify = false;
            };
        })
    };

    finalReceiptMobile = $('#input-mobile').val();
    if (finalReceiptMobile === receiptMobileStar) {
        finalReceiptMobile = receiptMobile;
    } else {
        verifymobile = /[1][34578][0-9]{9}/;
        if (!verifymobile.test(finalReceiptMobile)) {
            $('.invoice-phone .enpty-input').removeClass('hide');
            if (finalReceiptMobile.length === 0) {
                $('.invoice-phone .enpty-input').html('&#xe63e; 请填写手机号码');
            } else {
                $('.invoice-phone .enpty-input').html('&#xe63e; 手机号码不正确');
            }
            passInfoVerify = false;
        }
    }

    if (!passInfoVerify) {
        return;
    } else {
        $('.invoice-title .enpty-input').addClass('hide');
        $('.invoice-phone .enpty-input').addClass('hide');
        if (receiptTitle === "单位") {
            receiptTitle = $('#input-organization-name').val();
        }
    }

    receiptContent = $('input[name="content"]:checked').val();
    receiptTypeID = $('input[name="content"]:checked').data('receiptid');

    $('.invoice-type').html(receiptType);
    $('.invoice-partment').html(receiptTitle);
    $('.invoice-kind').html(receiptContent);

    $('.invoice-upload-type').html(invoiceTypeInt);
    $('.invoice-upload-content').html(receiptTypeID);
    $('.invoice-upload-mobile').html(finalReceiptMobile);
    $('.invoice-upload-title').html(receiptTitle);

    $receiptInfo.addClass('hide');
    $('.invoice-content-outter').removeClass('hide');
}

function rejustReceiptInfo() {
    var inviceType = $('.invoice-type').html(),
        invoicePartment = $('.invoice-partment').html(),
        invoiceKind = $('.invoice-kind').html(),
        invoiceMobile,
        invoiceMobileStar;
    if (inviceType === "电子发票") {
        $eReceiptBtn.trigger("click");
    } else {
        $paperReceiptBtn.trigger("click");
    }

    if (invoicePartment === "个人") {
        $("input[name='title']").eq(0).attr("checked","checked");
        $('.invoice-title').addClass('invoice-title-personal-hight');
        $('.invoice-title .input-line').addClass('hide');
    } else {
        $("input[name='title']").eq(1).attr("checked","checked");
        $('.invoice-title').removeClass('invoice-title-personal-hight');
        $('.invoice-title .input-line').removeClass('hide');
        $('#input-organization-name').val(invoicePartment);
    }

    $("input[name='content']").each(function(){
        if ($(this).attr('value') === invoiceKind) {
            $(this).attr("checked","checked");
        };
    })

    invoiceMobile = $('.invoice-upload-mobile').html()
    invoiceMobileStar = invoiceMobile.substr(0, 3) + '****' + invoiceMobile.substr(7);

    if ($('#input-mobile').val() !== invoiceMobileStar) {
        $('#input-mobile').val(invoiceMobileStar);
    };        
}

$receiptInfoSaveBtn.click(function(){
    saveReceiptInfo();
});

$receiptInfoCancelBtn.click(function(){
    $receiptInfo.addClass('hide');

    if ($('.invoice-content-outter').hasClass('hide')) {
        $invoiceCheck.removeAttr('checked');
        $invoiceCheck.removeClass('active');
    } else {

    }
});

$('.revise').click(function(){
    $receiptInfo.removeClass('hide');
    rejustReceiptInfo();
});

$eReceiptBtn.click(function(){
    $eReceiptBtn.addClass('receipt-type-selected');
    $paperReceiptBtn.removeClass('receipt-type-selected');
    $('.e-receipt-desc').removeClass('hide');
    $('.invoice-phone').removeClass('hide');
});

$paperReceiptBtn.click(function(){
    $paperReceiptBtn.addClass('receipt-type-selected');
    $eReceiptBtn.removeClass('receipt-type-selected');
    $('.e-receipt-desc').addClass('hide');
    $('.invoice-phone').addClass('hide');
});

$('.invoice-title').change(function(){
    var selectType = $('input[name="title"]:checked').val();
    if (selectType === "1") {
        receiptTitle = "个人";
        $('.invoice-title').addClass('invoice-title-personal-hight');
        $('.invoice-title .input-line').addClass('hide');
    } else {
        receiptTitle = "单位";
        $('.invoice-title').removeClass('invoice-title-personal-hight');
        $('.invoice-title .input-line').removeClass('hide');
    }
})

$('#input-organization-name').bind('input propertychange', function() {
    if ($(this).val().length >= 1) {
        $('.invoice-title .enpty-input').addClass('hide');
    }
});

$('#input-mobile').bind('input propertychange', function() {
    if ($(this).val().length >= 1) {
        $('.invoice-phone .enpty-input').addClass('hide');
    }
});

//有货币帮助弹框
$yohoCoinHelp.hover(function() {
    $(this).find('.yoho-coin-help-dialog').removeClass('hide');
}, function() {
    $(this).find('.yoho-coin-help-dialog').addClass('hide');
});