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 協議》,轉載必須註明作者和本文連結