From bf177f47e96cfe18ca06e12724916aabf1154401 Mon Sep 17 00:00:00 2001 From: RedDragon0293 <1352003034@qq.com> Date: Thu, 29 Aug 2024 10:30:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BF=9E=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E5=B0=86=E4=BC=9A=E5=9C=A8=E7=94=A8=E6=88=B7=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E4=B8=8B=E7=BA=BF=E5=90=8E=E6=9A=82=E6=97=B6=E5=85=B3?= =?UTF-8?q?=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +- .../config/configs/AutoReconnectConfig.java | 6 +- .../eportal/utils/Authenticator.java | 91 +++++++++---------- .../window/controllers/MainController.java | 11 +++ 4 files changed, 60 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index 96cb26e..60c2161 100644 --- a/.gitignore +++ b/.gitignore @@ -6,10 +6,7 @@ run/ !**/src/test/**/build/ ### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ +.idea/ *.iws *.iml *.ipr diff --git a/src/main/java/cn/reddragon/eportal/config/configs/AutoReconnectConfig.java b/src/main/java/cn/reddragon/eportal/config/configs/AutoReconnectConfig.java index 51d9106..e9d2656 100644 --- a/src/main/java/cn/reddragon/eportal/config/configs/AutoReconnectConfig.java +++ b/src/main/java/cn/reddragon/eportal/config/configs/AutoReconnectConfig.java @@ -15,6 +15,10 @@ public static boolean getAutoReconnect() { return ((CheckMenuItem) MainWindow.controller.menuBar.getMenus().get(0).getItems().get(0)).isSelected(); } + public static void setAutoReconnect(boolean b) { + ((CheckMenuItem) MainWindow.controller.menuBar.getMenus().get(0).getItems().get(0)).setSelected(b); + } + @Override public JsonElement toJson() { return new JsonPrimitive(getAutoReconnect()); @@ -22,6 +26,6 @@ public JsonElement toJson() { @Override public void fromJson(JsonElement element) { - ((CheckMenuItem) MainWindow.controller.menuBar.getMenus().get(0).getItems().get(0)).setSelected(element.getAsBoolean()); + setAutoReconnect(element.getAsBoolean()); } } diff --git a/src/main/java/cn/reddragon/eportal/utils/Authenticator.java b/src/main/java/cn/reddragon/eportal/utils/Authenticator.java index 423dca4..baf4c8f 100644 --- a/src/main/java/cn/reddragon/eportal/utils/Authenticator.java +++ b/src/main/java/cn/reddragon/eportal/utils/Authenticator.java @@ -172,62 +172,57 @@ public static void updateSession() { }).start(); } - private static String updateSessionInternal() { + private static String updateSessionInternal() throws IOException { HttpURLConnection connection = Authenticator.getUserInfo(); if (connection == null) { return "fail"; } - JsonObject resultJson; - try { - resultJson = JsonParser.parseString(IOUtils.readText(connection.getInputStream())).getAsJsonObject(); - //System.out.println(resultJson.toString()); - String r = resultJson.get("result").getAsString(); - if (Objects.equals(r, "success")) { - // 设置当前用户 - Platform.runLater(() -> MainWindow.controller.user.setText("当前用户: " + resultJson.get("userName").getAsString() + " (" + resultJson.get("userId").getAsString() + ")")); - // 设置运营商、剩余时间 - JsonArray ballArray = JsonParser.parseString(resultJson.get("ballInfo").getAsString()).getAsJsonArray(); - if (ballArray.get(1).getAsJsonObject().get("displayName").getAsString().equals("我的运营商")) { - for (LoginType it : LoginType.values()) { - if (it.authName.contains(ballArray.get(1).getAsJsonObject().get("value").getAsString())) { - Authenticator.type = it; - } + JsonObject resultJson = JsonParser.parseString(IOUtils.readText(connection.getInputStream())).getAsJsonObject(); + //System.out.println(resultJson.toString()); + String r = resultJson.get("result").getAsString(); + if (Objects.equals(r, "success")) { + // 设置当前用户 + Platform.runLater(() -> MainWindow.controller.user.setText("当前用户: " + resultJson.get("userName").getAsString() + " (" + resultJson.get("userId").getAsString() + ")")); + // 设置运营商、剩余时间 + JsonArray ballArray = JsonParser.parseString(resultJson.get("ballInfo").getAsString()).getAsJsonArray(); + if (ballArray.get(1).getAsJsonObject().get("displayName").getAsString().equals("我的运营商")) { + for (LoginType it : LoginType.values()) { + if (it.authName.contains(ballArray.get(1).getAsJsonObject().get("value").getAsString())) { + Authenticator.type = it; } - Platform.runLater(() -> MainWindow.controller.remainLabel.setText("剩余时长: ∞")); - return r; } - Authenticator.type = LoginType.WAN; - int duration = ballArray.get(1).getAsJsonObject().get("value").getAsInt(); - StringBuilder sb = new StringBuilder(); - sb.append("剩余时长: "); - //计算剩余时间 - int h = duration / 3600; - int m = (duration % 3600) / 60; - int s = (duration % 3600) % 60; - if (h > 0) { - sb.append(h).append("h "); - if (s > 0) { - sb.append(m).append("m "); - sb.append(s).append("s"); - } else if (m > 0) { - sb.append(m).append("m"); - } - } else { - if (m > 0) { - sb.append(m).append("m "); - } - if (s > 0) { - sb.append(s).append("s"); - } + Platform.runLater(() -> MainWindow.controller.remainLabel.setText("剩余时长: ∞")); + return r; + } + Authenticator.type = LoginType.WAN; + int duration = ballArray.get(1).getAsJsonObject().get("value").getAsInt(); + StringBuilder sb = new StringBuilder(); + sb.append("剩余时长: "); + //计算剩余时间 + int h = duration / 3600; + int m = (duration % 3600) / 60; + int s = (duration % 3600) % 60; + if (h > 0) { + sb.append(h).append("h "); + if (s > 0) { + sb.append(m).append("m "); + sb.append(s).append("s"); + } else if (m > 0) { + sb.append(m).append("m"); + } + } else { + if (m > 0) { + sb.append(m).append("m "); + } + if (s > 0) { + sb.append(s).append("s"); } - Platform.runLater(() -> MainWindow.controller.remainLabel.setText(sb.toString())); - } else if (!Objects.equals(r, "wait")) { - //Authenticator.type = LoginType.OFFLINE; - Platform.runLater(() -> MainWindow.controller.resultText.setText(resultJson.get("message").getAsString())); } - return r; - } catch (IOException e) { - return "fail"; + Platform.runLater(() -> MainWindow.controller.remainLabel.setText(sb.toString())); + } else if (!Objects.equals(r, "wait")) { + //Authenticator.type = LoginType.OFFLINE; + Platform.runLater(() -> MainWindow.controller.resultText.setText(resultJson.get("message").getAsString())); } + return r; } } diff --git a/src/main/java/cn/reddragon/eportal/window/controllers/MainController.java b/src/main/java/cn/reddragon/eportal/window/controllers/MainController.java index aa4de13..ebc6621 100644 --- a/src/main/java/cn/reddragon/eportal/window/controllers/MainController.java +++ b/src/main/java/cn/reddragon/eportal/window/controllers/MainController.java @@ -3,6 +3,7 @@ import cn.reddragon.eportal.Main; import cn.reddragon.eportal.account.Account; import cn.reddragon.eportal.account.AccountManager; +import cn.reddragon.eportal.config.configs.AutoReconnectConfig; import cn.reddragon.eportal.utils.*; import cn.reddragon.eportal.window.AccountWindow; import cn.reddragon.eportal.window.MainWindow; @@ -47,6 +48,8 @@ public class MainController { @FXML public MenuBar menuBar; + private static boolean temp = false; + public void updateUI() { StringBuilder sb = new StringBuilder(); sb.append("状态: "); @@ -141,6 +144,10 @@ public void onLoginButtonClick() { resultText.setText("当前设备已登录!"); button.setText("登出"); } else if (button.getText().equals("登出")) { + if (AutoReconnectConfig.getAutoReconnect()) { + temp = true; + AutoReconnectConfig.setAutoReconnect(false); + } new Thread(() -> { HttpURLConnection connection = null; try { @@ -228,6 +235,10 @@ public void onLoginButtonClick() { Platform.runLater(() -> resultText.setText(resultMessage.get("result").getAsString() + ":" + resultMessage.get("message").getAsString())); if (result.equals("success")) { Authenticator.userIndex = resultMessage.get("userIndex").getAsString(); + if (temp) { + AutoReconnectConfig.setAutoReconnect(true); + temp = false; + } } } catch (IOException e) { Main.logger.error("登录线程出错!", e);