Files
yx-nav/frontend/src/views/Home.vue
T
Agent 0d3153f29e feat: 商汤科技额度状态显示(后端代理+前端绿点/红点)
- 后端: /api/proxy/sensenova/status 代理查询API Key有效性
- 前端: 商汤卡片下方显示绿点(有效)/红点(无效)
- 5分钟缓存+前端自动轮询
- API Key通过后台设置页面配置
2026-07-18 00:31:16 +08:00

540 lines
16 KiB
Vue

<template>
<div class="home">
<div class="header">
<div class="header-content">
<div class="brand">
<span class="logo">{{ data.settings.site_name || '导航' }}</span>
<p class="desc">{{ data.settings.site_description }}</p>
</div>
<a href="#/admin" class="admin-link">
<el-icon><Setting /></el-icon>
后台管理
</a>
<el-button class="dark-toggle" @click="darkToggle" :icon="isDark ? 'Sunny' : 'Moon'" circle size="small" />
<el-input
v-model="search"
placeholder="搜索网址..."
class="search-box"
@input="onSearchInput"
@focus="showSuggestions = true"
@blur="hideSuggestions"
>
<template #prefix>
<el-icon><Search /></el-icon>
</template>
</el-input>
<div v-if="showSuggestions && suggestions.length > 0" class="suggestions-dropdown">
<div
v-for="s in suggestions"
:key="s.id"
class="suggestion-item"
@mousedown.prevent="jumpTo(s.url)"
>
<span class="sug-icon">{{ s.title[0] }}</span>
<div class="sug-info">
<span class="sug-title" v-html="highlight(s.title, search)"></span>
<span class="sug-cat">{{ s.categoryName }}</span>
</div>
</div>
</div>
</div>
</div>
<div class="main-content">
<div
v-for="parent in filteredCategories"
:key="parent.id"
class="category-section"
>
<div class="category-header">
<span class="icon">{{ parent.icon }}</span>
<span class="name">{{ parent.name }}</span>
<span v-if="parent.children.length" class="collapse-btn" @click="toggleParent(parent.id)">
{{ collapsedParents.has(parent.id) ? '展开' : '收起' }}
</span>
</div>
<div v-show="!collapsedParents.has(parent.id)">
<!-- 父分类下的直属链接 -->
<div v-if="linksInCat(parent.id).length" class="links-grid">
<a
v-for="link in linksInCat(parent.id)"
:key="link.id"
:href="link.url"
target="_blank"
rel="noopener noreferrer"
class="link-card"
@click="handleVisit(link)"
>
<img v-if="link.iconUrl" :src="link.iconUrl" :alt="link.title" class="link-icon" @error="onIconError($event, link)" />
<div v-else class="link-icon default-icon">{{ link.title[0] }}</div>
<div class="link-info">
<div class="link-title" v-html="highlight(link.title, search)"></div>
<div class="link-desc" v-html="highlight(link.description, search)"></div>
</div>
</a>
<!-- 商汤科技额度状态 -->
<div v-if="link.url && link.url.includes('sensenova') && sensenovaStatus" class="sensenova-status">
<span :class="'status-dot ' + (sensenovaStatus.valid ? 'online' : 'offline')"></span>
<span class="status-text">{{ sensenovaStatus.message }}</span>
</div>
</div>
<!-- 子分类 -->
<div
v-for="child in parent.children"
:key="child.id"
class="sub-category"
>
<div class="sub-category-header">
<span class="icon">{{ child.icon }}</span>
<span class="name">{{ child.name }}</span>
<span class="count">{{ linksInCat(child.id).length }}</span>
<span class="collapse-btn" @click="toggleCat(child.id)">
{{ collapsedCats.has(child.id) ? '展开' : '收起' }}
</span>
</div>
<div v-show="!collapsedCats.has(child.id)" class="links-grid">
<a
v-for="link in linksInCat(child.id)"
:key="link.id"
:href="link.url"
target="_blank"
rel="noopener noreferrer"
class="link-card"
@click="handleVisit(link)"
>
<img v-if="link.iconUrl" :src="link.iconUrl" :alt="link.title" class="link-icon" @error="onIconError($event, link)" />
<div v-else class="link-icon default-icon">{{ link.title[0] }}</div>
<div class="link-info">
<div class="link-title" v-html="highlight(link.title, search)"></div>
<div class="link-desc" v-html="highlight(link.description, search)"></div>
</div>
</a>
<!-- 商汤科技额度状态 -->
<div v-if="link.url && link.url.includes('sensenova') && sensenovaStatus" class="sensenova-status">
<span :class="'status-dot ' + (sensenovaStatus.valid ? 'online' : 'offline')"></span>
<span class="status-text">{{ sensenovaStatus.message }}</span>
</div>
</div>
</div>
</div>
</div>
</div>
<footer class="footer">{{ data.settings.footer_text }}</footer>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { Search, Setting } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { siteApi } from '@/api'
import { useDarkMode } from '@/composables/useDarkMode'
const { isDark, toggle: darkToggle } = useDarkMode()
const data = reactive({ categories: [] as any[], settings: {} as any, links: [] as any[] })
const search = ref('')
const filteredLinks = ref<any[]>([])
const searchTimer = ref<ReturnType<typeof setTimeout>>()
const collapsedCats = ref<Set<number>>(new Set())
const collapsedParents = ref<Set<number>>(new Set())
const showSuggestions = ref(false)
const suggestions = ref<any[]>([])
let suggestTimer: ReturnType<typeof setTimeout>
function toggleCat(id: number) {
const s = new Set(collapsedCats.value)
if (s.has(id)) s.delete(id); else s.add(id)
collapsedCats.value = s
}
function toggleParent(id: number) {
const s = new Set(collapsedParents.value)
if (s.has(id)) s.delete(id); else s.add(id)
collapsedParents.value = s
}
// 构建分类树
const categoryTree = computed(() => {
const cats = data.categories
const children = new Map<number, any[]>()
const roots: any[] = []
for (const c of cats) {
if (c.parent_id) {
if (!children.has(c.parent_id)) children.set(c.parent_id, [])
children.get(c.parent_id)!.push(c)
} else {
roots.push(c)
}
}
return roots.map(p => ({ ...p, children: children.get(p.id) || [] }))
})
const filteredCategories = computed(() => {
return categoryTree.value.filter((p: any) =>
linksInCat(p.id).length > 0 || p.children.some((c: any) => linksInCat(c.id).length > 0)
)
})
function linksInCat(catId: number) {
return filteredLinks.value.filter((l: any) => l.categoryId === catId)
}
function filterLinks() {
const q = search.value.trim().toLowerCase()
filteredLinks.value = q
? data.links.filter((l: any) => l.title.toLowerCase().includes(q) || l.description.toLowerCase().includes(q))
: [...data.links]
}
function onSearchInput() {
filterLinks()
clearTimeout(suggestTimer)
const q = search.value.trim().toLowerCase()
if (q.length < 1) { suggestions.value = []; return }
suggestTimer = setTimeout(() => {
suggestions.value = data.links
.filter((l: any) => l.title.toLowerCase().includes(q) || l.description.toLowerCase().includes(q))
.slice(0, 6)
}, 100)
}
function hideSuggestions() {
setTimeout(() => { showSuggestions.value = false }, 200)
}
function jumpTo(url: string) {
window.open(url, '_blank')
}
function highlight(text: string, keyword: string) {
if (!keyword.trim()) return text
const idx = text.toLowerCase().indexOf(keyword.toLowerCase())
if (idx === -1) return text
return text.slice(0, idx) + '<mark>' + text.slice(idx, idx + keyword.length) + '</mark>' + text.slice(idx + keyword.length)
}
async function loadData() {
try {
const res = await siteApi.getIndex()
const { categories, links, settings } = res.result
data.categories = categories
data.links = links
data.settings = settings
filteredLinks.value = links
} catch {
ElMessage.error('加载失败')
}
}
// 商汤科技额度状态
const sensenovaStatus = ref<any>(null)
async function fetchSensenovaStatus() {
try {
const res = await fetch('/api/proxy/sensenova/status')
const json = await res.json()
if (json.status === 200) {
sensenovaStatus.value = json.result
}
} catch { /* ignore */ }
}
fetchSensenovaStatus()
// 每5分钟刷新
setInterval(fetchSensenovaStatus, 5 * 60 * 1000)
function handleVisit(link: any) {
siteApi.addView(link.id)
}
function onIconError(e: any, link: any) {
e.target.style.display = 'none'
link.iconUrl = ''
}
loadData()
</script>
<style scoped lang="scss">
.home {
min-height: 100vh;
background: linear-gradient(135deg, #f5f7fa 0%, #e4e9f0 100%);
}
.header {
background: white;
box-shadow: 0 2px 12px rgba(0,0,0,0.08);
position: sticky;
top: 0;
z-index: 10;
.header-content {
max-width: 1200px;
margin: 0 auto;
padding: 24px 32px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
position: relative;
}
.brand {
.logo {
font-size: 28px;
font-weight: 700;
background: linear-gradient(135deg, #6366f1, #818cf8);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.desc { font-size: 14px; color: #888; margin-top: 4px; }
}
.search-box { width: 320px; }
.suggestions-dropdown {
position: absolute;
top: 100%;
right: 0;
width: 320px;
background: white;
border-radius: 8px;
box-shadow: 0 8px 24px rgba(0,0,0,0.12);
z-index: 20;
overflow: hidden;
margin-top: 4px;
}
.suggestion-item {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 14px;
cursor: pointer;
transition: background 0.15s;
}
.suggestion-item:hover { background: #f5f7fa; }
.sug-icon {
width: 28px;
height: 28px;
border-radius: 6px;
background: linear-gradient(135deg, #6366f1, #818cf8);
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: 600;
flex-shrink: 0;
}
.sug-info { display: flex; flex-direction: column; min-width: 0; }
.sug-title { font-size: 14px; font-weight: 500; color: #333; }
.sug-cat { font-size: 11px; color: #888; margin-top: 2px; }
html.dark .suggestions-dropdown { background: var(--bg-card); }
html.dark .suggestion-item:hover { background: var(--hover-bg); }
html.dark .sug-title { color: var(--text-primary); }
html.dark .sug-cat { color: var(--text-secondary); }
.admin-link {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 8px 16px;
font-size: 14px;
color: #6366f1;
text-decoration: none;
border: 1px solid #6366f1;
border-radius: 8px;
transition: all 0.2s;
white-space: nowrap;
&:hover {
background: #6366f1;
color: white;
}
.el-icon { font-size: 16px; }
}
}
.main-content {
max-width: 1200px;
margin: 0 auto;
padding: 32px;
}
.category-section { margin-bottom: 40px; }
.category-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 16px;
padding-bottom: 10px;
border-bottom: 2px solid #e5e7eb;
.icon { font-size: 20px; }
.name { font-size: 18px; font-weight: 600; }
.count {
font-size: 12px;
color: #888;
background: #f0f0f0;
padding: 2px 10px;
border-radius: 12px;
}
.collapse-btn {
margin-left: auto;
font-size: 12px;
color: #6366f1;
cursor: pointer;
padding: 2px 8px;
border-radius: 4px;
user-select: none;
}
.collapse-btn:hover { background: rgba(99,102,241,0.1); }
}
.sub-category {
margin-left: 20px;
margin-bottom: 20px;
}
.sub-category-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
padding-bottom: 6px;
border-bottom: 1px solid #e5e7eb;
.icon { font-size: 16px; }
.name { font-size: 15px; font-weight: 500; color: #555; }
.count {
font-size: 11px;
color: #888;
background: #f0f0f0;
padding: 1px 8px;
border-radius: 10px;
}
.collapse-btn {
margin-left: auto;
font-size: 12px;
color: #6366f1;
cursor: pointer;
padding: 2px 8px;
border-radius: 4px;
user-select: none;
}
.collapse-btn:hover { background: rgba(99,102,241,0.1); }
}
.links-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: 16px;
}
.link-card {
display: flex;
align-items: center;
gap: 12px;
background: white;
padding: 16px;
border-radius: 12px;
text-decoration: none;
color: inherit;
box-shadow: 0 1px 4px rgba(0,0,0,0.06);
transition: all 0.2s;
&:hover {
transform: translateY(-2px);
box-shadow: 0 6px 16px rgba(99,102,241,0.15);
}
.link-icon {
width: 40px;
height: 40px;
border-radius: 10px;
object-fit: cover;
flex-shrink: 0;
&.default-icon {
background: linear-gradient(135deg, #6366f1, #818cf8);
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: 600;
font-size: 16px;
}
}
.link-info { flex: 1; min-width: 0; }
.link-title { font-weight: 500; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-title :deep(mark) { background: #fef08a; color: #333; padding: 0 2px; border-radius: 2px; }
.link-desc { font-size: 12px; color: #888; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-desc :deep(mark) { background: #fef08a; color: #333; padding: 0 2px; border-radius: 2px; }
}
.footer {
text-align: center;
padding: 24px;
color: #aaa;
font-size: 13px;
}
@media (max-width: 767px) {
.header {
.header-content {
padding: 16px;
flex-wrap: wrap;
gap: 12px;
}
.brand {
.logo { font-size: 22px; }
.desc { display: none; }
}
.search-box { width: 100%; order: 10; }
.admin-link {
padding: 6px 12px;
font-size: 13px;
}
.dark-toggle { display: none; }
}
.main-content { padding: 16px; }
.links-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.link-card { padding: 12px; }
.category-header .name { font-size: 16px; }
.category-section { margin-bottom: 24px; }
}
/* 暗色模式 */
html.dark .home { background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); }
html.dark .header { background: var(--header-bg); box-shadow: 0 2px 12px rgba(0,0,0,0.3); }
html.dark .link-card { background: var(--bg-card); box-shadow: 0 1px 4px rgba(0,0,0,0.2); }
html.dark .link-card:hover { box-shadow: 0 6px 16px rgba(99,102,241,0.3); }
html.dark .link-desc { color: #64748b; }
html.dark .category-header { border-bottom-color: var(--border-color); }
html.dark .category-header .count { background: #334155; color: #94a3b8; }
html.dark .footer { color: #475569; }
html.dark .dark-toggle { color: #fbbf24 !important; }
/* 商汤科技额度状态 */
.sensenova-status {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 12px 8px 12px;
font-size: 11px;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
flex-shrink: 0;
}
.status-dot.online { background: #22c55e; box-shadow: 0 0 6px rgba(34,197,94,0.5); }
.status-dot.offline { background: #ef4444; box-shadow: 0 0 6px rgba(239,68,68,0.5); }
.status-text { color: #64748b; }
html.dark .status-text { color: #94a3b8; }
</style>