2020-09-22 发送短信验证码和验证

2020-09-27  本文已影响0人  zlchen

1.登录阿里云账号下载——aliyun-dysms-php-sdk(我使用的php版本)

下载地址:https://help.aliyun.com/document_detail/55359.html?spm=a2c4g.11174283.3.3.30d72c42m24zNH

2.下载的sdk包放在extend下(使用的是tp5框架)

image.png
   // 会员注册短信验证码 add 20200922170956 zlchen
    public function send_sms(){
        
        set_time_limit(0);
        header('Content-Type: text/plain; charset=utf-8');
        
        $phone = input('post.phone');
        //随机验证码
        $str = "0,1,2,3,4,5,6,7,8,9";      
        $list = explode(",", $str);
        $cmax = count($list) - 1;
        $code = '';
        for ( $i=0; $i < 6; $i++ ){
              $randnum = mt_rand(0, $cmax);
              //取出字符,组合成为我们要的验证码字符
              $code .= $list[$randnum]; 
        };
        $smssign = "签名";
        $templateid = "模版CODE";
        
        //引入短信验证码的类 
        $smsAlibaba = EXTEND_PATH.'/aliyun-dysms-php-sdk/api_demo/SmsDemo.php'; //将附件放入根目录下的/extend
        require_once $smsAlibaba;
        
        $request = new \SmsDemo();
        $res = $request::sendSms($phone,$smssign,$templateid,$code);
    
        if($res->Message=='触发天级流控Permits:10'){
            return json(['code'=>0,'msg'=>'发送失败','一天最多发布十条']);
        }
        else if($res->Message=='触发天级流控Permits:5'){
            return json(['code'=>0,'msg'=>'发送失败','一个小时之内只能发布五条']);
        }
        else if($res->Message=='触发天级流控Permits:1'){
            return json(['code'=>0,'msg'=>'发送失败','发送验证码时间超出一分钟才能再次发送']);
        }
        else if($res->Message=='OK'){
            // 插入到数据库
            $sendtime = time();
            // 验证码5分钟后过期
            $expiration_time = $sendtime + 5*60;
            $data = array(
                    'phone' => $phone,
                    'code' => $code,
                    'sendtime' => $sendtime,
                    'expiration_time' => $expiration_time,
                    'md5' => md5($phone.$code.$sendtime)
            );
            //短信验证码信息写入数据库
            $res = Db::name('sms_code')->insert($data);
            if($res > 0){
                return json(['code'=>1,'msg'=>'发送成功','data'=>['code'=>$code,'sendtime'=>$sendtime]]);
            }else{
                return json(['code'=>0,'msg'=>'发送成功,但写入数据库失败','data'=>['code'=>$code,'sendtime'=>$sendtime]]);
            }
            
        }
        
    }
    
    // 验证验证码  add 20200922174458
    public function verification_code(){
        
          $phone = input('post.phone');
          $code = input('post.code');
          $sendtime = input('post.sendtime');
          
          //9e0df4aed0ab2ce8fcd58c8f1a68820f
          $md5_str = md5($phone.$code.$sendtime);
          
          // expiration_time
          $smscode =  Db::name('sms_code')->field(['id','phone','code','sendtime','expiration_time','md5'])->where('md5',$md5_str)->find();
          
          if($md5_str == $smscode['md5']){
              if(time() > $smscode['expiration_time']){
                  return json(['code'=>0,'msg'=>'验证码已经过期','data'=>[]]);
              }
              $res = Db::name('sms_code')->where('md5', $md5_str)->update(['status'=>1]);
              if($res >0 ){
                   return json(['code'=>1,'msg'=>'验证码正确','data'=>[]]);
              }
               
          }else{
              return json(['code'=>0,'msg'=>'验证码不正确','data'=>[]]);
          }
    }
上一篇下一篇

猜你喜欢

热点阅读