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
+56 -194
View File
@@ -1,6 +1,7 @@
<?php
ini_set("display_errors","0");
include_once __DIR__."/config.php";
include_once __DIR__."/getEstate.inc.php";
$mysqli = get_mysqli_connection();
//ajax: get stock list for select
@@ -17,13 +18,9 @@ EOF;
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);
$result = db_query($mysqli, $sql);
$data = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode(array(
"status" => "1",
'stocks'=> $data,
"company"=>$_REQUEST['t_vendor'],
),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
jsonResponse(['stocks' => $data, 'company' => $_REQUEST['t_vendor']]);
}
/**
@@ -66,7 +63,7 @@ if($_REQUEST['t']=='financeData'){
//strip key from array $data
//dataTable must NOT have any key of json
$data=array_values($data);
echo json_encode(array("data"=>$data),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
jsonResponse($data);
}
/*
@@ -75,11 +72,7 @@ if($_REQUEST['t']=='financeData'){
if($_REQUEST['t']=='esfTBD'){
$dataTrade=esfTradeDaily();
$dataList=esfListDaily();
echo json_encode(array(
"status" => "1",
'dataTrade'=> $dataTrade,
'dataList'=>$dataList
),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
jsonResponse(['dataTrade' => $dataTrade, 'dataList' => $dataList]);
}
/*
@@ -88,204 +81,73 @@ if($_REQUEST['t']=='esfTBD'){
if($_REQUEST['t']=='esfListDaily'){
$data=esfListDaily();
echo json_encode(array(
"status" => "1",
'datas'=> $data
),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
jsonResponse(['datas' => $data]);
}
function esfTradeDaily(){
global $mysqli;
if($_REQUEST['dm']=='Daily'){
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m-%d') td,
eje.val district,
eje1.val qty,
eje2.val area
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='5' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
order by ej.tdate asc
EOF;
}
if($_REQUEST['dm']=='Monthly'){
$where="";
if($_REQUEST['t_start']) $where .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
if($_REQUEST['t_end']) {
$endDate = new DateTime($_REQUEST['t_end']);
$endDate->modify('last day of this month');
$where .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
}
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m') td,
eje.val district,
sum(eje1.val) qty,
sum(eje2.val) area
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='5' {$where}
group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
EOF;
//echo $sql;
}
$result = $mysqli->query($sql);
$data = $result->fetch_all(MYSQLI_ASSOC);
return $data;
}
function esfListDaily(){
global $mysqli;
if($_REQUEST['dm']=='Daily'){
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m-%d') td,
eje.val district,
eje1.val qty,
eje2.val price
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+5
where ej.data_type='8' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
order by ej.tdate asc
EOF;
}
if($_REQUEST['dm']=='Monthly'){
$where="";
if($_REQUEST['t_start']) $where .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
if($_REQUEST['t_end']) {
$endDate = new DateTime($_REQUEST['t_end']);
$endDate->modify('last day of this month');
$where .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
}
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m') td,
eje.val district,
sum(eje1.val) qty,
sum(eje2.val) price
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+5
where ej.data_type='8' {$where}
group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
EOF;
//echo $sql;
}
$result = $mysqli->query($sql);
$data = $result->fetch_all(MYSQLI_ASSOC);
return $data;
}
/*
*newTBD: 新房Trade By Day
*/
if($_REQUEST['t']=='newTBD'){
$district = $_REQUEST['district'];
$t_start = $_REQUEST['t_start'];
$t_end = $_REQUEST['t_end'];
if($_REQUEST['dm']=='Daily'){
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m-%d') td,
eje.val district,
eje1.val qty,
eje2.val area
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='1' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
order by date_format(tdate,'%Y-%m-%d') asc
EOF;
$sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, eje1.val qty, eje2.val area
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='1' and ej.tdate >= ? and ej.tdate <= ?
order by date_format(tdate,'%Y-%m-%d') asc";
$params = [$district, $t_start, $t_end];
}
if($_REQUEST['dm']=='Monthly'){
$where="";
if($_REQUEST['t_start']) $where .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
if($_REQUEST['t_end']) {
$endDate = new DateTime($_REQUEST['t_end']);
$sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m') td, eje.val district, sum(eje1.val) qty, sum(eje2.val) area
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='1'";
$params = [$district];
if($t_start) { $sql .= " and ej.tdate >= ?"; $params[] = $t_start . '-01'; }
if($t_end) {
$endDate = new DateTime($t_end);
$endDate->modify('last day of this month');
$where .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
}
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m') td,
eje.val district,
sum(eje1.val) qty,
sum(eje2.val) area
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='1' {$where}
group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
EOF;
$sql .= " and ej.tdate <= ?";
$params[] = $endDate->format('Y-m-d');
}
$sql .= " group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc";
}
$result = $mysqli->query($sql);
$result = db_query($mysqli, $sql, $params);
$data = $result->fetch_all(MYSQLI_ASSOC);
if($_REQUEST['dm']=='Daily'){
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m-%d') td,
eje.val district,
eje1.val area,
eje2.val qty
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='4' and (ej.tdate>='{$_REQUEST['t_start']}' and ej.tdate<='{$_REQUEST['t_end']}')
order by date_format(tdate,'%Y-%m-%d') asc
EOF;
$sql2 = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, eje1.val area, eje2.val qty
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='4' and ej.tdate >= ? and ej.tdate <= ?
order by date_format(tdate,'%Y-%m-%d') asc";
$params2 = [$district, $t_start, $t_end];
}
if($_REQUEST['dm']=='Monthly'){
$where="";
if($_REQUEST['t_start']) $where .=" and ej.tdate>='{$_REQUEST['t_start']}-01'";
if($_REQUEST['t_end']) {
$endDate = new DateTime($_REQUEST['t_end']);
$sql2 = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, max(eje1.val) area, max(eje2.val) qty
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='4'";
$params2 = [$district];
if($t_start) { $sql2 .= " and ej.tdate >= ?"; $params2[] = $t_start . '-01'; }
if($t_end) {
$endDate = new DateTime($t_end);
$endDate->modify('last day of this month');
$where .=" and ej.tdate<='{$endDate->format('Y-m-d')}'";
$sql2 .= " and ej.tdate <= ?";
$params2[] = $endDate->format('Y-m-d');
}
$sql= <<<EOF
SELECT
distinct(ej.uuid) uid ,
date_format(tdate,'%Y-%m-%d') td,
eje.val district,
max(eje1.val) area,
max(eje2.val) qty
FROM
`estate_json` ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val='{$_REQUEST['district']}'
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='4' $where
group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc
EOF;
}
$result = $mysqli->query($sql);
$sql2 .= " group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc";
}
$result = db_query($mysqli, $sql2, $params2);
$data2 = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode(array(
"status" => "1",
'datas'=> $data,
'dataExt'=> $data2
),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
jsonResponse(['datas' => $data, 'dataExt' => $data2]);
}
BIN
View File
Binary file not shown.
+1 -2
View File
@@ -60,7 +60,7 @@ EOF;
// 辅助函数:调用TuShare API
function callTushareApi($method, $params){
$token = '1bc28452ba375da19320cda845ae6307578964cb3ae473d0dc702aea';
$token = TUSHARE_API_TOKEN;
// 将参数编码为JSON格式
$postData = json_encode([
"api_name" => $method,
@@ -68,7 +68,6 @@ function callTushareApi($method, $params){
"params" => $params,
"fields" => "" // 其他字段需要根据具体API调整
]);
print_r($postData);
// 初始化cURL会话
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.tushare.pro');
+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;
// 参数校验
+42 -42
View File
@@ -9,16 +9,14 @@ include_once __DIR__."/config.php";
function getIndexData($ts_code,$day_st,$day_end){
$mysqli = get_mysqli_connection();
#$ts_code = '000001.SH';
#$day_st = '20000101';
#$day_end = '20190701';
$where = " and trade_date>='".$day_st."'";
if($day_end) $where .= " and trade_date <='".$day_end."'";
$sql= <<<EOF
select trade_date, close from index_hist_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 index_hist_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();
@@ -44,24 +42,17 @@ return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data);
*/
function getIhData($lx,$share,$day_st,$day_end){
$mysqli = get_mysqli_connection();
/*
$where = " and rdate>='".$day_st."'";
if($day_end) $where .= " and rdate<='".$day_end."'";
if($share=='ShareHDNum' or $share=='vPosition') $shareSel='sum('.$share.')/100000000';
elseif($share=='VSRatio') $shareSel='vPosition/ShareHDNum';
$sql= <<<EOF
select rdate,$shareSel as ttl from ih_data where lx='$lx' $where group by rdate order by rdate
EOF;
*/
$where = " and ih_date>='".$day_st."'";
if($day_end) $where .= " and ih_date<='".$day_end."'";
if($share=='ShareHDNum' ) $shareSel="sum(f9)/100000000"; //持股数
elseif($share=='vPosition') $shareSel="sum(f10)/100000000"; //持股额
elseif($share=='VSRatio') $shareSel='f10/f9'; //每股价格
$sql= <<<EOF
select ih_date,$shareSel as ttl from ih_by_ts_code_ext where tp='$lx' $where group by ih_date order by ih_date
EOF;
$result = $mysqli->query($sql);
$sql = "select ih_date,$shareSel as ttl from ih_by_ts_code_ext where tp = ? and ih_date >= ?";
$params = [$lx, $day_st];
if($day_end) {
$sql .= " and ih_date <= ?";
$params[] = $day_end;
}
$sql .= " group by ih_date order by ih_date";
$result = db_query($mysqli, $sql, $params);
//echo $sql;
$tDate=$idx=$data=array();
@@ -177,25 +168,30 @@ function hkholdConv($code){
*/
function getIhDataByStock($ts_code,$item,$lx,$day_st,$day_end){
$mysqli = get_mysqli_connection();
$where = " and ih_date>'".$day_st."'";
if($day_end) $where .= " and ih_date<'".$day_end."'";
if($lx) $where .= " and tp='".$lx."'";
switch(strtoupper($item)){
case 'F9':
$item_sel = 'sum('.$item.')/10000'; #单位万股
$item_sel = 'sum(f9)/10000';
break;
case 'F10':
$item_sel = 'sum('.$item.')/10000'; #单位万元
$item_sel = 'sum(f10)/10000';
break;
case 'F11':
case 'F12':
$item_sel = 'sum('.$item.')'; #单位%
$item_sel = "sum($item)";
break;
}
$sql= <<<EOF
select ts_code,ih_date,$item_sel as ttl from ih_by_ts_code_ext where ts_code='$ts_code' $where group by ih_date order by ih_date asc
EOF;
$result = $mysqli->query($sql);
$sql = "select ts_code,ih_date,$item_sel as ttl from ih_by_ts_code_ext where ts_code = ? and ih_date > ?";
$params = [$ts_code, $day_st];
if($day_end) {
$sql .= " and ih_date < ?";
$params[] = $day_end;
}
if($lx) {
$sql .= " and tp = ?";
$params[] = $lx;
}
$sql .= " group by ih_date order by ih_date asc";
$result = db_query($mysqli, $sql, $params);
#echo $sql;
$tDate=$idx=$data=array();
@@ -221,15 +217,19 @@ EOF;
function getMoneyFlowData($itm, $day_st,$day_end, $stacked){
$mysqli = get_mysqli_connection();
$allowed_cols = ['ggt_ss','ggt_sz','hgt','sgt','north_money','south_money'];
if(!in_array($itm, $allowed_cols)) return array('tDate'=>[],'data'=>[],'data_max'=>0,'data_min'=>0,'data_avg'=>0,'data_last'=>0);
if($stacked==1) $tb = 'moneyflow_hsgt_pro_ext';
else $tb = 'moneyflow_hsgt_pro';
$where = " trade_date>='".$day_st."'";
if($day_end) $where .= " and trade_date <='".$day_end."'";
$sql= <<<EOF
select trade_date, $itm as itm from $tb where $where order by trade_date asc
EOF;
#echo $sql;
$result = $mysqli->query($sql);
$sql = "select trade_date, $itm as itm from $tb where trade_date >= ?";
$params = [$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=$data=array();
+83 -6
View File
@@ -1,12 +1,9 @@
<?php
include_once __DIR__."/config.php";
function getEstateData($city='宁波',$fDate,$toDate){
function getEstateData($city, $fDate, $toDate){
$mysqli = get_mysqli_connection();
$sql=<<<EOF
SELECT city, listdate,sum(nums) as num FROM `estate_listing`
where listdate>='$fDate' and listdate<='$toDate' and city='$city' group by listdate
EOF;
$result = $mysqli->query($sql);
$sql = "SELECT city, listdate, sum(nums) as num FROM estate_listing where listdate >= ? and listdate <= ? and city = ? group by listdate";
$result = db_query($mysqli, $sql, [$fDate, $toDate, $city]);
$data=array();
if($result && $result->num_rows>0) {
while($row = $result->fetch_assoc()){
@@ -16,4 +13,84 @@ EOF;
return $data;
}
function esfTradeDaily(){
global $mysqli;
$district = $_REQUEST['district'];
$t_start = $_REQUEST['t_start'];
$t_end = $_REQUEST['t_end'];
if($_REQUEST['dm']=='Daily'){
$sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, eje1.val qty, eje2.val area
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='5' and ej.tdate >= ? and ej.tdate <= ?
order by ej.tdate asc";
$params = [$district, $t_start, $t_end];
}
if($_REQUEST['dm']=='Monthly'){
$sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m') td, eje.val district, sum(eje1.val) qty, sum(eje2.val) area
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+4
where ej.data_type='5'";
$params = [$district];
if($t_start) {
$sql .= " and ej.tdate >= ?";
$params[] = $t_start . '-01';
}
if($t_end) {
$endDate = new DateTime($t_end);
$endDate->modify('last day of this month');
$sql .= " and ej.tdate <= ?";
$params[] = $endDate->format('Y-m-d');
}
$sql .= " group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc";
}
$result = db_query($mysqli, $sql, $params);
$data = $result->fetch_all(MYSQLI_ASSOC);
return $data;
}
function esfListDaily(){
global $mysqli;
$district = $_REQUEST['district'];
$t_start = $_REQUEST['t_start'];
$t_end = $_REQUEST['t_end'];
if($_REQUEST['dm']=='Daily'){
$sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m-%d') td, eje.val district, eje1.val qty, eje2.val price
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+5
where ej.data_type='8' and ej.tdate >= ? and ej.tdate <= ?
order by ej.tdate asc";
$params = [$district, $t_start, $t_end];
}
if($_REQUEST['dm']=='Monthly'){
$sql = "SELECT distinct(ej.uuid) uid, date_format(tdate,'%Y-%m') td, eje.val district, sum(eje1.val) qty, sum(eje2.val) price
FROM estate_json ej
left join estate_json_ext eje on ej.uuid=eje.uuid and eje.val = ?
left join estate_json_ext eje1 on eje1.id=eje.id+3
left join estate_json_ext eje2 on eje2.id=eje.id+5
where ej.data_type='8'";
$params = [$district];
if($t_start) {
$sql .= " and ej.tdate >= ?";
$params[] = $t_start . '-01';
}
if($t_end) {
$endDate = new DateTime($t_end);
$endDate->modify('last day of this month');
$sql .= " and ej.tdate <= ?";
$params[] = $endDate->format('Y-m-d');
}
$sql .= " group by date_format(tdate,'%Y-%m') order by date_format(tdate,'%Y-%m') asc";
}
$result = db_query($mysqli, $sql, $params);
$data = $result->fetch_all(MYSQLI_ASSOC);
return $data;
}
?>
+5 -5
View File
@@ -4,17 +4,17 @@ include_once __DIR__."/getBasic.inc.php"; //类外调用方法
class getFinance
{
private $url = "http://api.waditu.com";
private $token = '1bc28452ba375da19320cda845ae6307578964cb3ae473d0dc702aea';
public $ts_code = '002273.SZ'; //set ts_code default
public $httpjsonStr; //json string send to http_post_json
private $token;
public $ts_code = '002273.SZ';
public $httpjsonStr;
public $finDate;
public $preFinDate;
public $param = array();
private $unitFactor = 100000000; //单位因子,除以后单位亿
private $unitFactor = 100000000;
function __construct()
{
$this->token = TUSHARE_API_TOKEN;
$this->param['token'] = $this->token;
}
/**
+12 -16
View File
@@ -5,23 +5,19 @@
* @param $where: add select condition you need.
* @return array: return stock trade recode history data;
*/
function trade_rec($ts_code, $day_st, $day_end,$where = ''): array
function trade_rec($ts_code, $day_st, $day_end, $extra_where = ''): array
{
global $mysqli;
$tmp=explode('.',$ts_code);
$ts_code = $tmp[0];
$day_end = ($day_end)?$day_end:date('Y-m-d');
$where = $where." and tdate>= '{$day_st}' and tdate<='{$day_end}' and tprice>0"; //担保转出的时候,可能tprice<0
if($_REQUEST['t_vendor']=='方正证券') {
$sql = <<< EOF
select * from trade_record where ts_code = '$ts_code' $where and length(trade_id)=5 order by tdate asc,ttime asc
EOF;
} elseif($_REQUEST['t_vendor']=='长江证券') {
$sql = <<< EOF
select * from trade_record_cj where ts_code = '$ts_code' $where order by tdate asc,ttime asc
EOF;
}
$result = $mysqli->query($sql) or die($sql);
$vendor = $_REQUEST['t_vendor'] ?? '方正证券';
if($vendor == '方正证券') {
$sql = "select * from trade_record where ts_code = ? and tdate >= ? and tdate <= ? and tprice>0 $extra_where and length(trade_id)=5 order by tdate asc,ttime asc";
} elseif($vendor == '长江证券') {
$sql = "select * from trade_record_cj where ts_code = ? and tdate >= ? and tdate <= ? and tprice>0 $extra_where order by tdate asc,ttime asc";
}
$result = db_query($mysqli, $sql, [$ts_code, $day_st, $day_end]);
return $result->fetch_all(MYSQLI_ASSOC);
}
@@ -134,10 +130,10 @@ function tradeSummary($dataBuy,$dataSell,$dataAll):array{
function recentPrice($ts_code){
global $mysqli;
$sql = "SELECT close FROM `stock_his_pro` where ts_code='".ts_code_conv($ts_code)."' and trade_date=(select max(trade_date) from stock_his_pro)";
$result = $mysqli->query($sql) or die($sql);
$rt = $result->fetch_all(MYSQLI_ASSOC); //MYSQLI_ASSOC object $rt[0]['close']
return $rt[0]; //get arr['close']
$sql = "SELECT close FROM stock_his_pro where ts_code = ? and trade_date=(select max(trade_date) from stock_his_pro)";
$result = db_query($mysqli, $sql, [ts_code_conv($ts_code)]);
$rt = $result->fetch_all(MYSQLI_ASSOC);
return $rt[0];
}
/**
+117
View File
@@ -0,0 +1,117 @@
<?php
include_once __DIR__."/config.php";
/**
* 显示股票代码选择列表 (datalist widget)
* @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";
}
$ts_code_val = htmlspecialchars($_REQUEST['ts_code'] ?? '', ENT_QUOTES);
$html = <<< EOF
<input id="ts_code" name= "ts_code" list="codeList" autocomplete="off"/>
<datalist id="codeList">
$option
</datalist>
<script>
$("#ts_code").val('$ts_code_val');
</script>
EOF;
echo $html;
return true;
}
function vendorList($id='t_vendor'){
$val = htmlspecialchars($_REQUEST[$id] ?? '', ENT_QUOTES);
$html = <<<EOF
<select id="{$id}" name= "{$id}" autocomplete="off" >
<option value="方正证券">方正证券</option>
<option value="长江证券">长江证券</option>
</select>
<script>
$("#{$id}").val('{$val}');
</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);
}
}