首頁  >  文章  >  php框架  >  關於thinkphp下阿里大於簡訊驗證

關於thinkphp下阿里大於簡訊驗證

藏色散人
藏色散人轉載
2020-12-18 09:06:422472瀏覽

下面由thinkphp框架教學專欄跟大家介紹thinkphp下阿里大於簡訊驗證,希望對需要的朋友有幫助!

本方法是基於Thinkphp,用到了jquery.validate,當然具體項目中也可以不用,下面我就按步驟來寫下來。

1.準備

1.登陸
http://www.alidayu.com,進入網址
用淘寶號登陸即可
2.進入管理中心

關於thinkphp下阿里大於簡訊驗證

3.設定
(1)設定簡訊簽章

關於thinkphp下阿里大於簡訊驗證

關於thinkphp下阿里大於簡訊驗證

(2)設定簡訊模板

關於thinkphp下阿里大於簡訊驗證

關於thinkphp下阿里大於簡訊驗證

# 建好後,需要的東西就是APPkey APPsecret

關於thinkphp下阿里大於簡訊驗證

關於thinkphp下阿里大於簡訊驗證

也可以進行測試

關於thinkphp下阿里大於簡訊驗證

#2.資料庫

關於thinkphp下阿里大於簡訊驗證

3.後端

  • 引入
    下載阿里大於類文件,放在/Thinkphp/Library/org/Alidayu(文件夾要大寫,坑過自己,部署到伺服器後出問題)

關於thinkphp下阿里大於簡訊驗證

  • #通用類別裡

  • ##
  // 生成短信验证码
    public function createSMSCode($length = 4){
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return rand($min, $max);
    }

    //发送验证码
    public function send_phone($phone){
        $code=$this->createSMSCode($length = 4);
        import('Org.Alidayu.top.TopClient');
        import('Org.Alidayu.top.ResultSet');
        import('Org.Alidayu.top.RequestCheckUtil');
        import('Org.Alidayu.top.TopLogger');
        import('Org.Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
        $c = new \TopClient;
        $appkey="你的appkey";
        $secret="你的secret;
        $c ->appkey = $appkey ;
        $c ->secretKey = $secret ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "阿尚测试" );
        $req ->setSmsParam( "{name:'客户',code:'".$code."',time:'5分钟'}" );
        $req ->setRecNum( $phone);
        $req ->setSmsTemplateCode( "SMS_71300157" );
        $resp = $c ->execute( $req );
        $this->sendMsgResult($resp,$phone,$code);
    }
    
    //验证手机号是否发送成功  前端用ajax,发送成功则提示倒计时,如50秒后可以重新发送
    private function sendMsgResult($resp,$phone,$code){
        if ($resp->result->success && !$resp->result->code) {
            $data['phone']=$phone;
            $data['code']=$code;
            $data['send_time']=time();
            $result=M("code")->add($data);
            if($result){
                $data="发送成功";
            }else{
                $data="发送失败";
            }
        } else if ($resp->code || $resp->msg == "Remote service error") {
            $data="发送失败";
        } else {
            $data="发送失败";
        }
        return $data;
    }
    
    // 验证短信验证码是否有效,前端用jquery validate的remote
    public function checkSMSCode(){
        $mobile = $_POST['mobile'];
        $code = $_POST['code'];
        $nowTimeStr = date('Y-m-d H:i:s');
        $smscodeObj = M("code")->where("mobile='$mobile'")->find();
        if($smscodeObj){
            $smsCodeTimeStr = $smscodeObj['send_time'];
            $recordCode = $smscodeObj['code'];
            $flag = $this->checkTime($nowTimeStr, $smsCodeTimeStr);
            if($flag&&$code==$recordCode){
                echo true;
            }else{
                echo false;
            }
        }
    }
  • 用的類別裡,我是放在註冊類別裡呼叫

//發送短信,呼叫BaseController裡的方法,因為其他模組也要用到這方法

  public function send_message(){
     $phone=I("post.phone");
     //data返回失败 但不影响使用
     $data=$this->send_phone($phone);
     $this->ajaxReturn($data,"JSON");
 }
4.前台ajax部分功能已經具備,還待完善

//发送手机ajax
function send_message() {
        if($("#phone input").valid()){
            var phone=$("#phone input").val();
            $.post("{:U('Register/send_message')}","phone="+phone,function(data){
                
            });    
            $("#send_message").html("发送成功");
            $("#send_message").css("background-color","#2f9cff");
        }

}

以上是關於thinkphp下阿里大於簡訊驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除