coupons.page.js 1.58 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) {
                new dialog.Dialog({
                    className: 'coupon-code-confirm',
                    content: '兑换成功',
                    btns: [
                        {
                            id: 'confirm-sure',
                            btnClass: ['confirm-sure'],
                            name: '确定',
                            cb: function() {
                                window.location.reload();
                            }
                        }
                    ]
                }).show();
            } else {
                new dialog.Alert(data.message || '兑换失败,请稍后再试').show();
            }
        });
    }
});