Browse Source

feat(stock): 添加股票信息实时显示和图表动态刷新功能

- 在页脚组件中为股票价格、单位和涨跌幅添加动态颜色样式
- 将股票信息获取逻辑从投资页面移至头部组件以统一管理
- 为投资页面股票信息区域的价格、单位和涨跌幅添加颜色标识
- 将静态股票走势图替换为实时更新的新浪股票图表
- 实现股票图表定时刷新机制,每分钟自动更新一次
- 添加页面可见性检测,仅在页面可见时执行图表刷新
- 移除投资页面中重复的股票信息获取PHP代码
master
zhangf@suq.cn 2 weeks ago
parent
commit
62ccdcbc39
  1. 7
      public/themes/website/1/zh/demo/components/footer.html
  2. 14
      public/themes/website/1/zh/demo/components/header.html
  3. 63
      public/themes/website/1/zh/demo/investment.html

7
public/themes/website/1/zh/demo/components/footer.html

@ -7,11 +7,10 @@
<img src="{hcTaglib:setting name='dark_logo' type='2' /}" alt="RAYNEN 睿能科技" width="252" height="26" /> <img src="{hcTaglib:setting name='dark_logo' type='2' /}" alt="RAYNEN 睿能科技" width="252" height="26" />
</div> </div>
<p class="site-footer__stock">股票代码: 603933</p> <p class="site-footer__stock">股票代码: 603933</p>
<p class="site-footer__quote" aria-label="股票行情占位"> <p class="site-footer__quote" aria-label="股票行情占位">
<span class="site-footer__quote-price">{$stockInfo.price}</span>
<span class="site-footer__quote-unit"></span>
<span class="site-footer__quote-change">{$stockInfo.change}&nbsp;&nbsp;{$stockInfo.changePercent}</span>
<span class="site-footer__quote-price" style="{$priceColor}">{$stockInfo.price}</span>
<span class="site-footer__quote-unit" style="{$priceColor}"></span>
<span class="site-footer__quote-change" style="{$priceColor}">{$stockInfo.change}&nbsp;&nbsp;{$stockInfo.changePercent}</span>
</p> </p>
<p class="site-footer__follow">关注我们:</p> <p class="site-footer__follow">关注我们:</p>
<div class="site-footer__qrs"> <div class="site-footer__qrs">

14
public/themes/website/1/zh/demo/components/header.html

@ -603,3 +603,17 @@
</div> </div>
</div> </div>
</header> </header>
{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}

63
public/themes/website/1/zh/demo/investment.html

@ -140,21 +140,6 @@
</div> </div>
</section> </section>
{/hcTaglib:category} {/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}
<section class="iv-stock section__wrap" id="stock" data-header-dark> <section class="iv-stock section__wrap" id="stock" data-header-dark>
<h2 class="iv-stock__title">股票信息</h2> <h2 class="iv-stock__title">股票信息</h2>
<div class="iv-stock__body"> <div class="iv-stock__body">
@ -162,7 +147,7 @@
<p class="iv-stock__code">股票代码:{$stockInfo.code}</p> <p class="iv-stock__code">股票代码:{$stockInfo.code}</p>
<div class="iv-stock__quote"> <div class="iv-stock__quote">
<span class="iv-stock__price" id="stock-price" style="{$priceColor}">{$stockInfo.price}</span> <span class="iv-stock__price" id="stock-price" style="{$priceColor}">{$stockInfo.price}</span>
<span class="iv-stock__unit"></span>
<span class="iv-stock__unit" style="{$priceColor}"></span>
<span class="iv-stock__change" id="stock-change" style="{$priceColor}">{$stockInfo.change}&nbsp;&nbsp;{$stockInfo.changePercent}</span> <span class="iv-stock__change" id="stock-change" style="{$priceColor}">{$stockInfo.change}&nbsp;&nbsp;{$stockInfo.changePercent}</span>
</div> </div>
<ul class="iv-stock__metrics"> <ul class="iv-stock__metrics">
@ -186,7 +171,7 @@
<p class="iv-stock__note" id="stock-time">截止 {$stockInfo.updateTime}*报价有十五分钟或以上延迟。</p> <p class="iv-stock__note" id="stock-time">截止 {$stockInfo.updateTime}*报价有十五分钟或以上延迟。</p>
</div> </div>
<div class="iv-stock__chart"> <div class="iv-stock__chart">
<img src="/themes/dist_static/static/images/section-3-img-CK6u5Hdw.png" alt="股票走势图" width="720" height="450" />
<img id="stock-chart" src="https://image.sinajs.cn/newchart/min/n/sh603933.gif" alt="睿能科技分时走势图" width="720" height="450" />
</div> </div>
</div> </div>
</section> </section>
@ -289,6 +274,50 @@
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.min.js"></script>
<script src="https://unpkg.com/counterup2@2.0.2/dist/index.js"></script> <script src="https://unpkg.com/counterup2@2.0.2/dist/index.js"></script>
<script>
(function() {
// 股票图表定时刷新(只刷新图片,不刷新整个页面)
var refreshInterval = 60000; // 1分钟
var stockTimer = null;
var chartImg = document.getElementById('stock-chart');
// 刷新图片
function refreshChartImage() {
if (chartImg) {
var timestamp = new Date().getTime();
chartImg.src = 'https://image.sinajs.cn/newchart/min/n/sh603933.gif?t=' + timestamp;
}
}
// 开始定时刷新
function startChartRefresh() {
stockTimer = setInterval(refreshChartImage, refreshInterval);
}
// 页面加载完成后开始
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', startChartRefresh);
} else {
startChartRefresh();
}
// 页面可见性变化时处理
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
if (stockTimer) {
clearInterval(stockTimer);
stockTimer = null;
}
} else {
if (!stockTimer) {
refreshChartImage(); // 立即刷新一次
startChartRefresh();
}
}
});
})();
</script>
</body> </body>
</html> </html>
Loading…
Cancel
Save