magazine-confirm.page.js 3.09 KB
require('scss/cart/magazine-confirm.page.scss');

const $ = require('yoho-jquery');
const toast = require('js/plugin/tip');
const dialog = require('js/plugin/dialog');

let $activeItem = $('.choose-list .active');
let $totalPrice = $('.total-price .total-num');
let $customNumInput = $('#custom-num-input');


let buyNumber = 1;
let maxBuyNumber = 2000;
let productSku = $totalPrice.data('sku');
let submiting = false;


$('.choose-list').on('click', '.choose-item', function(e) {
    let $elm = $(e.currentTarget);

    if ($elm.hasClass('custom-choose')) {
        $customNumInput.change();
    } else {
        let data = $elm.data();

        $totalPrice.text(data.price);
        buyNumber = data.num;
    }

    $activeItem.length && $activeItem.removeClass('active');
    $activeItem = $elm.addClass('active');
});

$customNumInput.on('input', function() {
    let val = $(this).val();
    let handelVal = Math.abs(parseInt(`0${val}`, 10));

    handelVal = Math.min(handelVal, maxBuyNumber);
    handelVal = handelVal > 0 ? handelVal : '';

    if (val !== handelVal) {
        $(this).val(handelVal);
    }
}).on('change', function() {
    let val = $(this).val();

    if (val > 0) {
        $.ajax({
            method: 'POST',
            url: '/cart/magazine/order/compute',
            data: {
                sku: productSku,
                buyNumber: val
            },
            success: function(ret) {
                if (ret.code === 200) {
                    $totalPrice.text(`¥ ${ret.data.lastOrderAmount}`);
                } else {
                    toast.show(ret.message || '网络出了点问题~');
                }
            }
        });
    }
});

$('.buy-btn-wrap').on('click', 'button', function() {
    if (submiting) {
        return toast.show('点太快了,休息一下~');
    }
    submiting = true;

    setTimeout(() => {
        submiting = false;
    }, 3000);

    $.ajax({
        method: 'POST',
        url: '/cart/magazine/order/submit',
        data: {
            sku: productSku,
            buyNumber: buyNumber
        }
    }).then(ret => {
        if (ret.code === 200) {
            location.href = '/home/orders/paynew?order_code=' + ret.data.order_code;
        } else {
            toast.show(ret.message || '网络出了点问题~');
        }
    });
});

$('.view-buy-tip').on('click', function() {
    dialog.showDialog({
        hasClass: 'magazine-buytip-dialog',
        hasHeader: '购买须知',
        dialogText: '<ul><li>购买成功后,需要兑换阅读码阅读;</li>' +
        '<li>电子刊为虚拟商品,若无系统问题,购买成功后不支持退换;</li>' +
        '<li>购买多个电子刊阅读码,可以在【我的阅读码-阅读码】中赠送给好友兑换阅读;</li>' +
        '<li>若出现无法阅读的情况,可以联系客服反馈问题,情况核对属实,将为您补发;</li></ul>',
        hasFooter: {
            centerBtnText: '知道了'
        }
    });
});

if ($activeItem.length) {
    buyNumber = $activeItem.data().number || 1;
}

if ($customNumInput) {
    maxBuyNumber = $customNumInput.data('max');
}