1. 程式人生 > >基於TP5的微信的公眾號獲取登入使用者資訊

基於TP5的微信的公眾號獲取登入使用者資訊

之前講過微信的公眾號自動登入的選單配置,這次記錄一下在TP5專案中獲取自動登入的使用者資訊並存到資料庫的操作

基本的流程為:微信設定自動登入的選單—>訪問的URL指定的函式裡獲取使用者資訊—>將資訊存到資料庫中->返回檢視

也可以參考下列流程圖:

 

接下來我們一步步編寫程式碼。

 

一. 在公眾號內設定選單,可參考我的另一篇文章:公眾號開啟網頁自動登陸配置

二. 在配置檔案(config.php)中加上公眾號資訊

    // 公眾號配置
    'wechat'           => [ 
        
        // AppId
        'AppId'               => "你的公眾號AppId", // 微信支付APPID
        // AppSecret
        'AppSecret'           => "你的公眾號AppSecret", // 公眾帳號secert (公眾號支付專用)
       
    ],

三. 在指定的URL對應的函式裡編寫程式碼

     3.1 引入Session類和配置檔案類,例項化配置檔案類

use \think\Session;
use \think\Config;
    // 獲取配置檔案裡的資訊
    $wx_config = Config::get('wechat');

     

     3.2 另外編寫一個函式,用於請求微信介面,獲取資料 

    // 用於請求微信介面獲取資料
    function get_by_curl($url,$post = false){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($post){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

 

以下流程直接貼全部完整程式碼

     3.3 獲取頁面url的code引數值,並與AppId和AppSecret組合URL,呼叫get_by_curl()函式獲取使用者openid和access_token

       這裡的code引數是你之前設定的那個選單的url在微信內訪問後,會根據使用者資訊自動生成,跳轉到你的url後會自動加在url後面

     3.4 用openid和access_token組合url,呼叫get_by_curl()函式獲取使用者詳情資訊

     3.5 將使用者資訊取出,根據openid判斷當前使用者是否存在資料庫中,不存在則新增到資料庫

     3.6 如果當前使用者存在且資訊有改變,則改變資料庫中對應的使用者資訊

     3.7 將使用者openid作為標識存到session中

     3.8 返回檢視

        // 獲取頁面URL的CODE引數,判斷是否有值
        if(isset($_GET['code'])) {
            // 獲取openid和access_token
            $app_id = $wx_config['AppId'];
            $app_secret = $wx_config['AppSecret'];
            $code = $_GET['code'];
            // 傳送請求,獲取使用者openid和access_token
            $data = $this->get_by_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$app_id.'&secret='.$app_secret.'&code='.$code.'&grant_type=authorization_code');
            $data = json_decode($data);
            
            // 防止第二次訪問動態連結報錯
            // 判斷是否獲取到當前使用者的openid
            if(isset($data->openid)){
                $open_id = $data->openid;
                $access_token = $data->access_token;

                // 獲取當前使用者資訊
                $user_info = $this->get_by_curl('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN');
                $user_info = json_decode($user_info);

                // 取出使用者資訊
                /*
                *user_openid :使用者openId
                *user_nickname :使用者暱稱
                *user_sex :性別
                *user_province :省
                *user_city :城市
                *user_headimgurl :使用者頭像url
                */
                $user_openid = $user_info->openid;
                $user_nickname = $user_info->nickname;
                $user_sex = $user_info->sex;
                $user_province = $user_info->province;
                $user_city = $user_info->city;
                $user_headimgurl = $user_info->headimgurl;

                // 以下操作可按照自己的需求編寫,這裡只做例子
                
                // 判斷使用者是否存在
                $data_user = model('app\admin\model\User')
                            ->where('open_id','=',$user_openid)
                            ->find();
                if(empty($data_user)){
                    $new_user = model('app\admin\model\User');
                    $new_user->data([
                            'open_id' =>  $user_openid,
                            'name'  =>  $user_nickname,
                            'sex'  =>  $user_sex,
                            'province'  =>  $user_province,
                            'city'  =>  $user_city,
                            'headimgurl'  =>  $user_headimgurl,
                            'join_time' => time(),
                    ]);
                    // 新增使用者到資料庫
                    $new_user->save();
                }



                $cur_user = model('app\admin\model\User')
                            ->where('open_id','=',$user_openid)
                            ->find();

                // 判斷當前使用者是否修改過資訊,若有變動則更新
                if(strcmp($cur_user->name,$user_nickname)!=0||strcmp($cur_user->headimgurl,$user_headimgurl)!=0){
                    $cur_user->name = $user_nickname;
                    $cur_user->headimgurl = $user_headimgurl;
                    // 更新當前使用者資訊
                    $cur_user->save();
                }

                // 將當前使用者openid作為標識存到session裡
                Session::set('open_id', $cur_user->open_id);

            }
            
        }
return $this->view->fetch('index');

 

至此就完成了公眾號網頁獲取當前使用者資訊,並儲存到資料庫的操作

注意:如果無法獲得資料,請檢查AppIDAppSecret是否正確,同時,還要檢查是否給你訪問的網頁域名授權