| | |
| | | import set.Setsys; |
| | | import udpdell.UDPServer; |
| | | import user.Usrdell; |
| | | import Mqttmessage.Client; |
| | | import login.LoginVerifier; |
| | | import javax.swing.*; |
| | | import java.awt.*; |
| | | import java.awt.event.*; |
| | |
| | | boolean isRemember = "1".equals(rememberPwd); |
| | | rememberMe.setSelected(isRemember); |
| | | |
| | | // 如果记住我被选中,填充密码 |
| | | // 如果记住我被选中,填充密码并自动登录(延迟执行,确保窗口已显示) |
| | | if (isRemember) { |
| | | String savedPassword = UserChuShiHua.getProperty("password"); |
| | | if (savedPassword != null && !savedPassword.equals("-1")) { |
| | | String savedEmail = UserChuShiHua.getProperty("email"); |
| | | if (savedPassword != null && !savedPassword.equals("-1") && |
| | | savedEmail != null && !savedEmail.equals("-1")) { |
| | | passwordField.setText(savedPassword); |
| | | // 延迟执行自动登录,确保窗口已经显示 |
| | | SwingUtilities.invokeLater(() -> { |
| | | performAutoLogin(savedEmail, savedPassword); |
| | | }); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | private void performLogin() { |
| | | String username = usernameField.getText().trim(); |
| | | String usernameOrEmail = usernameField.getText().trim(); |
| | | String password = new String(passwordField.getPassword()).trim(); |
| | | |
| | | // 输入验证 |
| | | if (username.isEmpty()) { |
| | | if (usernameOrEmail.isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, |
| | | "请输入用户名或邮箱", |
| | | "输入错误", |
| | |
| | | return; |
| | | } |
| | | |
| | | // 验证用户名和密码 |
| | | if (validateLogin(username, password)) { |
| | | // 更新最后登录时间 |
| | | UserChuShiHua.updateProperty("lastLoginTime", String.valueOf(System.currentTimeMillis())); |
| | | |
| | | // 保存记住我状态 |
| | | if (rememberMe.isSelected()) { |
| | | UserChuShiHua.updateProperty("rememberPassword", "1"); |
| | | UserChuShiHua.updateProperty("userName", username); |
| | | } else { |
| | | UserChuShiHua.updateProperty("rememberPassword", "0"); |
| | | // 获取邮箱地址(如果输入的是用户名,从配置文件中获取对应的邮箱) |
| | | String email = getEmailFromInput(usernameOrEmail); |
| | | |
| | | // 禁用登录按钮,防止重复点击 |
| | | loginButton.setEnabled(false); |
| | | |
| | | // 在新线程中调用API,避免阻塞UI |
| | | new Thread(() -> { |
| | | try { |
| | | // 调用登录验证API |
| | | LoginVerifier.LoginVerifyResponse response = LoginVerifier.verifyLogin(email, password); |
| | | |
| | | // 在EDT线程中更新UI |
| | | SwingUtilities.invokeLater(() -> { |
| | | loginButton.setEnabled(true); |
| | | |
| | | if (response.isSuccess()) { |
| | | // 登录成功 |
| | | // 更新最后登录时间 |
| | | UserChuShiHua.updateProperty("lastLoginTime", String.valueOf(System.currentTimeMillis())); |
| | | |
| | | // 保存记住我状态 |
| | | if (rememberMe.isSelected()) { |
| | | UserChuShiHua.updateProperty("rememberPassword", "1"); |
| | | UserChuShiHua.updateProperty("userName", usernameOrEmail); |
| | | UserChuShiHua.updateProperty("email", email); |
| | | UserChuShiHua.updateProperty("password", password); |
| | | } else { |
| | | UserChuShiHua.updateProperty("rememberPassword", "0"); |
| | | } |
| | | |
| | | // 关闭登录窗口并打开首页 |
| | | openMainApplication(); |
| | | } else { |
| | | // 登录失败,显示错误信息 |
| | | String errorMessage = response.getMessage() != null ? |
| | | response.getMessage() : "用户名或密码错误!"; |
| | | JOptionPane.showMessageDialog(this, |
| | | errorMessage, |
| | | "登录失败", |
| | | JOptionPane.ERROR_MESSAGE); |
| | | passwordField.setText(""); |
| | | passwordField.requestFocus(); |
| | | } |
| | | }); |
| | | } catch (Exception e) { |
| | | // 异常处理,恢复按钮并显示错误 |
| | | SwingUtilities.invokeLater(() -> { |
| | | loginButton.setEnabled(true); |
| | | JOptionPane.showMessageDialog(this, |
| | | "登录时发生错误: " + e.getMessage(), |
| | | "错误", |
| | | JOptionPane.ERROR_MESSAGE); |
| | | passwordField.setText(""); |
| | | passwordField.requestFocus(); |
| | | }); |
| | | } |
| | | |
| | | // 关闭登录窗口并打开首页 |
| | | openMainApplication(); |
| | | |
| | | } else { |
| | | JOptionPane.showMessageDialog(this, |
| | | "用户名或密码错误!", |
| | | "登录失败", |
| | | JOptionPane.ERROR_MESSAGE); |
| | | passwordField.setText(""); |
| | | passwordField.requestFocus(); |
| | | } |
| | | }).start(); |
| | | } |
| | | |
| | | private boolean validateLogin(String username, String password) { |
| | | // 从UserChuShiHua获取存储的用户数据进行比较 |
| | | String storedUsername = UserChuShiHua.getProperty("userName"); |
| | | String storedPassword = UserChuShiHua.getProperty("password"); |
| | | String storedEmail = UserChuShiHua.getProperty("email"); |
| | | |
| | | // 检查用户名/邮箱和密码是否匹配 |
| | | boolean usernameMatch = username.equals(storedUsername) || username.equals(storedEmail); |
| | | boolean passwordMatch = password.equals(storedPassword); |
| | | |
| | | return usernameMatch && passwordMatch; |
| | | // 邮箱验证方法 |
| | | private boolean isValidEmail(String email) { |
| | | // 简单的邮箱格式验证 |
| | | String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; |
| | | return email != null && email.matches(emailRegex); |
| | | } |
| | | |
| | | // 获取邮箱地址(如果输入的是用户名,则从配置文件中获取邮箱) |
| | | private String getEmailFromInput(String usernameOrEmail) { |
| | | // 如果输入的是邮箱格式,直接返回 |
| | | if (isValidEmail(usernameOrEmail)) { |
| | | return usernameOrEmail; |
| | | } |
| | | // 否则,从配置文件中获取保存的邮箱 |
| | | String savedEmail = UserChuShiHua.getProperty("email"); |
| | | if (savedEmail != null && !savedEmail.equals("-1")) { |
| | | // 检查输入的用户名是否匹配保存的用户名 |
| | | String savedUsername = UserChuShiHua.getProperty("userName"); |
| | | if (usernameOrEmail.equals(savedUsername)) { |
| | | return savedEmail; |
| | | } |
| | | } |
| | | // 如果找不到邮箱,返回输入的字符串(可能是邮箱格式但验证失败,或者用户名不存在) |
| | | return usernameOrEmail; |
| | | } |
| | | |
| | | // 自动登录(从配置文件读取邮箱和密码) |
| | | private void performAutoLogin(String email, String password) { |
| | | // 在新线程中调用API,避免阻塞UI |
| | | new Thread(() -> { |
| | | try { |
| | | LoginVerifier.LoginVerifyResponse response = LoginVerifier.verifyLogin(email, password); |
| | | |
| | | SwingUtilities.invokeLater(() -> { |
| | | if (response.isSuccess()) { |
| | | // 登录成功,跳转到主页 |
| | | openMainApplication(); |
| | | } else { |
| | | // 登录失败,清空密码字段,让用户重新输入 |
| | | passwordField.setText(""); |
| | | usernameField.requestFocus(); |
| | | } |
| | | }); |
| | | } catch (Exception e) { |
| | | // 异常处理,清空密码字段 |
| | | SwingUtilities.invokeLater(() -> { |
| | | passwordField.setText(""); |
| | | usernameField.requestFocus(); |
| | | }); |
| | | } |
| | | }).start(); |
| | | } |
| | | |
| | | private void openMainApplication() { |
| | |
| | | public static void launchMainApp() { |
| | | System.out.println("准备打开主应用程序..."); |
| | | |
| | | SwingUtilities.invokeLater(() -> { |
| | | JFrame mainFrame = new JFrame("智能割草系统"); |
| | | mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| | | SwingUtilities.invokeLater(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | JFrame mainFrame = new JFrame("智能割草系统"); |
| | | mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| | | |
| | | Shouye homePanel = new Shouye(); |
| | | mainFrame.setContentPane(homePanel); |
| | | Shouye homePanel = new Shouye(); |
| | | mainFrame.setContentPane(homePanel); |
| | | |
| | | // 设置与登录页面相同的尺寸 |
| | | mainFrame.setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT); |
| | | mainFrame.setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT)); |
| | | mainFrame.setResizable(false); |
| | | mainFrame.setLocationRelativeTo(null); |
| | | mainFrame.setVisible(true); |
| | | // 设置与登录页面相同的尺寸 |
| | | mainFrame.setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT); |
| | | mainFrame.setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT)); |
| | | mainFrame.setResizable(true); |
| | | mainFrame.setLocationRelativeTo(null); |
| | | mainFrame.setVisible(true); |
| | | |
| | | System.out.println("主应用程序已启动"); |
| | | System.out.println("主应用程序已启动"); |
| | | |
| | | // 启动后连接MQTT |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | System.out.println("正在连接MQTT服务器..."); |
| | | Client.lianjiemqqt(); |
| | | } |
| | | }).start(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | |
| | | System.out.println("初始用户名: " + UserChuShiHua.getProperty("userName")); |
| | | System.out.println("初始密码: " + UserChuShiHua.getProperty("password")); |
| | | |
| | | // 检查是否记住密码,如果是则直接进入主页 |
| | | String rememberPwd = UserChuShiHua.getProperty("rememberPassword"); |
| | | if ("1".equals(rememberPwd)) { |
| | | String storedUser = UserChuShiHua.getProperty("userName"); |
| | | if (storedUser != null && !storedUser.equals("-1")) { |
| | | launchMainApp(); |
| | | return; |
| | | } |
| | | } |
| | | |
| | | // 启动登录界面 |
| | | // 启动登录界面(自动登录逻辑在 loadUserPreferences 中处理) |
| | | EventQueue.invokeLater(() -> { |
| | | try { |
| | | new Denglu().setVisible(true); |