Skip to content

Commit

Permalink
重构架构
Browse files Browse the repository at this point in the history
添加多账号支持
  • Loading branch information
RedDragon0293 committed Jun 6, 2024
1 parent c547cc5 commit bbd08f3
Show file tree
Hide file tree
Showing 18 changed files with 336 additions and 121 deletions.
4 changes: 3 additions & 1 deletion .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 6 additions & 14 deletions src/main/java/cn/reddragon/eportal/Main.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package cn.reddragon.eportal;

import cn.reddragon.eportal.config.ConfigManager;
import cn.reddragon.eportal.utils.Authenticator;
import cn.reddragon.eportal.window.MainWindow;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {
public static Logger logger = LogManager.getLogger("EPortal");

public static void main(String[] args) {
Thread.currentThread().setName("EPortal Main");
EPortal.askThread.setDaemon(true);
Authenticator.checkOnline();
try {
if (Authenticator.online) {
Authenticator.updateUserIndex();
}
} catch (NullPointerException e) {
EPortal.logger.error("启动时出错!", e);
System.exit(0);
}
EPortal.launch();
ConfigManager.saveConfigs();
MainWindow.main(args);
}
}
24 changes: 8 additions & 16 deletions src/main/java/cn/reddragon/eportal/account/Account.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package cn.reddragon.eportal.account;

public final class Account {
private final String name;
private final String password;

public Account(String name, String password) {
this.name = name;
this.password = password;
}

public String getName() {
return name;
public record Account(String name, String password) {
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Account a))
return false;
return name.equals(a.name);
}

