關於微信掃碼登錄的2種解決辦法thinkphp5


關於微信掃碼登錄的2種解決辦法

1 因為之前寫過微信的掃碼登錄 所以朋友有這個需求的時候 我直接讓他去 微信開放平台去注冊

https://open.weixin.qq.com/   當然是這里了, 因為是網站上的需求 所以

https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

就是這里了。

2 不過朋友沒有申請這個,只有公眾號的相關資料,公眾號沒有明文的掃碼登錄  但是有授權登錄這種接口。

無奈百度找找,結果還真有,就是用公眾號的授權登錄生成網址,然后用戶使用微信掃碼,實現登錄的過程。

直接上代碼吧  公眾號

//頁面授權,獲取用戶的code 第一步 訪問地址
    public function getWechatCode()
    {
        //這里直接校驗登陸碼
        $logincode=input('logincode');
        

        $map['code']=$logincode;
        if(empty($logincode)) {
            $this->error("logincode參數錯誤");exit;
        }
        $info=db('client_logincode')->where($map)->find();
        if(!empty($info)) {
            if($info['expire_time']<time()) {
                $data=array();
                $data['status']=3;
                $data['update_time']=time();
                db('client_logincode')->where($map)->update($data);
                
                $this->error("logincode已過期");exit;
            } else {
                $data=array();
                $data['status']=2;
                $data['update_time']=time();
                db('client_logincode')->where($map)->update($data);
            }
        } else {
            $this->error("logincode不存在");exit;
        }
        
        
        $appid = '111111';//改成你的APPID
        
        $redirect_uri=urlencode("http://www.***.net/index.php/api/user/getWechatAccessToken/");//改成你的域名地址
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
 
        $this->redirect($url);
    }

    //網頁授權,獲取用戶的openId 第二步
    public function getWechatAccessToken()
    {
        
        $appid = '1111';//改成你的APPID
        $appsecret='2222';//改成你的 SECRET
 
        $result=0;
        
        $request=request();
        $code=$request->param('code');
        $state=$request->param('state');
        if($code && $state=='STATE')
        {
            $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
            $response = file_get_contents($url);
            $arr = json_decode($response, true);
 
            if(!empty($arr)) {
                $accessToken=$arr['access_token'];
                $openId=$arr['openid'];
                $result=$this->getWechatUserInfo($accessToken,$openId);
            }

        }
        
        
           if($result) {
            $this->success("操作成功",$result);
        } else {
            $this->error("操作失敗");
        }

    }

    //網頁授權,獲取微信用戶信息
    public function getWechatUserInfo($accessToken,$openId)
    {
        $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$accessToken."&openid=".$openId."&lang=zh_CN";
        $response = file_get_contents($url);
        $arr = json_decode($response, true);
 
        $res['status']=0;
        
        if(!empty($arr)) {
            //這里可以數據處理
            //根據openid 查詢用戶ID
            $map['openid']=$arr['openid'];
            $info=db('client_users')->where($map)->find();
   
            if($info['user_type']==2) {
                $res['status']=1;
                $res['userinfo']=$info;
                $res['usertoken']=db('user_token')->where('user_id='.$info['user_id'])->find();
            }
        }
        
        return $res;
    }
   
 
   
    /**
     *
     * 前端請求這個接口 獲取登陸網址和code
     */
    public function getClientUserLoginCode()
    {
        $code=$this->getLoginCode();//code獲取
        //組裝訪問首頁面的網址
        $url='http://www.***.net/index.php/Api/User/getWechatCode/logincode/'.$code;
 
        $this->success("操作成功",$url);
    }
   
    //創建登陸碼
    public function getLoginCode()
    {
         
        $logincode='';
        
        //校驗下是否有未到期 且可用的
        $map['status']=1;
        $map['expire_time']=['>',time()];
        $info=db('client_logincode')->where($map)->order('create_time desc')->find();
        if(empty($info)) {
        
            //產生
            $logincode=md5($this->createLoginCode());
            
            //存入庫中
            $data=array();
            $data['code']=$logincode;
            $data['create_time']=time();
            $data['expire_time']=time()+300;//5分鍾
            db('client_logincode')->insert($data);
        } else {
            $logincode=$info['code'];
        }
        
        return $logincode;
    }
   
    //登陸碼生成函數
    public function createLoginCode()
    {
   
        $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打亂字符串
        $rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分
        return $rands;
 
    }

整體介紹下

1 因為是掃碼  所以第一步是前端請求 一個函數  生成掃碼使用的網址

在我的這里就是 http://www.***.net/index.php/Api/User/getClientUserLoginCode

 

http://www.***.net/index.php/Api/User/getWechatCode/logincode/1c5932c3b89b8a71db88f5a727b27419

這個是生成的網址  就是掃碼訪問的網址   會提示需要授權登錄  然后獲取token和openid  和表中的數據進行對比

使用了登錄碼做驗證  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM