Blame view

public/js/cart/order-ensure.js 16.7 KB
lijing authored
1 2 3 4 5
/**
 * 订单确认
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/12
 */
陈峰 authored
6
require('scss/cart/order-ensure.page.scss');
郭成尧 authored
7 8 9 10 11
const $ = require('yoho-jquery');
const lazyLoad = require('yoho-jquery-lazyload');
const cookie = require('yoho-cookie');
const qs = require('yoho-qs');
const OrderEnsure = require('./cartbuynow/order-ensure');
lijing authored
12
陈峰 authored
13 14
let tip = require('js/plugin/tip'),
    loading = require('js/plugin/loading'),
lijing authored
15
    order = require('./order-info'),
陈峰 authored
16 17
    richTip = require('js/plugin/rich-tip'),
    dialog = require('js/plugin/dialog');
lijing authored
18
lijing authored
19
let $invoice = $('.invoice'),
lijing authored
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    $addressWrap = $('.address-wrap'),
    $coinCheck = $('.coin-check'),
    $coinLi = $('li.coin'),
    $subBlock = $('.sub-block'),
    $ticketsMobile = $('#mobile'),
    payType,
    queryString = $.queryString(),
    orderInfo = order.orderInfo,
    isSubmiting,
    dispatchInfo,
    total,
    isTickets = $('#ticketsPage').val(),
    productSku = $('#productSku').val(),
    buyNumber = $('#buyNumber').val(),
    headerTop = $('#yoho-header').outerHeight(),
    isYohoCoinClick = $coinLi.data('yoho-coin-click') * 1, // 判断有货币是否可以单击
郭成尧 authored
36 37
    addressTop = $('.address-wrap').outerHeight(),
    $message = $('#msg'),
张孝茹 authored
38 39
    $noPrintPrice = $('.no-print-price'),
    delivery = 0;
lijing authored
40
郭成尧 authored
41
const orderEnsure = new OrderEnsure(order);
lijing authored
42
郭成尧 authored
43 44 45 46 47
// cookie 参数
const actCkOpthn = {
    path: '/cart/index'
};
陈峰 authored
48
require('js/common');
lijing authored
49 50 51 52

lazyLoad();

function isLimitGood() {
郭成尧 authored
53
    return qs.limitproductcode;
lijing authored
54 55 56
}

// 来自购物车的链接默认不使用优惠券
郭成尧 authored
57
if (cookie.get('ensure_from') === 'cart') {
郭成尧 authored
58
    orderInfo('coupon_code', null);
59 60
    orderInfo('usable_usual_code', null);
    orderInfo('usable_free_code', null);
郭成尧 authored
61
    cookie.removeSpecific('ensure_from', actCkOpthn);
lijing authored
62 63
}
郭成尧 authored
64 65 66
// 清除立即购买的 cookie,以免优惠券数据冲突
cookie.removeSpecific('buynow_info', actCkOpthn);
lijing authored
67
isLimitGood() && (function() {
lijing authored
68
    let a = [];
lijing authored
69
郭成尧 authored
70
    let data = qs;
lijing authored
71 72 73 74 75 76 77 78 79 80 81 82 83

    data.type = 'limitcode';

    a.push(data);
    orderInfo('skuList', JSON.stringify(a));
    orderInfo('limitUrlSufix', location.search);
}());

if (queryString.cartType || queryString.carttype || !orderInfo('cartType')) {
    orderInfo('cartType', queryString.cartType || queryString.carttype || 'ordinary');
}

$('.checkbox').on('touchstart', function(e) {
lijing authored
84
    let $this = $(this);
lijing authored
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    if ($(e.target).closest('.coin-check').length && !isYohoCoinClick) {
        return true;
    }

    if ($this.hasClass('icon-cb-radio')) {
        $this.removeClass('icon-cb-radio').addClass('icon-radio');
        return;
    }

    if ($this.hasClass('icon-radio')) {
        $this.removeClass('icon-radio').addClass('icon-cb-radio');
    }
});

function updateDeliverId(id) {
lijing authored
101
    let $moreJit = $('.more-jit a').get(0),
lijing authored
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        url;

    if ($moreJit) {
        url = $moreJit.href;
    } else {
        return;
    }

    if (url.indexOf('deliveryId') < 0) {
        $moreJit.href = url + '&deliveryId=' + id;
    } else {
        $moreJit.href = url.replace(/deliveryId=(\d)/, 'deliveryId=' + id);
    }
}

