Skip to content

Commit

Permalink
自动重连现在将会在用户手动下线后暂时关闭
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDragon0293 committed Aug 29, 2024
1 parent 6012329 commit bf177f4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 53 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ 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());
}

@Override
public void fromJson(JsonElement element) {
((CheckMenuItem) MainWindow.controller.menuBar.getMenus().get(0).getItems().get(0)).setSelected(element.getAsBoolean());
setAutoReconnect(element.getAsBoolean());
}
}
91 changes: 43 additions & 48 deletions src/main/java/cn/reddragon/eportal/utils/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("状态: ");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bf177f4

Please sign in to comment.