zero 1 miesiąc temu
rodzic
commit
c21aa746c9

+ 3 - 19
src/main/java/com/cslg/ppa/common/core/base/RedisConf.java

@@ -7,24 +7,8 @@ package com.cslg.ppa.common.core.base;
 public class RedisConf {
 
     public static final String SYMBOL_COLON = ":";
-    public static final String SYMBOL_LINE = "-";
-    public final static String COMMON_DATA = "common-data";
     public final static String LOGIN_USER = "login-user";
-    public final static String VERIFY_CODE = "verify-code";
-    public final static String SELECT_PATENT = "select-patent";
-    public final static String ANALYSIS_COUNT = "analysis-count";
-    public final static String AREA_LIST = "area-list";
-    public final static String USER_FIELD = "user-field";
-    public final static String FIELD_ORDER = "field-order";
-    public final static String USER_IMPORT = "user-import";
-    public final static String PROJECT_FIELD_ORDER = "project_field_order";
-    public final static String TASK_FIELD_ORDER = "task_field_order";
-    public final static String PATENT_COUNT = "patent-count";
-    public final static String TASK_FIELD_COUNT = "task_field_count";
-    public final static String PROJECT_FIELD_COUNT = "project_field_count";
-    public final static String FIELD_COUNT = "field_count";
-    public final static String PROJECT = "project";
-    public final static String PRODUCT = "product";
-    public final static String EVIDENCE_REASON = "evidence_reason";
-    public final static String GET_PATENT_SIMILAR_SCORE="get_patent_similar_score";
+    public final static String WECHAT_TOKEN = "wechat-token";
+    public final static String WECHAT_COOKIE = "wechat-cookie";
+
 }

+ 11 - 4
src/main/java/com/cslg/ppa/controller/ArticleInfoController.java

@@ -7,14 +7,12 @@ import com.cslg.ppa.dto.ArticleInfoIdDTO;
 import com.cslg.ppa.dto.SelectArticleInfoDTO;
 import com.cslg.ppa.dto.UpdateArticleInfoDTO;
 import com.cslg.ppa.service.ArticleInfoService;
+import com.cslg.ppa.service.commom.WeChatLoginCheckService;
 import io.swagger.v3.oas.annotations.Operation;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 @RequestMapping(Constants.API_PPA + "/articleInfo")
 @RestController
@@ -23,6 +21,8 @@ public class ArticleInfoController {
 
     @Autowired
     private ArticleInfoService articleInfoService;
+    @Autowired
+    private WeChatLoginCheckService weChatLoginCheckService;
 
     @Operation(summary = "查询资讯列表")
     @PostMapping("/selectArticleInfoList")
@@ -44,4 +44,11 @@ public class ArticleInfoController {
         articleInfoService.deleteArticleInfo(vo);
         return Response.success("删除成功");
     }
+
+    //---------------------------------------------------
+    @Operation(summary = "发送邮件提醒微信公众号登录失效")
+    @GetMapping("/sendQRCodeByEmail")
+    public String sendQRCodeByEmail() throws Exception {
+        return weChatLoginCheckService.sendQRCodeByEmail();
+    }
 }

+ 13 - 0
src/main/java/com/cslg/ppa/service/commom/MailSendService.java

@@ -29,4 +29,17 @@ public class MailSendService {
         }
     }
 
+    public void sendEmail1(MailMessageDTO mailMessageDTO) {
+        if (StringUtils.isNotEmpty(mailMessageDTO.getUrl())) {
+            Map<String, Object> map = new LinkedHashMap<>();
+            String title = "【PPA系统】微信公众号登录二维码 - 请扫码登录";
+            map.put("title", title);
+            map.put("img", "/logo.png");
+            map.put("email", mailMessageDTO.getEmail());
+            map.put("template", "mail/wechatLoginPPA.html");
+            map.put("value1", mailMessageDTO.getUrl());
+            rabbitMQUtils.sendEmailMessage(map);
+        }
+    }
+
 }

+ 110 - 58
src/main/java/com/cslg/ppa/service/commom/WeChatLoginCheckService.java

@@ -1,13 +1,17 @@
 package com.cslg.ppa.service.commom;
 
+import com.cslg.ppa.common.core.base.RedisConf;
 import com.cslg.ppa.common.okhttp.MyCookieStore;
 import com.cslg.ppa.common.utils.HttpUtils;
