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);