控制器
public function save(Request $request) { // echo 11;die(); $phone=input(); $last_time=Cache::store('redis')->get('register_time'); // return json($last_time); if(time()-$last_time<60){ return json(['code'=>200,'msg'=>'发送频繁']); } $code=mt_rand(1111,9999); $smsapi = "http://api.smsbao.com/"; $user = "qsn153025"; //短信平台帐号 $pass = md5("qsn153025"); //短信平台密码 $content="【穿行】短信内容:短信宝验证码:{$code}";//要发送的短信内容 $phone = $phone['phone'];//要发送短信的手机号码 $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content); $result =file_get_contents($sendurl) ; if($result==0){ Cache::store('redis')->set('register_time',time(),60); Cache::store('redis')->set('register_code',$code,60); return json(['code'=>200,'msg'=>'短信发送成功']); }else{ return json(['code'=>500,'msg'=>'短信发送成功']); } } function phone(){ $params= input(); $validate=$this->validate($params,[ 'phone'=>'require|regex:1[3-9]\d{9}|unique:user,phone', 'code'=>'require', 'password'=>'require|confirm:repassword' ]); if($validate!=true){ $this->error($validate); } $code=Cache::get('register_code') ; if($params['code']!=$code){ $this->error('验证码错误'); } // Cache('register_code',null); $result=[ 'phone'=>$params['phone'], 'password'=>md5( $params['password']) ]; $register= User::create($result,true); if($register){ $this->success('注册成功'); } } 页面::
<script> $(function (){ $('#reg_btn').click(function (){ $('form').submit(); }); $("#dyMobileButton").click(function (){ var phone=$('#phone').val(); if(phone=='') { $('#phone').next().html('手机号不能为空'); return; }else if(!/^1[3-9]\d{9}$/.test(phone)){ $('#phone').next().html('手机号格式不正确'); return; }else{ $('#phone').next().html(''); } var time=60; var timer=setInterval(function (){ time--; if(time > 0){ $('#dyMobileButton').html('请'+time+'后发送'); $('#dyMobileButton').prop('disabled',true); }else{ $('#dyMobileButton').html('发送验证码'); $('#dyMobileButton').prop('disabled',false); clearInterval(timer) } },1000); $.ajax({ url:'/save', data:{ phone:phone }, type:'post', dataType:'json', success:function (e){ console.log(e) }, }) }) }) </script>