function orderCompute() {
lijing authored
118
    let yohoCoin = orderInfo('yohoCoin'),
lijing authored
119 120 121 122 123
        deliverId = orderInfo('deliveryId'),
        data = {
            cartType: orderInfo('cartType') || 'ordinary',
            deliveryId: orderInfo('deliveryId'),
            paymentType: orderInfo('paymentType'),
郭成尧 authored
124
            couponCode: orderInfo('coupon_code'),
郭成尧 authored
125
            gift_card_code: orderInfo('gift_card_code'),
lijing authored
126
            yohoCoin: yohoCoin,
lijing authored
127
            skuList: isLimitGood() ? orderInfo('skuList') : void 0
lijing authored
128 129 130 131 132 133 134 135
        };

    // 门票
    if (isTickets) {
        data = {
            type: 'tickets',
            productSku: productSku,
            buyNumber: buyNumber,
郭成尧 authored
136 137
            yohoCoin: yohoCoin,
            gift_card_code: orderInfo('gift_card_code')
lijing authored
138 139 140 141 142 143 144 145 146 147 148 149
        };
    }

    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/cart/index/new/orderCompute',
        data: data
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            window.location.reload();
        } else {
lijing authored
150
            if (typeof res.last_order_amount !== 'undefined') {
lijing authored
151 152 153 154 155 156 157
                res.last_order_amount = (+res.last_order_amount).toFixed(2);
            }

            if (res.use_yoho_coin) {
                $coinCheck.find('em').html('- ¥ ' + res.use_yoho_coin);
                $coinCheck.find('em').show();
            }
郭成尧 authored
158 159 160
            if (res.gift_card) {
                $('#giftCardRightInfo').html('可以抵用¥' + res.gift_card.amount.toFixed(2));
            }
lijing authored
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            $coinLi.find('.msg').html(res.yohoCoinCompute.yohoCoinMsg);
            isYohoCoinClick = res.yohoCoinCompute.yohoCoinClick * 1;
            $('.coin').data('yoho-coin', res.yohoCoinCompute.yohoCoin);

            total = '';
            if (res.promotion_formula_list) {
                $.each(res.promotion_formula_list, function(index, value) {
                    total += '<li>' +
                        '<p>' + value.promotion + '</p>' +
                        '<span>' + value.promotion_amount + '</span>' +
                        '</li>';
                });
                $('.price-cost span').html('¥' + res.last_order_amount);
                $('.bill span').html('¥' + res.last_order_amount);
                $('.total').html(total);
郭成尧 authored
176 177
                $('.coupon .count').html(res.coupon.showText);
                $('.coupon .coupon-price-info').html(res.coupon.priceText);
lijing authored
178 179 180 181 182 183 184 185 186 187 188
            }

            updateDeliverId(deliverId);
        }
    }).fail(function() {
        window.location.reload();
    }).always(function() {
        loading.hideLoadingMask();
    });
}
郭成尧 authored
189 190
/**
 * 提交订单
郭成尧 authored
191
 * @param {短信验证码} verifyCode
郭成尧 authored
192 193
 */
