feat: 股票列新增自选选项卡

- 最热/自选选项卡; 输入6位代码添加(校验位数/重复), 行内名称+涨跌幅+现价, ✕移出, 红涨绿跌
- 新端点 GET /relay/stock/quotes?codes=... (东财批量行情, 20s缓存, 非法过滤)
- it-tools-my-stocks 纳入云同步(并集合并)
- 60s轮询同步刷新自选
- fix: ref(箭头函数)不执行致.map报错 -> 显式调用取初值

验证: 切换/校验/增删/切回最热10条/云键写入 ✓, 视觉整齐 ✓, 控制台无报错 ✓

CHANGELOG记录
This commit is contained in:
personal82555 committed 2026-10-13 23:00:30 +08:00
1 parent f5862778e8
commit 5301a8c840
3 files changed
+231 -15

No files matched your search

+9
View File
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file. See [standa
## Version 2026.10.12-it-tools-pro
### 📇 股票列新增「自选」选项卡
- 「当天最热股票」列顶部加 **最热 / 自选** 选项卡
- 自选面板:输入 6 位代码即可添加(校验位数/重复),行内显示 名称·代码·涨跌幅 + 现价,✕ 一键移出;**A股红涨绿跌**着色
- 行情走新端点 `GET /relay/stock/quotes?codes=...`(东方财富批量行情,20s 缓存,非法代码自动过滤)
- 自选列表存 `it-tools-my-stocks` 并纳入云同步(并集合并,换电脑登录即恢复)
- 交易时段首页 60s 轮询时同步刷新自选行情
- 修复:`ref(箭头函数)` 不会执行函数导致 `.map` 报错(改为显式调用取初值)
- 验证:选项卡切换/非法代码校验/添加 2 只/删除/切回最热 10 条/云同步键写入 ✓,视觉布局整齐 ✓,控制台无报错 ✓
### 📈 「当天最热股票」实时更新
- 后端:交易时段(周一至周五 9:15-15:05 北京时间)`/news` 按需重抓股票列,**60 秒节流**;其余 4 列仍每天 08:00 更新
- 前端:首页可见时**每 60 秒自动轮询** `/relay/news`,拿到最新涨幅;标签页隐藏时暂停
+2 -1
View File
@@ -8,6 +8,7 @@ const SYNC_KEYS = [
'menu-tool-option:collapsed-categories',
'it-tools-ai-chat-convs',
'it-tools-ai-chat-deleted', // 对话删除墓碑
'it-tools-my-stocks', // 自选股
];
function collectLocal(): Record<string, any> {
@@ -131,7 +132,7 @@ function mergeData(server: Record<string, any>, mine: Record<string, any>): Reco
}
else if (typeof v === 'string' && typeof out[k] === 'string') {
// prefer the larger/newer list per key
if (k === 'favoriteToolsName' || k === 'it-tools-recent-tools' || k === 'it-tools-bookmarks:sites') {
if (k === 'favoriteToolsName' || k === 'it-tools-recent-tools' || k === 'it-tools-bookmarks:sites' || k === 'it-tools-my-stocks') {
out[k] = mergeJsonArrays(out[k], v, k === 'it-tools-recent-tools' ? 'path' : null);
}
else if (k === 'it-tools-ai-chat-convs') {
+220 -14
View File
@@ -63,13 +63,88 @@ onMounted(() => {
loadNews();
// 页面可见时每 60s 拉一次,拿到刷新后的股票列(非交易时段 relay 不重抓,开销可忽略)
newsTimer = setInterval(() => {
if (document.visibilityState === 'visible') loadNews();
if (document.visibilityState !== 'visible') return;
loadNews();
if (stockTab.value === 'mine') loadMyQuotes();
}, 60000);
});
onUnmounted(() => {
if (newsTimer) clearInterval(newsTimer);
});
// ===== 股票列选项卡:最热 / 自选(自选实时行情,登录后随云同步)=====
const MY_STOCKS_KEY = 'it-tools-my-stocks';
const stockTab = ref<'hot' | 'mine'>('hot');
const stockInput = ref('');
const stockErr = ref('');
const myQuotes = ref<any[]>([]);
// 注意:ref(函数) 不会执行函数(那是 computed 的语义),必须显式调用取初值
function loadMyStocksList(): string[] {
try {
const a = JSON.parse(localStorage.getItem(MY_STOCKS_KEY) || '[]');
return Array.isArray(a) ? a : [];
}
catch { return []; }
}
const myStocks = ref<string[]>(loadMyStocksList());
function saveMyStocks() {
localStorage.setItem(MY_STOCKS_KEY, JSON.stringify(myStocks.value));
loadMyQuotes();
}
async function loadMyQuotes() {
if (!myStocks.value.length) {
myQuotes.value = [];
return;
}
try {
const res = await fetch('/relay/stock/quotes?codes=' + myStocks.value.join(','));
const j = await res.json();
if (j.code === 200 && Array.isArray(j.data?.items)) myQuotes.value = j.data.items;
}
catch { /* 网络失败保留上次行情 */ }
}
function addMyStock() {
const code = stockInput.value.trim();
if (!/^\d{6}$/.test(code)) {
stockErr.value = '请输入 6 位股票代码';
return;
}
if (myStocks.value.includes(code)) {
stockErr.value = '该股票已在自选中';
return;
}
myStocks.value = [...myStocks.value, code];
stockInput.value = '';
stockErr.value = '';
saveMyStocks();
}
function removeMyStock(code: string) {
myStocks.value = myStocks.value.filter(c => c !== code);
saveMyStocks();
}
function switchStockTab(t: 'hot' | 'mine') {
stockTab.value = t;
if (t === 'mine') loadMyQuotes();
}
// 按自选顺序渲染;行情没返回的代码给占位(仍可删除)
const myList = computed(() => myStocks.value.map((code) => {
const q = myQuotes.value.find(x => x.code === code);
return q || {
code,
name: code,
title: `${code} 暂无数据`,
desc: '',
pct: 0,
url: `https://quote.eastmoney.com/${code}.html`,
};
}));
// map recent paths to tool objects (newest first, cap 8)
const recentToolList = computed(() => {
const byPath = new Map(toolStore.tools.map(t => [t.path, t]));
@@ -114,20 +189,43 @@ function onUpdateFavoriteTools() {
<span class="news-col-icon">{{ col.icon }}</span>
<span class="news-col-title">{{ col.title }}</span>
</div>
<div v-if="col.key === 'stock'" class="stock-tabs">
<button class="stock-tab" :class="{ on: stockTab === 'hot' }" @click="switchStockTab('hot')">最热</button>
<button class="stock-tab" :class="{ on: stockTab === 'mine' }" @click="switchStockTab('mine')">自选<template v-if="myStocks.length">·{{ myStocks.length }}</template></button>
</div>
<div class="news-list">
<a
v-for="(it, i) in col.items"
:key="i"
class="news-item"
:href="it.url"
target="_blank"
rel="noopener noreferrer"
>
<span class="news-title">{{ it.title }}</span>
<span v-if="it.desc" class="news-desc">{{ it.desc }}</span>
<span class="news-time">{{ it.time }}</span>
</a>
<div v-if="!col.items.length" class="news-empty">暂无内容</div>
<!-- 自选面板 -->
<template v-if="col.key === 'stock' && stockTab === 'mine'">
<div class="mystock-add">
<input v-model="stockInput" class="mystock-input" placeholder="6位代码 600519" maxlength="6" @keydown.enter.prevent="addMyStock">
<button class="mystock-btn" @click="addMyStock">添加</button>
</div>
<div v-if="stockErr" class="mystock-err">{{ stockErr }}</div>
<div v-if="!myStocks.length" class="news-empty">输入 6 位代码添加自选股</div>
<div v-for="q in myList" :key="q.code" class="mystock-item">
<a class="mystock-main" :href="q.url" target="_blank" rel="noopener noreferrer">
<span class="news-title" :class="q.pct > 0 ? 'up' : (q.pct < 0 ? 'down' : '')">{{ q.title }}</span>
<span v-if="q.desc" class="news-desc">{{ q.desc }}</span>
</a>
<button class="mystock-x" title="移出自选" @click="removeMyStock(q.code)">✕</button>
</div>
</template>
<!-- 最热(默认) -->
<template v-else>
<a
v-for="(it, i) in col.items"
:key="i"
class="news-item"
:href="it.url"
target="_blank"
rel="noopener noreferrer"
>
<span class="news-title">{{ it.title }}</span>
<span v-if="it.desc" class="news-desc">{{ it.desc }}</span>
<span class="news-time">{{ it.time }}</span>
</a>
<div v-if="!col.items.length" class="news-empty">暂无内容</div>
</template>
</div>
</div>
</div>
@@ -566,6 +664,114 @@ function onUpdateFavoriteTools() {
opacity: 0.45;
padding: 8px 2px;
}
// 股票列:最热 / 自选 选项卡
.stock-tabs {
display: flex;
gap: 6px;
padding: 6px 10px 0;
}
.stock-tab {
flex: 1;
font-size: 11.5px;
padding: 3px 0;
border: 1px solid rgba(0, 0, 0, 0.08);
border-radius: 999px;
background: #fff;
color: rgba(0, 0, 0, 0.55);
cursor: pointer;
transition: all 0.15s;
&.on {
background: rgba(24, 160, 88, 0.12);
border-color: rgba(24, 160, 88, 0.45);
color: #18a058;
font-weight: 700;
}
}
// 自选面板
.mystock-add {
display: flex;
gap: 6px;
padding: 2px 0 6px;
}
.mystock-input {
flex: 1;
min-width: 0;
font-size: 11.5px;
padding: 4px 7px;
border: 1px solid rgba(0, 0, 0, 0.12);
border-radius: 6px;
outline: none;
&:focus {
border-color: #18a058;
}
}
.mystock-btn {
font-size: 11.5px;
padding: 4px 9px;
border: none;
border-radius: 6px;
background: #18a058;
color: #fff;
cursor: pointer;
&:hover {
opacity: 0.88;
}
}
.mystock-err {
font-size: 11px;
color: #d03050;
padding-bottom: 4px;
}
.mystock-item {
display: flex;
align-items: center;
gap: 4px;
padding: 7px 8px;
border-radius: 8px;
&:hover {
background: rgba(0, 0, 0, 0.035);
}
.mystock-main {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 2px;
text-decoration: none;
color: inherit;
}
.news-title {
&.up {
color: #d03050; // A股红涨
}
&.down {
color: #18a058; // 绿跌
}
}
.mystock-x {
flex-shrink: 0;
width: 18px;
height: 18px;
border: none;
border-radius: 50%;
background: transparent;
color: rgba(0, 0, 0, 0.3);
font-size: 11px;
line-height: 1;
cursor: pointer;
&:hover {
background: rgba(208, 48, 80, 0.12);
color: #d03050;
}
}
}
}
.height-enter-active,