微信开放平台-第三方平台-全网发布逻辑
easywechat 群里有个小伙伴一直测试不通过,抛砖引玉,直接上代码
/**
* 处理全网发布相关逻辑.
*
* @param Application $openPlatform
* @param $app_id
*
* @throws
*
* @return mixed
*/
private function releaseToNetWork($openPlatform, $app_id)
{
$message = $openPlatform->server->getMessage();
Log::record('全网发布测试开始'.json_encode($message));
//返回API文本消息
if ('text' === $message['MsgType'] && false !== strpos($message['Content'], 'QUERY_AUTH_CODE:')) {
$auth_code = str_replace('QUERY_AUTH_CODE:', '', $message['Content']);
$authorization = $openPlatform->handleAuthorize($auth_code);
$official = $openPlatform->officialAccount($app_id, $authorization['authorization_info']['authorizer_refresh_token']);
$content = $auth_code.'_from_api';
$official['customer_service']->send([
'touser' => $message['FromUserName'],
'msgtype' => 'text',
'text' => [
'content' => $content,
],
]);
//返回普通文本消息
} elseif ('text' === $message['MsgType'] && 'TESTCOMPONENT_MSG_TYPE_TEXT' === $message['Content']) {
$official = $openPlatform->officialAccount($app_id);
$official->server->push(function ($message) {
return 'TESTCOMPONENT_MSG_TYPE_TEXT_callback';
});
//发送事件消息
} elseif ('event' === $message['MsgType']) {
$official = $openPlatform->officialAccount($app_id);
$official->server->push(function ($message) {
return $message['Event'].'from_callback';
});
}
$response = $official->server->serve();
Log::record('全网发布测试结束');
return $response->send();
}
composer.json 文件
{
"require": {
"php": ">=7.1.0",
"topthink/framework": "^6.0.0",
"topthink/think-orm": "^2.0",
"topthink/think-multi-app": "^1.0",
"topthink/think-view": "^1.0",
"overtrue/wechat": "~4.0",
"ext-json": "*",
"predis/predis": "^1.1"
},
"require-dev": {
"symfony/var-dumper": "^4.2",
"topthink/think-trace":"^1.0"
},
"autoload": {
"psr-4": {
"app\\": "app"
}
},
"config": {
"preferred-install": "dist"
},
"scripts": {
"post-autoload-dump": [
"@php think service:discover",
"@php think vendor:publish"
]
}
}
有时候,测试案例不通过,不是代码有问题。
本作品采用《CC 协议》,转载必须注明作者和本文链接