From 84f1bf69ca2b4df134cca5881c834394666c9f79 Mon Sep 17 00:00:00 2001 From: "zhangf@suq.cn" Date: Mon, 7 Sep 2026 10:31:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(stock):=20=E6=B7=BB=E5=8A=A0=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E9=9B=86=E6=88=90=E5=88=B0=E7=BD=91=E7=AB=99=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 getStockInfo 函数用于获取股票数据,默认显示睿能科技股票信息 - 新增 fetchStockFromSina 函数从新浪财经接口获取实时股票数据 - 在 footer 组件中添加股票价格和涨跌幅动态显示功能 - 在投资页面中集成股票信息展示模块,包括价格、涨跌、高低、成交量等指标 - 添加页面定时刷新机制确保股票数据及时更新 - 实现股票涨跌颜色动态变色功能 - 集成缓存机制减少接口请求频率,提升性能 --- app/common.php | 103 +++++++++++++++++++++ .../website/1/zh/demo/components/footer.html | 45 ++++++++- public/themes/website/1/zh/demo/investment.html | 30 ++++-- 3 files changed, 167 insertions(+), 11 deletions(-) diff --git a/app/common.php b/app/common.php index 29ba2fc..f3885a6 100644 --- a/app/common.php +++ b/app/common.php @@ -1731,3 +1731,106 @@ if (!function_exists('getDescriptionSeparate')) { } } } + +/** + * 获取股票信息 + * @param string $stockCode 股票代码 + * @return array 股票数据 + */ +if (!function_exists('getStockInfo')) { + function getStockInfo(string $stockCode = '603933'): array + { + // 默认返回数据 + $defaultData = [ + 'code' => $stockCode, + 'name' => '睿能科技', + 'price' => '--', + 'change' => '--', + 'changePercent' => '--', + 'high' => '--', + 'low' => '--', + 'volume' => '--', + 'amount' => '--', + 'updateTime' => date('Y-m-d H:i:s') + ]; + + try { + // 尝试从缓存获取 + $cacheKey = 'stock_info_' . $stockCode; + $cached = \think\facade\Cache::get($cacheKey); + if (!empty($cached)) { + return $cached; + } + + // 从新浪财经获取数据 + $data = fetchStockFromSina($stockCode); + + if ($data) { + // 缓存14分50秒 + \think\facade\Cache::set($cacheKey, $data, 60*15-10); + return $data; + } + + return $defaultData; + } catch (\Exception $e) { + return $defaultData; + } + } +} + +/** + * 从新浪财经获取股票数据 + * @param string $stockCode 股票代码 + * @return array|null + */ +if (!function_exists('fetchStockFromSina')) { + function fetchStockFromSina(string $stockCode): ?array + { + $market = substr($stockCode, 0, 1) == '6' ? 'sh' : 'sz'; + $url = "https://hq.sinajs.cn/list={$market}{$stockCode}"; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Referer: https://finance.sina.com.cn' + ]); + + $response = curl_exec($ch); + curl_close($ch); + + if (empty($response)) { + return null; + } + + // 解析新浪数据格式:var hq_str_sh603933="名称,开盘价,昨收,当前价,最高,最低,..."; + if (preg_match('/="(.+)"/', $response, $matches)) { + $fields = explode(',', $matches[1]); + if (count($fields) >= 32) { + $price = floatval($fields[3]); + $prevClose = floatval($fields[2]); + $change = round($price - $prevClose, 2); + $changePercent = $prevClose > 0 ? round(($change / $prevClose) * 100, 2) : 0; + + return [ + 'code' => $stockCode, + 'name' => $fields[0], + 'price' => $price > 0 ? number_format($price, 2) : '--', + 'change' => $price > 0 ? (($change >= 0 ? '+' : '') . number_format($change, 2)) : '--', + 'changePercent' => $price > 0 ? (($changePercent >= 0 ? '+' : '') . number_format($changePercent, 2) . '%') : '--', + 'high' => floatval($fields[4]) > 0 ? number_format(floatval($fields[4]), 2) : '--', + 'low' => floatval($fields[5]) > 0 ? number_format(floatval($fields[5]), 2) : '--', + 'volume' => floatval($fields[8]) > 0 ? round(floatval($fields[8]) / 10000, 2) : '--', + 'amount' => floatval($fields[9]) > 0 ? round(floatval($fields[9]) / 10000, 2) : '--', + 'updateTime' => $fields[30] . ' ' . $fields[31] + ]; + } + } + + return null; + } +} diff --git a/public/themes/website/1/zh/demo/components/footer.html b/public/themes/website/1/zh/demo/components/footer.html index 081ab56..0b15f12 100644 --- a/public/themes/website/1/zh/demo/components/footer.html +++ b/public/themes/website/1/zh/demo/components/footer.html @@ -7,10 +7,11 @@ RAYNEN 睿能科技 + - \ No newline at end of file + + \ No newline at end of file diff --git a/public/themes/website/1/zh/demo/investment.html b/public/themes/website/1/zh/demo/investment.html index 6f49b25..011f665 100644 --- a/public/themes/website/1/zh/demo/investment.html +++ b/public/themes/website/1/zh/demo/investment.html @@ -141,35 +141,49 @@ {/hcTaglib:category} + {php} + // 获取股票信息 + $stockInfo = getStockInfo('603933'); + // 判断涨跌颜色 + $priceColor = ''; + if ($stockInfo['change'] !== '--') { + $changeVal = floatval(str_replace(['+', '-'], '', $stockInfo['change'])); + if ($stockInfo['change'][0] === '+') { + $priceColor = 'color: #ef4335'; // 上涨红色 + } elseif ($stockInfo['change'][0] === '-') { + $priceColor = 'color: #00b894'; // 下跌绿色 + } + } + {/php}

股票信息

-

股票代码:603933

+

股票代码:{$stockInfo.code}

- 20.98 + {$stockInfo.price} - +0.37  +1.75% + {$stockInfo.change}  {$stockInfo.changePercent}
  • 最高(元) - 20.98 + {$stockInfo.high}
  • 最低(元) - 20.23 + {$stockInfo.low}
  • 成交量(万手) - 3.25 + {$stockInfo.volume}
  • 成交额(万元) - 6684.00 + {$stockInfo.amount}
-

截止 2026-08-14 09:40:31*报价有十五分钟或以上延迟。

+

截止 {$stockInfo.updateTime}*报价有十五分钟或以上延迟。

股票走势图