Skip to content

Commit 6f9a5a8

Browse files
committed
更改账户保存文件格式与解析格式,查询数量和查询区间可保存设置
Signed-off-by: 10cks <1195011908@qq.com>
1 parent c31ca11 commit 6f9a5a8

1 file changed

Lines changed: 129 additions & 39 deletions

File tree

src/main/java/Main.java

Lines changed: 129 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import com.google.gson.Gson;
2020
import com.google.gson.GsonBuilder;
21+
import com.google.gson.JsonElement;
22+
import com.google.gson.JsonObject;
23+
import com.google.gson.reflect.TypeToken;
2124
import org.apache.commons.text.StringEscapeUtils;
2225
import org.apache.http.HttpEntity;
2326
import org.apache.http.HttpResponse;
@@ -166,7 +169,7 @@ public static void main(String[] args) throws ClassNotFoundException, Unsupporte
166169
JMenu configureMenu = new JMenu("查询设置");
167170
JMenuItem configureMenuItem = new JMenuItem("默认查询数量");
168171
JMenuItem configureFullItem = new JMenuItem("默认查询区间");
169-
JCheckBox defaultCheckFullBox = new JCheckBox("是否默认查询近一年数据?", false);
172+
JCheckBox defaultCheckFullBox = new JCheckBox("是否默认仅查询近一年数据?", false);
170173
defaultCheckFullBox.setFocusPainted(false);
171174
configureMenu.add(configureMenuItem);
172175
configureMenu.add(configureFullItem);
@@ -381,10 +384,9 @@ public void keyPressed(KeyEvent e) {
381384
File rulesFile = new File(rulesPath);
382385
if (!rulesFile.exists()) {
383386
rulesFile.createNewFile();
384-
System.out.println("[+] The current path does not contain rules.txt. Create rules.txt.");
387+
System.out.println("[+] The current path does not contain " + rulesPath + ". Create "+ rulesPath +".");
385388
}
386389
rulesReader = new BufferedReader(new FileReader(rulesFile));
387-
388390
// 创建 accounts.txt 文件如果它不存在
389391
accountsFile = new File(accountsPath);
390392
if (!accountsFile.exists()) {
@@ -798,28 +800,30 @@ public void actionPerformed(ActionEvent e) {
798800
saveSettingsButton.setFocusable(false);
799801

800802
saveSettingsButton.addActionListener(SaveError -> {
801-
// 获取文本框中的值,并保存到我们刚才创建的Settings对象中
802-
AccountSetting settings = new AccountSetting();
803-
settings.setFofaEmail(fofaEmail.getText());
804-
settings.setFofaKey(fofaKey.getText());
805-
806-
// 创建一个Gson对象,用于序列化
807-
Gson gson = new GsonBuilder().setPrettyPrinting().create();
808-
// 将Settings对象转化为JSON字符串
809-
String contentToWrite = gson.toJson(settings);
810-
811-
File rulesFile = new File(accountsPath);
812-
try {
813-
// 如果文件不存在,则创建新文件
814-
if (!rulesFile.exists()) {
815-
rulesFile.createNewFile();
803+
// 准备一个 JsonObject
804+
JsonObject jsonObject = new JsonObject();
805+
File jsonFile = new File(accountsPath);
806+
807+
// 如果文件存在且其内容不为空,从中读取 JsonObject
808+
if (jsonFile.exists() && jsonFile.length() != 0) {
809+
try (FileReader reader = new FileReader(jsonFile)) {
810+
Gson gson = new Gson();
811+
jsonObject = gson.fromJson(reader, JsonObject.class);
812+
} catch (Exception ex) {
813+
// parse error handling
814+
ex.printStackTrace();
816815
}
816+
}
817817

818-
// 写入内容到文件,使用 try-with-resources 自动关闭 FileWriter
819-
try (FileWriter writer = new FileWriter(rulesFile, false)) {
820-
writer.write(contentToWrite);
821-
JOptionPane.showMessageDialog(null, "设置已保存。");
822-
}
818+
// 更新邮件和密钥设置
819+
jsonObject.addProperty("fofaEmail", fofaEmail.getText());
820+
jsonObject.addProperty("fofaKey", fofaKey.getText());
821+
822+
// 将已更新的 jsonObject 写回到 "accounts.json" 文件
823+
try (FileWriter writer = new FileWriter(accountsPath)) {
824+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
825+
gson.toJson(jsonObject, writer);
826+
JOptionPane.showMessageDialog(null, "设置已保存。");
823827
} catch (IOException ex) {
824828
ex.printStackTrace();
825829
JOptionPane.showMessageDialog(null, "保存设置时发生错误!", "错误", JOptionPane.ERROR_MESSAGE);
@@ -849,31 +853,110 @@ public void actionPerformed(ActionEvent e) {
849853
configureMenuItem.addActionListener(new ActionListener() {
850854
@Override
851855
public void actionPerformed(ActionEvent e) {
852-
// 创建一个JTextField 初始化为 sizeSetting 的值
853856
JTextField inputField = new JTextField(String.valueOf(sizeSetting));
854-
int result = JOptionPane.showConfirmDialog(null, inputField,
855-
"请输入默认查询数量", JOptionPane.OK_CANCEL_OPTION);
856-
if (result == JOptionPane.OK_OPTION) {
857+
JButton okButton = new JButton("保存");
858+
JButton cancelButton = new JButton("取消");
859+
JOptionPane optionPane = new JOptionPane(inputField,
860+
JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION);
861+
optionPane.setOptions(new Object[] { okButton, cancelButton });
862+
863+
JDialog dialog = optionPane.createDialog("请输入默认查询数量");
864+
okButton.addActionListener(ev -> {
857865
try {
858-
// 尝试将输入的文本解析为整数并更新 sizeSetting
859866
sizeSetting = Integer.parseInt(inputField.getText());
867+
868+
// 读取JSON文件
869+
JsonObject jsonObject;
870+
File jsonFile = new File(accountsPath);
871+
if (jsonFile.exists() && jsonFile.length() != 0) {
872+
try (FileReader reader = new FileReader(jsonFile)) {
873+
Gson gson = new Gson();
874+
jsonObject = gson.fromJson(reader, JsonObject.class);
875+
} catch (Exception ex) {
876+
ex.printStackTrace();
877+
return;
878+
}
879+
} else {
880+
jsonObject = new JsonObject();
881+
}
882+
// 更新queryNumber并重新写入文件
883+
jsonObject.addProperty("queryNumber", sizeSetting);
884+
try (FileWriter writer = new FileWriter(accountsPath)) {
885+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
886+
gson.toJson(jsonObject, writer);
887+
}
888+
889+
// 显示保存成功提醒
890+
JOptionPane.showMessageDialog(null, "保存成功!");
891+
// 关闭原始对话框
892+
// dialog.dispose();
860893
} catch (NumberFormatException ex) {
861-
// 输入的不是有效的整数,可以在这里处理错误
862-
JOptionPane.showMessageDialog(null, "请输入一个有效的整数值");
894+
JOptionPane.showMessageDialog(null, "请输入一个有效的整数值。");
895+
} catch (IOException ex) {
896+
// 处理文件读写异常
897+
JOptionPane.showMessageDialog(null, "发生错误,账户配置文件不存在。");
863898
}
864-
}
899+
});
900+
901+
cancelButton.addActionListener(ev -> dialog.dispose());
902+
903+
dialog.setVisible(true);
865904
}
866905
});
867906

868907
configureFullItem.addActionListener(new ActionListener() {
869908
@Override
870909
public void actionPerformed(ActionEvent e) {
871-
int result = JOptionPane.showConfirmDialog(null, defaultCheckFullBox,
872-
"否默认查询", JOptionPane.OK_CANCEL_OPTION);
873-
if (result == JOptionPane.OK_OPTION) {
874-
// 如果复选框选中,则设置 setFull 为 false,否则为 true
875-
setFull = !defaultCheckFullBox.isSelected();
910+
JsonObject jsonObject;
911+
File jsonFile = new File("accounts.json");
912+
913+
// 如果文件存在且其内容不为空,从中读取 JsonObject
914+
if (jsonFile.exists() && jsonFile.length() != 0) {
915+
try (FileReader reader = new FileReader(jsonFile)) {
916+
Gson gson = new Gson();
917+
jsonObject = gson.fromJson(reader, JsonObject.class);
918+
} catch (Exception ex) {
919+
// 解析错误处理
920+
ex.printStackTrace();
921+
return;
922+
}
923+
} else {
924+
jsonObject = new JsonObject();
925+
}
926+
927+
// 读取 queryFull 的值,并设置复选框的选择状态
928+
if (jsonObject.has("queryFull")) {
929+
defaultCheckFullBox.setSelected(!jsonObject.get("queryFull").getAsBoolean());
876930
}
931+
932+
JButton okButton = new JButton("保存");
933+
JButton cancelButton = new JButton("取消");
934+
JOptionPane optionPane = new JOptionPane(defaultCheckFullBox,
935+
JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION);
936+
optionPane.setOptions(new Object[]{okButton, cancelButton});
937+
938+
JDialog dialog = optionPane.createDialog("是否默认查询区间");
939+
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
940+
941+
okButton.addActionListener(ev -> {
942+
// 如果复选框选中,则设置 queryFull 为 false,否则为 true
943+
boolean queryFull = !defaultCheckFullBox.isSelected();
944+
jsonObject.addProperty("queryFull", queryFull);
945+
946+
// 将 jsonObject 保存到 "accounts.json" 文件中
947+
try (FileWriter writer = new FileWriter(jsonFile)) {
948+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
949+
gson.toJson(jsonObject, writer);
950+
JOptionPane.showMessageDialog(null, "保存成功!");
951+
} catch (IOException ex) {
952+
ex.printStackTrace();
953+
JOptionPane.showMessageDialog(null, "发生错误,保存设置失败。");
954+
}
955+
});
956+
957+
cancelButton.addActionListener(ev -> dialog.dispose());
958+
959+
dialog.setVisible(true);
877960
}
878961
});
879962
// 实验功能事件
@@ -1933,11 +2016,18 @@ static public void settingInit(BufferedReader rules, File accountsFile, JPanel i
19332016
if (accountsFile.exists() && accountsFile.length() != 0) {
19342017
BufferedReader br = new BufferedReader(new FileReader(accountsFile));
19352018
//转换为哈希映射类型
1936-
Map<String, String> accounts = gson.fromJson(br, Map.class);
2019+
Map<String, JsonElement> accounts = gson.fromJson(br, new TypeToken<Map<String, JsonElement>>(){}.getType());
19372020
// 从哈希映射中获取数据并设置
19382021
if (accounts.get("fofaEmail") != null && accounts.get("fofaKey") != null) {
1939-
fofaEmail.setText(accounts.get("fofaEmail"));
1940-
fofaKey.setText(accounts.get("fofaKey"));
2022+
fofaEmail.setText(accounts.get("fofaEmail").getAsString());
2023+
fofaKey.setText(accounts.get("fofaKey").getAsString());
2024+
}
2025+
if (accounts.get("queryNumber") != null) {
2026+
sizeSetting = accounts.get("queryNumber").getAsInt();
2027+
}
2028+
if(accounts.get("queryFull") != null) {
2029+
setFull = accounts.get("queryFull").getAsBoolean();
2030+
System.out.println(setFull);
19412031
}
19422032
}
19432033
} catch (IOException e) {

0 commit comments

Comments
 (0)