var tel = $("#tel").val();//用户的手机号
if(tel.length == 0){
alert('请先输入手机号');return false;
}else{
if (isPhoneNo(tel) == false) {
alert("手机号码不正确");return false;
}
};
//使用演示 显示为 秒
var countdown=60;
function sendmsg(){
if(countdown==0){
$("#code-tips").attr("disabled",false);
$("#code-tips").html("验证码");
countdown=60;
return false;
}
else{
$("#code-tips").attr("disabled",true);
$("#code-tips").html(countdown+"s"+'后失效');
countdown--;
}
setTimeout(function(){
sendmsg();
},1000);
}
sendmsg();
$.ajax({
type:"post",
url:"/sendSms.php",
data:{"type":"get_code","tel":tel},
success:function(res){
console.log("code",res);
if(res==500){
alert("验证码发送失败,请稍后重试");
}else{
var downloadCode=res;
$.ajax({
type:"post",
url:"/plus/downloadCode.php",
async: false, //同步
data:{'code':downloadCode,"tel":tel},
success:function(res){
console.log("验证码插入数据库",res);
}
})
}
}
})
});
// 获取验证码 end
// 下载 start
function download(src) {
var $a = document.createElement('a');
$a.setAttribute("href", src);
$a.setAttribute("download", "");
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, true, false, 0, null);
$a.dispatchEvent(evObj);
};
// 下载 end
// 提交表单时进行短信验证 start
$("#form-download").click(function(){
function isPhoneNo(phone) {
var pattern = /^1[34578]\d{9}$/;
return pattern.test(phone);
}
var name = $("#name").val();
var tel = $("#tel").val();
if(name == ''){
alert('请输入姓名');return false;
}
if(tel.length == 0){
alert('请输入手机号');return false;
}else{
if (isPhoneNo(tel) == false) {
alert("手机号码不正确");return false;
}
}
var code = $("#code").val();
var downloadAid=$("#downloadAid").val();
$.ajax({
type:"post",
url:"/plus/downloadAid.php",
async: false, //同步
data:{"tel":tel,"code":code,"aid":downloadAid},
success:function(res){
console.log("验证码是否正确",res);
if(res == 400){
alert("验证码已失效");
}else if(res==500){
alert("验证码填写错误,请确认!");
return;
}else{
download(res);
var fromdata= $("#download-form").serialize();
console.log("fromdata",fromdata);
$.ajax({
type:"post",
url:"/plus/diy.php",
async: false, //同步
data:fromdata,
success:function(res){
$("#name").val("");
$("#tel").val("");
$("#code").val("");
$("#layui-layer1").remove();
}
})
}
}
})
})
// 提交表单时进行短信验证 end
})