|
|
var $ = require('yoho-jquery'),
|
|
|
Hbs = require('yoho-handlebars'),
|
|
|
dialog = require('../common/dialog'),
|
|
|
Captcha = require('../plugins/captcha');
|
|
|
|
|
|
var meGift;
|
|
|
|
|
|
require('../common');
|
|
|
|
|
|
meGift = {
|
|
|
isFlag: false, //防止用户频繁点击
|
|
|
// 验证邮箱模板
|
|
|
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.getMobileCode();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 消费明细
|
|
|
$('.me-gift-table').on('click', '.info-list', function() {
|
|
|
that.infoList($(this).closest('.me-gift-tr').data('card-code'));
|
|
|
});
|
|
|
|
|
|
// 显示手机区号或类型下拉列表
|
|
|
$('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());
|
|
|
});
|
|
|
},
|
|
|
|
|
|
postAjax: function(url, data, verifyData, captcha) {
|
|
|
var that = this;
|
|
|
var deferred = $.Deferred();
|
|
|
var x;
|
|
|
|
|
|
if (that.isFlag) {
|
|
|
return deferred.resolve({
|
|
|
code: 401,
|
|
|
message: '数据请求中...'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (verifyData) {
|
|
|
for(x in verifyData) {
|
|
|
if (!data[x]) {
|
|
|
that.meAlert(verifyData[x], false);
|
|
|
return deferred.resolve({
|
|
|
code: 401,
|
|
|
message: verifyData[x]
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
that.isFlag = true;
|
|
|
|
|
|
return $.ajax({
|
|
|
url: url,
|
|
|
type: 'post',
|
|
|
data: data
|
|
|
}).then(function(res) {
|
|
|
captcha && captcha.hideTip();
|
|
|
|
|
|
if (res.code === 405) {
|
|
|
captcha && captcha.showTip(res.message);
|
|
|
} else {
|
|
|
that.meAlert(res.message, false);
|
|
|
}
|
|
|
|
|
|
that.isFlag = false;
|
|
|
return res;
|
|
|
}, function() {
|
|
|
that.isFlag = false;
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 礼品卡详情列表
|
|
|
detailList: function() {
|
|
|
|
|
|
},
|
|
|
|
|
|
intTimer: function($dom) {
|
|
|
var intTime = 60;
|
|
|
var intval = setInterval(function() {
|
|
|
|
|
|
if (intTime-- <= 0) {
|
|
|
clearInterval(intval);
|
|
|
$dom.removeClass('intTimer').val('获取短信验证码');
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$dom.val(intTime + ' S');
|
|
|
}, 1000);
|
|
|
|
|
|
$dom.addClass('intTimer');
|
|
|
$dom.val(intTime + ' S');
|
|
|
},
|
|
|
// 获取邮箱验证码
|
|
|
getEmailCode: function(captcha) {
|
|
|
this.postAjax('/home/megift/sendEmailCode', {
|
|
|
email: this.userEmail,
|
|
|
verifyCode: captcha.getResults()
|
|
|
}).then(function(res) {
|
|
|
if (res.code === 200) {
|
|
|
that.intTimer($('.email-btn'));
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 效验邮箱验证码
|
|
|
verifyEmailCode: function() {
|
|
|
var that = this;
|
|
|
var captcha;
|
|
|
var dg = new dialog.Dialog({
|
|
|
content: that.emailTpl({}),
|
|
|
className: 'me-gift-confirm',
|
|
|
btns: [{
|
|
|
id: 'confirm-email-btn',
|
|
|
name: '绑定手机号',
|
|
|
btnClass: ['alert-sure'],
|
|
|
cb: function() {
|
|
|
var emailCode = $('.email-code').val();
|
|
|
var verifyData = {code: '验证码不能为空'};
|
|
|
|
|
|
that.postAjax('/home/megift/verifyEmail', {
|
|
|
email: that.userEmail,
|
|
|
code: emailCode
|
|
|
}, verifyData, captcha).then(function(res) {
|
|
|
if (res.code === 200) {
|
|
|
dg.close();
|
|
|
that.getMobileCode();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}]
|
|
|
}).show();
|
|
|
|
|
|
captcha = new Captcha('#captcha').init();
|
|
|
|
|
|
//发送邮箱验证码
|
|
|
$('.me-gift-confirm').find('.email-btn').unbind('click').bind('click', function() {
|
|
|
if (!$(this).hasClass('intTimer')) {
|
|
|
that.getEmailCode(captcha);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 获取手机验证码
|
|
|
getMobileCode: function() {
|
|
|
var that = this;
|
|
|
var captcha;
|
|
|
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() {
|
|
|
var verifyData = {
|
|
|
area: '请选择手机区号',
|
|
|
mobile: '手机号不能为空',
|
|
|
code: '手机验证码不能为空',
|
|
|
};
|
|
|
|
|
|
that.postAjax('/home/megift/changeMobile', {
|
|
|
area: ($('.mobile-area').find('em').text() || '').replace(/^\+/, ''),
|
|
|
mobile: $('.mobile').val(),
|
|
|
code: $('.mobile-code').val()
|
|
|
}, verifyData, captcha).then(function(res) {
|
|
|
if (res.code === 200) {
|
|
|
dg.close();
|
|
|
that.activateGift();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}]
|
|
|
}).show();
|
|
|
|
|
|
captcha = new Captcha('#captcha').init();
|
|
|
|
|
|
//发送邮箱验证码
|
|
|
$('.me-gift-confirm').find('.mobile-btn').unbind('click').bind('click', function() {
|
|
|
if (!$(this).hasClass('intTimer')) {
|
|
|
that.smsBind(captcha);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 发送手机验证码
|
|
|
smsBind: function(captcha) {
|
|
|
var that = this;
|
|
|
var area = ($('.mobile-area').find('em').text() || '').replace(/^\+/, '');
|
|
|
var mobile = $('.mobile').val();
|
|
|
var verifyData = {
|
|
|
area: '请选择手机区号',
|
|
|
mobile: '手机号不能为空'
|
|
|
};
|
|
|
|
|
|
this.postAjax('/home/megift/smsBind', {
|
|
|
area: area,
|
|
|
mobile: mobile,
|
|
|
verifyCode: captcha.getResults()
|
|
|
}, verifyData, captcha).then(function(res) {
|
|
|
if (res.code === 200) {
|
|
|
that.intTimer($('.email-btn'));
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 激活礼品卡
|
|
|
activateGift: function() {
|
|
|
var that = this;
|
|
|
var captcha;
|
|
|
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');
|
|
|
var verifyData = {
|
|
|
cardCode: '礼品卡卡号不能为空',
|
|
|
cardPwd: '礼品卡卡密不能为空'
|
|
|
};
|
|
|
|
|
|
that.postAjax('/home/meGift/activateGift', {
|
|
|
cardCode: $confirm.find('.card-code').val(),
|
|
|
cardPwd: $confirm.find('.card-pwd').val(),
|
|
|
verifyCode: captcha.getResults()
|
|
|
}, verifyData, captcha).then(function(res) {
|
|
|
if (res.code === 200) {
|
|
|
dg.close();
|
|
|
that.meAlert('<p>您的礼品卡激活成功</p><p>请尽情享用</p>', false);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}]
|
|
|
}).show();
|
|
|
|
|
|
captcha = new Captcha('#captcha').init();
|
|
|
},
|
|
|
|
|
|
// 消费明细
|
|
|
infoList: function(cardCode) {
|
|
|
var that = this;
|
|
|
|
|
|
new dialog.Dialog({
|
|
|
content: that.detailGiftTpl({}),
|
|
|
className: 'me-gift-page me-gift-confirm detail-gift-list'
|
|
|
}).show();
|
|
|
|
|
|
$.get('/home/megift/detail', {
|
|
|
cardCode: cardCode
|
|
|
}).then(function(res) {
|
|
|
if (!res) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
$('.detail-gift-content').html(res)
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 我的弹框
|
|
|
meAlert: function(content, mask) {
|
|
|
var dg = new dialog.Dialog({
|
|
|
content: content,
|
|
|
className: 'me-gift-alert',
|
|
|
mask: mask || true,
|
|
|
btns: [{
|
|
|
name: '我知道了',
|
|
|
btnClass: ['btn-close', 'alert-sure'],
|
|
|
cb: function() {
|
|
|
dg.close();
|
|
|
}
|
|
|
}]
|
|
|
}).show();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
$(function() {
|
|
|
meGift.init();
|
|
|
}); |
...
|
...
|
|