public String getPassword() {
return password;
}

}
21 changes: 21 additions & 0 deletions src/main/java/cn/reddragon/eportal/account/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ public class AccountManager {
public static final ArrayList<Account> accounts = new ArrayList<>();

public static void addAccount(String username, String password) {
accounts.removeIf(it -> it.name().equals(username));
accounts.add(new Account(username, password));
}

public static void deleteAccount(String username) {
accounts.removeIf(it -> it.name().equals(username));
}

public static boolean contains(String username) {
for (Account it : accounts) {
if (it.name().equals(username))
return true;
}
return false;
}

public static Account getAccount(String username) {
for (Account it : accounts) {
if (it.name().equals(username))
return it;
}
return null;
}
}
12 changes: 9 additions & 3 deletions src/main/java/cn/reddragon/eportal/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.reddragon.eportal.config.configs.AutoReconnectConfig;
import cn.reddragon.eportal.config.configs.NetTypeConfig;
import com.google.gson.*;
import javafx.scene.control.Alert;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -16,7 +17,7 @@ public class ConfigManager {
private static final File configFile = new File("EPortal.json");
private static final ArrayList<AbstractConfig> configs = new ArrayList<>();
private static final Logger logger = LogManager.getLogger("ConfigManager");
private static final int configVersion = 1;
private static final int configVersion = 2;

static {
if (!configFile.exists()) {
Expand All @@ -39,8 +40,13 @@ public static void loadConfigs() {
if (!element.isJsonNull()) {
JsonObject object = element.getAsJsonObject();
for (Entry<String, JsonElement> next : object.entrySet()) {
if (next.getKey().equals("ConfigVersion")) {
// TODO
if (next.getKey().equals("ConfigVersion") && next.getValue().getAsInt() < configVersion) {
logger.warn("配置文件版本过旧!");
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("警告");
alert.setHeaderText(null);
alert.setContentText("配置文件版本过旧! 这可能导致配置加载错误. 由于软件会在退出时保存配置文件, 请考虑在软件退出前备份旧版配置文件.");
alert.showAndWait();
}
for (AbstractConfig config : configs) {
if (config.name.equals(next.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@
import cn.reddragon.eportal.account.Account;
import cn.reddragon.eportal.account.AccountManager;
import cn.reddragon.eportal.config.AbstractConfig;
import cn.reddragon.eportal.window.MainWindow;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class AccountConfig extends AbstractConfig {

public static int index;
public AccountConfig() {
super("Account");
}

@Override
public JsonElement toJson() {
JsonObject o = new JsonObject();
o.addProperty("Selector", MainWindow.controller.accountSelector.getItems().indexOf(MainWindow.controller.accountSelector.getValue()));
JsonArray array = new JsonArray();
for (Account it : AccountManager.accounts) {
JsonObject object = new JsonObject();
object.addProperty("Username", it.getName());
object.addProperty("Password", it.getPassword());
object.addProperty("Username", it.name());
object.addProperty("Password", it.password());
array.add(object);
}
return array;
o.add("Data", array);
return o;
}

@Override
public void fromJson(JsonElement element) {
JsonArray array = element.getAsJsonArray();
JsonObject o = element.getAsJsonObject();
index = o.get("Selector").getAsInt();
JsonArray array = o.get("Data").getAsJsonArray();
for (JsonElement it : array) {
JsonObject object = it.getAsJsonObject();
AccountManager.addAccount(object.get("Username").getAsString(), object.get("Password").getAsString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.reddragon.eportal.config.configs;

import cn.reddragon.eportal.EPortal;
import cn.reddragon.eportal.config.AbstractConfig;
import cn.reddragon.eportal.window.MainWindow;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import javafx.scene.control.CheckMenuItem;
Expand All @@ -12,7 +12,7 @@ public AutoReconnectConfig() {
}

public static boolean getAutoReconnect() {
return ((CheckMenuItem) EPortal.controller.menuBar.getMenus().get(0).getItems().get(0)).isSelected();
return ((CheckMenuItem) MainWindow.controller.menuBar.getMenus().get(0).getItems().get(0)).isSelected();
}

@Override
Expand All @@ -22,6 +22,6 @@ public JsonElement toJson() {

@Override
public void fromJson(JsonElement element) {
((CheckMenuItem) EPortal.controller.menuBar.getMenus().get(0).getItems().get(0)).setSelected(element.getAsBoolean());
((CheckMenuItem) MainWindow.controller.menuBar.getMenus().get(0).getItems().get(0)).setSelected(element.getAsBoolean());
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package cn.reddragon.eportal.config.configs;

import cn.reddragon.eportal.EPortal;
import cn.reddragon.eportal.config.AbstractConfig;
import cn.reddragon.eportal.window.MainWindow;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import javafx.scene.control.ChoiceBox;

public class NetTypeConfig extends AbstractConfig {
private final ChoiceBox<String> selector = EPortal.controller.selector;
public static int index;

public NetTypeConfig() {
super("NetType");
}

@Override
public JsonElement toJson() {
return new JsonPrimitive(EPortal.controller.selector.getItems().indexOf(EPortal.controller.selector.getValue()));
return new JsonPrimitive(MainWindow.controller.typeSelector.getItems().indexOf(MainWindow.controller.typeSelector.getValue()));
}

@Override
public void fromJson(JsonElement element) {
if (element.getAsInt() != -1)
selector.setValue(selector.getItems().get(element.getAsInt()));
else
selector.setValue("");
index = element.getAsInt();
}
}

This file was deleted.

28 changes: 14 additions & 14 deletions src/main/java/cn/reddragon/eportal/utils/Authenticator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.reddragon.eportal.utils;

import cn.reddragon.eportal.EPortal;
import cn.reddragon.eportal.window.MainWindow;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -105,8 +105,8 @@ public static void checkOnline() {
connection.setInstanceFollowRedirects(false);
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_MOVED_TEMP) {
if (EPortal.controller != null) {
Platform.runLater(() -> EPortal.controller.resultText.setText("错误: 与认证服务器通信时出错!"));
if (MainWindow.controller != null) {
Platform.runLater(() -> MainWindow.controller.resultText.setText("错误: 与认证服务器通信时出错!"));
error = true;
}
return;
Expand All @@ -118,21 +118,21 @@ public static void checkOnline() {
online = false;
} else online = redirectLocation.contains(ePortalUrl + "/./success.jsp?");

if (EPortal.controller != null && error) {
Platform.runLater(() -> EPortal.controller.resultText.setText(""));
if (MainWindow.controller != null && error) {
Platform.runLater(() -> MainWindow.controller.resultText.setText(""));
error = false;
}
} catch (SocketTimeoutException e) {
logger.error("无法连接到认证服务器! {}", e.getMessage());
if (EPortal.controller != null) {
Platform.runLater(() -> EPortal.controller.resultText.setText("错误: 无法连接到认证服务器!"));
if (MainWindow.controller != null) {
Platform.runLater(() -> MainWindow.controller.resultText.setText("错误: 无法连接到认证服务器!"));
error = true;
}
online = false;
} catch (SocketException e) {
logger.error("客户端网络错误! {}", e.getMessage());
if (EPortal.controller != null) {
Platform.runLater(() -> EPortal.controller.resultText.setText("错误: 无网络连接!"));
if (MainWindow.controller != null) {
Platform.runLater(() -> MainWindow.controller.resultText.setText("错误: 无网络连接!"));
error = true;
}
online = false;
Expand Down Expand Up @@ -160,7 +160,7 @@ public static void updateSession() {
if (Objects.equals(r, "wait")) {
Thread.sleep(1000);
} else if (Objects.equals(r, "success")) {
EPortal.controller.updateUI();
MainWindow.controller.updateUI();
break;
} else {
break;
Expand All @@ -184,7 +184,7 @@ private static String updateSessionInternal() {
String r = resultJson.get("result").getAsString();
if (Objects.equals(r, "success")) {
// 设置当前用户
Platform.runLater(() -> EPortal.controller.user.setText("当前用户: " + resultJson.get("userName").getAsString() + " (" + resultJson.get("userId").getAsString() + ")"));
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("我的运营商")) {
Expand All @@ -193,7 +193,7 @@ private static String updateSessionInternal() {
Authenticator.type = it;
}
}
Platform.runLater(() -> EPortal.controller.remainLabel.setText("剩余时长: ∞"));
Platform.runLater(() -> MainWindow.controller.remainLabel.setText("剩余时长: ∞"));
return r;
}
Authenticator.type = LoginType.WAN;
Expand All @@ -220,10 +220,10 @@ private static String updateSessionInternal() {
sb.append(s).append("s");
}
}
Platform.runLater(() -> EPortal.controller.remainLabel.setText(sb.toString()));
Platform.runLater(() -> MainWindow.controller.remainLabel.setText(sb.toString()));
} else if (!Objects.equals(r, "wait")) {
//Authenticator.type = LoginType.OFFLINE;
Platform.runLater(() -> EPortal.controller.resultText.setText(resultJson.get("message").getAsString()));
Platform.runLater(() -> MainWindow.controller.resultText.setText(resultJson.get("message").getAsString()));
}
return r;
} catch (IOException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/reddragon/eportal/utils/IOUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.reddragon.eportal.utils;

import cn.reddragon.eportal.EPortal;
import cn.reddragon.eportal.Main;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -22,7 +22,7 @@ public static String readText(InputStream stream) {
}
return sb.toString();*/
} catch (IOException e) {
EPortal.logger.error("读入数据时出错!", e);
Main.logger.error("读入数据时出错!", e);
return "";
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/cn/reddragon/eportal/window/AccountWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.reddragon.eportal.window;

import cn.reddragon.eportal.account.Account;
import cn.reddragon.eportal.account.AccountManager;
import cn.reddragon.eportal.window.controllers.AccountController;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;

public class AccountWindow {
public static AccountController controller;
private static Stage stage;

public static Stage getStage() {
return stage;
}

public static void init(Stage parent) throws IOException {
FXMLLoader loader = new FXMLLoader(AccountWindow.class.getResource("account-view.fxml"));
Parent root = loader.load();
controller = loader.getController();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setTitle("账号管理");
stage.setScene(scene);
stage.initOwner(parent);
stage.initModality(Modality.WINDOW_MODAL);
ListView<String> listView = controller.listView;
listView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
controller.onSelectionChanged(newValue);
});
ObservableList<String> items = listView.getItems();
for (Account account : AccountManager.accounts) {
items.add(account.name());
}
AccountWindow.stage = stage;
}

public static void open() {
stage.show();
}
}
Loading

0 comments on commit bbd08f3

Please sign in to comment.