+import com.cslg.ppa.common.utils.RedisUtil;
 import com.cslg.ppa.dto.MailMessageDTO;
 import com.cslg.ppa.entity.commom.Article;
 import com.cslg.ppa.entity.commom.WxResultBody;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.compress.utils.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -31,33 +35,125 @@ public class WeChatLoginCheckService {
     @Value("${FileDownloadUrl}")
     private String fileDownloadUrl;
 
+    @Autowired
+    private RedisUtil redisUtil;
+
     // 线程安全控制
     private volatile boolean isWaitingForLogin = false;
 
+//    @Scheduled(cron = "0 0 9 * * ?")
+    public void checkWeChatLoginStatus() {
+        final String s = RedisConf.WECHAT_TOKEN + RedisConf.SYMBOL_COLON;
+        System.out.println(s);
+        log.info("开始检查微信登录状态...");
+        String token = redisUtil.get(RedisConf.WECHAT_TOKEN + RedisConf.SYMBOL_COLON);
+        String cookieStr = redisUtil.get(RedisConf.WECHAT_COOKIE + RedisConf.SYMBOL_COLON);
+        if (!StringUtils.isEmpty(token) && !StringUtils.isEmpty(cookieStr) && WeiXinApi.checkLoginStatus(token, cookieStr)) {
+            log.info("微信登录状态正常。");
+            return;
+        }
+        log.warn("微信登录凭证已失效,准备发送登录二维码邮件...");
+        MailMessageDTO messageDTO = new MailMessageDTO();
+        messageDTO.setUrl("http://192.168.2.107:8099/api/xiaoshi/ppa/articleInfo/sendQRCodeByEmail");
+        messageDTO.setEmail("2672485393@qq.com");
+        emailService.sendEmail(messageDTO);
+        log.info("已触发发送微信登录二维码邮件流程。");
+    }
+
+    public String sendQRCodeByEmail() throws Exception {
+        //1.立即返回"任务已接收"给前端
+        String uaid = WeiXinApi.initCookieUaid();
+        String uuid = WeiXinApi.initCookieUuid();
+        String cookie = uaid + ";" + uuid;
+        InputStream qrCodeInputStream = WeiXinApi.getQRCode(cookie);
+
+        File tempFile = File.createTempFile("qrCode_", ".jpg");
+        tempFile.deleteOnExit();
+        Files.copy(qrCodeInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+
+        List<String> ids = fileManagerService.uploadFileGetGuid2(Collections.singletonList(tempFile));
+        String link = "";
+        if (!CollectionUtils.isEmpty(ids)) {
+            String guid = ids.get(0).trim();
+            link = fileDownloadUrl + guid;
+        }
+
+        // 2. 启动异步任务执行循环逻辑
+        new Thread(() -> {
+            while (true) {
+                WxResultBody askQRCode = null;
+                try {
+                    askQRCode = WeiXinApi.askQRCode(cookie);
+                    Integer status = askQRCode.getStatus();
+                    if (status == 3) {
+                        log.warn("二维码已过期");
+                        checkWeChatLoginStatus();
+                        break;
+                    } else if (status == 1) {
+                        handleLoginSuccess(cookie);
+                        break;
+                    }
+                } catch (Exception ignored) {
+
+                }
+
+                try {
+                    Thread.sleep(2000);// 线程休眠2秒
+                } catch (Exception ignored) {
+
+                }
+            }
+        }).start();
+
+        return "<img src=\"" + link + "\" " + "style=\"width:100px;\" />";
+    }
+
+    /**
+     * 处理登录成功逻辑
+     */
+    private void handleLoginSuccess(String cookie) {
+        try {
+            // 确认登录后,请求登录接口,拿到登录状态的cookie
+            WxResultBody bizlogin = WeiXinApi.bizlogin1(cookie);
+            String cookies = bizlogin.getCookies();
+            String cookieRes = cookie + ";" + cookies;
+            //重定向地址
+            String redirect_url = bizlogin.getRedirect_url();
+            //解析成键值对
+            Map<String, String> loginRes = HttpUtils.parseQueryParams(redirect_url);
+            //得到token
+            String token = loginRes.get("token");
+            redisUtil.set(RedisConf.WECHAT_TOKEN + RedisConf.SYMBOL_COLON, token);
+            redisUtil.set(RedisConf.WECHAT_COOKIE + RedisConf.SYMBOL_COLON, cookieRes);
+            log.info("微信登录成功,token已保存: {}", token);
+        } catch (Exception e) {
+            log.error("处理登录成功失败: {}", e.getMessage(), e);
+        }
+    }
+
     /**
      * 定时检查微信登录状态(注释掉的定时任务需根据实际需求启用)
      */
-    // @Scheduled(fixedRate = 30, timeUnit = TimeUnit.MINUTES)
     // @Scheduled(cron = "0 0/30 * * * ?")
-    public void checkWeChatLoginStatus() throws Exception {
+    public void checkWeChatLoginStatus1() throws Exception {
         log.info("开始检查微信登录状态...");
         if (isWaitingForLogin) {
             log.warn("系统正在等待扫码登录,跳过本次检查。");
             return;
         }
 
+        String token = redisUtil.get(RedisConf.WECHAT_TOKEN + RedisConf.SYMBOL_COLON);
+        String cookieStr = redisUtil.get(RedisConf.WECHAT_COOKIE + RedisConf.SYMBOL_COLON);
+        if (!StringUtils.isEmpty(token) && !StringUtils.isEmpty(cookieStr) && WeiXinApi.checkLoginStatus(token, cookieStr)) {
+            log.info("微信登录状态正常。");
+            return;
+        }
+        log.warn("微信登录凭证已失效,准备发送登录二维码邮件...");
+        isWaitingForLogin = true;
         String uaid = WeiXinApi.initCookieUaid();
         String uuid = WeiXinApi.initCookieUuid();
         String cookie = uaid + ";" + uuid;
-
-        boolean isLoggedIn = WeiXinApi.checkLoginStatus(MyCookieStore.getToken(), cookie);
-        if (!isLoggedIn) {
-            log.warn("微信登录凭证已失效,准备发送登录二维码邮件...");
-            isWaitingForLogin = true;
-            sendLoginQRCodeEmail(cookie);
-        } else {
-            log.info("微信登录状态正常。");
-        }
+        sendLoginQRCodeEmail(cookie);
     }
 
     /**
@@ -66,10 +162,6 @@ public class WeChatLoginCheckService {
     private void sendLoginQRCodeEmail(String cookie) {
         try {
             InputStream qrCodeInputStream = WeiXinApi.getQRCode(cookie);
-            if (qrCodeInputStream == null) {
-                log.error("获取微信登录二维码失败,输入流为空。");
-                return;
-            }
 
             File tempFile = File.createTempFile("qrCode_", ".jpg");
             tempFile.deleteOnExit();
@@ -92,7 +184,8 @@ public class WeChatLoginCheckService {
                         System.out.println(status);
                         if (status == 3) {
                             log.warn("二维码已过期");
-                            checkWeChatLoginStatus();
+                            System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+//                            checkWeChatLoginStatus1();
                             break;
                         } else if (status == 1) {
                             handleLoginSuccess(cookie);
@@ -103,7 +196,7 @@ public class WeChatLoginCheckService {
                     }
 
                     try {
-                        Thread.sleep(1000);// 线程休眠一
+                        Thread.sleep(2000);// 线程休眠2
                     } catch (Exception ignored) {
 
                     }
@@ -114,45 +207,4 @@ public class WeChatLoginCheckService {
             log.error("生成或发送微信登录二维码邮件时发生错误: {}", e.getMessage(), e);
         }
     }
-
-    /**
-     * 处理登录成功逻辑
-     */
-    private void handleLoginSuccess(String cookie) {
-        try {
-            // 确认登录后,请求登录接口,拿到登录状态的cookie
-            WxResultBody bizlogin = WeiXinApi.bizlogin1(cookie);
-            String cookies = bizlogin.getCookies();
-            String cookieRes = cookie + ";" + cookies;
-            //重定向地址
-            String redirect_url = bizlogin.getRedirect_url();
-            //解析成键值对
-            Map<String, String> loginRes = HttpUtils.parseQueryParams(redirect_url);
-            //得到token
-            String token = loginRes.get("token");
-            System.out.println("------------------");
-            System.out.println(cookie);
-            System.out.println(token);
-            //todo   token与cookie的存储方法
-//            //设置全局token值
-//            MyCookieStore.setToken(token);
-//
-//            final String token1 = MyCookieStore.getToken();
-//            System.out.println("AAA:" + token1);
-//            try {
-//                WxResultBody<List<Article>> findExList = WeiXinApi.findExList("MzA4NDAzMjcyOA==", token, cookieRes);
-//                List<Article> exList = findExList.getApp_msg_list();
-//                System.out.println("=====================");
-//                System.out.println(exList);
-//                System.out.println(exList.size());
-//            } catch (Exception ignored) {
-//
-//            }
-            log.info("微信登录成功,token已保存: {}", token);
-        } catch (Exception e) {
-            log.error("处理登录成功失败: {}", e.getMessage(), e);
-        }
-    }
-
-
 }