me-gift.page.js 5.89 KB
var $ = require('yoho-jquery'),
    Hbs = require('yoho-handlebars'),
    dialog = require('../common/dialog');

var meGift;

require('../common');

meGift = {
    // 验证邮箱模板
    emailTpl: Hbs.compile($('#verify-email-tpl').html()),

    // 绑定手机模板
    mobileTpl: Hbs.compile($('#verify-mobile-tpl').html()),

    // 激活礼品卡模板
    giftTpl: Hbs.compile($('#activate-gift-tpl').html()),

    // 消费明细模板
    detailGiftTpl: Hbs.compile($('#detail-gift-tpl').html()),

    // 添加礼品卡按钮
    $addGift: $('.add-gift'),

    // 是否绑定手机
    isBinMobile: +$('.me-content').data('is-bin-mobile'),

    // 用户邮箱
    userEmail: +$('.me-content').data('email'),

    init: function() {
        var that = this;

        // 添加礼品卡
        this.$addGift.click(function() {
            if (that.isBinMobile) {
                that.activateGift();
            } else {
                that.verifyEmailCode();
            }
        });

        // 消费明细
        $('.me-gift-table').on('click', '.info-list', function() {
            that.infoList();
        });

        // 显示手机区号或类型下拉列表
        $('body').on('click', '.mobile-area,.gift-filter', function() {
            $(this).find('.ul-list').toggle();
        });

        // 选择手机区号
        $('body').on('click', '.mobile-area-list', function(event) {
            var $li = $(event.target).closest('li');

            $('.mobile-area').find('em').text($li.data('cc'));
        });

        // 选择类型筛选
        $('body').on('click', '.gift-filter ul', function(event) {
            var $li = $(event.target).closest('li');

            $('.gift-filter').find('em').text($li.text());
        });
    },

    // 我的礼品卡列表
    getList: function() {

    },

    // 礼品卡详情列表
    detailList: function() {

    },

    // 获取邮箱验证码
    getEmailCode: function() {
    },

    // 效验邮箱验证码
    verifyEmailCode: function() {
        var that = this;
        var dg = new dialog.Dialog({
            content: that.emailTpl({}),
            className: 'me-gift-confirm',
            btns: [{
                id: 'confirm-email-btn',
                name: '绑定手机号',
                btnClass: ['alert-sure'],
                cb: function() {
                    dg.close();
                    that.getMobileCode();
                }
            }]
        }).show();
    },

    // 获取手机验证码
    getMobileCode: function() {
        var that = this;
        var dg = new dialog.Dialog({
            content: that.mobileTpl({}),
            className: 'me-gift-confirm',
            btns: [{
                id: 'confirm-mobile-cancel',
                name: '取消',
                btnClass: ['confirm-cancel'],
                cb: function() {
                    dg.close();
                }
            }, {
                id: 'confirm-mobile-sure',
                name: '激活',
                btnClass: ['confirm-sure'],
                cb: function() {
                    dg.close();
                    that.activateGift();
                }
            }]
        }).show();
    },

    // 效验手机验证码并激活
    verifyMobileCode: function() {

    },

    // 激活礼品卡
    activateGift: function() {
        var that = this;
        var isFlag = false;
        var dg = new dialog.Dialog({
            content: that.giftTpl({}),
            className: 'me-gift-confirm',
            btns: [{
                id: 'confirm-gift-cancel',
                name: '取消',
                btnClass: ['confirm-cancel'],
                cb: function() {
                    dg.close();
                }
            }, {
                id: 'confirm-gift-sure',
                name: '激活',
                btnClass: ['confirm-sure'],
                cb: function() {
                    var $confirm = $('.me-gift-confirm');

                    if ($confirm.find('.card-code').val() === '') {
                        that.meAlert('礼品卡卡号不能为空');
                        return false;
                    } else if ($confirm.find('.card-pwd').val() === '') {
                        that.meAlert('礼品卡卡密不能为空');
                        return false;
                    }

                    if (isFlag) {
                        return false;
                    }

                    isFlag = true;
                    $.ajax({
                        url: '/home/activateGift',
                        type: 'post',
                        data: {
                            cardCode: $confirm.find('.card-code').val(),
                            cardPwd: $confirm.find('.card-pwd').val()
                        }
                    }).then(function(res) {
                        isFlag = false;

                        if (res.code === 200) {
                            dg.close();
                            that.meAlert('<p>您的礼品卡激活成功</p><p>请尽情享用</p>');
                        } else {
                            that.meAlert(res.message);
                        }
                    }, function() {
                        isFlag = false;
                    });
                }
            }]
        }).show();
    },

    // 消费明细
    infoList: function() {
        var that = this;

        new dialog.Dialog({
            content: that.detailGiftTpl({}),
            className: 'me-gift-page me-gift-confirm'
        }).show();
    },

    // 我的弹框
    meAlert: function(content) {
        var dg = new dialog.Dialog({
            content: content,
            className: 'me-gift-alert',
            btns: [{
                name: '我知道了',
                btnClass: ['btn-close', 'alert-sure'],
                cb: function() {
                    dg.close();
                }
            }]
        }).show();
    }
};

$(function() {
    meGift.init();
});