| @@ -32,6 +32,14 @@ public interface WxMaSecCheckService { | |||||
| */ | */ | ||||
| boolean checkImage(File file) throws WxErrorException; | boolean checkImage(File file) throws WxErrorException; | ||||
| /** | |||||
| * 校验一张图片是否含有违法违规内容 | |||||
| * @param fileUrl 文件网络地址 | |||||
| * @return 是否违规 | |||||
| * @throws WxErrorException . | |||||
| */ | |||||
| boolean checkImage(String fileUrl) throws WxErrorException; | |||||
| /** | /** | ||||
| * <pre> | * <pre> | ||||
| * 检查一段文本是否含有违法违规内容。 | * 检查一段文本是否含有违法违规内容。 | ||||
| @@ -4,11 +4,16 @@ import cn.binarywang.wx.miniapp.api.WxMaSecCheckService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | import cn.binarywang.wx.miniapp.api.WxMaService; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult; | import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult; | ||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||
| import java.io.File; | |||||
| import lombok.AllArgsConstructor; | import lombok.AllArgsConstructor; | ||||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | 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.error.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | 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> | * <pre> | ||||
| @@ -20,17 +25,28 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||||
| */ | */ | ||||
| @AllArgsConstructor | @AllArgsConstructor | ||||
| public class WxMaSecCheckServiceImpl implements WxMaSecCheckService { | public class WxMaSecCheckServiceImpl implements WxMaSecCheckService { | ||||
| private WxMaService service; | private WxMaService service; | ||||
| @Override | @Override | ||||
| public boolean checkImage(File file) throws WxErrorException { | public boolean checkImage(File file) throws WxErrorException { | ||||
| //这里只是借用MediaUploadRequestExecutor,并不使用其返回值WxMediaUploadResult | |||||
| WxMediaUploadResult result = this.service.execute(MediaUploadRequestExecutor | WxMediaUploadResult result = this.service.execute(MediaUploadRequestExecutor | ||||
| .create(this.service.getRequestHttp()), IMG_SEC_CHECK_URL, file); | .create(this.service.getRequestHttp()), IMG_SEC_CHECK_URL, file); | ||||
| return result != null; | 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 | @Override | ||||
| public boolean checkMessage(String msgString) throws WxErrorException { | public boolean checkMessage(String msgString) throws WxErrorException { | ||||
| JsonObject jsonObject = new JsonObject(); | JsonObject jsonObject = new JsonObject(); | ||||
| @@ -2,10 +2,16 @@ package cn.binarywang.wx.miniapp.bean; | |||||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | ||||
| import com.google.gson.annotations.SerializedName; | import com.google.gson.annotations.SerializedName; | ||||
| import com.oracle.webservices.internal.api.databinding.DatabindingMode; | |||||
| import lombok.Data; | |||||
| import java.io.Serializable; | import java.io.Serializable; | ||||
| /** | |||||
| * @author borisbao | |||||
| */ | |||||
| @Data | |||||
| public class WxMaMediaAsyncCheckResult implements Serializable { | public class WxMaMediaAsyncCheckResult implements Serializable { | ||||
| private static final long serialVersionUID = 3928132365399916183L; | private static final long serialVersionUID = 3928132365399916183L; | ||||
| /** | /** | ||||
| @@ -1,12 +1,5 @@ | |||||
| package cn.binarywang.wx.miniapp.bean; | 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.config.WxMaConfig; | ||||
| import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; | import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; | ||||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | 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 com.thoughtworks.xstream.annotations.XStreamConverter; | ||||
| import lombok.Data; | import lombok.Data; | ||||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | 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> | * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
| @@ -108,6 +107,34 @@ public class WxMaMessage implements Serializable { | |||||
| @XStreamConverter(value = XStreamCDataConverter.class) | @XStreamConverter(value = XStreamCDataConverter.class) | ||||
| private String sessionFrom; | 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) { | public static WxMaMessage fromXml(String xml) { | ||||
| return XStreamTransformer.fromXml(WxMaMessage.class, xml); | return XStreamTransformer.fromXml(WxMaMessage.class, xml); | ||||
| } | } | ||||