refactor: 使用FileManager统一文件操作
将直接的文件操作(file_put_contents/file_get_contents/file_exists/filemtime)替换为FileManager的封装方法,提高代码可维护性和一致性
This commit is contained in:
21
api.php
21
api.php
@@ -101,7 +101,7 @@ class FundMonitorAPI extends ApiBase {
|
||||
'to_emails' => []
|
||||
];
|
||||
|
||||
file_put_contents($this->emailConfigFile, json_encode($defaultEmailConfig, JSON_PRETTY_PRINT));
|
||||
FileManager::saveJsonFile($this->emailConfigFile, $defaultEmailConfig);
|
||||
$this->emailConfig = $defaultEmailConfig;
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ class FundMonitorAPI extends ApiBase {
|
||||
'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' => []
|
||||
];
|
||||
|
||||
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,
|
||||
'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) {
|
||||
$file = $this->getFundCachePath($fundCode);
|
||||
if (!file_exists($file)) return false;
|
||||
if (!FileManager::fileExists($file)) return false;
|
||||
// 过期判断
|
||||
if (time() - filemtime($file) > $this->fundCacheTTL) return false;
|
||||
$raw = @file_get_contents($file);
|
||||
if ($raw === false) return false;
|
||||
$data = json_decode($raw, true);
|
||||
return is_array($data) ? $data : false;
|
||||
if (time() - FileManager::getFileMTime($file) > $this->fundCacheTTL) return false;
|
||||
return FileManager::loadJsonFile($file, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -597,7 +594,7 @@ class FundMonitorAPI extends ApiBase {
|
||||
private function cacheFundData($fundCode, $fundData) {
|
||||
// 保证目录存在
|
||||
FileManager::ensureDirExists($this->cacheDir);
|
||||
@file_put_contents($this->getFundCachePath($fundCode), json_encode($fundData, JSON_UNESCAPED_UNICODE));
|
||||
FileManager::saveJsonFile($this->getFundCachePath($fundCode), $fundData);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user