package com.ruoyi.web.controller.spi;

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.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.UtilException;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 封装微信小程序检测敏感词spi
 *
 * @author: long
 * @create: 2023/07/3 2:41 下午
 **/
@Component
public class CheckSensitiveWordSpi {


    @Autowired
    private   WxMaService wxMaService;




    /**
     * 检查敏感词
     *
     * @param openid  用户openid
     * @param content 内容
     * @param scene   场景枚举值（1 资料；2 评论；3 论坛；4 社交日志）
     */
    public void checkSensitiveWords(String openid,String content,int scene) {
        if(2500<content.length()){
            throw new UtilException("检测内容不能超过2500个字符");
        }
        //需检测的文本内容，文本字数的上限为2500字，需使用UTF违规类型-8编码

        WxMaMsgSecCheckCheckRequest build = WxMaMsgSecCheckCheckRequest.builder()
            .content(content)
            .openid(openid)
            .scene(scene)
            .version("2").build();
        try {
            WxMaMsgSecCheckCheckResponse  wxMaMsgSecCheckCheckResponse = wxMaService.getSecCheckService().checkMessage(build);
            StaticLog.info("敏感词过滤:{}",wxMaMsgSecCheckCheckResponse.toString());
            if(!"100".equals(wxMaMsgSecCheckCheckResponse.getResult().getLabel())){
                StringBuilder sensitiveWords =new StringBuilder("违规词汇:");
                for (WxMaMsgSecCheckCheckResponse.DetailBean detailBean : wxMaMsgSecCheckCheckResponse.getDetail()) {
//                  命中标签枚举值，100 正常；10001 广告；20001 时政；20002 色情；20003 辱骂；20006 违法犯罪；20008 欺诈；20012 低俗；20013 版权；21000 其他
                    if(null ==detailBean || null ==detailBean.getLabel()){
                        continue;
                    }
                    String keyword = StrUtil.isBlank(detailBean.getKeyword()) ? "**;" : detailBean.getKeyword()+";";
                    switch (detailBean.getLabel()){
                        case "100":
                            break;
                        case "10001":
                            sensitiveWords.append(keyword+"违规类型-广告;");
                            break;
                        case "20001":
                            sensitiveWords.append(keyword+"违规类型-时政;");
                            break;
                        case "20002":
                            sensitiveWords.append(keyword+"违规类型-色情;");
                            break;
                        case "20003":
                            sensitiveWords.append(keyword+"违规类型-辱骂;");
                            break;
                        case "20006":
                            sensitiveWords.append(keyword+"违规类型-违法犯罪;");
                            break;
                        case "20008":
                            sensitiveWords.append(keyword+"违规类型-欺诈;");
                            break;
                        case "20012":
                            sensitiveWords.append(keyword+"违规类型-低俗;");
                            break;
                        case "20013":
                            sensitiveWords.append(keyword+"违规类型-版权;");
                            break;
                        default:
                            sensitiveWords.append(keyword+"违规类型-其他;");
                            break;

                    }
                }
                throw new ServiceException(sensitiveWords.toString());
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new ServiceException(e.getError().getErrorMsg());

        }
    }
}
