介紹thinkphp5.1 easywechat4微信第三方開放平臺

今天thinkphp框架欄目給大家帶來的主題就是《thinkphp5.1 easywechat4 微信第三方開放平臺》,下面我們來詳細介紹~

thinkphp5.1 easywechat4 微信第三方開放平臺

thinkphp框架

$ composer require overtrue/wechat:~4.0 -vvv
use EasyWeChatFactory;
/**  * 開發平臺授權跳轉  *  * @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(); }
/**  * 開發平臺跳轉授權掃碼頁  *  * @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); }
引入  use EasyWeChatOpenPlatformServerGuard;
/**  * 開發平臺授權回調  *  * @return void  */ public function wxcallback(){     // 這個表是記錄授權成功的     //$Wxpublic   = new Wxpublic;     // 這個表是記錄授權成功后傳過來所屬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;           // 處理授權成功事件-第一次回調     // 閉包方法!里面調用外面的方法請在use里面填寫     $server->push(function ($message) use ($openPlatform /*, $Wxpublic*/) {                  $authCode = $message['AuthorizationCode'];         $res      = $openPlatform->handleAuthorize($authCode);          if($res['authorization_info']['authorizer_refresh_token']){             //授權成功記錄到數據庫             //$Wxpublic->insert(['appid' => $res['authorization_info']['authorizer_appid'] , 'createtime' => time()]);         }      }, Guard::EVENT_AUTHORIZED);      // 處理授權取消事件-第一次回調     // 閉包方法!里面調用外面的方法請在use里面填寫     $server->push(function ($message) use(/*$Wxpublic , $ShopConfig*/) {         //處理數據庫邏輯         //$Wxpublic::appid($message['AppId'])->delete();         //$ShopConfig::appid($message['AppId'])->update(['token' => '']);     }, Guard::EVENT_UNAUTHORIZED);          // 第二次回調會帶一個授權code和自定義參數商城id(uid)     if(isset($get['auth_code']) && isset($get['uid'])){                  $res      = $openPlatform->handleAuthorize($get['auth_code']);         $appid    = $res['authorization_info']['authorizer_appid'];         //數據庫邏輯         //$isConfig = $Wxpublic::appid($appid)->count();                  //if($isConfig){         //$add = $ShopConfig->where('uid' , $get['uid'])->update(['token' => $appid]);         //}     }      return $server->serve(); }
/**  * 網頁授權調起  *  * @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;          // 回調授權地址     $url      = "http://qgcloud.capsui.com/public/index/wxopen/callbackOpenid";     $response = $officialAccount->oauth->scopes(['snsapi_userinfo'])->redirect($url)->send();  }
/**  * 網頁授權回調  *  * @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();          // 注意 這里我是帶參數跳轉到其他TP5項目里面再用緩存處理一下     $url = 'http://quguoshop.capsui.com/public/wxoauthCallback?data=' . json_encode($user->toArray()) . '&token=' . $shopInfo['id'] . '&wxconfig=' . $wxconfig;     $this->redirect($url); }

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享