您好,登錄后才能下訂單哦!
小編給大家分享一下thinkphp5.1 easywechat4微信第三方開放平臺的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
thinkphp5.1 easywechat4 微信第三方開放平臺
當前商城(uid標識)授權(quán)第三方開發(fā)平臺.
網(wǎng)頁授權(quán)成功后跳轉(zhuǎn)到另一個商城項目鏈接并帶上當前微信用戶信息和微信初始化驗證簽名.
安裝easywechat4
$ composer require overtrue/wechat:~4.0 -vvv
引用
use EasyWeChat\Factory;
創(chuàng)建一個跳轉(zhuǎn)到微信掃二維碼授權(quán)頁面
/** * 開發(fā)平臺授權(quán)跳轉(zhuǎn) * * @return void */ public function accessView(){ // $uid = Request()->route('uid' , 0); $url = 'http://qgcloud.capsui.com/public/index/wxopen/config?uid=' . $uid; $this->assign('url' , $url); return $this->fetch(); }
跳轉(zhuǎn)方法(為什么我不寫到上一個方法呢 因為微信要求同一個地址)
/** * 開發(fā)平臺跳轉(zhuǎn)授權(quán)掃碼頁 * * @return void */ public function config(){ $uid = Request()->get('uid' , 0); $config = [ 'app_id' => '開放平臺第三方平臺 APPID', 'secret' => '開放平臺第三方平臺 Secret', 'token' => '開放平臺第三方平臺 Token', 'aes_key' => '開放平臺第三方平臺 AES Key' ]; $openPlatform = Factory::openPlatform($config); $url = $openPlatform->getPreAuthorizationUrl('http://qgcloud.capsui.com/public/index/wxopen/wxcallback?uid=' . $uid); $this->redirect($url); }
授權(quán)回調(diào)(注意:掃碼確認授權(quán)后他第一次回調(diào)不會帶uid參數(shù),)
引入 use EasyWeChat\OpenPlatform\Server\Guard;
/** * 開發(fā)平臺授權(quán)回調(diào) * * @return void */ public function wxcallback(){ // 這個表是記錄授權(quán)成功的 //$Wxpublic = new Wxpublic; // 這個表是記錄授權(quán)成功后傳過來所屬uid商城綁定appid //$ShopConfig = new ShopConfig; $get = Request()->param(); $config = [ 'app_id' => '開放平臺第三方平臺 APPID', 'secret' => '開放平臺第三方平臺 Secret', 'token' => '開放平臺第三方平臺 Token', 'aes_key' => '開放平臺第三方平臺 AES Key' ]; $openPlatform = Factory::openPlatform($config); $server = $openPlatform->server; // 處理授權(quán)成功事件-第一次回調(diào) // 閉包方法!里面調(diào)用外面的方法請在use里面填寫 $server->push(function ($message) use ($openPlatform /*, $Wxpublic*/) { $authCode = $message['AuthorizationCode']; $res = $openPlatform->handleAuthorize($authCode); if($res['authorization_info']['authorizer_refresh_token']){ //授權(quán)成功記錄到數(shù)據(jù)庫 //$Wxpublic->insert(['appid' => $res['authorization_info']['authorizer_appid'] , 'createtime' => time()]); } }, Guard::EVENT_AUTHORIZED); // 處理授權(quán)取消事件-第一次回調(diào) // 閉包方法!里面調(diào)用外面的方法請在use里面填寫 $server->push(function ($message) use(/*$Wxpublic , $ShopConfig*/) { //處理數(shù)據(jù)庫邏輯 //$Wxpublic::appid($message['AppId'])->delete(); //$ShopConfig::appid($message['AppId'])->update(['token' => '']); }, Guard::EVENT_UNAUTHORIZED); // 第二次回調(diào)會帶一個授權(quán)code和自定義參數(shù)商城id(uid) if(isset($get['auth_code']) && isset($get['uid'])){ $res = $openPlatform->handleAuthorize($get['auth_code']); $appid = $res['authorization_info']['authorizer_appid']; //數(shù)據(jù)庫邏輯 //$isConfig = $Wxpublic::appid($appid)->count(); //if($isConfig){ //$add = $ShopConfig->where('uid' , $get['uid'])->update(['token' => $appid]); //} } return $server->serve(); }
/** * 網(wǎng)頁授權(quán)調(diào)起 * * @return void */ public function htmlAccess(){ $appid = Request()->get('appid' , 0); $config = [ 'app_id' => '開放平臺第三方平臺 APPID', 'secret' => '開放平臺第三方平臺 Secret', 'token' => '開放平臺第三方平臺 Token', 'aes_key' => '開放平臺第三方平臺 AES Key' ]; $openPlatform = Factory::openPlatform($config); $data = $openPlatform->getAuthorizer($appid); $appid = $data['authorization_info']['authorizer_appid']; $refreshToken = $data['authorization_info']['authorizer_refresh_token']; $officialAccount = $openPlatform->officialAccount($appid , $refreshToken); $oauth = $officialAccount->oauth; // 回調(diào)授權(quán)地址 $url = "http://qgcloud.capsui.com/public/index/wxopen/callbackOpenid"; $response = $officialAccount->oauth->scopes(['snsapi_userinfo'])->redirect($url)->send(); }
網(wǎng)頁授權(quán)回調(diào)方法
/** * 網(wǎng)頁授權(quán)回調(diào) * * @return void */ public function callbackOpenid(){ $appid = Request()->get('appid' , null); $config = [ 'app_id' => '開放平臺第三方平臺 APPID', 'secret' => '開放平臺第三方平臺 Secret', 'token' => '開放平臺第三方平臺 Token', 'aes_key' => '開放平臺第三方平臺 AES Key' ]; $openPlatform = Factory::openPlatform($config); $data = $openPlatform->getAuthorizer($appid); $appid = $data['authorization_info']['authorizer_appid']; $refreshToken = $data['authorization_info']['authorizer_refresh_token']; // 獲取微信用戶信息 如openid nickname等信息 $officialAccount = $openPlatform->officialAccount($appid , $refreshToken); $oauth = $officialAccount->oauth; $user = $oauth->user(); // 處理wxconfig初始化JSSDK $officialAccount->jssdk->setUrl('http://quguoshop.capsui.com/'); $wxconfig = $officialAccount->jssdk->buildConfig(['chooseWXPay'], $debug = true, $beta = false, $json = true); $ShopConfig = new ShopConfig; $shopInfo = $ShopConfig::appid($appid)->find(); // 注意 這里我是帶參數(shù)跳轉(zhuǎn)到其他TP5項目里面再用緩存處理一下 $url = 'http://quguoshop.capsui.com/public/wxoauthCallback?data=' . json_encode($user->toArray()) . '&token=' . $shopInfo['id'] . '&wxconfig=' . $wxconfig; $this->redirect($url); }
以上是“thinkphp5.1 easywechat4微信第三方開放平臺的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。