function submitOrder(verifyCode) {
郭成尧 authored
194
    let msg = $('#msg').find('input').val() || orderInfo('msg');
lijing authored
195 196 197 198 199 200 201 202 203 204 205 206 207

    if (isSubmiting) {
        return false;
    }

    if (msg) {
        if (msg.length > 40) {
            tip.show('留言不得超过40个汉字');
            return;
        }
    }
    loading.showLoadingMask();
    isSubmiting = true;
郭成尧 authored
208 209

    let postData = {
郭成尧 authored
210
        addressId: orderInfo('address_id'),
郭成尧 authored
211 212 213 214 215 216 217
        cartType: orderInfo('cartType') || 'ordinary',
        deliveryId: orderInfo('deliveryId'),
        deliveryTimeId: orderInfo('deliveryTimeId'),
        msg: msg,
        isPrintPrice: orderInfo('isPrintPrice'),
        paymentTypeId: orderInfo('paymentTypeId'),
        paymentType: orderInfo('paymentType'), // 支付方式
郭成尧 authored
218
        couponCode: orderInfo('coupon_code'),
郭成尧 authored
219
        gift_card_code: orderInfo('gift_card_code'),
郭成尧 authored
220
        verifyCode: verifyCode || null,
郭成尧 authored
221 222 223 224 225 226 227 228 229 230 231 232 233 234
        yohoCoin: orderInfo('yohoCoin'),
        skuList: isLimitGood() ? orderInfo('skuList') : void 0
    };

    if (orderInfo('invoice')) {
        Object.assign(postData, {
            invoice: orderInfo('invoice'),
            invoices_title: orderInfo('invoices_title'), // 发票抬头
            invoices_type: orderInfo('invoices_type'), // 发票类型 1 纸质 2 电子
            receiverMobile: orderInfo('receiverMobile'), // 接收人电话
            buyerTaxNumber: orderInfo('buyerTaxNumber') // 购买方纳税人识别号,需要开具
        });
    }
lijing authored
235 236 237
    $.ajax({
        method: 'POST',
        url: '/cart/index/new/orderSub',
郭成尧 authored
238
        data: postData
lijing authored
239
    }).then(function(res) {
lijing authored
240
        let url;
lijing authored
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

        if (!res) {
            tip.show('系统繁忙,请稍后再试!');
            return;
        }

        if (res.code === 200) {
            if (payType === 2) {

                // 货到付款的进入订单页面
                url = '/home/orderDetail?order_code=' + res.data.order_code;
            } else {
                url = '/home/orders/paynew?order_code=' + res.data.order_code;
            }

            /* tar add 161116 */
            if (window._yas && window._yas.sendCustomInfo) {
                window._yas.sendCustomInfo({
                    op: 'YB_SC_TOPAY_CLICK',
                    param: JSON.stringify({
                        C_ID: window._ChannelVary[cookie.get('_Channel')],
                        ORDER_CODE: res.data.order_code + '',
                        PRD_NUM: $('#goods-num').val(),
                        ORDER_AMOUNT: res.data.order_amount
                    })
                }, true);
            }
郭成尧 authored
269
            /* tar add 170426 品众代码去除 */
郭成尧 authored
270 271 272 273 274 275 276 277 278
            /* tar note 170601 品众代码恢复 */
            if (window._fxcmd) {
                window._fxcmd.push(['trackOrder', {
                    oid: res.data.order_code,
                    otp: res.data.order_amount,
                    u_info: cookie.get('_UID'),
                    u_type: cookie.get('_isNewUser') ? 1 : 0
                }, []]);
            }
lijing authored
279
郭成尧 authored
280 281 282
            if (history && history.pushState) {
                history.pushState(null, null, '/home/orders');
            }
lijing authored
283 284 285 286
            cookie.remove(['order-info', 'activity-info']);
            window.location.href = url;
        } else if (res.code === 409) {
            richTip.show(res.message, res.buttons);
郭成尧 authored
287
        } else if (res.code === 440) {
郭成尧 authored
288
            dialog.showDialog({
郭成尧 authored
289 290 291 292 293
                dialogText: res.message,
                hasFooter: {
                    centerBtnText: '我知道了'
                }
            });
郭成尧 authored
294
        } else if (res.code === 411) {
295 296 297
            orderEnsure.showSafeCheckDialog({
                message: res.message
            }, submitOrder);
李靖 authored
298 299
        } else if (res.code === 9999992) {
            tip.show('人太多拉,稍后再试!');
lijing authored
300 301 302 303 304 305 306 307 308 309 310 311
        } else if (res.message) {
            tip.show(res.message);
        }
    }).fail(function() {
        tip.show('系统繁忙,请稍后再试!');
    }).always(function() {
        isSubmiting = false;
        loading.hideLoadingMask();
    });
}

// 界面点击,状态存 cookie
郭成尧 authored
312 313
if (!orderInfo('address_id')) {
    orderInfo('address_id', $addressWrap.data('id'));
lijing authored
314 315
}
郭成尧 authored
316
$('.delivery-id').on('touchend', 'li', function(event) {
zhangxiaoru authored
317
    if ($(this).hasClass('no-support')) {
zhangxiaoru authored
318 319 320
        return false;
    }
lijing authored
321 322 323 324 325 326 327
    orderInfo('deliveryId', $(this).data('id'));

    // 实付金额发生变化,使用有货币为0
    orderInfo('yohoCoin', 0);
    $('.coin').find('.checkbox').removeClass('icon-cb-radio').addClass('icon-radio');

    orderCompute();
郭成尧 authored
328
    event.stopPropagation();
lijing authored
329 330 331
});

