From e2cc99b2a41954723656a1ff698400f1639821be Mon Sep 17 00:00:00 2001 From: XueSi <1592328848@qq.com> Date: Wed, 15 Sep 2021 20:19:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E5=BC=80=E6=94=BE?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=BF=AB=E9=80=9F=E6=B3=A8=E5=86=8C=E4=B8=AA?= =?UTF-8?q?=E4=BA=BA=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OfficialAccount/Account/Client.php | 22 +++++++ src/OpenPlatform/Component/Client.php | 65 ++++++++++++++++++- .../OfficialAccount/Account/ClientTest.php | 27 ++++++++ tests/OpenPlatform/Component/ClientTest.php | 55 +++++++++++++++- .../mock_data/getPersonalWeappStatus.json | 7 ++ .../mock_data/registerPersonalWeapp.json | 7 ++ 6 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 tests/OpenPlatform/Component/mock_data/getPersonalWeappStatus.json create mode 100644 tests/OpenPlatform/Component/mock_data/registerPersonalWeapp.json diff --git a/src/OpenPlatform/Authorizer/OfficialAccount/Account/Client.php b/src/OpenPlatform/Authorizer/OfficialAccount/Account/Client.php index 5e6798e..5f613c0 100644 --- a/src/OpenPlatform/Authorizer/OfficialAccount/Account/Client.php +++ b/src/OpenPlatform/Authorizer/OfficialAccount/Account/Client.php @@ -69,4 +69,26 @@ public function register(string $ticket, float $timeout = 10) $this->checkResponse($response, $data); return $data; } + + /** + * 第三方平台调用快速注册 API 完成管理员换绑 + * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/fast_registration_of_mini_program.html + * + * @param string $taskId + * @return bool + * @throws HttpException + */ + public function componentRebindAdmin(string $taskId) + { + $response = $this->getClient() + ->setMethod("POST") + ->setBody($this->jsonDataToStream([ + 'taskid' => $taskId, + ]))->send($this->buildUrl( + '/cgi-bin/account/componentrebindadmin', + ['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()] + )); + + return $this->checkResponse($response, $data); + } } diff --git a/src/OpenPlatform/Component/Client.php b/src/OpenPlatform/Component/Client.php index 546f2d9..67023b4 100644 --- a/src/OpenPlatform/Component/Client.php +++ b/src/OpenPlatform/Component/Client.php @@ -17,7 +17,7 @@ class Client extends BaseClient /** * 通过法人微信快速创建小程序. * 代注册小程序 - 快速创建小程序 - * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Register_Mini_Programs/Fast_Registration_Interface_document.html + * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/Fast_Registration_Interface_document.html * * @param array $params * @return mixed @@ -42,7 +42,7 @@ public function registerMiniProgram(array $params) /** * 查询创建任务状态. * 代注册小程序 - 快速创建小程序 - 查询创建任务状态 - * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Register_Mini_Programs/Fast_Registration_Interface_document.html + * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/Fast_Registration_Interface_document.html * * @param string $companyName * @param string $legalPersonaWechat @@ -68,4 +68,65 @@ public function getRegistrationStatus(string $companyName, string $legalPersonaW return $this->checkResponse($response); } + + /** + * 快速创建个人小程序 - 创建个人主体小程序 + * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/fastregisterpersonalweapp.html + * + * @param string $idName + * @param string $wxUser + * @param string|null $componentPhone + * @return mixed + * @throws HttpException + */ + public function registerPersonalWeapp(string $idName, string $wxUser, string $componentPhone = null) + { + $param = [ + 'idname' => $idName, + 'wxuser' => $wxUser, + ]; + + if (!is_null($componentPhone)) { + $param['component_phone'] = $componentPhone; + } + + $response = $this->getClient() + ->setMethod("POST") + ->setBody($this->jsonDataToStream($param)) + ->send($this->buildUrl( + "/wxa/component/fastregisterpersonalweapp", + [ + 'action' => 'create', + 'component_access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ] + )); + + $this->checkResponse($response, $data); + return $data; + } + + /** + * 快速创建个人小程序 - 查询创建任务状态 + * doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/fastregisterpersonalweapp.html + * + * @param string $taskId + * @return mixed + * @throws HttpException + */ + public function getPersonalWeappStatus(string $taskId) + { + $response = $this->getClient() + ->setMethod("POST") + ->setBody($this->jsonDataToStream(['taskid' => $taskId])) + ->send($this->buildUrl( + "/wxa/component/fastregisterpersonalweapp", + [ + 'action' => 'query', + 'component_access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ] + )); + + $this->checkResponse($response, $data); + return $data; + } } diff --git a/tests/OpenPlatform/Authorizer/OfficialAccount/Account/ClientTest.php b/tests/OpenPlatform/Authorizer/OfficialAccount/Account/ClientTest.php index 1d94a5a..3cfdc3b 100644 --- a/tests/OpenPlatform/Authorizer/OfficialAccount/Account/ClientTest.php +++ b/tests/OpenPlatform/Authorizer/OfficialAccount/Account/ClientTest.php @@ -64,6 +64,33 @@ public function testRegister() $this->assertSame(json_decode($this->readMockResponseJson('register.json'), true), $ret); } + public function testComponentRebindAdmin() + { + $response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('register.json')); + + /** @var Application $component */ + $component = $this->mockAccessToken(new Application([ + 'appId' => 'COMPONENT_APPID', + 'token' => 'COMPONENT_TOKEN' + ])); + + $officialAccount = $component->miniProgram( + 'mock_app_id', 'mock_refresh_token' + ); + $officialAccount = $this->mockAccessToken($officialAccount); + + $officialAccount = $this->mockHttpClient(function (ServerRequestInterface $request) { + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('/cgi-bin/account/componentrebindadmin', $request->getUri()->getPath()); + $this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery()); + $this->assertEquals('{"taskid":"b25519093b1e97239eff9d2bfc07e08e"}', $request->getBody()->getContents()); + }, $response, $officialAccount); + + $client = new Client($officialAccount, $component); + + $this->assertTrue($client->componentRebindAdmin('b25519093b1e97239eff9d2bfc07e08e')); + } + protected function readMockResponseJson(string $filename): string { return file_get_contents(__DIR__ . '/mock_data/' . $filename); diff --git a/tests/OpenPlatform/Component/ClientTest.php b/tests/OpenPlatform/Component/ClientTest.php index 5020431..6be20e7 100644 --- a/tests/OpenPlatform/Component/ClientTest.php +++ b/tests/OpenPlatform/Component/ClientTest.php @@ -63,4 +63,57 @@ public function testGetRegistrationStatus() $this->assertTrue($client->getRegistrationStatus($companyName, $legalPersonaWechat, $legalPersonaName)); } -} \ No newline at end of file + + public function testRegisterPersonalWeapp() + { + $response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('registerPersonalWeapp.json')); + + $app = $this->mockAccessToken(new ServiceContainer(['appId' => 'mock-componentAppId'])); + + $app = $this->mockHttpClient(function (ServerRequestInterface $request) { + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('/wxa/component/fastregisterpersonalweapp', $request->getUri()->getPath()); + $this->assertEquals('action=create&component_access_token=mock_access_token', $request->getUri()->getQuery()); + $this->assertEquals('{"idname":"tencent","wxuser":"wxidnnn","component_phone":"1234567"}', $request->getBody()->getContents()); + }, $response, $app); + + $client = new Client($app); + + $idName = 'tencent'; // 个人用户名字 + $wxUser = 'wxidnnn'; // 个人用户微信id + $componentPhone = '1234567'; // 第三方联系电话 + + $ret = $client->registerPersonalWeapp($idName, $wxUser, $componentPhone); + + $this->assertIsArray($ret); + + $this->assertSame(json_decode($this->readMockResponseJson('registerPersonalWeapp.json'), true), $ret); + } + + public function testGetPersonalWeappStatus() + { + $response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('getPersonalWeappStatus.json')); + + $app = $this->mockAccessToken(new ServiceContainer(['appId' => 'mock-componentAppId'])); + + $app = $this->mockHttpClient(function (ServerRequestInterface $request) { + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('/wxa/component/fastregisterpersonalweapp', $request->getUri()->getPath()); + $this->assertEquals('action=query&component_access_token=mock_access_token', $request->getUri()->getQuery()); + $this->assertEquals('{"taskid":"xxxxx"}', $request->getBody()->getContents()); + }, $response, $app); + + $client = new Client($app); + + $ret = $client->getPersonalWeappStatus('xxxxx'); + + $this->assertIsArray($ret); + + $this->assertSame(json_decode($this->readMockResponseJson('getPersonalWeappStatus.json'), true), $ret); + } + + protected function readMockResponseJson(string $file): string + { + return file_get_contents(dirname(__FILE__) . '/mock_data/' . $file); + } +} diff --git a/tests/OpenPlatform/Component/mock_data/getPersonalWeappStatus.json b/tests/OpenPlatform/Component/mock_data/getPersonalWeappStatus.json new file mode 100644 index 0000000..186f394 --- /dev/null +++ b/tests/OpenPlatform/Component/mock_data/getPersonalWeappStatus.json @@ -0,0 +1,7 @@ +{ + "errcode": 0, + "errmsg": "OK", + "taskid": "xxxxx", + "authorize_url": "https://mp.weixin.qq.com/xxxx", + "status": 0 +} diff --git a/tests/OpenPlatform/Component/mock_data/registerPersonalWeapp.json b/tests/OpenPlatform/Component/mock_data/registerPersonalWeapp.json new file mode 100644 index 0000000..186f394 --- /dev/null +++ b/tests/OpenPlatform/Component/mock_data/registerPersonalWeapp.json @@ -0,0 +1,7 @@ +{ + "errcode": 0, + "errmsg": "OK", + "taskid": "xxxxx", + "authorize_url": "https://mp.weixin.qq.com/xxxx", + "status": 0 +}