token获取1

This commit is contained in:
xuelijun
2026-01-21 18:06:01 +08:00
parent 01dd1d5191
commit 3d9c6dcfb9
5 changed files with 96 additions and 28 deletions

View File

@@ -1,15 +1,16 @@
package com.tem.bocai.controller; package com.tem.bocai.controller;
import com.tem.bocai.param.LoginInfoParam;
import com.tem.bocai.service.LoginService; import com.tem.bocai.service.LoginService;
import com.tem.bocai.util.ImageOcrService; import com.tem.bocai.util.ImageOcrService;
import net.sourceforge.tess4j.TesseractException; import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import java.io.IOException;
@RestController @RestController
@RequestMapping("/ocr")
public class LoginCrawler { public class LoginCrawler {
private final LoginService loginService; private final LoginService loginService;
@@ -18,14 +19,14 @@ public class LoginCrawler {
public LoginCrawler(LoginService loginService) { public LoginCrawler(LoginService loginService) {
this.loginService = loginService; this.loginService = loginService;
} }
@GetMapping("/ocr/login") @PostMapping("/login")
public ResponseEntity<String> ocrLocalImage(String username, String password,String loginUrl,Integer winNum,Integer loseNum) throws IOException, TesseractException { public ResponseEntity<String> ocrLocalImage(@RequestBody LoginInfoParam loginInfoParam) throws IOException, TesseractException {
String result = loginService.loginAutomatic(username,password,loginUrl,winNum,loseNum); String result = loginService.loginAutomatic(loginInfoParam);
return ResponseEntity.ok(result); return ResponseEntity.ok(result);
} }
//今日已结爬取 //今日已结爬取
@GetMapping("/ocr/completedToday") @GetMapping("/completedToday")
public ResponseEntity<String> completedToday() throws IOException, TesseractException { public ResponseEntity<String> completedToday() throws IOException, TesseractException {
String result = loginService.completedToday(); String result = loginService.completedToday();
return ResponseEntity.ok(result); return ResponseEntity.ok(result);

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
*
* https://www.mall4j.com/
*
* 未经允许,不可做商业用途!
*
* 版权所有,侵权必究!
*/
package com.tem.bocai.param;
import lombok.Data;
@Data
public class LoginInfoParam {
public String username;
public String password;
public String loginUrl;
public Integer winNum;
public Integer loseNum;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getLoginUrl() {
return loginUrl;
}
public void setLoginUrl(String loginUrl) {
this.loginUrl = loginUrl;
}
public Integer getWinNum() {
return winNum;
}
public void setWinNum(Integer winNum) {
this.winNum = winNum;
}
public Integer getLoseNum() {
return loseNum;
}
public void setLoseNum(Integer loseNum) {
this.loseNum = loseNum;
}
}

View File

@@ -1,9 +1,11 @@
package com.tem.bocai.service; package com.tem.bocai.service;
import com.tem.bocai.param.LoginInfoParam;
public interface LoginService { public interface LoginService {
String loginAutomatic(String username, String password,String loginUrl,Integer winNum,Integer loseNum); String loginAutomatic(LoginInfoParam loginInfoParam);
//获取token //获取token
String completedToday(); String completedToday();

View File

@@ -1,6 +1,7 @@
package com.tem.bocai.service.impl; package com.tem.bocai.service.impl;
import com.tem.bocai.entity.LoginInfoResult; import com.tem.bocai.entity.LoginInfoResult;
import com.tem.bocai.param.LoginInfoParam;
import com.tem.bocai.service.LoginService; import com.tem.bocai.service.LoginService;
import com.tem.bocai.util.*; import com.tem.bocai.util.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -43,16 +44,16 @@ public class LoginServiceImpl implements LoginService {
private TokenCacheService tokenCacheService; private TokenCacheService tokenCacheService;
@Override @Override
public String loginAutomatic(String username, String password, String loginUrl, Integer winNum, Integer loseNum) { public String loginAutomatic(LoginInfoParam loginInfoParam) {
String token = ""; String token = "";
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 {
token = tokenCacheService.attemptLogin(); token = tokenCacheService.attemptLogin(loginInfoParam);
tokenCacheService.saveToken(token); tokenCacheService.saveToken(token);
if (token != null && !token.isEmpty()) { if (token != null && !token.isEmpty()) {
//保存用户信息 //保存用户信息
addLoginInfo(username, password, loginUrl, winNum, loseNum); addLoginInfo(loginInfoParam);
// 2. 创建爬虫实例传入token // 2. 创建爬虫实例传入token
LotteryWebMagicCrawler crawler = new LotteryWebMagicCrawler(token); LotteryWebMagicCrawler crawler = new LotteryWebMagicCrawler(token);
@@ -109,14 +110,13 @@ public class LoginServiceImpl implements LoginService {
/** /**
* 添加登录信息 * 添加登录信息
*/ */
public boolean addLoginInfo(String username, String password, public boolean addLoginInfo(LoginInfoParam loginInfoParam) {
String loginUrl, Integer winNum, Integer loseNum) {
LoginInfoResult loginInfo = new LoginInfoResult(); LoginInfoResult loginInfo = new LoginInfoResult();
loginInfo.setUsername(username); loginInfo.setUsername(loginInfoParam.getUsername());
loginInfo.setPassword(password); loginInfo.setPassword(loginInfoParam.getPassword());
loginInfo.setLoginUrl(loginUrl); loginInfo.setLoginUrl(loginInfoParam.getLoginUrl());
loginInfo.setWinNum(winNum != null ? winNum : 0); loginInfo.setWinNum(loginInfoParam.getWinNum() != null ? loginInfoParam.getWinNum() : 0);
loginInfo.setLoseNum(loseNum != null ? loseNum : 0); loginInfo.setLoseNum(loginInfoParam.getLoseNum() != null ? loginInfoParam.getLoseNum() : 0);
loginInfo.setCreateTime(new Date()); loginInfo.setCreateTime(new Date());
loginInfo.setUpdateTime(new Date()); loginInfo.setUpdateTime(new Date());

View File

@@ -1,5 +1,6 @@
package com.tem.bocai.util; package com.tem.bocai.util;
import com.tem.bocai.param.LoginInfoParam;
import com.tem.bocai.service.LoginService; import com.tem.bocai.service.LoginService;
import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException; import net.sourceforge.tess4j.TesseractException;
@@ -126,7 +127,8 @@ 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 {
token = attemptLogin();
// token = attemptLogin();
if (token != null && !token.isEmpty()) { if (token != null && !token.isEmpty()) {
return token; return token;
@@ -153,7 +155,7 @@ public class TokenCacheService {
/** /**
* 单次登录尝试 * 单次登录尝试
*/ */
public String attemptLogin() { public String attemptLogin(LoginInfoParam loginInfoParam) {
CookieStore cookieStore = new BasicCookieStore(); CookieStore cookieStore = new BasicCookieStore();
try (CloseableHttpClient httpClient = createHttpClient(cookieStore)) { try (CloseableHttpClient httpClient = createHttpClient(cookieStore)) {
// 1. 获取验证码 // 1. 获取验证码
@@ -167,7 +169,7 @@ public class TokenCacheService {
return null; return null;
} }
// 3. 执行登录 // 3. 执行登录
return performLogin(httpClient, cookieStore, code); return performLogin(httpClient, cookieStore, code,loginInfoParam);
} catch (Exception e) { } catch (Exception e) {
System.out.println("登录尝试失败" + e); System.out.println("登录尝试失败" + e);
@@ -262,13 +264,13 @@ public class TokenCacheService {
*/ */
private String performLogin(CloseableHttpClient httpClient, private String performLogin(CloseableHttpClient httpClient,
CookieStore cookieStore, CookieStore cookieStore,
String code) throws IOException, InterruptedException { String code,LoginInfoParam loginInfoParam) throws IOException, InterruptedException {
System.out.println("执行登录..."); System.out.println("执行登录...");
// 等待一下再发送登录请求 // 等待一下再发送登录请求
Thread.sleep(1500 + (long) (Math.random() * 1000)); Thread.sleep(1500 + (long) (Math.random() * 1000));
HttpPost loginPost = createLoginRequest(code); HttpPost loginPost = createLoginRequest(code,loginInfoParam);
try (CloseableHttpResponse loginResponse = httpClient.execute(loginPost)) { try (CloseableHttpResponse loginResponse = httpClient.execute(loginPost)) {
return processLoginResponse(loginResponse, cookieStore); return processLoginResponse(loginResponse, cookieStore);
@@ -278,20 +280,20 @@ public class TokenCacheService {
/** /**
* 创建登录请求 * 创建登录请求
*/ */
private HttpPost createLoginRequest(String code) throws UnsupportedEncodingException { private HttpPost createLoginRequest(String code,LoginInfoParam loginInfoParam) throws UnsupportedEncodingException {
HttpPost loginPost = new HttpPost(BASE_URL + "/login"); HttpPost loginPost = new HttpPost(loginInfoParam.loginUrl + "/login");
// 设置请求头 // 设置请求头
setCommonHeaders(loginPost); setCommonHeaders(loginPost);
loginPost.setHeader("Referer", BASE_URL + "/login"); loginPost.setHeader("Referer", loginInfoParam.loginUrl + "/login");
loginPost.setHeader("Origin", BASE_URL); loginPost.setHeader("Origin", loginInfoParam.loginUrl);
loginPost.setHeader("Accept", "application/json, text/plain, */*"); loginPost.setHeader("Accept", "application/json, text/plain, */*");
// 构建登录参数 // 构建登录参数
List<NameValuePair> params = new ArrayList<>(); List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("type", "1")); params.add(new BasicNameValuePair("type", "1"));
params.add(new BasicNameValuePair("account", "pmk1")); params.add(new BasicNameValuePair("account", loginInfoParam.username));
params.add(new BasicNameValuePair("password", "Asd123123")); params.add(new BasicNameValuePair("password", loginInfoParam.password));
params.add(new BasicNameValuePair("code", code)); params.add(new BasicNameValuePair("code", code));
loginPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); loginPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));