$('.payment-type').on('touchend', 'li', function() {
lijing authored
332
    let $paymentType = $('.icon-cb-radio', this);
lijing authored
333 334 335 336 337 338 339 340 341 342 343

    orderInfo('paymentTypeId', $paymentType.data('id')); // 支付方式id
    orderInfo('paymentType', $paymentType.data('payment-type')); // 支付方式
});

$('.dispatch-time').on('touchend', 'li', function() {
    orderInfo('deliveryTimeId', $(this).data('id'));
});

$('.yoho-coin-help-dialog-bg, .yoho-coin-footer').on('touchend', function(e) {
    e.preventDefault();
郭成尧 authored
344 345
    $('.yoho-coin-help-dialog-bg').removeClass('show');
    $('.yoho-coin-help-dialog').removeClass('show');
lijing authored
346 347 348
});

$('.coin').on('touchend', function(e) {
lijing authored
349
    let $this = $(this);
lijing authored
350 351

    if ($(e.target).closest('.yoho-coin-help').length) {
郭成尧 authored
352 353
        $('.yoho-coin-help-dialog-bg').addClass('show');
        $('.yoho-coin-help-dialog').addClass('show');
lijing authored
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        return true;
    }

    if ($(e.target).closest('.coin-check').length <= 0) {
        return false;
    }

    if (!isYohoCoinClick) {
        return true;
    }

    if ($this.find('.checkbox').hasClass('icon-cb-radio')) {
        orderInfo('yohoCoin', $this.data('yoho-coin'));
        $this.find('.can-use').hide();
    } else {
        orderInfo('yohoCoin', 0);
        $this.find('.coin-check em').hide();
        $this.find('.can-use').show();
        $this.find('.used').hide();
    }
    orderCompute();
});
郭成尧 authored
377 378 379 380 381 382
/**
 * 是否打印价格
 */
$noPrintPrice.on('touchend', '.checkbox', function(e) {
    let $this = $(this);
郭成尧 authored
383
    orderInfo('isPrintPrice', $this.hasClass('icon-cb-radio') ? 'N' : 'Y');
郭成尧 authored
384 385 386 387
    e.preventDefault();
    e.stopPropagation();
});
lijing authored
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
$invoice.find('[name="invoice-title"]').on('blur', function() {
    orderInfo('invoiceText', $(this).val());
}).end().find('.invoice-type').on('change', function() {
    orderInfo('invoiceType', $(this).val());
});

$('#msg').find('textarea').on('blur', function() {
    orderInfo('msg', $(this).val());
});

$('form').on('submit', function() {
    return false;
});

// xwg 2016/3/21 13:22
$('.dispatch').on('touchend', 'h3', function() {
    if ($(this).siblings('ul').is(':hidden')) {
        $('.dispatch h3').removeClass('border-none');
        $(this).addClass('border-none');
        $('.down').removeClass('hide');
        $('.up').addClass('hide');
        $('.up', this).removeClass('hide');
        $('.down', this).addClass('hide');
        $('.dispatch ul').hide();
        $(this).siblings('ul').show();
    } else {
        $(this).removeClass('border-none');
        $('.down', this).removeClass('hide');
        $('.up', this).addClass('hide');
        $(this).siblings('ul').hide();
    }
});
421
$subBlock.on('click', 'li', function() {
zhangxiaoru authored
422
lijing authored
423 424 425 426 427
    // 送货时间提示语li,不响应事件
    if ($(this).hasClass('dispatch-time-info')) {
        return true;
    }
zhangxiaoru authored
428
    if ($(this).hasClass('no-support')) {
郭成尧 authored
429
        return false;
Zhang authored
430 431
    }
郭成尧 authored
432 433 434 435 436
    $.each($(this).parents('ul').find('i'), function() {
        $(this).parents('ul').find('i').removeClass('icon-cb-radio').addClass('icon-radio');
    });
    let self = $(this);
张孝茹 authored
437
    // 点击货到付款弹出提示语
张孝茹 authored
438 439 440 441 442 443 444 445 446 447 448 449 450 451
    if (self.find('span').data('message') && delivery === 0 &&
        parseInt(self.find('i').data('payment-type'), 10) === 2) {

        dialog.showDialog({
            hasHeader: '温馨提示',
            dialogText: self.find('span').data('message'),
            hasFooter: {
                centerBtnText: '我知道了'
            }
        });

        delivery = 1;
    }
lijing authored
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    setTimeout(function() {
        self.parents('ul').hide();
    }, 300);

    $('.down').removeClass('hide');
    $('.up').addClass('hide');
    $('.dispatch h3').removeClass('border-none');
    dispatchInfo = $(this).find('span').html();
    $(this).parents('.sub-block').find('h3 span').html(dispatchInfo);
    if ($(this).find('i').hasClass('icon-cb-radio')) {
        $(this).find('i').addClass('icon-radio');
    } else if ($(this).find('i').hasClass('icon-radio')) {
        $(this).find('i').addClass('icon-cb-radio');
    }
});


