Skip to content

Commit

Permalink
🎨 #1307 优化完善微信小程序内容安全校验接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Dec 22, 2019
1 parent f60a7d9 commit 491134b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
* 检查一段文本是否含有违法违规内容。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
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>
Expand All @@ -20,17 +25,28 @@
*/
@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

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;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +9,12 @@
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>
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 491134b

Please sign in to comment.