com.zopen.wechat.mp.service.miniprogram.WechatProgramRiskyService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.wechat.mp.service.miniprogram;
import com.google.gson.Gson;
import com.zcj.util.UtilString;
import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.exception.WechatException;
import com.zopen.wechat.mp.dto.BaseResponse;
import com.zopen.wechat.mp.dto.miniprogram.MiniProgramRiskyCheck;
import com.zopen.wechat.mp.service.WechatHttpUrl;
import com.zopen.wechat.mp.task.WechatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component("atoWechatProgramRiskyService")
public class WechatProgramRiskyService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private WechatMpProperties wechatMpProperties;
// 敏感词检测(使用 application 中配置的 appId)
public void check(String content) {
String appId = wechatMpProperties.getAppId();
check(content, appId);
}
/**
* 敏感词检测
*
* @param content * 需要检测的内容
* @param appId * app_id
* @throws WechatException 如果检测不通过,则抛出 WechatException 异常
*/
public void check(String content, String appId) {
if (UtilString.isBlank(content)) {
return;
}
// 请求地址
String accessToken = WechatInfo.getAccessToken(appId);
String url = String.format(WechatHttpUrl.MINI_RISKY_CHECK, accessToken);
// 请求参数
MiniProgramRiskyCheck message = new MiniProgramRiskyCheck(content);
// 发送请求并解析返回内容
String resp = restTemplate.postForObject(url, message, String.class);
WechatAssert.notNullAndEmpty(resp, "敏感词检测失败:接口返回的内容为空");
BaseResponse respObj = new Gson().fromJson(resp, BaseResponse.class);
WechatAssert.notNull(respObj, "敏感词检测失败:解析返回的内容失败");
respObj.valid("内容违规:" + content);
}
}