Laravel + overtrue/wechat 開發微信第三方平臺
說明
本文使用的是 overtrue/wechat 的開發版本(即最新的4.0版本)
開始之前
整合到Laravel中
1.安裝 overture/wechat
composer require "overtrue/wechat:dev-master"
2.安裝 overtrue/laravel-wechat
composer require "overtrue/laravel-wechat:dev-master"
3.釋出配置檔案
php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
4.修改配置檔案
全網釋出
本文中使用的 域名為 wx.test.com,實際使用中請替換成自己的域名。
第三方管理平臺,假設有如下配置
授權事件接收URL http://wx.test.com/openPlatform/serve
公眾號訊息與事件接收URL http://wx.test.com/api/wx/openPlatform/officialAccount/events?appid=/$APPID$
微信全網釋出驗證
-
元件Ticket正確接收
- 內建,只需要如上 修改配置檔案 即可
-
生成預授權碼
- 無需實現
-
獲取授權code
- 無需實現
-
授權
- 無需實現
-
返回Api文字訊息
- 自己實現
-
返回普通文字訊息
- 自己實現
-
傳送事件訊息
- 自己實現
-
取消授權
- 無需實現
本文中有如下類 OpenPlatformAPIController 用於處理相關請求,程式碼如下
/**
* 公眾號訊息與事件接收
* @param Request $request
* @return \Illuminate\Http\JsonResponse|mixed|\Symfony\Component\HttpFoundation\Response
*/
public function officialAccountEvents(Request $request)
{
/** @var \EasyWeChat\OpenPlatform\Application $open_platform */
$open_platform = EasyWeChat::openPlatform();
$authorizer_appid = substr($request->get('appid'), 1);
/**
* 全網釋出
*/
if ($authorizer_appid == 'wx570bc396a51b8ff8') {
return $this->releaseToNetWork($open_platform, $authorizer_appid);
}
/**
* 正常業務,根據實際需求自己實現
*/
$official_account = WeChatAuthorizer::where('authorizer_appid', '=', $authorizer_appid)->first();
if (empty($official_account)) {
return $this->sendResponse(null, 'official account not authorization');
}
$official_account_client = $open_platform->officialAccount($official_account->authorizer_appid, $official_account->authorizer_refresh_token);
$server = $official_account_client->server;
/**
* 簡單的處理 文字訊息和事件
*/
$server->push(TextMessageHandler::class, Message::TEXT);
$server->push(EventMessageHandler::class, Message::EVENT);
$response = $server->serve();
return $response;
}
全網釋出需要自己實現的部分
/**
* 處理全網釋出相關邏輯
* @param \EasyWeChat\OpenPlatform\Application $open_platform
* @param $authorizer_appid
* @return mixed
*/
private function releaseToNetWork($open_platform, $authorizer_appid)
{
$message = $open_platform->server->getMessage();
//返回API文字訊息
if ($message['MsgType'] == 'text' && strpos($message['Content'], "QUERY_AUTH_CODE:") !== false) {
$auth_code = str_replace("QUERY_AUTH_CODE:", "", $message['Content']);
$authorization = $open_platform->handleAuthorize($auth_code);
$official_account_client = $open_platform->officialAccount($authorizer_appid, $authorization['authorization_info']['authorizer_refresh_token']);
$content = $auth_code . '_from_api';
$official_account_client['customer_service']->send([
'touser' => $message['FromUserName'],
'msgtype' => 'text',
'text' => [
'content' => $content
]
]);
//返回普通文字訊息
} elseif ($message['MsgType'] == 'text' && $message['Content'] == 'TESTCOMPONENT_MSG_TYPE_TEXT') {
$official_account_client = $open_platform->officialAccount($authorizer_appid);
$official_account_client->server->push(function ($message) {
return $message['Content'] . "_callback";
});
//傳送事件訊息
} elseif ($message['MsgType'] == 'event') {
$official_account_client = $open_platform->officialAccount($authorizer_appid);
$official_account_client->server->push(function ($message) {
return $message['Event'] . 'from_callback';
});
}
$response = $official_account_client->server->serve();
return $response;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結