refactor: 使用FileManager统一文件操作

将分散在各处的文件操作统一封装到FileManager工具类中,包括文件读写、目录创建、JSON处理等操作
This commit is contained in:
LL
2025-12-12 14:22:40 +08:00
parent fbf39f81c2
commit 6c168246ca
6 changed files with 81 additions and 78 deletions

View File

@@ -1,5 +1,6 @@
<?php
// 基金监控系统登录页面
require_once 'utils/FileManager.php';
// 初始化错误信息
$error = '';
@@ -41,19 +42,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
function saveLog($log_entry) {
$log_file = 'data/operation_log.json';
// 检查目录是否存在,不存在则创建
if (!file_exists(dirname($log_file))) {
mkdir(dirname($log_file), 0777, true);
}
// 确保目录存在
FileManager::ensureDirExists(dirname($log_file));
// 读取现有日志
$logs = [];
if (file_exists($log_file)) {
$content = file_get_contents($log_file);
if (!empty($content)) {
$logs = json_decode($content, true);
}
}
$logs = FileManager::loadJsonFile($log_file, []);
// 添加新日志
array_unshift($logs, $log_entry);
@@ -64,7 +57,7 @@ function saveLog($log_entry) {
}
// 保存日志
file_put_contents($log_file, json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
FileManager::saveJsonFile($log_file, $logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
?>
<!DOCTYPE html>