Skip to content

Commit 3062347

Browse files
authored
Merge pull request #177 from XueSiLf/2.x
fix(OpenPlatform):privacy inteface api
2 parents 446008b + 8551e5d commit 3062347

File tree

11 files changed

+264
-51
lines changed

11 files changed

+264
-51
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface;
4+
5+
use EasySwoole\WeChat\Kernel\ServiceProviders;
6+
use EasySwoole\WeChat\OpenPlatform\BaseClient;
7+
8+
class Client extends BaseClient
9+
{
10+
/**
11+
* 代小程序实现业务 - 申请隐私接口 - 获取接口列表
12+
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/apply_api/get_privacy_interface.html
13+
*
14+
* @return bool
15+
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
16+
*/
17+
public function get()
18+
{
19+
$response = $this->getClient()
20+
->setMethod("GET")
21+
->send($this->buildUrl(
22+
"/wxa/security/get_privacy_interface",
23+
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
24+
));
25+
26+
$this->checkResponse($response, $jsonData);
27+
28+
return $jsonData;
29+
}
30+
31+
/**
32+
* 代小程序实现业务 - 申请隐私接口 - 申请接口
33+
* doc link: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/apply_api/apply_privacy_interface.html
34+
*
35+
* @param array $params
36+
*
37+
* @return bool
38+
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
39+
*/
40+
public function apply(array $params)
41+
{
42+
$response = $this->getClient()
43+
->setMethod("POST")
44+
->setHeaders(['content-type' => 'application/json'])
45+
->setBody($this->jsonDataToStream($params))
46+
->send($this->buildUrl(
47+
"/wxa/security/apply_privacy_interface",
48+
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()]
49+
));
50+
51+
$this->checkResponse($response, $jsonData);
52+
53+
return $jsonData;
54+
}
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface;
4+
5+
use EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\Application;
6+
use Pimple\Container;
7+
use Pimple\ServiceProviderInterface;
8+
9+
class ServiceProvider implements ServiceProviderInterface
10+
{
11+
public function register(Container $app)
12+
{
13+
$app[Application::PrivacyInterface] = function ($app) {
14+
return new Client($app);
15+
};
16+
}
17+
}

src/OpenPlatform/Authorizer/MiniProgram/QrCodeJump/Client.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\QrCodeJump;
44

55
use EasySwoole\WeChat\Kernel\Psr\Stream;
6+
use EasySwoole\WeChat\Kernel\Psr\StreamResponse;
67
use EasySwoole\WeChat\Kernel\ServiceProviders;
78
use EasySwoole\WeChat\OpenPlatform\BaseClient;
89

@@ -143,7 +144,7 @@ public function getShorturl(string $longUrl)
143144

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

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

164-
return $jsonData;
167+
return $this->checkResponse($response);
165168
}
166169

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

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

187-
return $jsonData;
192+
return $this->checkResponse($response);
188193
}
189194

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

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

