Skip to content

Commit

Permalink
feat:添加开放平台快速注册个人小程序接口
Browse files Browse the repository at this point in the history
  • Loading branch information
XueSiLf authored and Player626 committed Sep 17, 2021
1 parent 521a62d commit e2cc99b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/OpenPlatform/Authorizer/OfficialAccount/Account/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
65 changes: 63 additions & 2 deletions src/OpenPlatform/Component/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 54 additions & 1 deletion tests/OpenPlatform/Component/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,57 @@ public function testGetRegistrationStatus()

$this->assertTrue($client->getRegistrationStatus($companyName, $legalPersonaWechat, $legalPersonaName));
}
}

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"errcode": 0,
"errmsg": "OK",
"taskid": "xxxxx",
"authorize_url": "https://mp.weixin.qq.com/xxxx",
"status": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"errcode": 0,
"errmsg": "OK",
"taskid": "xxxxx",
"authorize_url": "https://mp.weixin.qq.com/xxxx",
"status": 0
}

0 comments on commit e2cc99b

Please sign in to comment.