|
|
@@ -16,6 +16,7 @@ import com.jzg.quotation.huanong.crawler.entity.response.*;
|
|
|
import com.jzg.quotation.huanong.crawler.service.HuaNongCrawlerQuoteProcessService;
|
|
|
import com.jzg.quotation.huanong.crawler.utils.ImageIdGenerate;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -23,6 +24,7 @@ import java.time.Duration;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
|
public class HuaNongCrawlerQuoteProcessServiceImpl implements HuaNongCrawlerQuoteProcessService {
|
|
|
@@ -38,13 +40,13 @@ public class HuaNongCrawlerQuoteProcessServiceImpl implements HuaNongCrawlerQuot
|
|
|
private final RedissonComponent redissonComponent;
|
|
|
|
|
|
// 登录循环次数
|
|
|
- private final static int LOGIN_RETRY_COUNT = 3;
|
|
|
+ private final static int LOGIN_RETRY_COUNT = 5;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void login(String username, String password, boolean isFirst) {
|
|
|
+ public void login(String username, String password,String deviceId, boolean isFirst) {
|
|
|
try {
|
|
|
- distributedLockComponent.executeWithLock(HuaNongCrawlerCacheComponent.LOGIN_LOCKOUT + username, () -> authorization(username, password, isFirst), 20, 30);
|
|
|
+ distributedLockComponent.executeWithLock(HuaNongCrawlerCacheComponent.LOGIN_LOCKOUT + username, () -> authorization(username, password,deviceId, isFirst), 20, 30);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
throw new SystemException("登录失败");
|
|
|
@@ -62,23 +64,48 @@ public class HuaNongCrawlerQuoteProcessServiceImpl implements HuaNongCrawlerQuot
|
|
|
.orElse("登录失败,未知错误");
|
|
|
}
|
|
|
|
|
|
- private HuaNongCrawlerLoginResponse attemptLoginWithRetry(String username, String password) {
|
|
|
+ private HuaNongCrawlerLoginResponse attemptLoginWithRetry(String username, String password,String deviceId) {
|
|
|
// 循环次数
|
|
|
for (int i = 0; i < LOGIN_RETRY_COUNT; i++) {
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- // 验证码
|
|
|
- HuaNongCrawlerVerificationCodeRequestRequest huaNongCrawlerVerificationCodeRequest = new HuaNongCrawlerVerificationCodeRequestRequest();
|
|
|
- HuaNongCrawlerVerificationCodeResponse huaNongCrawlerVerificationCodeResponse = huaNongCrawlerRequestComponent.verificationCode(huaNongCrawlerVerificationCodeRequest, httpHeaders);
|
|
|
- // 识别验证码
|
|
|
- String captcha = exteriorUtilsRequest.captchaCalculate(huaNongCrawlerVerificationCodeResponse.getImageStream());
|
|
|
- // 验证码结果
|
|
|
- //String captchaResult = parseCaptcha(captcha);
|
|
|
- // 登录
|
|
|
- HuaNongCrawlerLoginRequestRequest huaNongCrawlerLoginRequest = new HuaNongCrawlerLoginRequestRequest(username, password, huaNongCrawlerVerificationCodeResponse.getCaptchaKey(), captcha);
|
|
|
- HuaNongCrawlerLoginResponse login = huaNongCrawlerRequestComponent.login(huaNongCrawlerLoginRequest, httpHeaders);
|
|
|
- if (isLoginSuccessful(login)) {
|
|
|
- huaNongCrawlerCacheComponent.cacheLoginInfo(login, username);
|
|
|
- return login;
|
|
|
+ try {
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+
|
|
|
+ // 1. 获取验证码
|
|
|
+ HuaNongCrawlerVerificationCodeRequestRequest request = new HuaNongCrawlerVerificationCodeRequestRequest();
|
|
|
+ HuaNongCrawlerVerificationCodeResponse response = huaNongCrawlerRequestComponent.verificationCode(request, httpHeaders);
|
|
|
+
|
|
|
+ // 2. 识别验证码文本
|
|
|
+ String captchaText = exteriorUtilsRequest.captchaIdentify(response.getImageStream());
|
|
|
+ log.info("第 {} 次尝试,识别结果: {}", i + 1, captchaText);
|
|
|
+
|
|
|
+ // 3. 计算验证码结果
|
|
|
+ String captchaResult = parseCaptcha(captchaText);
|
|
|
+ log.info("计算结果: {}", captchaResult);
|
|
|
+
|
|
|
+ // 4. 执行登录(用计算后的结果)
|
|
|
+ HuaNongCrawlerLoginRequestRequest loginRequest = new HuaNongCrawlerLoginRequestRequest(
|
|
|
+ username,
|
|
|
+ password,
|
|
|
+ deviceId,
|
|
|
+ response.getCaptchaKey(),
|
|
|
+ captchaResult // ← 用计算结果而不是原始文本
|
|
|
+ );
|
|
|
+
|
|
|
+ HuaNongCrawlerLoginResponse loginResponse = huaNongCrawlerRequestComponent.login(loginRequest, httpHeaders);
|
|
|
+
|
|
|
+ // 5. 判断是否成功
|
|
|
+ if (isLoginSuccessful(loginResponse)) {
|
|
|
+ log.info("登录成功,第 {} 次尝试", i + 1);
|
|
|
+ huaNongCrawlerCacheComponent.cacheLoginInfo(loginResponse, username);
|
|
|
+ return loginResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 失败时记录原因
|
|
|
+ String errorMsg = extractErrorMessage(loginResponse);
|
|
|
+ log.warn("第 {} 次登录失败: {}", i + 1, errorMsg);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("第 {} 次尝试发生异常: {}", i + 1, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
throw new SystemException("超过最大重试次数,登录失败");
|
|
|
@@ -90,15 +117,19 @@ public class HuaNongCrawlerQuoteProcessServiceImpl implements HuaNongCrawlerQuot
|
|
|
*/
|
|
|
private String parseCaptcha(String captcha) {
|
|
|
String result = "";
|
|
|
- if (captcha == null || captcha.length() < 3) {
|
|
|
+ if (captcha == null) {
|
|
|
throw new SystemException("验证码长度解析失败");
|
|
|
+ }else if(captcha.length() == 2 ){
|
|
|
+ int firstNum = Integer.parseInt(String.valueOf(captcha.charAt(0)));
|
|
|
+ int lastNum = Integer.parseInt(String.valueOf(captcha.charAt(1)));
|
|
|
+ result = String.valueOf(firstNum - lastNum);
|
|
|
}
|
|
|
else if (captcha.length() == 3 && (captcha.contains("x") || captcha.contains("+"))) {
|
|
|
try {
|
|
|
int firstNum = Integer.parseInt(String.valueOf(captcha.charAt(0)));
|
|
|
int lastNum = Integer.parseInt(String.valueOf(captcha.charAt(2)));
|
|
|
if (captcha.contains("x")) {
|
|
|
- result = String.valueOf(firstNum * lastNum);
|
|
|
+ result = String.valueOf(firstNum + lastNum);
|
|
|
}
|
|
|
else if (captcha.contains("+")) {
|
|
|
result = String.valueOf(firstNum + lastNum);
|
|
|
@@ -117,18 +148,18 @@ public class HuaNongCrawlerQuoteProcessServiceImpl implements HuaNongCrawlerQuot
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- private void authorization(String username, String password, boolean isFirst) {
|
|
|
+ private void authorization(String username, String password,String deviceId, boolean isFirst) {
|
|
|
// 判断是否在首次登录(协议、账号)
|
|
|
if (!isFirst) {
|
|
|
huaNongCrawlerCacheComponent.isAccountLockout(username);
|
|
|
}
|
|
|
- HuaNongCrawlerLoginResponse login = attemptLoginWithRetry(username, password);
|
|
|
+ HuaNongCrawlerLoginResponse login = attemptLoginWithRetry(username, password,deviceId);
|
|
|
// 判断是否在首次登录(协议、账号)
|
|
|
if (!isFirst) {
|
|
|
huaNongCrawlerCacheComponent.accountLockout(username);
|
|
|
}
|
|
|
- String s = extractErrorMessage(login);
|
|
|
- throw new SystemException(s);
|
|
|
+ /* String s = extractErrorMessage(login);
|
|
|
+ throw new SystemException(s);*/
|
|
|
}
|
|
|
|
|
|
@Override
|