发送验证码 禁止重复绑定 后端不传notice 前端alert
<script>
var count=60,timer=null;
jQuery("#send_validation_code").click(function() {
let email = jQuery('#email').val();
if(email==''){
alert('邮箱不能为空')
}else{
let url = '/front_ends/send_validation_code?type=email&to=' + email +'&account_id=<%= current_account.id %>'
jQuery.get(url , function(data){
console.info(data)
if(data.result == 'ok'){
if(timer==null){
timer=setInterval(function(){count--;$("#send_validation_code").text(count+"s");
if(count<=0){clearInterval(timer);$("#send_validation_code").text("获取验证码");timer=null;}},1000);
}
}else{
alert('该邮箱已经存在,无法被绑定,请确认邮箱是否正确')
}
})
}
});
def send_validation_code
type = params[:type]
to = params[:to]
code = ((0..9).to_a + (0..9).to_a).shuffle[0,4].join
subject = "您的验证码是#{code}"
if Account.where('email = ? or mobile = ?', to, to).blank?
if type == 'mobile'
YunPianJob.perform_now to, code
else
UserMailer.send_validation_email(to, code, subject).deliver_now
end
if current_account.present?
ValidationCode.create code_type: type, code: code, valid_through: (Time.now + 15 * 60), account_id: current_account.id, receive: to, is_used: false
else
ValidationCode.create code_type: type, code: code, valid_through: (Time.now + 15 * 60), receive: to, is_used: false
end
render json: {
result: 'ok',
to: to,
code: code
}
else
render json: {
result: 'error'
}
end
end