$('.bill a').on('touchstart', function() {
lijing authored
470
    let $paymentType;
lijing authored
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

    if (isTickets) {
        ticketsConfirm(); // eslint-disable-line
        return;
    }

    $paymentType = $('.payment-type .icon-cb-radio');
    orderInfo('paymentTypeId', $paymentType.data('id')); // 支付方式id
    orderInfo('paymentType', $paymentType.data('payment-type')); // 支付方式

    payType = $paymentType.data('payment-type');
    submitOrder();
});

function phoneHidden(phone) {
    phone = phone || '';
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}

$('.info-phone').html(phoneHidden($('.info-phone').html()));

if (orderInfo('address') && orderInfo('address').is_support === 'N') {
    orderInfo('deliveryId', $('.delivery-id .icon-cb-radio').data('id'));
    orderCompute();
}

// 电子票下单
郭成尧 authored
498
function ticketsConfirm(verifyCode) {
lijing authored
499
    let data = {
lijing authored
500 501 502
        productSku: productSku,
        buyNumber: buyNumber,
        mobile: $ticketsMobile.val(),
郭成尧 authored
503
        useYohoCoin: orderInfo('yohoCoin'),
郭成尧 authored
504 505
        gift_card_code: orderInfo('gift_card_code'),
        verifyCode: verifyCode || null
lijing authored
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    };

    if (!$ticketsMobile.val()) {
        tip.show('手机号必填');
        return;
    }

    $.ajax({
        url: '/cart/index/submitTicket',
        type: 'POST',
        dataType: 'json',
        data: data,
        success: function(ticket) {

            // 下单成功调整支付页面
            if (ticket.code === 200) {
                window.location.href = '/home/orders/paynew?order_code=' + ticket.data.order_code;
郭成尧 authored
523 524 525 526 527
            } else if (ticket.code === 411) {
                orderEnsure.showSafeCheckDialog({
                    message: ticket.message,
                    mobile: $ticketsMobile.val()
                }, ticketsConfirm);
lijing authored
528 529 530 531 532 533 534 535 536 537 538 539
            } else {
                tip.show(ticket.message);
            }

        },
        error: function() {
            tip.show('网络异常~');
        }
    });
}

// 校验手机号
李靖 authored
540 541 542 543 544 545 546 547
// $ticketsMobile.blur(function() {
//     let reg = /^1[34578]\d{9}$/;
//     let mobile = $ticketsMobile.val();

//     if (!reg.test(mobile)) {
//         tip.show('手机号码不正确!');
//     }
// });
lijing authored
548
郭成尧 authored
549 550 551 552 553 554 555
// 留言点击滚动屏幕
$message.on('click', function() {
    $('html,body').animate({
        scrollTop: $message.offset().top
    }, 500);
});
lijing authored
556 557 558 559 560 561 562 563
$(window).scroll(function() {

    if ($(this).scrollTop() >= (headerTop + addressTop)) {
        $('.address-bottom').show();
    } else {
        $('.address-bottom').hide();
    }
});
Zhang authored
564 565 566 567 568 569 570 571 572 573

// 商品是否支持退换货原因弹框
$('.no-support-reason').click(function() {
    dialog.showDialog({
        dialogText: '该商品支持7天无理由退换,不支持7天以上15天内的无理由换货,如有疑问,请联系客服',
        hasFooter: {
            centerBtnText: '我知道了'
        }
    });
});