代授權(quán)方實(shí)現(xiàn)業(yè)務(wù)
授權(quán)方已經(jīng)把公眾號(hào)、小程序授權(quán)給你的開(kāi)放平臺(tái)第三方平臺(tái)了,接下來(lái)的代授權(quán)方實(shí)現(xiàn)業(yè)務(wù)只需一行代碼即可獲得授權(quán)方實(shí)例。
實(shí)例化
<?php
$config = [
// 開(kāi)放平臺(tái)第三方平臺(tái) APPID
'appId' => 'wxefe41fdeexxxxxx',
// 開(kāi)放平臺(tái)第三方平臺(tái) Token
'token' => 'dczmnau31ea9nzcnxxxxxxxxx',
// 開(kāi)放平臺(tái)第三方平臺(tái) AES Key
'aesKey' => 'easyswoole',
// 開(kāi)放平臺(tái)第三方平臺(tái) Secret
'secret' => 'your-AppSecret'
];
// 開(kāi)放平臺(tái)
$openPlatform = \EasySwoole\WeChat\Factory::openPlatform($config);
獲取授權(quán)方實(shí)例
// 代公眾號(hào)實(shí)現(xiàn)業(yè)務(wù)
$officialAccount = $openPlatform->officialAccount(string $appId, string $refreshToken);
// 代小程序?qū)崿F(xiàn)業(yè)務(wù)
$miniProgram = $openPlatform->miniProgram(string $appId, string $refreshToken);
-
$appId
為授權(quán)方公眾號(hào)APPID
,非開(kāi)放平臺(tái)第三方平臺(tái)APPID
-
$refreshToken
為授權(quán)方的refresh_token
,可通過(guò) 獲取授權(quán)方授權(quán)信息 接口獲得。
幫助授權(quán)方管理開(kāi)放平臺(tái)賬號(hào)
<?php
// 代公眾號(hào)實(shí)現(xiàn)業(yè)務(wù)
$account = $officialAccount->account;
// 代小程序?qū)崿F(xiàn)業(yè)務(wù)
$account = $miniProgram->account;
// 創(chuàng)建開(kāi)放平臺(tái)賬號(hào)
// 并綁定公眾號(hào)或小程序
$result = $account->create();
// 將公眾號(hào)或小程序綁定到指定開(kāi)放平臺(tái)帳號(hào)下
$result = $account->bindTo($openAppId);
// 將公眾號(hào)/小程序從開(kāi)放平臺(tái)帳號(hào)下解綁
$result = $account->unbindFrom($openAppid);
// 獲取公眾號(hào)/小程序所綁定的開(kāi)放平臺(tái)帳號(hào)
$result = $account->getBinding();
授權(quán)第三方平臺(tái)注冊(cè)的開(kāi)放平臺(tái)帳號(hào)只可用于獲取用戶 unionid
實(shí)現(xiàn)用戶身份打通。第三方平臺(tái)不可操作(包括綁定/解綁)通過(guò) open.weixin.qq.com
線上流程注冊(cè)的開(kāi)放平臺(tái)帳號(hào)。公眾號(hào)只可將此權(quán)限集授權(quán)給一個(gè)第三方平臺(tái),授權(quán)互斥。
代碼示例(在 EasySwoole 框架中使用)
使用示例 1:在
App\HttpController\Router.php
(即路由)中使用:
示例代碼如下:
<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\AbstractRouter;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use FastRoute\RouteCollector;
class Router extends AbstractRouter
{
function initialize(RouteCollector $routeCollector)
{
// 假設(shè)你的公眾號(hào)消息與事件接收 URL 為:https://easyswoole.wechat.com/callback?appId=Xxxx ...
$routeCollector->post('/callback', function (Request $request, Response $response) {
$appId = $request->getQueryParam('appId');
// $openPlatform 為你實(shí)例化的開(kāi)放平臺(tái)對(duì)象,此處省略實(shí)例化步驟
$officialAccount = $openPlatform->officialAccount($appId);
// 這里的 server 為授權(quán)方的 server,而不是開(kāi)放平臺(tái)的 server,請(qǐng)注意!!!
$server = $officialAccount->server;
$server->push(function (\EasySwoole\WeChat\Kernel\Contracts\MessageInterface $message) {
return new \EasySwoole\WeChat\Kernel\Messages\Text('Welcome!');
});
// $psr7esponse 是一個(gè)顯式實(shí)現(xiàn)了 PSR-7 的對(duì)象,用戶只需要處理該對(duì)象即可正確響應(yīng)給微信
$psr7Response = $server->serve($request); // Done!
$response->withStatus($psr7Response->getStatusCode());
// PSR-7 的 Header 并不是單純的 k => v 結(jié)構(gòu)
foreach ($psr7Response->getHeaders() as $name => $values) {
$response->withHeader($name, implode(", ", $values));
}
$response->write($psr7Response->getBody()->__toString());
return false;
});
// 調(diào)用授權(quán)方業(yè)務(wù)例子
$routeCollector->get('/how-to-use', function (Request $request, Response $response) {
$officialAccount = $openPlatform->officialAccount('已授權(quán)的公眾號(hào) APPID', 'Refresh-token');
// 獲取用戶列表:
$officialAccount->user->list();
$miniProgram = $openPlatform->miniProgram('已授權(quán)的小程序 APPID', 'Refresh-token');
// 根據(jù) code 獲取 session
$miniProgram->auth->session('js-code');
// 其他同理
return false;
});
}
}
使用示例 2:在
App\HttpController\Index.php
(即控制器類)中使用,用戶可在自定義其他控制器中實(shí)現(xiàn):
假設(shè)你的開(kāi)放平臺(tái)第三方平臺(tái)設(shè)置的授權(quán)事件接收 URL
為: https://easyswoole.wechat.com/openPlatform
(其他事件推送同樣會(huì)推送到這個(gè) URL
)
示例代碼如下:
首先在 App\HttpController\Router.php
中定義路由:
<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\AbstractRouter;
use FastRoute\RouteCollector;
use EasySwoole\WeChat\OpenPlatform\Server\Guard;
class Router extends AbstractRouter
{
function initialize(RouteCollector $routeCollector)
{
// 假設(shè)你的公眾號(hào)消息與事件接收 URL 為:https://easyswoole.wechat.com/callback?appId=Xxxx ...
$routeCollector->post('/callback', '/Index/callback');
// 調(diào)用授權(quán)方業(yè)務(wù)例子
$routeCollector->get('/how-to-use', '/Index/how_to_use');
}
}
然后在 App\HttpController\Index.php
控制器中處理事件:
<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\WeChat\Kernel\Messages\Message;
class Index extends Controller
{
// 假設(shè)你的公眾號(hào)消息與事件接收 URL 為:https://easyswoole.wechat.com/callback?appId=Xxxx ...
public function callback()
{
$appId = $this->request()->getQueryParam('appId');
// $openPlatform 為你實(shí)例化的開(kāi)放平臺(tái)對(duì)象,此處省略實(shí)例化步驟
$officialAccount = $openPlatform->officialAccount($appId);
// 這里的 server 為授權(quán)方的 server,而不是開(kāi)放平臺(tái)的 server,請(qǐng)注意!!!
$server = $officialAccount->server;
$server->push(function (\EasySwoole\WeChat\Kernel\Contracts\MessageInterface $message) {
return new \EasySwoole\WeChat\Kernel\Messages\Text('Welcome!');
});
/** @var \Psr\Http\Message\ServerRequestInterface $psr7Request */
$psr7Request = $this->request();
// $psr7esponse 是一個(gè)顯式實(shí)現(xiàn)了 PSR-7 的對(duì)象,用戶只需要處理該對(duì)象即可正確響應(yīng)給微信
$psr7Response = $server->serve($psr7Request);
$this->response()->withStatus($psr7Response->getStatusCode());
// PSR-7 的 Header 并不是單純的 k => v 結(jié)構(gòu)
foreach ($psr7Response->getHeaders() as $name => $values) {
$this->response()->withHeader($name, implode(", ", $values));
}
$this->response()->write($psr7Response->getBody()->__toString());
}
// 調(diào)用授權(quán)方業(yè)務(wù)例子
public function how_to_use()
{
$officialAccount = $openPlatform->officialAccount('已授權(quán)的公眾號(hào) APPID', 'Refresh-token');
// 獲取用戶列表:
$officialAccount->user->list();
$miniProgram = $openPlatform->miniProgram('已授權(quán)的小程序 APPID', 'Refresh-token');
// 根據(jù) code 獲取 session
$miniProgram->auth->session('js-code');
// 其他同理
}
}