Skip to content

Commit

Permalink
Merge pull request #177 from XueSiLf/2.x
Browse files Browse the repository at this point in the history
fix(OpenPlatform):privacy inteface api
  • Loading branch information
XueSiLf authored May 27, 2022
2 parents 446008b + 8551e5d commit 3062347
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface;

use EasySwoole\WeChat\Kernel\ServiceProviders;
use EasySwoole\WeChat\OpenPlatform\BaseClient;

class Client extends BaseClient
{
/**
* 代小程序实现业务 - 申请隐私接口 - 获取接口列表
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/apply_api/get_privacy_interface.html
*
* @return bool
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
*/
public function get()
{
$response = $this->getClient()
->setMethod("GET")
->send($this->buildUrl(
"/wxa/security/get_privacy_interface",
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
));

$this->checkResponse($response, $jsonData);

return $jsonData;
}

/**
* 代小程序实现业务 - 申请隐私接口 - 申请接口
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/apply_api/apply_privacy_interface.html
*
* @param array $params
*
* @return bool
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
*/
public function apply(array $params)
{
$response = $this->getClient()
->setMethod("POST")
->setHeaders(['content-type' => 'application/json'])
->setBody($this->jsonDataToStream($params))
->send($this->buildUrl(
"/wxa/security/apply_privacy_interface",
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
));

$this->checkResponse($response, $jsonData);

return $jsonData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface;

use EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\Application;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

class ServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app[Application::PrivacyInterface] = function ($app) {
return new Client($app);
};
}
}
25 changes: 16 additions & 9 deletions src/OpenPlatform/Authorizer/MiniProgram/QrCodeJump/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\QrCodeJump;

use EasySwoole\WeChat\Kernel\Psr\Stream;
use EasySwoole\WeChat\Kernel\Psr\StreamResponse;
use EasySwoole\WeChat\Kernel\ServiceProviders;
use EasySwoole\WeChat\OpenPlatform\BaseClient;

Expand Down Expand Up @@ -143,7 +144,7 @@ public function getShorturl(string $longUrl)

/**
* 代小程序实现业务 - 普通链接二维码与小程序码 - 获取 unlimited 小程序码
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/qrcode/getwxacodeunlimit.html
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
*
* @param array $param
* @return mixed
Expand All @@ -159,14 +160,16 @@ public function getUnlimitWxCode(array $param)
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
));

$this->checkResponse($response, $jsonData);
if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
return new StreamResponse($response->getBody());
}

return $jsonData;
return $this->checkResponse($response);
}

/**
* 代小程序实现业务 - 普通链接二维码与小程序码 - 获取小程序码
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/qrcode/getwxacode.html
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html
*
* @param array $param
* @return mixed
Expand All @@ -182,14 +185,16 @@ public function getWxCode(array $param)
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
));

$this->checkResponse($response, $jsonData);
if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
return new StreamResponse($response->getBody());
}

return $jsonData;
return $this->checkResponse($response);
}

/**
* 代小程序实现业务 - 普通链接二维码与小程序码 - 获取小程序二维码
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/qrcode/createwxaqrcode.html
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
*
* @param string $path
* @param int $width
Expand All @@ -209,8 +214,10 @@ public function getWxQrCode(string $path, int $width = 430)
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
));

$this->checkResponse($response, $jsonData);
if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
return new StreamResponse($response->getBody());
}

return $jsonData;
return $this->checkResponse($response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Created by PhpStorm.
* Author: hlh XueSi
* Email: [email protected]
* Date: 2022/5/18 9:28:04
*/
declare(strict_types=1);

namespace EasySwoole\WeChat\Tests\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface;

use EasySwoole\WeChat\Kernel\BaseClient;
use EasySwoole\WeChat\Kernel\ServiceContainer;
use EasySwoole\WeChat\OpenPlatform\Application;
use EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface\Client;
use EasySwoole\WeChat\Tests\Mock\Message\Status;
use EasySwoole\WeChat\Tests\TestCase;
use Psr\Http\Message\ServerRequestInterface;

class ClientTest extends TestCase
{
public function testGet()
{
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('get.json'));

/** @var Application $component */
$component = $this->mockAccessToken(new Application([
'appId' => 'COMPONENT_APPID',
'token' => 'COMPONENT_TOKEN'
]));

$miniProgram = $component->miniProgram(
'mock_app_id', 'mock_refresh_token'
);
$miniProgram = $this->mockAccessToken($miniProgram);

$miniProgram = $this->mockHttpClient(function (ServerRequestInterface $request) {
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/wxa/security/get_privacy_interface', $request->getUri()->getPath());
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
}, $response, $miniProgram);

$client = new Client($miniProgram);

$ret = $client->get();

$this->assertIsArray($ret);
$this->assertSame(json_decode($this->readMockResponseJson('get.json'), true), $ret);
}

public function testApply()
{
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('apply.json'));

/** @var Application $component */
$component = $this->mockAccessToken(new Application([
'appId' => 'COMPONENT_APPID',
'token' => 'COMPONENT_TOKEN'
]));

$miniProgram = $component->miniProgram(
'mock_app_id', 'mock_refresh_token'
);
$miniProgram = $this->mockAccessToken($miniProgram);

$miniProgram = $this->mockHttpClient(function (ServerRequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('/wxa/security/apply_privacy_interface', $request->getUri()->getPath());
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
}, $response, $miniProgram);

$client = new Client($miniProgram);

$params = [
"api_name" => "wx.test",
"content" => "1312",
"pic_list" => [
"pic_url1",
"pic_url2"
],
"video_list" => ["video_url1", "video_ul2"],
"url_list" => ["url1", "url2"]
];

$ret = $client->apply($params);

$this->assertIsArray($ret);
$this->assertSame(json_decode($this->readMockResponseJson('apply.json'), true), $ret);
}

protected function readMockResponseJson(string $filename): string
{
return file_get_contents(__DIR__ . '/mock_data/' . $filename);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errcode": 0,
"errmsg": "getuserriskrank succ",
"audit_id": 123456
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"errcode": 0,
"errmsg": "ok",
"interface_list": [
{
"api_name": "wx.chooseAddress",
"api_ch_name": "获取用户收货地址",
"api_desc": "调起用户编辑收货地址原生界面,并在编辑完成后返回用户选择的地址。",
"status": 1,
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/open-api/address/wx.chooseAddress.html",
"group_name": "地理位置"
},
{
"api_name": "wx.choosePoi",
"api_ch_name": "选择位置,支持模糊定位(精确到市)和精确定位混选",
"api_desc": "选择位置,支持模糊定位和精确定位混选",
"status": 4,
"audit_id": 421610267,
"fail_reason": "小程序内未含有相应使用场景",
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.choosePoi.html",
"group_name": "地理位置"
},
{
"api_name": "wx.getLocation",
"api_ch_name": "获取当前的地理位置、速度 ",
"api_desc": "获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。 ",
"status": 1,
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html ",
"group_name": "地理位置"
},
{
"api_name": "wx.onLocationChange",
"api_ch_name": "监听实时地理位置变化事件",
"api_desc": "监听实时地理位置变化事件。当用户离开小程序后,此接口无法调用。",
"status": 1,
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.onLocationChange.html",
"group_name": "地理位置"
},
{
"api_name": "wx.chooseLocation",
"api_ch_name": "打开地图选择位置",
"api_desc": "打开地图选择位置。",
"status": 1,
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.chooseLocation.html",
"group_name": "地理位置"
}
]
}
Loading

0 comments on commit 3062347

Please sign in to comment.