Blame view

public/js/home/gift.page.js 2.97 KB
hongweigao authored
1 2 3 4 5
/**
 * 个人中心页-兑换礼品卡
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/02/22
 */
hongweigao authored
6 7
var $ = require('yoho-jquery'),
    Captcha = require('../plugins/captcha');
hongweigao authored
8 9 10 11 12 13 14 15 16 17 18

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

var Alert = dialog.Alert;

var $giftError = $('.giftCardCode').find('.gift-error'),
    reg = /^[0-9a-zA-Z]{4,4}$/,
    i = 1;

var active;
yyq authored
19 20
var captcha;
hongweigao authored
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
var Gift = {
    suc: [
        false,
        false,
        false,
        false
    ],
    checkCard: function(num) {

        if (!reg.test($('#giftCardCode' + num).val())) {
            $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
            Gift.suc[num - 1] = false;
        } else {
            for (i = 1; i <= 3; i++) {
                if (!reg.test($('#giftCardCode' + i).val())) {
                    $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
                    Gift.suc[i] = false;
                } else {
                    $giftError.html('');
                    Gift.suc[i] = true;
                }
            }

            if (num !== 3) {
                $('#giftCardCode' + (num + 1)).focus();
            }
        }
    },
    bindGiftCardForm: function() {
        $('#giftCardCode1').bind('blur keyup', function() {
            Gift.checkCard(1);
        });

        $('#giftCardCode2').bind('blur keyup', function() {
            Gift.checkCard(2);
        });

        $('#giftCardCode3').bind('blur keyup', function() {
            Gift.checkCard(3);
        });
    },

    checkForm: function() {
        if (!reg.test($('#giftCardCode1').val()) || !reg.test($('#giftCardCode2').val()) ||
            !reg.test($('#giftCardCode3').val())) {
            $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
yyq authored
67
            return $.Deferred().reject().promise(); // eslint-disable-line
hongweigao authored
68 69
        }
hongweigao authored
70
        return captcha.check();
hongweigao authored
71 72 73
    }
};
yyq authored
74
captcha = new Captcha('.captcha-img').init();
hongweigao authored
75
hongweigao authored
76 77 78 79 80 81 82 83 84 85 86
require('../common');

// 更换验证码
function refreshCaptcha() {
    var dt = new Date();

    $('#imgcode').attr('src', '/passport/imagesNode?t=' + dt.getTime());
    return false;
}

$('#sub-gift').on('click', function() {
hongweigao authored
87
yyq authored
88
    Gift.checkForm().then(function() {
hongweigao authored
89 90 91 92 93 94 95 96 97 98 99
        $.post('/home/gift/exchange', $('#giftCardForm').serialize(), function(data) {
            if (data.code === 200) {
                window.location.href = '/home/gift?type=1';
            } else if (data.code === 400) {
                active = new Alert(data.message);
                active.show();
                refreshCaptcha();
            } else {
                window.location.href = '/home/gift?type=2';
            }
        }, 'json');
yyq authored
100
    });
hongweigao authored
101 102 103 104 105 106 107 108 109 110
});

$(document).on('click', '#imgcode,.check-img', function() {
    refreshCaptcha();
});

$(function() {
    refreshCaptcha();
    Gift.bindGiftCardForm();
});