feat(login): 优化登录界面交互体验

- 调整抖音和美团爬虫服务的浏览器视口宽度从1600px为1680px
- 移除未使用的导入类,清理冗余代码- 登录按钮点击后增加加载指示器并禁用按钮防止重复提交
- 将登录请求移至子线程执行避免阻塞UI线程
- 增加异常处理确保网络异常时界面可恢复正常状态
This commit is contained in:
wangchunxiang
2025-10-14 15:20:48 +08:00
parent a9b3ea942b
commit d808047c1c
3 changed files with 69 additions and 41 deletions

View File

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
import com.fantaibao.config.GlobalConfig;
import com.fantaibao.model.LoginUserDTO;
import com.fantaibao.model.LoginUserVO;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
@@ -13,6 +14,7 @@ import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@@ -54,17 +56,38 @@ public class LoginView {
// 设置鼠标悬停时为手型光标
loginButton.setCursor(javafx.scene.Cursor.HAND);
// 创建加载指示器(默认隐藏)
ProgressIndicator progressIndicator = new ProgressIndicator();
progressIndicator.setPrefSize(30, 30);
progressIndicator.setVisible(false);
// 添加登录按钮事件处理
loginButton.setOnAction(e -> {
// 获取用户输入的账号和密码
String account = accountField.getText();
String password = passwordField.getText();
// 显示加载指示器,禁用登录按钮
progressIndicator.setVisible(true);
loginButton.setDisable(true);
loginButton.setText("登录中...");
// 在新线程中执行登录操作
new Thread(() -> {
try {
LoginUserDTO loginUserDTO = new LoginUserDTO();
loginUserDTO.setAccount(account);
loginUserDTO.setPassword(password);
String result = HttpUtil.post(GlobalConfig.loginInterfaceAddress, JSON.toJSONString(loginUserDTO));
// 解析返回结果
JSONObject jsonResultObject = JSON.parseObject(result);
// 在JavaFX主线程中更新UI
Platform.runLater(() -> {
// 隐藏加载指示器,启用登录按钮
progressIndicator.setVisible(false);
loginButton.setDisable(false);
loginButton.setText("登 录");
if (jsonResultObject.getInteger("code") != 200) {
// 显示错误提示弹窗
Alert alert = new Alert(Alert.AlertType.ERROR);
@@ -84,9 +107,28 @@ public class LoginView {
GlobalConfig.dyLoginPage = loginUserVO.getDyLoginPage();
navigateToPlatformSelection(primaryStage);
});
} catch (Exception ex) {
// 发生异常时也要恢复UI状态
Platform.runLater(() -> {
progressIndicator.setVisible(false);
loginButton.setDisable(false);
loginButton.setText("登 录");
// 显示错误提示弹窗
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("登录失败");
alert.setHeaderText(null);
alert.setContentText("登录过程中发生错误: " + ex.getMessage());
alert.showAndWait();
});
}
}).start();
});
// 添加组件到主容器
root.getChildren().addAll(titleLabel, infoLabel, new Label("账号"), accountField,
new Label("密码"), passwordField, loginButton);
new Label("密码"), passwordField, loginButton, progressIndicator);
// 设置场景并显示窗口
Scene scene = new Scene(root, 400, 600);
primaryStage.setScene(scene);

View File

@@ -1,18 +1,12 @@
package com.fantaibao.service;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fantaibao.base.FtbCrawlNetBase;
import com.fantaibao.config.GlobalConfig;
import com.fantaibao.model.UpdateUserCookieDTO;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.Cookie;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -29,7 +23,7 @@ public class FtbCrawlNetDy extends AbstractFtbCrawlNetBase implements FtbCrawlNe
try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
// 设置浏览器窗口大小为1920x1080
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
.setViewportSize(1600, 900);
.setViewportSize(1680, 900);
BrowserContext context = browser.newContext(contextOptions);
Page page = context.newPage();
AtomicBoolean dyCookie = new AtomicBoolean(false);

View File

@@ -1,20 +1,12 @@
package com.fantaibao.service;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fantaibao.base.FtbCrawlNetBase;
import com.fantaibao.config.GlobalConfig;
import com.fantaibao.model.UpdateUserCookieDTO;
import com.microsoft.playwright.*;
import jakarta.annotation.Resource;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -31,7 +23,7 @@ public class FtbCrawlNetMt extends AbstractFtbCrawlNetBase implements FtbCrawlNe
try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
// 设置浏览器窗口大小为1920x1080
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
.setViewportSize(1600, 900);
.setViewportSize(1680, 900);
BrowserContext context = browser.newContext(contextOptions);
// 美团cookie
AtomicBoolean mtCookie = new AtomicBoolean(false);