refactor: 全面代码重构与项目结构整理

安全性:
- 所有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>
This commit is contained in:
2026-05-26 08:20:33 +08:00
co-authored by Claude Code
parent 07da6a2ad4
commit 7e57cae242
49 changed files with 848 additions and 733 deletions
+33 -138
View File
@@ -1,6 +1,7 @@
<?php
include_once __DIR__."/config.php";
include_once __DIR__."/functions.inc.php";
include_once __DIR__."/widgets.inc.php";
/**
* 获取PE_TTM,PB,PS等历史数据
@@ -11,19 +12,20 @@ include_once __DIR__."/functions.inc.php";
* @return array
*/
function getBasicData($ts_code,$day_st,$day_end,$item){
$day_st=date('Ymd',strtotime($day_st));
$allowed_items = ['PE_TTM', 'PB', 'PS', 'pe_ttm', 'pb', 'ps'];
if(!in_array($item, $allowed_items)) return array('tDate'=>[],'idx'=>[],'data'=>[],'data_max'=>0,'data_min'=>0,'data_avg'=>0,'data_last'=>0);
$day_st=date('Ymd',strtotime($day_st));
$mysqli = get_mysqli_connection();
$where = " and trade_date>'".$day_st."'";
$sql = "select DATE_FORMAT(trade_date,'%Y-%m-%d') as trade_date, $item as pe_ttm from stock_his_basic_pro where ts_code = ? and trade_date > ?";
$params = [$ts_code, $day_st];
if($day_end) {
$day_end=date('Ymd',strtotime($day_end));
$where .= " and trade_date <'".$day_end."'";
$sql .= " and trade_date < ?";
$params[] = $day_end;
}
$sql= <<<EOF
select DATE_FORMAT(trade_date,'%Y-%m-%d') as trade_date, $item as pe_ttm from stock_his_basic_pro where ts_code = '$ts_code' $where order by trade_date asc
EOF;
#echo $sql;
$result = $mysqli->query($sql);
$sql .= " order by trade_date asc";
$result = db_query($mysqli, $sql, $params);
$tDate=$idx=$data=$data_clean=array(); # $data_clean is an array without null
@@ -75,13 +77,14 @@ function ts_code_conv($ts_code){
function getStockHist($ts_code,$day_st,$day_end){
$mysqli = get_mysqli_connection();
$where = " and trade_date>='".$day_st."'";
if($day_end) $where .= " and trade_date <='".$day_end."'";
$sql= <<<EOF
select trade_date, close from stock_his_pro where ts_code = '$ts_code' $where order by trade_date asc
EOF;
//echo $sql;
$result = $mysqli->query($sql);
$sql = "select trade_date, close from stock_his_pro where ts_code = ? and trade_date >= ?";
$params = [$ts_code, $day_st];
if($day_end) {
$sql .= " and trade_date <= ?";
$params[] = $day_end;
}
$sql .= " order by trade_date asc";
$result = db_query($mysqli, $sql, $params);
$tDate=$idx=$data=array();
@@ -103,9 +106,7 @@ return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data);
function tscodeToName($ts_code){
$mysqli = get_mysqli_connection();
$sql= "select name from stock_all_pro where ts_code='{$ts_code}'";
$result = $mysqli->query($sql);
$result = db_query($mysqli, "select name from stock_all_pro where ts_code = ?", [$ts_code]);
if($result && $result->num_rows >0) $row = $result->fetch_assoc();
$result->free();
$mysqli->close();
@@ -114,18 +115,23 @@ function tscodeToName($ts_code){
function getBasicExtData($ts_code,$item,$day_st,$day_end){
$mysqli = get_mysqli_connection();
$where = " and trade_date>'".$day_st."' ";
$allowed_items = ['total_mv','circ_mv','total_mv_all','circ_mv_all','pe_ttm','pb','ps','total_share','float_share','free_share'];
if(!in_array($item, $allowed_items)) return array('tDate'=>[],'idx'=>[],'data'=>[],'data_max'=>0,'data_min'=>0,'data_avg'=>0,'data_last'=>0);
$extra_where = "";
if($item=='total_mv_all' or $item=='circ_mv_all') {
$where .= "and vol/10000/10000 > 1";
$extra_where = " and vol/10000/10000 > 1";
$ts_code='all';
$item=substr($item,0,-4);
}
if($day_end) $where .= " and trade_date <'".$day_end."'";
$sql= <<<EOF
select * from stock_basic_ext where code = '$ts_code' and item='$item' $where order by trade_date asc
EOF;
//echo $sql;
$result = $mysqli->query($sql);
$sql = "select * from stock_basic_ext where code = ? and item = ? and trade_date > ?";
$params = [$ts_code, $item, $day_st];
if($day_end) {
$sql .= " and trade_date < ?";
$params[] = $day_end;
}
$sql .= $extra_where . " order by trade_date asc";
$result = db_query($mysqli, $sql, $params);
$tDate=$idx=$data=array();
@@ -164,121 +170,10 @@ function dateGap($date,$gap){
}
/**
* @param string $email: default null
* @return bool true
*/
function tradeStocksList($email=null){
$mysqli = get_mysqli_connection();
if($_REQUEST['t_vendor']=='方正证券'){
$sql= <<< EOF
select distinct(ts_code) ts_code,ts_name from trade_record
where tprice>0 and (ts_code not like '7%' and ts_code not like '1%') and length(trade_id)=5 order by ts_code
EOF;
}elseif($_REQUEST['t_vendor']=='长江证券'){
//length(0+ts_name)!=length(ts_name) 判断ts_name 不能为纯数字
$sql= <<< EOF
select distinct(ts_code) ts_code,ts_name from trade_record_cj
where tprice>0 and ts_code not like '7%' and length(0+ts_name)!=length(ts_name) order by ts_code
EOF;
}
$result = $mysqli->query($sql);
$data = $result->fetch_all(MYSQLI_ASSOC);
$option='';
foreach ( $data as $item){
$option .= "<option label='{$item['ts_name']}' value='{$item['ts_code']}'></option>\n";
}
$html = <<< EOF
<input id="ts_code" name= "ts_code" list="codeList" autocomplete="off"/>
<datalist id="codeList">
$option
</datalist>
<script>
$("#ts_code").val('{$_REQUEST['ts_code']}');
</script>
EOF;
echo $html;
return true;
}
function vendorList($id='t_vendor'){
$html = <<<EOF
<select id="{$id}" name= "{$id}" autocomplete="off" >
<option value="方正证券">方正证券</option>
<option value="长江证券">长江证券</option>
</select>
<script>
$("#{$id}").val('{$_REQUEST[$id]}');
</script>
EOF;
echo $html;
return true;
}
/**
* 根据年份区间,获取财报日期清单
* @param $yst
* @param $yed
*/
function yearList($yst,$yed){
$n = $yed-$yst+1;
$thisYear=date('Y'); //yyyy,eg.2022
$thisMonDay=date('md'); //mmdd,eg.0331
$years=array();
for($i=0;$i<$n;$i++){
if(($yst+$i)<($thisYear-1) ) $years[]=($yst+$i)."1231"; //前年及以前
//去年
if(($yst+$i)==($thisYear-1)and $thisMonDay<='0430') $years[]=($yst+$i)."0930";
if(($yst+$i)==($thisYear-1)and $thisMonDay >'0430') $years[]=($yst+$i)."1231";
//今年
if(($yst+$i)==$thisYear){
switch ($thisMonDay){
case $thisMonDay<='0430':
//一季报未公布do nothing
break;
case $thisMonDay>'0430' && $thisMonDay<= '0831':
//公布一季报
$years[]=($yst+$i)."0331";
break;
case $thisMonDay>'0831' && $thisMonDay<= '1031':
//公布一季报
$years[]=($yst+$i)."0630";
break;
case $thisMonDay>'1031':
//公布一季报
$years[]=($yst+$i)."0930";
break;
default: break;
}
}
}
return $years;
}
/**
* $year 20201231 转为2020年报
* @param $year
*/
function yearToname($year){
switch (substr($year,4,4)){
case '1231':
return substr($year,0,4).'年报';
case '0930':
return substr($year,0,4).'三季';
case '0630':
return substr($year,0,4).'半年';
case '0331':
return substr($year,0,4).'一季';
default:
return substr($year,4,4);
}
}
// 函数一:获取股票代码ts_code对应的历史pe_ttm、pb或ps
function getStockHistoryDataByTS($ts_code, $day_st, $day_end = null, $item){
function getStockHistoryDataByTS($ts_code, $day_st, $item, $day_end = null){
//global $token;
// 参数校验