[更新]修改第三方消息处理机制

This commit is contained in:
Anyon 2019-07-15 19:25:46 +08:00
parent 05f14ef416
commit 1717fcfd85
2 changed files with 12 additions and 8 deletions

View File

@ -52,11 +52,15 @@ class PublishHandler
case 'text': case 'text':
$receive = $wechat->getReceive(); $receive = $wechat->getReceive();
if ($receive['Content'] === 'TESTCOMPONENT_MSG_TYPE_TEXT') { if ($receive['Content'] === 'TESTCOMPONENT_MSG_TYPE_TEXT') {
return $wechat->text('TESTCOMPONENT_MSG_TYPE_TEXT_callback')->reply([], true); $xml = $wechat->text('TESTCOMPONENT_MSG_TYPE_TEXT_callback')->reply([], true);
p('====测试回复文本======');
p($xml);
return $xml;
} else {
$key = str_replace("QUERY_AUTH_CODE:", '', $receive['Content']);
WechatService::instance('Service')->getQueryAuthorizerInfo($key);
return $wechat->text("{$key}_from_api")->reply([], true);
} }
$key = str_replace("QUERY_AUTH_CODE:", '', $receive['Content']);
WechatService::instance('Service')->getQueryAuthorizerInfo($key);
return $wechat->text("{$key}_from_api")->reply([], true);
case 'event': case 'event':
$receive = $wechat->getReceive(); $receive = $wechat->getReceive();
return $wechat->text("{$receive['Event']}from_callback")->reply([], true); return $wechat->text("{$receive['Event']}from_callback")->reply([], true);

View File

@ -39,7 +39,7 @@ class ReceiveHandler
public static function handler($appid) public static function handler($appid)
{ {
try { try {
$service = WechatService::WeChatReceive($appid); $wechat = WechatService::WeChatReceive($appid);
} catch (\Exception $e) { } catch (\Exception $e) {
return "Wechat message handling failed, {$e->getMessage()}"; return "Wechat message handling failed, {$e->getMessage()}";
} }
@ -50,12 +50,12 @@ class ReceiveHandler
return $message; return $message;
} }
try { try {
list($data, $openid) = [$service->getReceive(), $service->getOpenid()]; list($data, $openid) = [$wechat->getReceive(), $wechat->getOpenid()];
if (isset($data['EventKey']) && is_object($data['EventKey'])) $data['EventKey'] = (array)$data['EventKey']; if (isset($data['EventKey']) && is_object($data['EventKey'])) $data['EventKey'] = (array)$data['EventKey'];
$input = ['openid' => $openid, 'appid' => $appid, 'receive' => serialize($data), 'encrypt' => intval($service->isEncrypt())]; $input = ['openid' => $openid, 'appid' => $appid, 'receive' => serialize($data), 'encrypt' => intval($wechat->isEncrypt())];
if (is_string($result = http_post($config['appuri'], $input, ['timeout' => 30]))) { if (is_string($result = http_post($config['appuri'], $input, ['timeout' => 30]))) {
if (is_array($json = json_decode($result, true))) { if (is_array($json = json_decode($result, true))) {
$xml = $service->reply($json, true, true); $xml = $wechat->reply($json, true, $wechat->isEncrypt());
p(""); p("");
p("====== 准备回复的内容如下 ========"); p("====== 准备回复的内容如下 ========");
p($xml); p($xml);