diff --git a/composer.json b/composer.json index dae339e..1f6c18a 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ ], "require": { "php": ">=7.1.0", + "ext-json": "*", "ext-swoole": "^4.4.6", "easyswoole/component": ">=1.0", "easyswoole/spl": "^1.1", diff --git a/src/Bean/OfficialAccount/QrCodeRequest.php b/src/Bean/OfficialAccount/QrCodeRequest.php index a715829..a87d962 100644 --- a/src/Bean/OfficialAccount/QrCodeRequest.php +++ b/src/Bean/OfficialAccount/QrCodeRequest.php @@ -103,4 +103,32 @@ public function setSceneStr($scene_str): void { $this->scene_str = $scene_str; } + + /** + * @return array + */ + public function buildRequest(): array + { + if (is_string($this->getActionInfo())) { + $this->action_info = json_decode($this->action_info, true); + } + + if (!is_null($this->getActionInfo())) { + return parent::toArray(['action_name', 'action_info']); + } + + $scene = []; + if (!is_null($this->getSceneId())) { + $scene['scene_id'] = (int)$this->getSceneId(); + } + + if (!is_null($this->getSceneStr())) { + $scene['scene_str'] = (string)$this->getSceneStr(); + } + + $this->action_info = [ + 'scene' => $scene + ]; + return parent::toArray(['action_name', 'action_info']); + } } \ No newline at end of file diff --git a/src/OfficialAccount/QrCode.php b/src/OfficialAccount/QrCode.php index 4451487..6374b2c 100644 --- a/src/OfficialAccount/QrCode.php +++ b/src/OfficialAccount/QrCode.php @@ -8,9 +8,11 @@ namespace EasySwoole\WeChat\OfficialAccount; +use EasySwoole\HttpClient\Exception\InvalidUrl; use EasySwoole\WeChat\Bean\OfficialAccount\QrCodeRequest; use EasySwoole\WeChat\Bean\OfficialAccount\QrCode as QrCodeBean; use EasySwoole\WeChat\Exception\OfficialAccountError; +use EasySwoole\WeChat\Exception\RequestError; use EasySwoole\WeChat\Utility\NetWork; class QrCode extends OfficialAccountBase @@ -20,14 +22,15 @@ class QrCode extends OfficialAccountBase * @param QrCodeRequest $codeRequest * @return QrCodeBean|null * @throws OfficialAccountError - * @throws \EasySwoole\WeChat\Exception\RequestError + * @throws InvalidUrl + * @throws RequestError */ function getTick(QrCodeRequest $codeRequest): ?QrCodeBean { $token = $this->getOfficialAccount()->accessToken()->getToken(); $response = NetWork::postJsonForJson(ApiUrl::generateURL(ApiUrl::QRCODE_CREATE, [ 'ACCESS_TOKEN' => $token - ]), $codeRequest->toArray()); + ]), $codeRequest->buildRequest()); $ex = OfficialAccountError::hasException($response); if ($ex) {