refactor: 使用FileManager统一文件操作

将直接的文件操作(file_put_contents/file_get_contents/file_exists/filemtime)替换为FileManager的封装方法,提高代码可维护性和一致性
This commit is contained in:
LL
2025-12-12 14:15:30 +08:00
parent 0cfefbebd8
commit fbf39f81c2

21
api.php
View File

@@ -101,7 +101,7 @@ class FundMonitorAPI extends ApiBase {
'to_emails' => [] 'to_emails' => []
]; ];
file_put_contents($this->emailConfigFile, json_encode($defaultEmailConfig, JSON_PRETTY_PRINT)); FileManager::saveJsonFile($this->emailConfigFile, $defaultEmailConfig);
$this->emailConfig = $defaultEmailConfig; $this->emailConfig = $defaultEmailConfig;
} }
@@ -119,7 +119,7 @@ class FundMonitorAPI extends ApiBase {
'unique_ips' => [] 'unique_ips' => []
]; ];
file_put_contents($this->statsFile, json_encode($initialStats, JSON_PRETTY_PRINT)); FileManager::saveJsonFile($this->statsFile, $initialStats);
} }
/** /**
@@ -159,7 +159,7 @@ class FundMonitorAPI extends ApiBase {
] ]
]; ];
file_put_contents($this->configFile, json_encode($defaultConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); FileManager::saveJsonFile($this->configFile, $defaultConfig);
} }
/** /**
@@ -172,7 +172,7 @@ class FundMonitorAPI extends ApiBase {
'today_data' => [] 'today_data' => []
]; ];
file_put_contents($this->historyFile, json_encode($initialHistory, JSON_PRETTY_PRINT)); FileManager::saveJsonFile($this->historyFile, $initialHistory);
} }
/** /**
@@ -208,7 +208,7 @@ class FundMonitorAPI extends ApiBase {
'sent_count' => 0, 'sent_count' => 0,
'failed_count' => 0 'failed_count' => 0
]; ];
file_put_contents($this->emailStatusFile, json_encode($initialStatus, JSON_PRETTY_PRINT)); FileManager::saveJsonFile($this->emailStatusFile, $initialStatus);
} }
/** /**
@@ -582,13 +582,10 @@ class FundMonitorAPI extends ApiBase {
*/ */
private function getFundFromCache($fundCode) { private function getFundFromCache($fundCode) {
$file = $this->getFundCachePath($fundCode); $file = $this->getFundCachePath($fundCode);
if (!file_exists($file)) return false; if (!FileManager::fileExists($file)) return false;
// 过期判断 // 过期判断
if (time() - filemtime($file) > $this->fundCacheTTL) return false; if (time() - FileManager::getFileMTime($file) > $this->fundCacheTTL) return false;
$raw = @file_get_contents($file); return FileManager::loadJsonFile($file, false);
if ($raw === false) return false;
$data = json_decode($raw, true);
return is_array($data) ? $data : false;
} }
/** /**
@@ -597,7 +594,7 @@ class FundMonitorAPI extends ApiBase {
private function cacheFundData($fundCode, $fundData) { private function cacheFundData($fundCode, $fundData) {
// 保证目录存在 // 保证目录存在
FileManager::ensureDirExists($this->cacheDir); FileManager::ensureDirExists($this->cacheDir);
@file_put_contents($this->getFundCachePath($fundCode), json_encode($fundData, JSON_UNESCAPED_UNICODE)); FileManager::saveJsonFile($this->getFundCachePath($fundCode), $fundData);
} }
/** /**