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

@@ -5,6 +5,8 @@
*/
// 设置响应头
header('Content-Type: text/html; charset=utf-8');
// 引入工具类
require_once __DIR__ . '/utils/FileManager.php';
// 设置时区
date_default_timezone_set('Asia/Shanghai');
@@ -21,12 +23,8 @@ const MAX_TOTAL_FUNDS = 3000; // 系统最大基金数量
// 确保目录存在
function ensureDirsExist() {
if (!file_exists(DATA_DIR)) {
mkdir(DATA_DIR, 0755, true);
}
if (!file_exists(CACHE_DIR)) {
mkdir(CACHE_DIR, 0755, true);
}
FileManager::ensureDirExists(DATA_DIR);
FileManager::ensureDirExists(CACHE_DIR);
}
ensureDirsExist();
@@ -54,11 +52,7 @@ function getUserIP() {
*/
function loadRecommendedFunds() {
$file = DATA_DIR . '/recommended_funds.json';
if (file_exists($file)) {
$data = file_get_contents($file);
return json_decode($data, true) ?? [];
}
return [];
return FileManager::loadJsonFile($file, []);
}
/**
@@ -81,7 +75,7 @@ function cleanNonExistentFunds() {
// 如果有基金被移除,保存更新后的列表
if ($fundsRemoved > 0) {
$file = DATA_DIR . '/recommended_funds.json';
file_put_contents($file, json_encode($fundsToKeep, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
FileManager::saveJsonFile($file, $fundsToKeep, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// 记录清理日志
logAction('clean_nonexistent_funds', ['funds_removed' => $fundsRemoved]);
@@ -97,17 +91,14 @@ function cleanNonExistentFunds() {
*/
function logAction($action, $data = []) {
$logFile = DATA_DIR . '/recommend_logs.json';
$logs = [];
if (file_exists($logFile)) {
$logs = json_decode(file_get_contents($logFile), true) ?? [];
}
$logs = FileManager::loadJsonFile($logFile, []);
$logs[] = array_merge([
'action' => $action,
'timestamp' => date('Y-m-d H:i:s')
], $data);
file_put_contents($logFile, json_encode($logs, JSON_PRETTY_PRINT));
FileManager::saveJsonFile($logFile, $logs, JSON_PRETTY_PRINT);
}
/**
@@ -126,21 +117,16 @@ function getFundCachePath($fundCode) {
*/
function getFundFromCache($fundCode) {
$cacheFile = getFundCachePath($fundCode);
if (!file_exists($cacheFile)) {
if (!FileManager::fileExists($cacheFile)) {
return false;
}
// 检查缓存是否过期
if (time() - filemtime($cacheFile) > FUND_CACHE_TTL) {
if (time() - FileManager::getFileMTime($cacheFile) > FUND_CACHE_TTL) {
return false;
}
$data = file_get_contents($cacheFile);
if ($data === false) {
return false;
}
$fundData = json_decode($data, true);
$fundData = FileManager::loadJsonFile($cacheFile);
return is_array($fundData) ? $fundData : false;
}
@@ -151,7 +137,7 @@ function getFundFromCache($fundCode) {
*/
function cacheFundData($fundCode, $fundData) {
$cacheFile = getFundCachePath($fundCode);
file_put_contents($cacheFile, json_encode($fundData, JSON_UNESCAPED_UNICODE));
FileManager::saveJsonFile($cacheFile, $fundData, JSON_UNESCAPED_UNICODE);
}
/**
@@ -169,7 +155,7 @@ function fetchFundDataFromAPI($fundCode) {
]
]);
$response = @file_get_contents($apiUrl, false, $context);
$response = @FileManager::loadFile($apiUrl);
if (!$response) {
return false;
@@ -294,7 +280,7 @@ function saveRecommendedFund($fundCode, $ip) {
// 保存到文件
$file = DATA_DIR . '/recommended_funds.json';
file_put_contents($file, json_encode($funds, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
FileManager::saveJsonFile($file, $funds, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// 记录操作日志
logAction('recommend_fund', [
@@ -316,13 +302,13 @@ unset($_SESSION['message'], $_SESSION['messageType']);
// 定期清理不存在的基金每10分钟执行一次
$cleanupFile = DATA_DIR . '/last_cleanup.txt';
$lastCleanup = @file_get_contents($cleanupFile);
$lastCleanup = @FileManager::loadFile($cleanupFile);
$currentTime = time();
// 如果距离上次清理超过10分钟执行清理
if (!$lastCleanup || ($currentTime - $lastCleanup) > 600) {
$removedCount = cleanNonExistentFunds();
file_put_contents($cleanupFile, $currentTime);
FileManager::saveFile($cleanupFile, $currentTime);
// 如果有基金被清理,记录日志但不显示给用户
if ($removedCount > 0) {