Skip to content

Commit 8e97b77

Browse files
borisbaobinarywang
authored andcommitted
#1217 小程序增加校验图片/音频是否含有违法违规内容的接口
* 添加 微信内容异步检测接口 * 消息route 增加 title 参数
1 parent 779f1d0 commit 8e97b77

File tree

6 files changed

+103
-10
lines changed

6 files changed

+103
-10
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaSecCheckService.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.binarywang.wx.miniapp.api;
22

3+
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
34
import java.io.File;
45

56
import me.chanjar.weixin.common.error.WxErrorException;
@@ -18,6 +19,8 @@ public interface WxMaSecCheckService {
1819

1920
String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check";
2021

22+
String MEDIA_CHECK_ASYNC_URL = "https://api.weixin.qq.com/wxa/media_check_async";
23+
2124
/**
2225
* <pre>
2326
* 校验一张图片是否含有违法违规内容.
@@ -39,5 +42,25 @@ public interface WxMaSecCheckService {
3942
* 详情请见: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/sec-check/msgSecCheck.html
4043
* </pre>
4144
*/
42-
boolean checkMessage(String msgString);
45+
boolean checkMessage(String msgString) throws WxErrorException;
46+
47+
48+
/**
49+
* <pre>
50+
* 异步校验图片/音频是否含有违法违规内容。
51+
* 应用场景举例:
52+
* 语音风险识别:社交类用户发表的语音内容检测;
53+
* 图片智能鉴黄:涉及拍照的工具类应用(如美拍,识图类应用)用户拍照上传检测;电商类商品上架图片检测;媒体类用户文章里的图片检测等;
54+
* 敏感人脸识别:用户头像;媒体类用户文章里的图片检测;社交类用户上传的图片检测等。
55+
* 频率限制:
56+
* 单个 appId 调用上限为 2000 次/分钟,200,000 次/天;文件大小限制:单个文件大小不超过10M
57+
* 详情请见:
58+
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.mediaCheckAsync.html
59+
* </pre>
60+
* @param mediaUrl 要检测的多媒体url
61+
* @param mediaType 媒体类型,{@link cn.binarywang.wx.miniapp.constant.WxMaConstants.SecCheckMediaType}
62+
* @return
63+
*/
64+
WxMaMediaAsyncCheckResult mediaCheckAsync(String mediaUrl,int mediaType) throws WxErrorException;
65+
4366
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaSecCheckServiceImpl.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
44
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
56
import com.google.gson.JsonObject;
7+
import java.io.File;
68
import lombok.AllArgsConstructor;
79
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
810
import me.chanjar.weixin.common.error.WxErrorException;
911
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
1012

11-
import java.io.File;
12-
1313
/**
1414
* <pre>
1515
*
@@ -20,6 +20,7 @@
2020
*/
2121
@AllArgsConstructor
2222
public class WxMaSecCheckServiceImpl implements WxMaSecCheckService {
23+
2324
private WxMaService service;
2425

2526
@Override
@@ -31,16 +32,24 @@ public boolean checkImage(File file) throws WxErrorException {
3132
}
3233

3334
@Override
34-
public boolean checkMessage(String msgString) {
35+
public boolean checkMessage(String msgString) throws WxErrorException {
3536
JsonObject jsonObject = new JsonObject();
3637
jsonObject.addProperty("content", msgString);
37-
try {
38-
this.service.post(MSG_SEC_CHECK_URL, jsonObject.toString());
39-
} catch (WxErrorException e) {
40-
return false;
41-
}
38+
39+
this.service.post(MSG_SEC_CHECK_URL, jsonObject.toString());
4240

4341
return true;
4442
}
4543

44+
@Override
45+
public WxMaMediaAsyncCheckResult mediaCheckAsync(String mediaUrl, int mediaType)
46+
throws WxErrorException {
47+
JsonObject jsonObject = new JsonObject();
48+
jsonObject.addProperty("media_url", mediaUrl);
49+
jsonObject.addProperty("media_type", mediaType);
50+
51+
return WxMaMediaAsyncCheckResult
52+
.fromJson(this.service.post(MEDIA_CHECK_ASYNC_URL, jsonObject.toString()));
53+
}
54+
4655
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import java.io.Serializable;
6+
7+
public class WxMaMediaAsyncCheckResult implements Serializable {
8+
9+
private static final long serialVersionUID = 3928132365399916183L;
10+
11+
/**
12+
* 任务id,用于匹配异步推送结果
13+
*/
14+
@SerializedName("trace_id")
15+
private String traceId;
16+
17+
18+
public static WxMaMediaAsyncCheckResult fromJson(String json) {
19+
return WxMaGsonBuilder.create().fromJson(json, WxMaMediaAsyncCheckResult.class);
20+
}
21+
22+
public String toJson() {
23+
return WxMaGsonBuilder.create().toJson(this);
24+
}
25+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaConstants.java

+16
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ public static final class ErrorCode {
6969
*/
7070
public static final int ERR_40014 = 40014;
7171
}
72+
73+
/**
74+
* 内容安全检测的媒体类型
75+
*/
76+
public static final class SecCheckMediaType {
77+
78+
/**
79+
* 音频
80+
*/
81+
public static final int VOICE = 1;
82+
83+
/**
84+
* 图片
85+
*/
86+
public static final int IMAGE = 2;
87+
}
7288
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouterRule.java

+19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class WxMaMessageRouterRule {
3232

3333
private String rContent;
3434

35+
private String title;
36+
3537
private WxMaMessageMatcher matcher;
3638

3739
private boolean reEnter = false;
@@ -60,6 +62,16 @@ public WxMaMessageRouterRule msgType(String msgType) {
6062
return this;
6163
}
6264

65+
/**
66+
* 标题,发送小程序页卡时有效
67+
* @param title
68+
* @return
69+
*/
70+
public WxMaMessageRouterRule title(String title){
71+
this.title = title;
72+
return this;
73+
}
74+
6375
/**
6476
* 如果event等于某值.
6577
*/
@@ -100,6 +112,8 @@ public WxMaMessageRouterRule fromUser(String fromUser) {
100112
return this;
101113
}
102114

115+
116+
103117
/**
104118
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候.
105119
*/
@@ -164,6 +178,8 @@ public WxMaMessageRouter next() {
164178
return end();
165179
}
166180

181+
182+
167183
/**
168184
* 将微信自定义的事件修正为不区分大小写.
169185
* 比如框架定义的事件常量为click,但微信传递过来的却是CLICK
@@ -183,6 +199,9 @@ protected boolean test(WxMaMessage wxMessage) {
183199
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
184200
&&
185201
(this.matcher == null || this.matcher.match(wxMessage))
202+
&&
203+
(this.title == null || this.title
204+
.equals(wxMessage.getTitle() == null ? null : wxMessage.getTitle().trim()))
186205
;
187206
}
188207

weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSecCheckServiceImplTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ public Object[][] secData() {
3838
return new Object[][]{
3939
{"特3456书yuuo莞6543李zxcz蒜7782法fgnv级", false},
4040
{"完2347全dfji试3726测asad感3847知qwez到", false},
41+
{"提现&下载&棋牌游戏&网页", false},
4142
{"hello world!", true}
4243
};
4344
}
4445

4546
@Test(dataProvider = "secData")
46-
public void testCheckMessage(String msg, boolean result) {
47+
public void testCheckMessage(String msg, boolean result) throws WxErrorException {
4748
assertThat(this.wxService.getSecCheckService()
4849
.checkMessage(msg))
4950
.isEqualTo(result);

0 commit comments

Comments
 (0)