Files
yx-nav/frontend/src/views/Home.vue
T
Agent cf12869614 feat: 第二次优化迭代
- Links 后端分页+前端分页器
- 搜索关键词高亮(mark标签)
- 搜索防抖200ms,支持描述搜索
- 移除不可用的 type-check 脚本
- 更新 README 项目文档
2026-07-14 21:34:02 +08:00

293 lines
7.2 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-input
v-model="search"
placeholder="搜索网址..."
class="search-box"
@input="filterLinks"
>
<template #prefix>
<el-icon><Search /></el-icon>
</template>
</el-input>
</div>
</div>
<div class="main-content">
<div
v-for="cat in filteredCategories"
:key="cat.id"
class="category-section"
>
<div class="category-header">
<span class="icon">{{ cat.icon }}</span>
<span class="name">{{ cat.name }}</span>
<span class="count">{{ linksInCat(cat.id).length }}</span>
</div>
<div class="links-grid">
<a
v-for="link in linksInCat(cat.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>
</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'
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 filteredCategories = computed(() => {
return data.categories.filter((cat: any) =>
linksInCat(cat.id).length > 0
)
})
function linksInCat(catId: number) {
return filteredLinks.value.filter((l: any) => l.categoryId === catId)
}
function filterLinks() {
if (searchTimer.value) clearTimeout(searchTimer.value)
searchTimer.value = setTimeout(() => {
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]
}, 200)
}
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('加载失败')
}
}
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;
}
.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; }
.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;
}
}
.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;
}
}
.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; }
}
</style>