feat(crawler): 实现数据抓取功能并优化UI交互
- 新增 CrawlerConfigAppraisalDTO 类用于封装抓取配置参数 - 在 DataCrawlView 中集成日期选择器和平台选择逻辑 - 添加 ProgressIndicator 加载指示器提升用户体验 - 引入多线程处理网络请求避免界面卡顿 - 集成 HttpUtil 发送抓取请求并与后端接口通信 - 通过 GlobalConfig 配置抓取接口地址 - 增强错误处理机制并在失败时显示相应提示 - 调整按钮状态以防止重复提交抓取任务
This commit is contained in:
@@ -30,6 +30,11 @@ public class GlobalConfig {
|
||||
*/
|
||||
public static final String configInterfaceAddress = BASE_NET_URL+"/crawler/appraisal/config";
|
||||
|
||||
/**
|
||||
* 数据抓取接口
|
||||
*/
|
||||
public static final String crawlInterfaceAddress = BASE_NET_URL+"/crawler/appraisal/evaluation-list-scraping-interface";
|
||||
|
||||
/**
|
||||
* 网络图标接口地址
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.fantaibao.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class CrawlerConfigAppraisalDTO {
|
||||
|
||||
/**
|
||||
* 开始时间yyyy-MM-dd HH:mm:ss,例如2025-12-01 00:00:00
|
||||
*/
|
||||
private Date startTime;
|
||||
/**
|
||||
* 结束时间yyyy-MM-dd HH:mm:ss,例如2025-12-10 23:59:59
|
||||
*/
|
||||
private Date endTime;
|
||||
/**
|
||||
* 动态投递topic名称
|
||||
*/
|
||||
private String topic;
|
||||
/**
|
||||
* 指定租户
|
||||
*/
|
||||
private String tenantId;
|
||||
/**
|
||||
* 平台0美团,1抖音,2饿了么商家端3美团外卖商家版4京东5大众点评
|
||||
*/
|
||||
private Integer platform;
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package com.fantaibao.page;
|
||||
|
||||
import com.fantaibao.page.PlatformSelectionView;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fantaibao.config.GlobalConfig;
|
||||
import com.fantaibao.model.CrawlerConfigAppraisalDTO;
|
||||
import javafx.application.Platform;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
@@ -9,7 +14,12 @@ import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
public class DataCrawlView {
|
||||
|
||||
@@ -17,6 +27,7 @@ public class DataCrawlView {
|
||||
private DatePicker startDatePicker;
|
||||
private DatePicker endDatePicker;
|
||||
private Button crawlButton;
|
||||
private ProgressIndicator progressIndicator; // 添加加载指示器
|
||||
private Stage primaryStage;
|
||||
private Scene previousScene;
|
||||
|
||||
@@ -124,11 +135,16 @@ public class DataCrawlView {
|
||||
"-fx-padding: 15px 40px; -fx-border-radius: 30px; -fx-background-radius: 30px; " +
|
||||
"-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.2), 10, 0, 0, 0);"));
|
||||
|
||||
// 创建加载指示器(默认隐藏)
|
||||
progressIndicator = new ProgressIndicator();
|
||||
progressIndicator.setPrefSize(30, 30);
|
||||
progressIndicator.setVisible(false);
|
||||
|
||||
// 为按钮添加点击事件
|
||||
crawlButton.setOnAction(e -> handleCrawlAction());
|
||||
|
||||
// 添加按钮到按钮区域
|
||||
buttonBox.getChildren().addAll(backButton, crawlButton);
|
||||
buttonBox.getChildren().addAll(backButton, crawlButton, progressIndicator);
|
||||
|
||||
// 将组件添加到主容器
|
||||
root.getChildren().addAll(titleLabel, formPane, buttonBox);
|
||||
@@ -142,15 +158,14 @@ public class DataCrawlView {
|
||||
private void handleCrawlAction() {
|
||||
// 获取选择的平台值
|
||||
String selectedPlatform = platformComboBox.getValue();
|
||||
int platformValue = 0; // 默认美团
|
||||
|
||||
int platformValue; // 默认美团
|
||||
if ("大众点评".equals(selectedPlatform)) {
|
||||
platformValue = 5;
|
||||
} else {
|
||||
platformValue = 0;
|
||||
}
|
||||
|
||||
LocalDate startDate = startDatePicker.getValue();
|
||||
LocalDate endDate = endDatePicker.getValue();
|
||||
|
||||
// 验证日期
|
||||
if (startDate == null || endDate == null) {
|
||||
showAlert(Alert.AlertType.WARNING, "警告", "请选择完整的日期范围");
|
||||
@@ -161,14 +176,63 @@ public class DataCrawlView {
|
||||
showAlert(Alert.AlertType.WARNING, "警告", "开始时间不能晚于结束时间");
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示成功提示
|
||||
showAlert(Alert.AlertType.INFORMATION, "抓取成功",
|
||||
"平台: " + selectedPlatform + "\n" +
|
||||
"平台值: " + platformValue + "\n" +
|
||||
"开始时间: " + startDate + "\n" +
|
||||
"结束时间: " + endDate + "\n" +
|
||||
"数据抓取任务已启动!");
|
||||
|
||||
// 显示加载指示器,禁用抓取按钮
|
||||
progressIndicator.setVisible(true);
|
||||
crawlButton.setDisable(true);
|
||||
crawlButton.setText("抓取中...");
|
||||
|
||||
// 在新线程中执行抓取操作
|
||||
new Thread(() -> {
|
||||
try {
|
||||
// 构造请求参数
|
||||
CrawlerConfigAppraisalDTO configDTO = new CrawlerConfigAppraisalDTO();
|
||||
// 设置开始时间(当天00:00:00)
|
||||
LocalDateTime startDateTime = LocalDateTime.of(startDate, LocalTime.MIN);
|
||||
configDTO.setStartTime(Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant()));
|
||||
// 设置结束时间(当天23:59:59)
|
||||
LocalDateTime endDateTime = LocalDateTime.of(endDate, LocalTime.MAX);
|
||||
configDTO.setEndTime(Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant()));
|
||||
configDTO.setTenantId(GlobalConfig.tenantId);
|
||||
configDTO.setTopic("ftb-crawler-mt-evaluate-"+GlobalConfig.tenantId);
|
||||
configDTO.setPlatform(platformValue);
|
||||
System.out.println(JSON.toJSONString(configDTO));
|
||||
|
||||
// 发送网络请求
|
||||
String result = HttpUtil.post(GlobalConfig.crawlInterfaceAddress, JSON.toJSONString(configDTO));
|
||||
|
||||
// 解析返回结果
|
||||
JSONObject jsonResultObject = JSON.parseObject(result);
|
||||
|
||||
// 在JavaFX主线程中更新UI
|
||||
Platform.runLater(() -> {
|
||||
// 隐藏加载指示器,启用抓取按钮
|
||||
progressIndicator.setVisible(false);
|
||||
crawlButton.setDisable(false);
|
||||
crawlButton.setText("开始抓取");
|
||||
// 根据结果提示用户
|
||||
if (jsonResultObject.getInteger("code") == 200) {
|
||||
showAlert(Alert.AlertType.INFORMATION, "抓取成功",
|
||||
"平台: " + selectedPlatform + "\n" +
|
||||
"开始时间: " + startDate + "\n" +
|
||||
"结束时间: " + endDate + "\n" +
|
||||
"数据抓取任务已启动!");
|
||||
} else {
|
||||
showAlert(Alert.AlertType.ERROR, "抓取失败", jsonResultObject.getString("msg"));
|
||||
}
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
// 发生异常时也要恢复UI状态
|
||||
Platform.runLater(() -> {
|
||||
progressIndicator.setVisible(false);
|
||||
crawlButton.setDisable(false);
|
||||
crawlButton.setText("开始抓取");
|
||||
|
||||
// 显示错误提示弹窗
|
||||
showAlert(Alert.AlertType.ERROR, "抓取失败", "抓取过程中发生错误: " + ex.getMessage());
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void handleBackAction() {
|
||||
|
||||
Reference in New Issue
Block a user