Laravel EasyWechat 全网发布
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 协议》,转载必须注明作者和本文链接
本帖由 Summer
于 7年前 加精
推荐文章: