mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-02 20:02:37 +08:00
🎨 #1307 优化完善微信小程序内容安全校验接口
This commit is contained in:
parent
f60a7d911b
commit
491134b4ac
@ -32,6 +32,14 @@ public interface WxMaSecCheckService {
|
||||
*/
|
||||
boolean checkImage(File file) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 校验一张图片是否含有违法违规内容
|
||||
* @param fileUrl 文件网络地址
|
||||
* @return 是否违规
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
boolean checkImage(String fileUrl) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 检查一段文本是否含有违法违规内容。
|
||||
|
@ -4,11 +4,16 @@ import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
|
||||
import com.google.gson.JsonObject;
|
||||
import java.io.File;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@ -20,17 +25,28 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class WxMaSecCheckServiceImpl implements WxMaSecCheckService {
|
||||
|
||||
private WxMaService service;
|
||||
|
||||
@Override
|
||||
public boolean checkImage(File file) throws WxErrorException {
|
||||
//这里只是借用MediaUploadRequestExecutor,并不使用其返回值WxMediaUploadResult
|
||||
WxMediaUploadResult result = this.service.execute(MediaUploadRequestExecutor
|
||||
.create(this.service.getRequestHttp()), IMG_SEC_CHECK_URL, file);
|
||||
return result != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkImage(String fileUrl) throws WxErrorException {
|
||||
File file = new File(FileUtils.getTempDirectory(), System.currentTimeMillis() + ".tmp");
|
||||
try {
|
||||
URL url = new URL(fileUrl);
|
||||
FileUtils.copyURLToFile(url, file);
|
||||
} catch (IOException e) {
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件地址读取异常").build(), e);
|
||||
}
|
||||
|
||||
return this.checkImage(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkMessage(String msgString) throws WxErrorException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
@ -2,10 +2,16 @@ package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.oracle.webservices.internal.api.databinding.DatabindingMode;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author borisbao
|
||||
*/
|
||||
@Data
|
||||
public class WxMaMediaAsyncCheckResult implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3928132365399916183L;
|
||||
|
||||
/**
|
||||
|
@ -1,12 +1,5 @@
|
||||
package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
|
||||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
|
||||
@ -16,6 +9,12 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamConverter;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
@ -108,6 +107,34 @@ public class WxMaMessage implements Serializable {
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String sessionFrom;
|
||||
|
||||
/**
|
||||
* 以下是异步校验图片/音频是否含有违法违规内容的异步检测结果推送报文中的参数
|
||||
*/
|
||||
@SerializedName("isrisky")
|
||||
@XStreamAlias("isrisky")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String isRisky;
|
||||
|
||||
@SerializedName("extra_info_json")
|
||||
@XStreamAlias("extra_info_json")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String extraInfoJson;
|
||||
|
||||
@SerializedName("appid")
|
||||
@XStreamAlias("appid")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String appid;
|
||||
|
||||
@SerializedName("trace_id")
|
||||
@XStreamAlias("trace_id")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String traceId;
|
||||
|
||||
@SerializedName("status_code")
|
||||
@XStreamAlias("status_code")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String statusCode;
|
||||
|
||||
public static WxMaMessage fromXml(String xml) {
|
||||
return XStreamTransformer.fromXml(WxMaMessage.class, xml);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user