安全性: - 所有SQL查询改用参数化查询(db_query),防止SQL注入 - TUSHARE_API_TOKEN集中到config.php常量,移除硬编码和多份拷贝 - getBasic.inc.php中$_REQUEST输出到JS时添加htmlspecialchars转义 代码组织: - 21个页面文件移至charts/子目录,根目录仅保留入口文件 - ajax.inc.php拆分:esfTradeDaily/esfListDaily迁至getEstate.inc.php - getBasic.inc.php拆分:HTML组件函数迁至新建的widgets.inc.php - 前端依赖去重:移除lib/echarts.min.js、libai/3.4.16.js、research/js/3.4.16.js 规范化: - AJAX响应格式统一为jsonResponse()标准封装 - 前端全局变量统一为window.chartConfig对象模式 - 修复PHP 8.4 Deprecated警告(getEstateData、getStockHistoryDataByTS参数顺序) - html/head.php中jQuery/CSS路径改为绝对路径,修复charts/子目录加载问题 Co-Authored-By: Claude Code <noreply@anthropic.com>
63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
define('TUSHARE_API_TOKEN', '1bc28452ba375da19320cda845ae6307578964cb3ae473d0dc702aea');
|
|
|
|
function get_db_config() {
|
|
return [
|
|
'host' => 'localhost',
|
|
'username' => 'myquant',
|
|
'password' => '_H(lU1_fF*9baRTp',
|
|
'database' => 'myquant',
|
|
'charset' => 'utf8mb4'
|
|
];
|
|
}
|
|
|
|
function get_mysqli_connection() {
|
|
$db_config = get_db_config();
|
|
$mysqli = new mysqli(
|
|
$db_config['host'],
|
|
$db_config['username'],
|
|
$db_config['password'],
|
|
$db_config['database']
|
|
);
|
|
|
|
if ($mysqli->connect_error) {
|
|
die("Connection failed: " . $mysqli->connect_error);
|
|
}
|
|
|
|
$mysqli->set_charset($db_config['charset']);
|
|
return $mysqli;
|
|
}
|
|
|
|
/**
|
|
* 参数化查询,防止SQL注入。返回mysqli_result或false。
|
|
* @param mysqli $mysqli
|
|
* @param string $sql 带 ? 占位符的SQL
|
|
* @param array $params 参数值数组
|
|
* @return \mysqli_result|false
|
|
*/
|
|
function db_query($mysqli, $sql, $params = []) {
|
|
$stmt = $mysqli->prepare($sql);
|
|
if (!$stmt) return false;
|
|
if ($params) {
|
|
$types = str_repeat('s', count($params));
|
|
$stmt->bind_param($types, ...$params);
|
|
}
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$stmt->close();
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 统一JSON响应格式
|
|
* @param mixed $data 响应数据
|
|
* @param string $status ok|error
|
|
* @param string $message 错误信息
|
|
*/
|
|
function jsonResponse($data = null, $status = 'ok', $message = '') {
|
|
$response = ['status' => $status];
|
|
if ($data !== null) $response['data'] = $data;
|
|
if ($message) $response['message'] = $message;
|
|
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
exit();
|
|
} |