feat(client): 实现跟卖精灵异步启动和Chrome驱动预加载- 在GenmaiServiceImpl中添加@Async注解实现异步启动跟卖精灵- 增加ChromeDriverPreloader组件预加载Chrome驱动- 添加AsyncConfig配置类启用异步支持

- 优化跟卖精灵启动提示信息和加载状态显示
- 移除Java代码中关于刷新令牌的相关逻辑和依赖- 更新版本号从2.5.5到2.5.6
This commit is contained in:
2025-10-27 16:49:37 +08:00
parent 7e065c1a0b
commit 84087ddf80
20 changed files with 138 additions and 302 deletions

View File

@@ -0,0 +1,9 @@
package com.tashow.erp.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
public class AsyncConfig {
}

View File

@@ -84,14 +84,9 @@ public class SystemController {
@PostMapping("/genmai/open")
public JsonData openGenmaiWebsite(@RequestParam(required = false) Long accountId, HttpServletRequest request) {
try {
String username = com.tashow.erp.utils.JwtUtil.getUsernameFromRequest(request);
genmaiService.openGenmaiWebsite(accountId, username);
return JsonData.buildSuccess("跟卖精灵已打开");
} catch (Exception e) {
logger.error("打开跟卖精灵失败", e);
return JsonData.buildError(e.getMessage() != null ? e.getMessage() : "打开跟卖精灵失败");
}
String username = com.tashow.erp.utils.JwtUtil.getUsernameFromRequest(request);
genmaiService.openGenmaiWebsite(accountId, username);
return JsonData.buildSuccess("跟卖精灵正在启动");
}
@GetMapping("/proxy/image")

View File

@@ -0,0 +1,21 @@
package com.tashow.erp.service.impl;
import jakarta.annotation.PostConstruct;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Component;
@Component
public class ChromeDriverPreloader {
@PostConstruct
public void preloadDriver() {
new Thread(() -> {
try {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu");
ChromeDriver driver = new ChromeDriver(options);
driver.quit();
} catch (Exception ignored) {}
}).start();
}
}

View File

@@ -7,6 +7,7 @@ import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@@ -19,24 +20,27 @@ public class GenmaiServiceImpl {
private final RestTemplate restTemplate = new RestTemplate();
private final ObjectMapper objectMapper = new ObjectMapper();
public void openGenmaiWebsite(Long accountId, String username) throws Exception {
String token = getAndValidateToken(accountId, username);
Runtime.getRuntime().exec("taskkill /f /im chrome.exe");
Thread.sleep(1000);
ChromeOptions options = new ChromeOptions();
String systemUser = System.getProperty("user.name");
char firstChar = systemUser.charAt(0);
char flipped = Character.isUpperCase(firstChar) ? Character.toLowerCase(firstChar) : Character.toUpperCase(firstChar);
String chromeUserData = System.getProperty("user.home")
.replace(systemUser, UrlUtils.urlEncode(flipped + systemUser.substring(1)))
+ "\\AppData\\Local\\Google\\Chrome\\User Data";
options.addArguments("user-data-dir=" + chromeUserData.toLowerCase(), "profile-directory=Default");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.genmaijl.com/#/profile");
((JavascriptExecutor) driver).executeScript("localStorage.setItem('token','" + token + "')");
driver.navigate().refresh();
@Async
public void openGenmaiWebsite(Long accountId, String username) {
try {
String token = getAndValidateToken(accountId, username);
Runtime.getRuntime().exec("taskkill /f /im chrome.exe");
Thread.sleep(1000);
ChromeOptions options = new ChromeOptions();
String systemUser = System.getProperty("user.name");
char firstChar = systemUser.charAt(0);
char flipped = Character.isUpperCase(firstChar) ? Character.toLowerCase(firstChar) : Character.toUpperCase(firstChar);
String chromeUserData = System.getProperty("user.home")
.replace(systemUser, UrlUtils.urlEncode(flipped + systemUser.substring(1)))
+ "\\AppData\\Local\\Google\\Chrome\\User Data";
options.addArguments("user-data-dir=" + chromeUserData.toLowerCase(), "profile-directory=Default");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.genmaijl.com/#/profile");
((JavascriptExecutor) driver).executeScript("localStorage.setItem('token','" + token + "')");
driver.navigate().refresh();
} catch (Exception ignored) {}
}
@SuppressWarnings("unchecked")