feat: 分类层级支持(parent_id)
- 数据库 migration: categories 加 parent_id 字段(幂等) - 后端 API: POST/PUT 支持 parentId 参数 - 前端 Home.vue: 分类树展示,父→子层级渲染 - 前端 Categories.vue: 编辑弹窗加父级选择,表格显示父级 - 前端 Links.vue: 分类下拉显示层级前缀
This commit is contained in:
@@ -53,6 +53,8 @@ export async function initDB() {
|
|||||||
)`);
|
)`);
|
||||||
// 给已存在的 links 表补唯一索引(幂等)
|
// 给已存在的 links 表补唯一索引(幂等)
|
||||||
try { await conn.query("ALTER TABLE links ADD UNIQUE KEY uk_link_url (url)"); } catch { /* 已存在则忽略 */ }
|
try { await conn.query("ALTER TABLE links ADD UNIQUE KEY uk_link_url (url)"); } catch { /* 已存在则忽略 */ }
|
||||||
|
// 给 categories 表补 parent_id 字段(幂等,支持层级分类)
|
||||||
|
try { await conn.query("ALTER TABLE categories ADD COLUMN parent_id INT DEFAULT NULL"); } catch { /* 已存在则忽略 */ }
|
||||||
await conn.query(`
|
await conn.query(`
|
||||||
CREATE TABLE IF NOT EXISTS settings (
|
CREATE TABLE IF NOT EXISTS settings (
|
||||||
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
|||||||
@@ -107,8 +107,8 @@ const router = (app) => {
|
|||||||
|
|
||||||
app.post('/api/admin/categories', authenticate, async (req, res) => {
|
app.post('/api/admin/categories', authenticate, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, icon, sort } = req.body;
|
const { name, icon, sort, parentId } = req.body;
|
||||||
const [result] = await pool.query('INSERT INTO categories (name, icon, sort) VALUES (?, ?, ?)', [name, icon || '🔗', sort || 0]);
|
const [result] = await pool.query('INSERT INTO categories (name, icon, sort, parent_id) VALUES (?, ?, ?, ?)', [name, icon || '🔗', sort || 0, parentId || null]);
|
||||||
res.json({ status: 200, message: '创建成功', result: { data: { id: Number(result.insertId) } } });
|
res.json({ status: 200, message: '创建成功', result: { data: { id: Number(result.insertId) } } });
|
||||||
} catch {
|
} catch {
|
||||||
res.json({ status: 500, message: '创建失败' });
|
res.json({ status: 500, message: '创建失败' });
|
||||||
@@ -117,8 +117,8 @@ const router = (app) => {
|
|||||||
|
|
||||||
app.put('/api/admin/categories/:id', authenticate, async (req, res) => {
|
app.put('/api/admin/categories/:id', authenticate, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, icon, sort } = req.body;
|
const { name, icon, sort, parentId } = req.body;
|
||||||
await pool.query('UPDATE categories SET name = ?, icon = ?, sort = ? WHERE id = ?', [name, icon || '🔗', sort ?? 0, Number(req.params.id)]);
|
await pool.query('UPDATE categories SET name = ?, icon = ?, sort = ?, parent_id = ? WHERE id = ?', [name, icon || '🔗', sort ?? 0, parentId || null, Number(req.params.id)]);
|
||||||
res.json({ status: 200, message: '更新成功' });
|
res.json({ status: 200, message: '更新成功' });
|
||||||
} catch {
|
} catch {
|
||||||
res.json({ status: 500, message: '更新失败' });
|
res.json({ status: 500, message: '更新失败' });
|
||||||
|
|||||||
+104
-18
@@ -42,21 +42,22 @@
|
|||||||
|
|
||||||
<div class="main-content">
|
<div class="main-content">
|
||||||
<div
|
<div
|
||||||
v-for="cat in filteredCategories"
|
v-for="parent in filteredCategories"
|
||||||
:key="cat.id"
|
:key="parent.id"
|
||||||
class="category-section"
|
class="category-section"
|
||||||
>
|
>
|
||||||
<div class="category-header">
|
<div class="category-header">
|
||||||
<span class="icon">{{ cat.icon }}</span>
|
<span class="icon">{{ parent.icon }}</span>
|
||||||
<span class="name">{{ cat.name }}</span>
|
<span class="name">{{ parent.name }}</span>
|
||||||
<span class="count">{{ linksInCat(cat.id).length }}</span>
|
<span v-if="parent.children.length" class="collapse-btn" @click="toggleParent(parent.id)">
|
||||||
<span class="collapse-btn" @click="toggleCat(cat.id)">
|
{{ collapsedParents.has(parent.id) ? '展开' : '收起' }}
|
||||||
{{ collapsedCats.has(cat.id) ? '展开' : '收起' }}
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div v-show="!collapsedCats.has(cat.id)" class="links-grid">
|
<div v-show="!collapsedParents.has(parent.id)">
|
||||||
|
<!-- 父分类下的直属链接 -->
|
||||||
|
<div v-if="linksInCat(parent.id).length" class="links-grid">
|
||||||
<a
|
<a
|
||||||
v-for="link in linksInCat(cat.id)"
|
v-for="link in linksInCat(parent.id)"
|
||||||
:key="link.id"
|
:key="link.id"
|
||||||
:href="link.url"
|
:href="link.url"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
@@ -64,13 +65,7 @@
|
|||||||
class="link-card"
|
class="link-card"
|
||||||
@click="handleVisit(link)"
|
@click="handleVisit(link)"
|
||||||
>
|
>
|
||||||
<img
|
<img v-if="link.iconUrl" :src="link.iconUrl" :alt="link.title" class="link-icon" @error="onIconError($event, link)" />
|
||||||
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 v-else class="link-icon default-icon">{{ link.title[0] }}</div>
|
||||||
<div class="link-info">
|
<div class="link-info">
|
||||||
<div class="link-title" v-html="highlight(link.title, search)"></div>
|
<div class="link-title" v-html="highlight(link.title, search)"></div>
|
||||||
@@ -78,6 +73,40 @@
|
|||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
</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>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -99,6 +128,7 @@ const search = ref('')
|
|||||||
const filteredLinks = ref<any[]>([])
|
const filteredLinks = ref<any[]>([])
|
||||||
const searchTimer = ref<ReturnType<typeof setTimeout>>()
|
const searchTimer = ref<ReturnType<typeof setTimeout>>()
|
||||||
const collapsedCats = ref<Set<number>>(new Set())
|
const collapsedCats = ref<Set<number>>(new Set())
|
||||||
|
const collapsedParents = ref<Set<number>>(new Set())
|
||||||
const showSuggestions = ref(false)
|
const showSuggestions = ref(false)
|
||||||
const suggestions = ref<any[]>([])
|
const suggestions = ref<any[]>([])
|
||||||
let suggestTimer: ReturnType<typeof setTimeout>
|
let suggestTimer: ReturnType<typeof setTimeout>
|
||||||
@@ -109,9 +139,31 @@ function toggleCat(id: number) {
|
|||||||
collapsedCats.value = s
|
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(() => {
|
const filteredCategories = computed(() => {
|
||||||
return data.categories.filter((cat: any) =>
|
return categoryTree.value.filter((p: any) =>
|
||||||
linksInCat(cat.id).length > 0
|
linksInCat(p.id).length > 0 || p.children.some((c: any) => linksInCat(c.id).length > 0)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -314,6 +366,40 @@ loadData()
|
|||||||
.collapse-btn:hover { background: rgba(99,102,241,0.1); }
|
.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 {
|
.links-grid {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
|
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
|
||||||
|
|||||||
@@ -12,6 +12,11 @@
|
|||||||
<el-table :data="categories">
|
<el-table :data="categories">
|
||||||
<el-table-column prop="icon" label="图标" width="70" align="center" />
|
<el-table-column prop="icon" label="图标" width="70" align="center" />
|
||||||
<el-table-column prop="name" label="名称" min-width="120" />
|
<el-table-column prop="name" label="名称" min-width="120" />
|
||||||
|
<el-table-column label="父级" width="100">
|
||||||
|
<template #default="scope">
|
||||||
|
{{ parentName(scope.row.parent_id) }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
<el-table-column prop="sort" label="排序" width="70" align="center" />
|
<el-table-column prop="sort" label="排序" width="70" align="center" />
|
||||||
<el-table-column label="操作" width="120" fixed="right">
|
<el-table-column label="操作" width="120" fixed="right">
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
@@ -36,6 +41,16 @@
|
|||||||
<el-form-item label="名称">
|
<el-form-item label="名称">
|
||||||
<el-input v-model="form.name" />
|
<el-input v-model="form.name" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
<el-form-item label="所属父级">
|
||||||
|
<el-select v-model="form.parentId" placeholder="不选则为顶级分类" clearable filterable>
|
||||||
|
<el-option
|
||||||
|
v-for="p in rootCategories"
|
||||||
|
:key="p.id"
|
||||||
|
:label="`${p.icon || ''} ${p.name}`"
|
||||||
|
:value="p.id"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
</el-form-item>
|
||||||
<el-form-item label="排序">
|
<el-form-item label="排序">
|
||||||
<el-input-number v-model="form.sort" :min="0" />
|
<el-input-number v-model="form.sort" :min="0" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
@@ -57,9 +72,25 @@ import { adminApi } from '@/api'
|
|||||||
const categories = ref([])
|
const categories = ref([])
|
||||||
const dialogVisible = ref(false)
|
const dialogVisible = ref(false)
|
||||||
const editId = ref(null)
|
const editId = ref(null)
|
||||||
const form = ref({ icon: '', name: '', sort: 0 })
|
const form = ref({ icon: '', name: '', sort: 0, parentId: null })
|
||||||
const dialogWidth = computed(() => window.innerWidth < 500 ? '92%' : '420px')
|
const dialogWidth = computed(() => window.innerWidth < 500 ? '92%' : '420px')
|
||||||
|
|
||||||
|
// 顶级分类列表(用于选择父级)
|
||||||
|
const rootCategories = computed(() =>
|
||||||
|
categories.value.filter((c: any) => !c.parent_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
const catMap = computed(() => {
|
||||||
|
const m = new Map<number, any>()
|
||||||
|
categories.value.forEach((c: any) => m.set(c.id, c))
|
||||||
|
return m
|
||||||
|
})
|
||||||
|
|
||||||
|
function parentName(id: number | null) {
|
||||||
|
if (!id) return ''
|
||||||
|
return catMap.value.get(id)?.name || ''
|
||||||
|
}
|
||||||
|
|
||||||
const iconOptions: Record<string, string> = {
|
const iconOptions: Record<string, string> = {
|
||||||
'🌟': '星星',
|
'🌟': '星星',
|
||||||
'🔥': '火',
|
'🔥': '火',
|
||||||
@@ -97,7 +128,7 @@ async function load() {
|
|||||||
|
|
||||||
function openDialog(row?: any) {
|
function openDialog(row?: any) {
|
||||||
editId.value = row?.id || null
|
editId.value = row?.id || null
|
||||||
form.value = row ? { icon: row.icon || '', name: row.name || '', sort: row.sort ?? 0 } : { icon: '', name: '', sort: 0 }
|
form.value = row ? { icon: row.icon || '', name: row.name || '', sort: row.sort ?? 0, parentId: row.parent_id || null } : { icon: '', name: '', sort: 0, parentId: null }
|
||||||
dialogVisible.value = true
|
dialogVisible.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
</el-col>
|
</el-col>
|
||||||
<el-col :xs="24" :sm="12" :md="6">
|
<el-col :xs="24" :sm="12" :md="6">
|
||||||
<el-select v-model="catFilter" placeholder="选择分类" @change="loadLinks" clearable style="width:100%">
|
<el-select v-model="catFilter" placeholder="选择分类" @change="loadLinks" clearable style="width:100%">
|
||||||
<el-option v-for="c in categories" :key="c.id" :label="c.name" :value="c.id" />
|
<el-option v-for="c in categoryOptions" :key="c.value" :label="c.label" :value="c.value" />
|
||||||
</el-select>
|
</el-select>
|
||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
<span>已选 {{ selectedIds.length }} 条</span>
|
<span>已选 {{ selectedIds.length }} 条</span>
|
||||||
<el-button size="small" type="danger" @click="batchDelete">批量删除</el-button>
|
<el-button size="small" type="danger" @click="batchDelete">批量删除</el-button>
|
||||||
<el-select v-model="batchMoveCat" placeholder="移动至..." size="small" style="width:140px;margin-left:8px">
|
<el-select v-model="batchMoveCat" placeholder="移动至..." size="small" style="width:140px;margin-left:8px">
|
||||||
<el-option v-for="c in categories" :key="c.id" :label="c.name" :value="c.id" />
|
<el-option v-for="c in categoryOptions" :key="c.value" :label="c.label" :value="c.value" />
|
||||||
</el-select>
|
</el-select>
|
||||||
<el-button size="small" @click="batchMove" :disabled="!batchMoveCat">移动</el-button>
|
<el-button size="small" @click="batchMove" :disabled="!batchMoveCat">移动</el-button>
|
||||||
<el-button size="small" text @click="selectedIds = []">取消选择</el-button>
|
<el-button size="small" text @click="selectedIds = []">取消选择</el-button>
|
||||||
@@ -84,7 +84,7 @@
|
|||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="分类">
|
<el-form-item label="分类">
|
||||||
<el-select v-model="form.categoryId" placeholder="选择分类" style="width:100%">
|
<el-select v-model="form.categoryId" placeholder="选择分类" style="width:100%">
|
||||||
<el-option v-for="c in categories" :key="c.id" :label="c.name" :value="c.id" />
|
<el-option v-for="c in categoryOptions" :key="c.value" :label="c.label" :value="c.value" />
|
||||||
</el-select>
|
</el-select>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="图标URL">
|
<el-form-item label="图标URL">
|
||||||
@@ -129,6 +129,26 @@ const selectedIds = ref<number[]>([])
|
|||||||
const batchMoveCat = ref<number>()
|
const batchMoveCat = ref<number>()
|
||||||
const dialogWidth = computed(() => window.innerWidth < 500 ? '92%' : '500px')
|
const dialogWidth = computed(() => window.innerWidth < 500 ? '92%' : '500px')
|
||||||
|
|
||||||
|
// 分类选项(含层级前缀)
|
||||||
|
const categoryOptions = computed(() => {
|
||||||
|
const roots = categories.value.filter((c: any) => !c.parent_id)
|
||||||
|
const children = new Map<number, any[]>()
|
||||||
|
categories.value.forEach((c: any) => {
|
||||||
|
if (c.parent_id) {
|
||||||
|
if (!children.has(c.parent_id)) children.set(c.parent_id, [])
|
||||||
|
children.get(c.parent_id)!.push(c)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const opts: { label: string; value: number }[] = []
|
||||||
|
for (const r of roots) {
|
||||||
|
opts.push({ label: `${r.icon || ''} ${r.name}`, value: r.id })
|
||||||
|
for (const child of (children.get(r.id) || [])) {
|
||||||
|
opts.push({ label: ` └ ${child.icon || ''} ${child.name}`, value: child.id })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
})
|
||||||
|
|
||||||
async function loadLinks() {
|
async function loadLinks() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user