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

36
api.php
View File

@@ -41,32 +41,32 @@ class FundMonitorAPI extends ApiBase {
FileManager::ensureDirExists('data');
// 初始化统计文件(如果不存在)
if (!file_exists($this->statsFile)) {
if (!FileManager::fileExists($this->statsFile)) {
$this->initStats();
}
// 初始化配置文件(如果不存在)
if (!file_exists($this->configFile)) {
if (!FileManager::fileExists($this->configFile)) {
$this->initConfig();
}
// 初始化历史数据文件(如果不存在)
if (!file_exists($this->historyFile)) {
if (!FileManager::fileExists($this->historyFile)) {
$this->initHistory();
}
// 初始化操作日志文件(如果不存在)
if (!file_exists($this->operationFile)) {
if (!FileManager::fileExists($this->operationFile)) {
$this->initOperationLog();
}
// 初始化每日数据文件(如果不存在)
if (!file_exists($this->dailyDataFile)) {
if (!FileManager::fileExists($this->dailyDataFile)) {
$this->initDailyData();
}
// 初始化邮件状态文件(如果不存在)
if (!file_exists($this->emailStatusFile)) {
if (!FileManager::fileExists($this->emailStatusFile)) {
$this->initEmailStatus();
}
@@ -78,8 +78,8 @@ class FundMonitorAPI extends ApiBase {
* 加载邮箱配置
*/
private function loadEmailConfig() {
if (file_exists($this->emailConfigFile)) {
$this->emailConfig = json_decode(file_get_contents($this->emailConfigFile), true);
if (FileManager::fileExists($this->emailConfigFile)) {
$this->emailConfig = FileManager::loadJsonFile($this->emailConfigFile);
} else {
// 如果配置文件不存在,使用默认配置
$this->initEmailConfig();
@@ -335,7 +335,7 @@ class FundMonitorAPI extends ApiBase {
} else {
$status['failed_count']++;
// 保存错误日志
file_put_contents('email_error_log.txt', date('Y-m-d H:i:s') . "\n" . $errorLog . "\n\n", FILE_APPEND);
FileManager::appendToFile('email_error_log.txt', date('Y-m-d H:i:s') . "\n" . $errorLog . "\n\n");
}
$this->saveEmailStatus($status);
@@ -609,12 +609,10 @@ class FundMonitorAPI extends ApiBase {
*/
private function getFromBatchCache($key) {
$file = $this->getBatchCachePath($key);
if (!file_exists($file)) return false;
if (!FileManager::fileExists($file)) return false;
// 过期判断
if (time() - filemtime($file) > $this->batchCacheTTL) return false;
$raw = @file_get_contents($file);
if ($raw === false) return false;
$data = json_decode($raw, true);
if (time() - FileManager::getFileMTime($file) > $this->batchCacheTTL) return false;
$data = FileManager::loadJsonFile($file);
return is_array($data) ? $data : false;
}
@@ -624,7 +622,7 @@ class FundMonitorAPI extends ApiBase {
private function cacheBatchData($key, $data) {
// 保证目录存在
FileManager::ensureDirExists($this->cacheDir);
@file_put_contents($this->getBatchCachePath($key), json_encode($data, JSON_UNESCAPED_UNICODE));
FileManager::saveJsonFile($this->getBatchCachePath($key), $data, JSON_UNESCAPED_UNICODE);
}
@@ -640,7 +638,7 @@ class FundMonitorAPI extends ApiBase {
* 保存访问记录
*/
private function saveVisits($visits) {
return file_put_contents($this->dataFile, json_encode($visits));
return FileManager::saveJsonFile($this->dataFile, $visits);
}
/**
@@ -674,7 +672,7 @@ class FundMonitorAPI extends ApiBase {
$visits = array_slice($visits, -1000);
}
file_put_contents($this->dataFile, json_encode($visits, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
FileManager::saveJsonFile($this->dataFile, $visits, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// 更新统计
$this->updateStats($clientIP);
@@ -789,7 +787,7 @@ class FundMonitorAPI extends ApiBase {
// 保存唯一IP列表用于快速计算
$stats['unique_ips'] = array_values($uniqueIPs);
file_put_contents($this->statsFile, json_encode($stats, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
FileManager::saveJsonFile($this->statsFile, $stats, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return $stats;
}
@@ -874,7 +872,7 @@ class FundMonitorAPI extends ApiBase {
]
]);
$response = @file_get_contents($apiUrl, false, $context);
$response = @FileManager::loadFile($apiUrl);
if (!$response) {
return false;
}