:sparkles: #1217 小程序增加校验图片/音频是否含有违法违规内容的接口 · fireinjava/WxJava@8e97b77 · GitHub
Skip to content

Commit 8e97b77

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

6 files changed

Lines changed: 103 additions & 10 deletions

File tree

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

Lines changed: 24 additions & 1 deletion

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

Lines changed: 17 additions & 8 deletions
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
}
Lines changed: 25 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)