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 表补唯一索引(幂等)
|
||||
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,
|
||||
|
||||
@@ -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: '更新失败' });
|
||||
|
||||
Reference in New Issue
Block a user