保存用户登入信息2

This commit is contained in:
xuelijun
2026-01-22 09:59:27 +08:00
parent 04fcd991f1
commit 9477b47399
3 changed files with 16 additions and 6 deletions

View File

@@ -12,4 +12,6 @@ public interface LoginInfoRepository extends JpaRepository<LoginInfoResult, Long
Optional<LoginInfoResult> findByUsername(String username); Optional<LoginInfoResult> findByUsername(String username);
boolean existsByUsername(String username); boolean existsByUsername(String username);
void deleteByUsername(String username); void deleteByUsername(String username);
// 方法1: 按创建时间倒序取第一条
Optional<LoginInfoResult> findFirstByOrderByCreateTimeDesc();
} }

View File

@@ -35,6 +35,7 @@ public class LoginServiceImpl implements LoginService {
System.out.println("\n=== 第 " + attempt + " 次尝试 ==="); System.out.println("\n=== 第 " + attempt + " 次尝试 ===");
try { try {
token = tokenCacheService.attemptLogin(loginInfoParam); token = tokenCacheService.attemptLogin(loginInfoParam);
System.out.println("1token = " + token);
tokenCacheService.saveToken(token); tokenCacheService.saveToken(token);
if (token != null && !token.isEmpty()) { if (token != null && !token.isEmpty()) {
//保存用户信息 //保存用户信息
@@ -55,8 +56,6 @@ public class LoginServiceImpl implements LoginService {
// 5. 返回爬取的数据 // 5. 返回爬取的数据
List<Map<String, Object>> result = pipeline.getLotteryData(); List<Map<String, Object>> result = pipeline.getLotteryData();
System.out.println("爬虫完成,获取到 " + result.size() + " 条数据");
System.out.println("===="+result);
return result.toString(); return result.toString();
} }

View File

@@ -1,7 +1,10 @@
package com.tem.bocai.util; package com.tem.bocai.util;
import com.tem.bocai.entity.LoginInfoResult;
import com.tem.bocai.param.LoginInfoParam; import com.tem.bocai.param.LoginInfoParam;
import com.tem.bocai.repository.LoginInfoRepository;
import com.tem.bocai.service.LoginService; import com.tem.bocai.service.LoginService;
import lombok.val;
import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException; import net.sourceforge.tess4j.TesseractException;
import org.apache.http.Header; import org.apache.http.Header;
@@ -45,7 +48,8 @@ public class TokenCacheService {
private Tesseract tesseract; private Tesseract tesseract;
@Autowired @Autowired
private CacheManager tokenCacheManager; private CacheManager tokenCacheManager;
@Autowired
private LoginInfoRepository loginInfoRepository;
/** /**
* 保存token到缓存 * 保存token到缓存
*/ */
@@ -127,9 +131,14 @@ public class TokenCacheService {
for (int attempt = 1; attempt <= MAX_RETRY; attempt++) { for (int attempt = 1; attempt <= MAX_RETRY; attempt++) {
System.out.println("\n=== 第 " + attempt + " 次尝试 ==="); System.out.println("\n=== 第 " + attempt + " 次尝试 ===");
try { try {
LoginInfoResult firstByOrderByCreateTimeDesc = loginInfoRepository.findFirstByOrderByCreateTimeDesc()
// token = attemptLogin(); .orElse(null);
LoginInfoParam loginInfoParam = new LoginInfoParam();
loginInfoParam.setUsername(firstByOrderByCreateTimeDesc.getUsername());
loginInfoParam.setPassword(firstByOrderByCreateTimeDesc.getPassword());
loginInfoParam.setLoginUrl(firstByOrderByCreateTimeDesc.getLoginUrl());
token = attemptLogin(loginInfoParam);
System.out.println("2token = " + token);
if (token != null && !token.isEmpty()) { if (token != null && !token.isEmpty()) {
return token; return token;
} }