Skip to content

Commit bdbaacf

Browse files
XueSiLfPlayer626
authored andcommitted
feat:新增小程序内容安全检测接口
1 parent df90950 commit bdbaacf

File tree

8 files changed

+294
-0
lines changed

8 files changed

+294
-0
lines changed

src/MiniProgram/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @property Auth\Client $auth
2020
* @property Base\Client $base
2121
* @property Broadcast\Client $broadcast
22+
* @property ContentSecurity\Client $contentSecurity
2223
* @property CustomerService\Client $customerService
2324
* @property DataCube\Client $dataCube
2425
* @property Express\Client $express
@@ -49,6 +50,7 @@ class Application extends ServiceContainer
4950
const Auth = 'auth';
5051
const Base = 'base';
5152
const Broadcast = 'broadcast';
53+
const ContentSecurity = 'contentSecurity';
5254
const CustomerService = 'customerService';
5355
const DataCube = "dataCube";
5456
const Express = "express";
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace EasySwoole\WeChat\MiniProgram\ContentSecurity;
4+
5+
use EasySwoole\WeChat\Kernel\Exceptions\InvalidArgumentException;
6+
use EasySwoole\WeChat\Kernel\ServiceProviders;
7+
use EasySwoole\WeChat\MiniProgram\BaseClient;
8+
9+
/**
10+
* Class Client
11+
* @package EasySwoole\WeChat\MiniProgram\ContentSecurity
12+
* @author: XueSi
13+
* @email: <[email protected]>
14+
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html
15+
* desc 内容安全相关接口
16+
*
17+
*/
18+
class Client extends BaseClient
19+
{
20+
/**
21+
* imgSecurityCheck
22+
* 校验一张图片是否含有违法违规内容
23+
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html
24+
*
25+
* @param string $path
26+
* @return mixed
27+
* @throws InvalidArgumentException
28+
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
29+
*/
30+
public function checkImg(string $path)
31+
{
32+
if (!file_exists($path) || !is_readable($path)) {
33+
throw new InvalidArgumentException(sprintf("File does not exist, or the file is unreadable: '%s'", $path));
34+
}
35+
36+
$client = $this->getClient()
37+
->setMethod('POST')
38+
->addFile($path, 'media');
39+
40+
$response = $client->send($this->buildUrl(
41+
'/wxa/img_sec_check',
42+
[
43+
'access_token' => $this->app[ServiceProviders::AccessToken]->getToken(),
44+
]
45+
));
46+
47+
$this->checkResponse($response, $parseData);
48+
49+
return $parseData;
50+
}
51+
52+
/**
53+
* mediaCheckAsync
54+
* 异步校验图片/音频是否含有违法违规内容。
55+
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.mediaCheckAsync.html
56+
*
57+
* @param string $mediaUrl
58+
* @param int $mediaType
59+
* @param string $openId
60+
* @param int $scene
61+
* @param int $version
62+
* @return mixed
63+
* @throws InvalidArgumentException
64+
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
65+
*/
66+
public function checkMediaAsync(string $mediaUrl, int $mediaType, string $openId, int $scene, int $version = 2)
67+
{
68+
if (!in_array($mediaType, [1, 2], true)) {
69+
throw new InvalidArgumentException("The value of parameter mediaType is illegal!");
70+
}
71+
72+
if (!in_array($scene, [1, 2, 3, 4], true)) {
73+
throw new InvalidArgumentException("The value of parameter scene is illegal!");
74+
}
75+
76+
$params = [
77+
'media_url' => $mediaUrl,
78+
'media_type' => $mediaType,
79+
'version' => $version,
80+
'openid' => $openId,
81+
'scene' => $scene
82+
];
83+
84+
$response = $this->getClient()
85+
->setMethod('POST')
86+
->setBody($this->jsonDataToStream($params))
87+
->send($this->buildUrl(
88+
'/wxa/media_check_async',
89+
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()])
90+
);
91+
92+
$this->checkResponse($response, $parseData);
93+
94+
return $parseData;
95+
}
96+
97+
/**
98+
* msgSecurityCheck
99+
* 检查一段文本是否含有违法违规内容。
100+
* doc link: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
101+
*
102+
* @param array $params
103+
* @return mixed
104+
* @throws InvalidArgumentException
105+
* @throws \EasySwoole\WeChat\Kernel\Exceptions\HttpException
106+
*/
107+
public function checkMsg(array $params)
108+
{
109+
if (!isset($params['scene']) || !in_array($params['scene'], [1, 2, 3, 4], true)) {
110+
throw new InvalidArgumentException("The value of parameter scene is illegal!");
111+
}
112+
113+
$params['version'] = 2;
114+
115+
$response = $this->getClient()
116+
->setMethod('POST')
117+
->setBody($this->jsonDataToStream($params))
118+
->send($this->buildUrl(
119+
'/wxa/msg_sec_check',
120+
['access_token' => $this->app[ServiceProviders::AccessToken]->getToken()])
121+
);
122+
123+
$this->checkResponse($response, $parseData);
124+
125+
return $parseData;
126+
}
127+
}
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\MiniProgram\ContentSecurity;
4+
5+
use EasySwoole\WeChat\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::ContentSecurity] = function ($app) {
14+
return new Client($app);
15+
};
16+
}
17+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: XueSi
5+
* Email: <[email protected]>
6+
* Date: 2021/12/01
7+
* Time: 20:13
8+
*/
9+
10+
namespace EasySwoole\WeChat\Tests\MiniProgram\ContentSecurity;
11+
12+
use EasySwoole\WeChat\Kernel\ServiceContainer;
13+
use EasySwoole\WeChat\MiniProgram\ContentSecurity\Client;
14+
use EasySwoole\WeChat\Tests\Mock\Message\Status;
15+
use EasySwoole\WeChat\Tests\TestCase;
16+
use Psr\Http\Message\ServerRequestInterface;
17+
18+
class ClientTest extends TestCase
19+
{
20+
public function testCheckImg()
21+
{
22+
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('checkImg.json'));
23+
24+
$app = $this->mockAccessToken(new ServiceContainer([
25+
'appId' => 'mock_appid',
26+
'appSecret' => 'mock_secret'
27+
]));
28+
29+
$app = $this->mockHttpClient(function (ServerRequestInterface $request) {
30+
$this->assertEquals('POST', $request->getMethod());
31+
$this->assertEquals('/wxa/img_sec_check', $request->getUri()->getPath());
32+
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
33+
}, $response, $app);
34+
35+
$client = new Client($app);
36+
37+
$ret = $client->checkImg(__DIR__ . '/mock_data/mock_image.jpg');
38+
39+
$this->assertIsArray($ret);
40+
41+
$this->assertSame(json_decode($this->readMockResponseJson('checkImg.json'), true), $ret);
42+
}
43+
44+
public function testCheckMediaAsync()
45+
{
46+
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('checkMediaAsync.json'));
47+
48+
$app = $this->mockAccessToken(new ServiceContainer([
49+
'appId' => 'mock_appid',
50+
'appSecret' => 'mock_secret'
51+
]));
52+
53+
$app = $this->mockHttpClient(function (ServerRequestInterface $request) {
54+
$this->assertEquals('POST', $request->getMethod());
55+
$this->assertEquals('/wxa/media_check_async', $request->getUri()->getPath());
56+
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
57+
}, $response, $app);
58+
59+
$client = new Client($app);
60+
61+
$mediaUrl = 'https://developers.weixin.qq.com/miniprogram/assets/images/[email protected]';
62+
$mediaType = 2;
63+
$openId = 'OPENID';
64+
$scene = 1;
65+
66+
$ret = $client->checkMediaAsync($mediaUrl, $mediaType, $openId, $scene);
67+
68+
$this->assertIsArray($ret);
69+
70+
$this->assertSame(json_decode($this->readMockResponseJson('checkMediaAsync.json'), true), $ret);
71+
}
72+
73+
public function testCheckMsg()
74+
{
75+
$response = $this->buildResponse(Status::CODE_OK, $this->readMockResponseJson('checkMsg.json'));
76+
77+
$app = $this->mockAccessToken(new ServiceContainer([
78+
'appId' => 'mock_appid',
79+
'appSecret' => 'mock_secret'
80+
]));
81+
82+
$app = $this->mockHttpClient(function (ServerRequestInterface $request) {
83+
$this->assertEquals('POST', $request->getMethod());
84+
$this->assertEquals('/wxa/msg_sec_check', $request->getUri()->getPath());
85+
$this->assertEquals('access_token=mock_access_token', $request->getUri()->getQuery());
86+
}, $response, $app);
87+
88+
$client = new Client($app);
89+
90+
$ret = $client->checkMsg([
91+
'openid' => 'OPENID',
92+
'scene' => 1,
93+
'content' => 'hello world!'
94+
]);
95+
96+
$this->assertIsArray($ret);
97+
98+
$this->assertSame(json_decode($this->readMockResponseJson('checkMsg.json'), true), $ret);
99+
}
100+
101+
private function readMockResponseJson($filename)
102+
{
103+
return file_get_contents(__DIR__ . '/mock_data/' . $filename);
104+
}
105+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"errcode": 0,
3+
"errmsg": "ok"
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"errcode": 0,
3+
"errmsg": "ok",
4+
"trace_id": "967e945cd8a3e458f3c74dcb886068e9"
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"errcode": 0,
3+
"errmsg": "ok",
4+
"result": {
5+
"suggest": "risky",
6+
"label": 20001
7+
},
8+
"detail": [
9+
{
10+
"strategy": "content_model",
11+
"errcode": 0,
12+
"suggest": "risky",
13+
"label": 20006,
14+
"prob": 90
15+
},
16+
{
17+
"strategy": "keyword",
18+
"errcode": 0,
19+
"suggest": "pass",
20+
"label": 20006,
21+
"level": 20,
22+
"keyword": "命中的关键词1"
23+
},
24+
{
25+
"strategy": "keyword",
26+
"errcode": 0,
27+
"suggest": "risky",
28+
"label": 20006,
29+
"level": 90,
30+
"keyword": "命中的关键词2"
31+
}
32+
],
33+
"trace_id": "60ae120f-371d5872-7941a05b"
34+
}
905 Bytes
Loading

0 commit comments

Comments
 (0)