| | |
| | | package com.panzhihua.applets.weixin; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import com.panzhihua.applets.config.WxMaConfiguration; |
| | | |
| | | import cn.binarywang.wx.miniapp.api.WxMaSecCheckService; |
| | | import cn.binarywang.wx.miniapp.api.WxMaService; |
| | | import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest; |
| | | import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse; |
| | | import cn.binarywang.wx.miniapp.config.WxMaConfig; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.panzhihua.applets.config.WxMaConfiguration; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.chanjar.weixin.common.error.WxErrorException; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | |
| | | private WxMaConfiguration wxMaConfiguration; |
| | | |
| | | public boolean checkMessage(String msg) { |
| | | WxMaSecCheckService wxMaSecCheckService = wxMaConfiguration.getMaService("").getSecCheckService(); |
| | | WxMaService maService = wxMaConfiguration.getMaService(""); |
| | | |
| | | WxMaConfig wxMaConfig = maService.getWxMaConfig(); |
| | | WxMaMsgSecCheckCheckRequest wxMaMsgSecCheckCheckRequest = WxMaMsgSecCheckCheckRequest.builder().version("2").openid(wxMaConfig.getAppid()).scene(2).content(msg).build(); |
| | | WxMaSecCheckService wxMaSecCheckService = maService.getSecCheckService(); |
| | | try { |
| | | boolean b = wxMaSecCheckService.checkMessage(msg); |
| | | return b; |
| | | WxMaMsgSecCheckCheckResponse wxMaMsgSecCheckCheckResponse = wxMaSecCheckService.checkMessage(wxMaMsgSecCheckCheckRequest); |
| | | wxMaMsgSecCheckCheckResponse.getResult(); |
| | | return true; |
| | | } catch (WxErrorException e) { |
| | | log.error("微信审核文字内容出错【{}】", e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public String checkMessageBy(String msg, String openId, String appId) { |
| | | WxMaMsgSecCheckCheckRequest wxMaMsgSecCheckCheckRequest = WxMaMsgSecCheckCheckRequest.builder().version("2").openid(openId).scene(2).content(msg).build(); |
| | | WxMaSecCheckService wxMaSecCheckService = wxMaConfiguration.getMaService(appId).getSecCheckService(); |
| | | try { |
| | | WxMaMsgSecCheckCheckResponse result = wxMaSecCheckService.checkMessage(wxMaMsgSecCheckCheckRequest); |
| | | return this.handleResult(result); |
| | | } catch (WxErrorException e) { |
| | | log.error("微信审核文字内容出错【{}】", e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public boolean checkImageBy(String url, String appId) { |
| | | String[] imageUrlArr = url.split(","); |
| | | for (String imageUrl : imageUrlArr) { |
| | | WxMaSecCheckService wxMaSecCheckService = wxMaConfiguration.getMaService(appId).getSecCheckService(); |
| | | try { |
| | | return wxMaSecCheckService.checkImage(imageUrl); |
| | | } catch (WxErrorException e) { |
| | | log.error("微信审核图片内容出错【{}】", e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 处理微信返回的违规结果 |
| | | * |
| | | * @param result |
| | | * @return |
| | | */ |
| | | private String handleResult(WxMaMsgSecCheckCheckResponse result) { |
| | | List<WxMaMsgSecCheckCheckResponse.DetailBean> detailBeanList = result.getDetail(); |
| | | if (null != detailBeanList && detailBeanList.size() > 0) { |
| | | List<String> resultMsgs = new ArrayList<>(); |
| | | for (WxMaMsgSecCheckCheckResponse.DetailBean detailBean : detailBeanList) { |
| | | if (0 == detailBean.getErrcode()) { |
| | | String label = detailBean.getLabel(); |
| | | if (StrUtil.isNotBlank(label)) { |
| | | switch (label) { |
| | | case "100": |
| | | continue; |
| | | case "10001": |
| | | resultMsgs.add("广告"); |
| | | continue; |
| | | case "20001": |
| | | resultMsgs.add("时政"); |
| | | continue; |
| | | case "20002": |
| | | resultMsgs.add("色情"); |
| | | continue; |
| | | case "20003": |
| | | resultMsgs.add("辱骂"); |
| | | continue; |
| | | case "20006": |
| | | resultMsgs.add("违法犯罪"); |
| | | continue; |
| | | case "20008": |
| | | resultMsgs.add("欺诈"); |
| | | continue; |
| | | case "20012": |
| | | resultMsgs.add("低俗"); |
| | | continue; |
| | | case "20013": |
| | | resultMsgs.add("版权"); |
| | | continue; |
| | | case "21000": |
| | | resultMsgs.add("其他"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | if (CollUtil.isNotEmpty(resultMsgs)) { |
| | | return StringUtils.join(resultMsgs, ","); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | } |