feat: 添加基金监控系统基础功能
添加基金监控系统相关文件,包括邮件发送功能、基金数据配置、测试脚本等。主要包含以下内容: 1. 添加PHPMailer库及相关语言文件 2. 添加基金配置数据文件(fund_config.json, fund_names.json等) 3. 添加邮件发送测试脚本(test_email.php, test_fund_email.php等) 4. 添加.gitignore文件忽略不必要的文件 5. 添加composer.json配置依赖 Signed-off-by: LL <LL>
This commit is contained in:
144
utils/ApiBase.php
Normal file
144
utils/ApiBase.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
/**
|
||||
* API基类
|
||||
* 提供API请求处理的通用功能,减少代码重复
|
||||
*/
|
||||
class ApiBase {
|
||||
|
||||
/**
|
||||
* 解析请求参数
|
||||
* @return array 包含action和其他参数的数组
|
||||
*/
|
||||
protected function parseRequest() {
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
$params = [];
|
||||
$action = '';
|
||||
|
||||
// 解析GET参数
|
||||
$queryString = $_SERVER['QUERY_STRING'] ?? '';
|
||||
parse_str($queryString, $params);
|
||||
$action = $params['action'] ?? '';
|
||||
|
||||
// 解析POST数据
|
||||
if ($method === 'POST') {
|
||||
$input = file_get_contents('php://input');
|
||||
$postData = json_decode($input, true);
|
||||
|
||||
// 如果POST数据是有效的JSON,合并到params中
|
||||
if (json_last_error() === JSON_ERROR_NONE && is_array($postData)) {
|
||||
$params = array_merge($params, $postData);
|
||||
$action = $postData['action'] ?? $action;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'method' => $method,
|
||||
'action' => $action,
|
||||
'params' => $params
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送JSON响应
|
||||
* @param mixed $data 要发送的数据
|
||||
* @param int $statusCode HTTP状态码(默认200)
|
||||
*/
|
||||
protected function sendResponse($data, $statusCode = 200) {
|
||||
http_response_code($statusCode);
|
||||
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送错误响应
|
||||
* @param string $message 错误信息
|
||||
* @param int $statusCode HTTP状态码(默认400)
|
||||
*/
|
||||
protected function sendError($message, $statusCode = 400) {
|
||||
$this->sendResponse([
|
||||
'success' => false,
|
||||
'message' => $message
|
||||
], $statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送成功响应
|
||||
* @param mixed $data 响应数据(默认空数组)
|
||||
* @param string $message 成功信息(默认空字符串)
|
||||
*/
|
||||
protected function sendSuccess($data = [], $message = '') {
|
||||
$response = [
|
||||
'success' => true
|
||||
];
|
||||
|
||||
if (!empty($data)) {
|
||||
$response['data'] = $data;
|
||||
}
|
||||
|
||||
if (!empty($message)) {
|
||||
$response['message'] = $message;
|
||||
}
|
||||
|
||||
$this->sendResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理OPTIONS请求
|
||||
*/
|
||||
protected function handleOptionsRequest() {
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录操作日志
|
||||
* @param string $action 操作类型
|
||||
* @param array $data 操作数据
|
||||
* @param string $logFile 日志文件路径
|
||||
*/
|
||||
protected function logOperation($action, $data = [], $logFile = 'data/operation_log.json') {
|
||||
require_once 'FileManager.php';
|
||||
|
||||
// 加载现有日志
|
||||
$logData = FileManager::loadJsonFile($logFile, ['operations' => []]);
|
||||
|
||||
// 添加新日志条目
|
||||
$logEntry = [
|
||||
'timestamp' => date('Y-m-d H:i:s'),
|
||||
'action' => $action,
|
||||
'data' => $data,
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown'
|
||||
];
|
||||
|
||||
array_unshift($logData['operations'], $logEntry);
|
||||
|
||||
// 限制日志数量(保留最新的500条)
|
||||
if (count($logData['operations']) > 500) {
|
||||
$logData['operations'] = array_slice($logData['operations'], 0, 500);
|
||||
}
|
||||
|
||||
// 保存日志
|
||||
FileManager::saveJsonFile($logFile, $logData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作日志
|
||||
* @param int $limit 日志数量限制(默认20)
|
||||
* @param string $logFile 日志文件路径
|
||||
* @return array 日志数据
|
||||
*/
|
||||
protected function getOperationLog($limit = 20, $logFile = 'data/operation_log.json') {
|
||||
require_once 'FileManager.php';
|
||||
|
||||
$logData = FileManager::loadJsonFile($logFile, ['operations' => []]);
|
||||
$operations = $logData['operations'] ?? [];
|
||||
|
||||
// 限制返回的日志数量
|
||||
if (count($operations) > $limit) {
|
||||
$operations = array_slice($operations, 0, $limit);
|
||||
}
|
||||
|
||||
return ['success' => true, 'operations' => $operations];
|
||||
}
|
||||
}
|
||||
?>
|
||||
87
utils/FileManager.php
Normal file
87
utils/FileManager.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* 文件管理工具类
|
||||
* 提供通用的文件和目录操作功能,减少代码重复
|
||||
*/
|
||||
class FileManager {
|
||||
|
||||
/**
|
||||
* 确保目录存在,如果不存在则创建
|
||||
* @param string $dirPath 目录路径
|
||||
* @param int $mode 目录权限(默认0755)
|
||||
* @return bool 创建成功或目录已存在返回true,失败返回false
|
||||
*/
|
||||
public static function ensureDirExists($dirPath, $mode = 0755) {
|
||||
if (!is_dir($dirPath)) {
|
||||
return mkdir($dirPath, $mode, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载JSON文件内容
|
||||
* @param string $filePath 文件路径
|
||||
* @param mixed $default 默认值(文件不存在或解析失败时返回)
|
||||
* @param callable|null $initCallback 初始化回调函数(文件不存在时调用)
|
||||
* @return mixed 解析后的JSON数据或默认值
|
||||
*/
|
||||
public static function loadJsonFile($filePath, $default = [], callable $initCallback = null) {
|
||||
if (!file_exists($filePath)) {
|
||||
if ($initCallback) {
|
||||
call_user_func($initCallback);
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
$data = file_get_contents($filePath);
|
||||
if (!$data) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$json = json_decode($data, true);
|
||||
return is_array($json) ? $json : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存JSON数据到文件
|
||||
* @param string $filePath 文件路径
|
||||
* @param mixed $data 要保存的数据
|
||||
* @param int $flags JSON编码标志(默认JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
|
||||
* @return bool 保存成功返回true,失败返回false
|
||||
*/
|
||||
public static function saveJsonFile($filePath, $data, $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) {
|
||||
// 确保目录存在
|
||||
$dirPath = dirname($filePath);
|
||||
if (!self::ensureDirExists($dirPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return file_put_contents($filePath, json_encode($data, $flags)) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $filePath 文件路径
|
||||
* @return bool 删除成功返回true,失败返回false
|
||||
*/
|
||||
public static function deleteFile($filePath) {
|
||||
if (file_exists($filePath)) {
|
||||
return unlink($filePath);
|
||||
}
|
||||
return true; // 文件不存在也视为删除成功
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件修改时间
|
||||
* @param string $filePath 文件路径
|
||||
* @return int 文件修改时间戳(文件不存在返回0)
|
||||
*/
|
||||
public static function getFileMTime($filePath) {
|
||||
if (file_exists($filePath)) {
|
||||
return filemtime($filePath);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user