Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加小程序内容安全接口兼容2.0版本 #2261

Merged
merged 15 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public static WxMinishopImageUploadCustomizeResult fromJson(String json) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@

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
public void execute(String uri, File data, ResponseHandler<WxMinishopImageUploadCustomizeResult> handler, WxType wxType) throws WxErrorException, IOException {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
@Slf4j
public class ApacheMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<CloseableHttpClient, HttpHost> {
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
super(requestHttp);
public ApacheMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
super(requestHttp, respType);
}

@Override
Expand All @@ -39,6 +39,7 @@ public WxMinishopImageUploadCustomizeResult execute(String uri, File file, WxTyp
HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("media", file)
.addTextBody("resp_type", this.respType)
.setMode(HttpMultipartMode.RFC6532)
.build();
httpPost.setEntity(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*/
@Slf4j
public class JoddHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<HttpConnectionProvider, ProxyInfo> {
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
super(requestHttp);
public JoddHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
super(requestHttp, respType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
@Slf4j
public class OkHttpMinishopMediaUploadRequestCustomizeExecutor extends MinishopUploadRequestCustomizeExecutor<OkHttpClient, OkHttpProxyInfo> {
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp) {
super(requestHttp);
public OkHttpMinishopMediaUploadRequestCustomizeExecutor(RequestHttp requestHttp, String respType) {
super(requestHttp, respType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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>
* 异步校验图片/音频是否含有违法违规内容。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
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;
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>
Expand Down Expand Up @@ -59,6 +65,16 @@ public boolean checkMessage(String msgString) throws WxErrorException {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;
Expand Down Expand Up @@ -49,4 +51,16 @@ public void testCheckMessage(String msg, boolean result) throws WxErrorException
.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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ public void testUploadImg() throws WxErrorException {
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();
}
}