coupons.page.js 1.08 KB
var $ = require('yoho-jquery');
var yas = require('../common/data-yas'),
    dialog = require('../common/dialog');

var $couponCodeUse = $('.code-sure-btn'),
    $codeInput = $('.code-input');

require('../common');

$('.use-now-btn').click(function() {
    var couponid = $(this).data('couponid');

    if (couponid) {
        yas.yasEvent('YB_COUPON_IMMEDIATE_USE_C', {COUPON_ID: couponid});
    }
});

$codeInput.keyup(function() {
    if ($.trim($(this).val())) {
        $couponCodeUse.addClass('sure-convert-btn');
    } else {
        $couponCodeUse.removeClass('sure-convert-btn');
    }
});

$couponCodeUse.click(function() {
    var code = $.trim($codeInput.val());

    if ($(this).hasClass('sure-convert-btn') && code) {
        $.ajax({
            type: 'GET',
            url: '/cart/ensure/couponcode',
            data: {code: code}
        }).then(function(data) {
            if (data.code === 200) {
                window.location.reload();
            } else {
                new dialog.Alert(data.message || '兑换失败,请稍后再试').show();
            }
        });
    }
});