0) { $file = DATA_DIR . '/recommended_funds.json'; FileManager::saveJsonFile($file, $fundsToKeep, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); // 记录清理日志 logAction('clean_nonexistent_funds', ['funds_removed' => $fundsRemoved]); } return $fundsRemoved; } /** * 记录操作日志 * @param string $action 操作类型 * @param array $data 操作数据 */ function logAction($action, $data = []) { $logFile = DATA_DIR . '/recommend_logs.json'; $logs = FileManager::loadJsonFile($logFile, []); $logs[] = array_merge([ 'action' => $action, 'timestamp' => date('Y-m-d H:i:s') ], $data); FileManager::saveJsonFile($logFile, $logs, JSON_PRETTY_PRINT); } /** * 获取基金缓存文件路径 * @param string $fundCode 基金代码 * @return string 缓存文件路径 */ function getFundCachePath($fundCode) { return CACHE_DIR . "/fund_{$fundCode}.json"; } /** * 从缓存获取基金数据 * @param string $fundCode 基金代码 * @return array|false 基金数据或false */ function getFundFromCache($fundCode) { $cacheFile = getFundCachePath($fundCode); if (!FileManager::fileExists($cacheFile)) { return false; } // 检查缓存是否过期 if (time() - FileManager::getFileMTime($cacheFile) > FUND_CACHE_TTL) { return false; } $fundData = FileManager::loadJsonFile($cacheFile); return is_array($fundData) ? $fundData : false; } /** * 缓存基金数据 * @param string $fundCode 基金代码 * @param array $fundData 基金数据 */ function cacheFundData($fundCode, $fundData) { $cacheFile = getFundCachePath($fundCode); FileManager::saveJsonFile($cacheFile, $fundData, JSON_UNESCAPED_UNICODE); } /** * 从API获取基金信息 * @param string $fundCode 基金代码 * @return array|false 基金数据或false */ function fetchFundDataFromAPI($fundCode) { $apiUrl = "http://fundgz.1234567.com.cn/js/{$fundCode}.js?" . time(); $context = stream_context_create([ 'http' => [ 'timeout' => 5, 'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\r\n", 'ignore_errors' => true ] ]); $response = @FileManager::loadFile($apiUrl); if (!$response) { return false; } // 解析返回的数据 if (preg_match('/jsonpgz\d*\((.*)\);?/', $response, $matches)) { $data = json_decode($matches[1], true); // 检查数据是否有效 if (is_array($data) && isset($data['fundcode']) && $data['fundcode'] == $fundCode) { return $data; } } return false; } /** * 检查基金是否存在 * @param string $fundCode 基金代码 * @return bool 基金是否存在 */ function checkFundExists($fundCode) { // 基金代码格式验证 if (!preg_match('/^\d{6}$/', $fundCode)) { return false; } // 尝试从缓存获取 $cachedData = getFundFromCache($fundCode); if ($cachedData) { return true; } // 从API获取 $fundData = fetchFundDataFromAPI($fundCode); if ($fundData) { // 缓存基金数据 cacheFundData($fundCode, $fundData); return true; } return false; } /** * 获取基金详细信息 * @param string $fundCode 基金代码 * @return array|false 基金详细数据或false */ function getFundInfo($fundCode) { // 先尝试从缓存获取 $cachedData = getFundFromCache($fundCode); if ($cachedData) { return $cachedData; } // 从API获取 $fundData = fetchFundDataFromAPI($fundCode); if ($fundData) { // 缓存基金数据 cacheFundData($fundCode, $fundData); return $fundData; } return false; } /** * 保存推荐基金 * @param string $fundCode 基金代码 * @param string $ip 用户IP * @return array 操作结果 */ function saveRecommendedFund($fundCode, $ip) { // 首先检查基金号是否存在 if (!checkFundExists($fundCode)) { return ['success' => false, 'message' => '基金号不存在,请输入正确的6位基金代码!']; } // 检查数量限制 $funds = loadRecommendedFunds(); $timestamp = date('Y-m-d H:i:s'); // 检查基金总数是否已达到上限 if (count($funds) >= MAX_TOTAL_FUNDS) { return ['success' => false, 'message' => "推荐基金数量已达到上限({MAX_TOTAL_FUNDS}支)"]; } // 检查IP推荐次数和基金是否已被推荐 $ipCount = 0; foreach ($funds as $fund) { if ($fund['ip'] == $ip) { $ipCount++; } if ($fund['fund_code'] == $fundCode) { return ['success' => false, 'message' => '该基金已被推荐过']; } } if ($ipCount >= MAX_FUNDS_PER_IP) { return ['success' => false, 'message' => "每个IP最多只能推荐{MAX_FUNDS_PER_IP}只基金"]; } // 获取基金信息 $fundInfo = getFundInfo($fundCode); $fundName = $fundInfo ? ($fundInfo['name'] ?? '未知基金') : '未知基金'; // 添加新基金 $newFund = [ 'fund_code' => $fundCode, 'ip' => $ip, 'timestamp' => $timestamp, 'channel' => '网站推荐', 'amount' => 1000, 'status' => 'pending', // 默认设为待审核,需要管理员批准 'fund_name' => $fundName // 保存基金名称 ]; $funds[] = $newFund; // 保存到文件 $file = DATA_DIR . '/recommended_funds.json'; FileManager::saveJsonFile($file, $funds, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); // 记录操作日志 logAction('recommend_fund', [ 'fund_code' => $fundCode, 'fund_name' => $fundName, 'ip' => $ip ]); return ['success' => true, 'message' => '基金推荐成功!']; } // 处理推荐请求 // 从session中获取消息(如果有) $message = isset($_SESSION['message']) ? $_SESSION['message'] : ''; $messageType = isset($_SESSION['messageType']) ? $_SESSION['messageType'] : ''; // 清除session中的消息,防止刷新页面再次显示 unset($_SESSION['message'], $_SESSION['messageType']); // 定期清理不存在的基金(每10分钟执行一次) $cleanupFile = DATA_DIR . '/last_cleanup.txt'; $lastCleanup = @FileManager::loadFile($cleanupFile); $currentTime = time(); // 如果距离上次清理超过10分钟,执行清理 if (!$lastCleanup || ($currentTime - $lastCleanup) > 600) { $removedCount = cleanNonExistentFunds(); FileManager::saveFile($cleanupFile, $currentTime); // 如果有基金被清理,记录日志但不显示给用户 if ($removedCount > 0) { error_log("清理了{$removedCount}个不存在的基金"); } } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fund_code'])) { $fundCode = trim($_POST['fund_code']); $ip = getUserIP(); // 简单验证基金代码格式(假设为6位数字) if (!preg_match('/^\d{6}$/', $fundCode)) { $_SESSION['message'] = '基金代码格式不正确,请输入6位数字'; $_SESSION['messageType'] = 'error'; } else { $result = saveRecommendedFund($fundCode, $ip); $_SESSION['message'] = $result['message']; $_SESSION['messageType'] = $result['success'] ? 'success' : 'error'; } // 重定向到同一页面,实现POST-REDIRECT-GET模式 header('Location: ' . $_SERVER['PHP_SELF']); exit; } // 获取IP推荐次数 $userIp = getUserIP(); $recommendedFunds = loadRecommendedFunds(); $userRecommendCount = 0; foreach ($recommendedFunds as $fund) { if (isset($fund['ip']) && $fund['ip'] == $userIp) { $userRecommendCount++; } } $totalFunds = count($recommendedFunds); // 预加载部分基金数据到页面中,提高初始加载速度 $preloadedFundData = []; $preloadLimit = 10; // 预加载10个基金的数据 $preloadCount = 0; foreach ($recommendedFunds as $fund) { if ($preloadCount >= $preloadLimit) break; $fundCode = $fund['fund_code'] ?? ''; if (!empty($fundCode)) { $fundInfo = getFundInfo($fundCode); if ($fundInfo) { $preloadedFundData[$fundCode] = $fundInfo; $preloadCount++; } } } ?>
推荐您关注的优质基金
| 基金代码 | 推荐时间 | 基金名称 | 当前涨幅 |
|---|---|---|---|
| 加载中... | -- |