Pu Zhibing
8 天以前 3244b550596e0330031b3f4547356927df83b0ad
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/WxLoginController.java
@@ -1,6 +1,7 @@
package com.ruoyi.account.controller;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.internal.util.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -15,8 +16,10 @@
import com.ruoyi.account.wx.pojo.AppletUserEncrypteData;
import com.ruoyi.account.wx.tools.WxAppletTools;
import com.ruoyi.account.wx.tools.WxUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.FileUploadUtils;
import com.ruoyi.common.core.utils.HttpUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
@@ -37,6 +40,7 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -63,11 +67,14 @@
    private WeixinProperties wxConfig;
    @Autowired
    private RestTemplate wxRestTemplate;
    @Resource
    private RedisService redisService;
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.upload.location}")
    private String localFilePath;
//    @Value("${file.upload.location}")
//    private String localFilePath;
@@ -76,7 +83,7 @@
    @PostMapping("/openIdByJsCode")
    public AjaxResult<Map<String, Object>> openIdByJsCode(@RequestBody AppletUserEncrypteData data) {
        log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", data.getCode());
        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig);
        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService);
        Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(data.getCode()));
        String openid = body.getOpenid();
        String sessionKey = body.getSessionKey();
@@ -93,7 +100,7 @@
    }
    
    
    /**data
    /**
     * 测试用
     * @param appletUserDecodeData
     * @return
@@ -113,7 +120,7 @@
    public AjaxResult getQRCode() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig);
        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService);
        String accessToken = appletTools.getAccessToken("");
        try {
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
@@ -162,5 +169,55 @@
        }
        return AjaxResult.success();
    }
    /**
     * 获取微信token
     * @return
     */
    @PostMapping("/getWXToken")
    public R<String> getWXToken(){
        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService);
        String accessToken = appletTools.getAccessToken("");
        return R.ok(accessToken);
    }
    /**
     * 敏感词检测
     * @param content
     * @param openid
     * @return
     */
    @PostMapping("/sensitiveWordDetection")
    public R<Boolean> sensitiveWordDetection (@RequestParam("content") String content, @RequestParam("openid") String openid){
        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService);
        String accessToken = appletTools.getAccessToken("");
        com.alibaba.fastjson2.JSONObject jsonObject = new com.alibaba.fastjson2.JSONObject();
        jsonObject.put("content", content);
        jsonObject.put("version", 2);
        jsonObject.put("scene", 2);
        jsonObject.put("openid", openid);
        String post = HttpUtils.post("https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken, jsonObject.toString());
        com.alibaba.fastjson2.JSONObject object = com.alibaba.fastjson2.JSONObject.parseObject(post);
        Integer errcode = object.getInteger("errcode");
        if(0 != errcode){
            throw new RuntimeException(object.getString("errmsg"));
        }
        JSONArray detail = object.getJSONArray("detail");
        for (int i = 0; i < detail.size(); i++) {
            JSONObject jsonObject1 = detail.getJSONObject(i);
            Integer errcode1 = jsonObject1.getInteger("errcode");
            if(0 == errcode1){
                String suggest = jsonObject1.getString("suggest");
                Integer label = jsonObject1.getInteger("label");
                String keyword = jsonObject1.getString("keyword");
                Integer prob = jsonObject1.getInteger("prob");
                if(("risky".equals(suggest) || "review".equals(suggest)) && 100 != label && StringUtils.isNotEmpty(keyword) && 80 <= prob){
                    return R.ok(true);
                }
            }
        }
        return R.ok(false);
    }
}