mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-05 13:17:46 +08:00
🆕 #2261 【小程序】增加内容安全接口(兼容2.0版本)
This commit is contained in:
parent
6cfe90b31e
commit
614a1c923e
@ -21,12 +21,13 @@ public class WxMinishopImageUploadCustomizeResult implements Serializable {
|
||||
if (result.getErrcode().equals("0")) {
|
||||
WxMinishopPicFileCustomizeResult picFileResult = new WxMinishopPicFileCustomizeResult();
|
||||
JsonObject picObject = jsonObject.get("img_info").getAsJsonObject();
|
||||
picFileResult.setMediaId(picObject.get("media_id").getAsString());
|
||||
if (picObject.has("media_id")) {
|
||||
picFileResult.setMediaId(picObject.get("media_id").getAsString());
|
||||
}
|
||||
if (picObject.has("temp_img_url")) {
|
||||
picFileResult.setTempImgUrl(picObject.get("temp_img_url").getAsString());
|
||||
}
|
||||
result.setImgInfo(picFileResult);
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -12,9 +12,11 @@ import java.io.IOException;
|
||||
|
||||
public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements RequestExecutor<WxMinishopImageUploadCustomizeResult, File> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
protected String respType;
|
||||
|
||||
public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
|
||||
public MinishopUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
|
||||
this.requestHttp = requestHttp;
|
||||
this.respType = respType;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -22,14 +24,14 @@ public abstract class MinishopUploadRequestCustomizeExecutor<H, P> implements Re
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp) {
|
||||
public static RequestExecutor<WxMinishopImageUploadCustomizeResult, File> create(RequestHttp requestHttp, String respType) {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp);
|
||||
return new ApacheMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
|
||||
case JODD_HTTP:
|
||||
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp);
|
||||
return new JoddHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
|
||||
case OK_HTTP:
|
||||
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp);
|
||||
return new OkHttpMinishopMediaUploadRequestCustomizeExecutor(requestHttp, respType);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
@ -24,8 +24,8 @@ import java.io.IOException;
|
||||
*/
|
||||
@Slf4j
|
||||
public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
|
||||
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
|
||||
super(requestHttp, respType);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,6 +39,7 @@ public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopU
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("media", file)
|
||||
.addTextBody("resp_type", this.respType)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
|
@ -22,8 +22,8 @@ import java.nio.charset.StandardCharsets;
|
||||
*/
|
||||
@Slf4j
|
||||
public class JoddHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
|
||||
super(requestHttp, respType);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,8 +18,8 @@ import java.io.IOException;
|
||||
*/
|
||||
@Slf4j
|
||||
public class OkHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
|
||||
super(requestHttp, respType);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,8 @@
|
||||
package cn.binarywang.wx.miniapp.api;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
|
||||
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
|
||||
import java.io.File;
|
||||
@ -55,6 +57,17 @@ public interface WxMaSecCheckService {
|
||||
boolean checkMessage(String msgString) throws WxErrorException;
|
||||
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 检查一段文本是否含有违法违规内容(新版本接口,主要是request和response做了参数优化)
|
||||
* 详情请见: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
|
||||
* </pre>
|
||||
* @param msgRequest
|
||||
* @return WxMaMsgSecCheckCheckResponse
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMaMsgSecCheckCheckResponse checkMessage(WxMaMsgSecCheckCheckRequest msgRequest) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 异步校验图片/音频是否含有违法违规内容。
|
||||
|
@ -14,8 +14,19 @@ public interface WxMaShopImgService {
|
||||
/**
|
||||
* 上传图片
|
||||
*
|
||||
* @param file
|
||||
* @return WxMinishopImageUploadCustomizeResult
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMinishopImageUploadCustomizeResult uploadImg(File file) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 上传图片,带respType参数
|
||||
*
|
||||
* @param file
|
||||
* @param respType
|
||||
* @return WxMinishopImageUploadCustomizeResult
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMinishopImageUploadCustomizeResult uploadImg(File file, String respType) throws WxErrorException;
|
||||
}
|
||||
|
@ -3,12 +3,17 @@ package cn.binarywang.wx.miniapp.api.impl;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
|
||||
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
@ -16,6 +21,7 @@ import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.SecCheck.*;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ERRCODE;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@ -59,6 +65,16 @@ public class WxMaSecCheckServiceImpl implements WxMaSecCheckService {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaMsgSecCheckCheckResponse checkMessage(WxMaMsgSecCheckCheckRequest msgRequest) throws WxErrorException {
|
||||
String response = this.service.post(MSG_SEC_CHECK_URL, msgRequest);
|
||||
JsonObject jsonObject = GsonParser.parse(response);
|
||||
if (jsonObject.get(ERRCODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(response, WxType.MiniApp));
|
||||
}
|
||||
return WxMaGsonBuilder.create().fromJson(response, WxMaMsgSecCheckCheckResponse.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaMediaAsyncCheckResult mediaCheckAsync(String mediaUrl, int mediaType)
|
||||
throws WxErrorException {
|
||||
|
@ -23,7 +23,14 @@ public class WxMaShopImgServiceImpl implements WxMaShopImgService {
|
||||
@Override
|
||||
public WxMinishopImageUploadCustomizeResult uploadImg(File file) throws WxErrorException {
|
||||
WxMinishopImageUploadCustomizeResult result = this.service.execute(
|
||||
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp()), IMG_UPLOAD, file);
|
||||
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), "0"), IMG_UPLOAD, file);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMinishopImageUploadCustomizeResult uploadImg(File file, String respType) throws WxErrorException {
|
||||
WxMinishopImageUploadCustomizeResult result = this.service.execute(
|
||||
MinishopUploadRequestCustomizeExecutor.create(this.service.getRequestHttp(), respType), IMG_UPLOAD, file);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author liming1019
|
||||
* @date 2021/8/17
|
||||
*/
|
||||
@Data
|
||||
public class WxMaBaseResponse implements Serializable {
|
||||
private static final long serialVersionUID = 3932406255203539965L;
|
||||
/**
|
||||
* 错误码
|
||||
* <pre>
|
||||
* 是否必填:
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("errcode")
|
||||
private Integer errcode;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* <pre>
|
||||
* 是否必填:
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("errmsg")
|
||||
private String errmsg;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package cn.binarywang.wx.miniapp.bean.security;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author liming1019
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class WxMaMsgSecCheckCheckRequest implements Serializable {
|
||||
private static final long serialVersionUID = 3233176903681625506L;
|
||||
|
||||
@SerializedName("version")
|
||||
private String version;
|
||||
|
||||
@SerializedName("openid")
|
||||
private String openid;
|
||||
|
||||
@SerializedName("scene")
|
||||
private Integer scene;
|
||||
|
||||
@SerializedName("content")
|
||||
private String content;
|
||||
|
||||
@SerializedName("nickname")
|
||||
private String nickname;
|
||||
|
||||
@SerializedName("title")
|
||||
private String title;
|
||||
|
||||
@SerializedName("signature")
|
||||
private String signature;
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package cn.binarywang.wx.miniapp.bean.security;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaBaseResponse;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author liming1019
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class WxMaMsgSecCheckCheckResponse extends WxMaBaseResponse implements Serializable {
|
||||
private static final long serialVersionUID = 1903247824980080974L;
|
||||
/**
|
||||
* result : {"suggest":"risky","label":20001}
|
||||
* detail : [{"strategy":"content_model","errcode":0,"suggest":"risky","label":20006,"prob":90},{"strategy":"keyword","errcode":0,"suggest":"pass","label":20006,"level":20,"keyword":"命中的关键词1"},{"strategy":"keyword","errcode":0,"suggest":"risky","label":20006,"level":90,"keyword":"命中的关键词2"}]
|
||||
* trace_id : 60ae120f-371d5872-7941a05b
|
||||
*/
|
||||
@SerializedName("result")
|
||||
private ResultBean result;
|
||||
@SerializedName("trace_id")
|
||||
private String traceId;
|
||||
@SerializedName("detail")
|
||||
private List<DetailBean> detail;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public static class ResultBean implements Serializable {
|
||||
/**
|
||||
* suggest : risky
|
||||
* label : 20001
|
||||
*/
|
||||
|
||||
@SerializedName("suggest")
|
||||
private String suggest;
|
||||
@SerializedName("label")
|
||||
private String label;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public static class DetailBean implements Serializable {
|
||||
/**
|
||||
* strategy : content_model
|
||||
* errcode : 0
|
||||
* suggest : risky
|
||||
* label : 20006
|
||||
* prob : 90
|
||||
* level : 20
|
||||
* keyword : 命中的关键词1
|
||||
*/
|
||||
|
||||
@SerializedName("strategy")
|
||||
private String strategy;
|
||||
@SerializedName("errcode")
|
||||
private Integer errcode;
|
||||
@SerializedName("suggest")
|
||||
private String suggest;
|
||||
@SerializedName("label")
|
||||
private String label;
|
||||
@SerializedName("prob")
|
||||
private Integer prob;
|
||||
@SerializedName("level")
|
||||
private String level;
|
||||
@SerializedName("keyword")
|
||||
private String keyword;
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@ package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
@ -49,4 +51,16 @@ public class WxMaSecCheckServiceImplTest {
|
||||
.checkMessage(msg))
|
||||
.isEqualTo(result);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "secData")
|
||||
public void testCheckMessage2(String msg, boolean result) throws WxErrorException {
|
||||
WxMaMsgSecCheckCheckRequest request = WxMaMsgSecCheckCheckRequest.builder()
|
||||
.content(msg)
|
||||
.scene(1)
|
||||
.version("2")
|
||||
.openid("xxx")
|
||||
.build();
|
||||
WxMaMsgSecCheckCheckResponse response = this.wxService.getSecCheckService().checkMessage(request);
|
||||
assertThat(response).isNotNull();
|
||||
}
|
||||
}
|
||||
|
@ -28,4 +28,11 @@ public class WxMaShopImgServiceImplTest {
|
||||
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(file);
|
||||
assertThat(result).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUploadImg2() throws WxErrorException {
|
||||
File file = new File("/Users/liming/Desktop/test.jpeg");
|
||||
WxMinishopImageUploadCustomizeResult result = wxService.getShopImgService().uploadImg(file, "1");
|
||||
assertThat(result).isNotNull();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user