diff --git a/backend/src/database.js b/backend/src/database.js
index 6e81c8d..ea2de5e 100644
--- a/backend/src/database.js
+++ b/backend/src/database.js
@@ -53,6 +53,8 @@ export async function initDB() {
)`);
// 给已存在的 links 表补唯一索引(幂等)
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(`
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
diff --git a/backend/src/routes/api.js b/backend/src/routes/api.js
index fa456ab..487db8c 100644
--- a/backend/src/routes/api.js
+++ b/backend/src/routes/api.js
@@ -107,8 +107,8 @@ const router = (app) => {
app.post('/api/admin/categories', authenticate, async (req, res) => {
try {
- const { name, icon, sort } = req.body;
- const [result] = await pool.query('INSERT INTO categories (name, icon, sort) VALUES (?, ?, ?)', [name, icon || '🔗', sort || 0]);
+ const { name, icon, sort, parentId } = req.body;
+ 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) } } });
} catch {
res.json({ status: 500, message: '创建失败' });
@@ -117,8 +117,8 @@ const router = (app) => {
app.put('/api/admin/categories/:id', authenticate, async (req, res) => {
try {
- const { name, icon, sort } = req.body;
- await pool.query('UPDATE categories SET name = ?, icon = ?, sort = ? WHERE id = ?', [name, icon || '🔗', sort ?? 0, Number(req.params.id)]);
+ const { name, icon, sort, parentId } = req.body;
+ 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: '更新成功' });
} catch {
res.json({ status: 500, message: '更新失败' });
diff --git a/frontend/src/views/Home.vue b/frontend/src/views/Home.vue
index bb161b5..541cd9c 100644
--- a/frontend/src/views/Home.vue
+++ b/frontend/src/views/Home.vue
@@ -42,41 +42,70 @@
@@ -99,6 +128,7 @@ const search = ref('')
const filteredLinks = ref
([])
const searchTimer = ref>()
const collapsedCats = ref>(new Set())
+const collapsedParents = ref>(new Set())
const showSuggestions = ref(false)
const suggestions = ref([])
let suggestTimer: ReturnType
@@ -109,9 +139,31 @@ function toggleCat(id: number) {
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()
+ 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 data.categories.filter((cat: any) =>
- linksInCat(cat.id).length > 0
+ return categoryTree.value.filter((p: any) =>
+ 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); }
}
+.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));
diff --git a/frontend/src/views/admin/Categories.vue b/frontend/src/views/admin/Categories.vue
index 74df9af..24f7599 100644
--- a/frontend/src/views/admin/Categories.vue
+++ b/frontend/src/views/admin/Categories.vue
@@ -12,6 +12,11 @@
+
+
+ {{ parentName(scope.row.parent_id) }}
+
+
@@ -36,6 +41,16 @@
+
+
+
+
+
@@ -57,9 +72,25 @@ import { adminApi } from '@/api'
const categories = ref([])
const dialogVisible = ref(false)
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 rootCategories = computed(() =>
+ categories.value.filter((c: any) => !c.parent_id)
+)
+
+const catMap = computed(() => {
+ const m = new Map()
+ 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 = {
'🌟': '星星',
'🔥': '火',
@@ -97,7 +128,7 @@ async function load() {
function openDialog(row?: any) {
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
}
diff --git a/frontend/src/views/admin/Links.vue b/frontend/src/views/admin/Links.vue
index 4258a93..dbe0802 100644
--- a/frontend/src/views/admin/Links.vue
+++ b/frontend/src/views/admin/Links.vue
@@ -14,7 +14,7 @@
-
+
@@ -27,7 +27,7 @@
已选 {{ selectedIds.length }} 条
批量删除
-
+
移动
取消选择
@@ -84,7 +84,7 @@
-
+
@@ -129,6 +129,26 @@ const selectedIds = ref([])
const batchMoveCat = ref()
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()
+ 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() {
loading.value = true
try {