|
|
@@ -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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|