214-
return $jsonData;
221+
return $this->checkResponse($response);
215222
}
216223
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* Author: hlh XueSi
5+
6+
* Date: 2022/5/18 9:28:04
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace EasySwoole\WeChat\Tests\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface;
11+
12+
use EasySwoole\WeChat\Kernel\BaseClient;
13+
use EasySwoole\WeChat\Kernel\ServiceContainer;
14+
use EasySwoole\WeChat\OpenPlatform\Application;
15+
use EasySwoole\WeChat\OpenPlatform\Authorizer\MiniProgram\PrivacyInterface\Client;
16+
use EasySwoole\WeChat\Tests\Mock\Message\Status;
17+
use EasySwoole\WeChat\Tests\TestCase;
18+
use Psr\Http\Message\ServerRequestInterface;
19+
20+
class ClientTest extends TestCase
21+
{
22+
public function testGet()
23+
{
24+
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('get.json'));
25+
26+
/** @var Application $component */
27+
$component = $this->mockAccessToken(new Application([
28+
'appId' => 'COMPONENT_APPID',
29+
'token' => 'COMPONENT_TOKEN'
30+
]));
31+
32+
$miniProgram = $component->miniProgram(
33+
'mock_app_id', 'mock_refresh_token'
34+
);
35+
$miniProgram = $this->mockAccessToken($miniProgram);
36+
37+
$miniProgram = $this->mockHttpClient(function (ServerRequestInterface $request) {
38+
$this->assertEquals('GET', $request->getMethod());
39+
$this->assertEquals('/wxa/security/get_privacy_interface', $request->getUri()->getPath());
40+
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
41+
}, $response, $miniProgram);
42+
43+
$client = new Client($miniProgram);
44+
45+
$ret = $client->get();
46+
47+
$this->assertIsArray($ret);
48+
$this->assertSame(json_decode($this->readMockResponseJson('get.json'), true), $ret);
49+
}
50+
51+
public function testApply()
52+
{
53+
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('apply.json'));
54+
55+
/** @var Application $component */
56+
$component = $this->mockAccessToken(new Application([
57+
'appId' => 'COMPONENT_APPID',
58+
'token' => 'COMPONENT_TOKEN'
59+
]));
60+
61+
$miniProgram = $component->miniProgram(
62+
'mock_app_id', 'mock_refresh_token'
63+
);
64+
$miniProgram = $this->mockAccessToken($miniProgram);
65+
66+
$miniProgram = $this->mockHttpClient(function (ServerRequestInterface $request) {
67+
$this->assertEquals('POST', $request->getMethod());
68+
$this->assertEquals('/wxa/security/apply_privacy_interface', $request->getUri()->getPath());
69+
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
70+
}, $response, $miniProgram);
71+
72+
$client = new Client($miniProgram);
73+
74+
$params = [
75+
"api_name" => "wx.test",
76+
"content" => "1312",
77+
"pic_list" => [
78+
"pic_url1",
79+
"pic_url2"
80+
],
81+
"video_list" => ["video_url1", "video_ul2"],
82+
"url_list" => ["url1", "url2"]
83+
];
84+
85+
$ret = $client->apply($params);
86+
87+
$this->assertIsArray($ret);
88+
$this->assertSame(json_decode($this->readMockResponseJson('apply.json'), true), $ret);
89+
}
90+
91+
protected function readMockResponseJson(string $filename): string
92+
{
93+
return file_get_contents(__DIR__ . '/mock_data/' . $filename);
94+
}
95+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"errcode": 0,
3+
"errmsg": "getuserriskrank succ",
4+
"audit_id": 123456
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"errcode": 0,
3+
"errmsg": "ok",
4+
"interface_list": [
5+
{
6+
"api_name": "wx.chooseAddress",
7+
"api_ch_name": "获取用户收货地址",
8+
"api_desc": "调起用户编辑收货地址原生界面,并在编辑完成后返回用户选择的地址。",
9+
"status": 1,
10+
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/open-api/address/wx.chooseAddress.html",
11+
"group_name": "地理位置"
12+
},
13+
{
14+
"api_name": "wx.choosePoi",
15+
"api_ch_name": "选择位置,支持模糊定位(精确到市)和精确定位混选",
16+
"api_desc": "选择位置,支持模糊定位和精确定位混选",
17+
"status": 4,
18+
"audit_id": 421610267,
19+
"fail_reason": "小程序内未含有相应使用场景",
20+
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.choosePoi.html",
21+
"group_name": "地理位置"
22+
},
23+
{
24+
"api_name": "wx.getLocation",
25+
"api_ch_name": "获取当前的地理位置、速度 ",
26+
"api_desc": "获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。 ",
27+
"status": 1,
28+
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html ",
29+
"group_name": "地理位置"
30+
},
31+
{
32+
"api_name": "wx.onLocationChange",
33+
"api_ch_name": "监听实时地理位置变化事件",
34+
"api_desc": "监听实时地理位置变化事件。当用户离开小程序后,此接口无法调用。",
35+
"status": 1,
36+
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.onLocationChange.html",
37+
"group_name": "地理位置"
38+
},
39+
{
40+
"api_name": "wx.chooseLocation",
41+
"api_ch_name": "打开地图选择位置",
42+
"api_desc": "打开地图选择位置。",
43+
"status": 1,
44+
"api_link": "https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.chooseLocation.html",
45+
"group_name": "地理位置"
46+
}
47+
]
48+
}

0 commit comments

Comments
 (0)