import { pool } from '../database.js'; import bcrypt from 'bcryptjs'; import jwt from 'jsonwebtoken'; import { authenticate } from '../middleware/auth.js'; const SECRET = process.env.JWT_SECRET || 'yx-nav-secret-2026'; const router = (app) => { // ---- 前端公开接口(首页数据) ---- app.get('/api/site', async (req, res) => { try { const [cats] = await pool.query('SELECT * FROM categories ORDER BY sort, id'); const [links] = await pool.query( 'SELECT l.*, c.name as categoryName FROM links l JOIN categories c ON l.category_id = c.id WHERE l.is_show = 1 ORDER BY c.sort, l.sort, l.id' ); const [sets] = await pool.query('SELECT * FROM settings'); const settings = {}; sets.forEach(s => { settings[s.key] = s.value; }); const linksMapped = links.map(r => ({ ...r, categoryId: r.category_id })); res.json({ status: 200, message: '加载成功', result: { categories: cats, links: linksMapped, settings } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); // ---- 公开接口 ---- app.post('/api/auth/login', async (req, res) => { try { const { username, password } = req.body; const [users] = await pool.query('SELECT * FROM users WHERE username = ?', [username]); if (!users.length || !bcrypt.compareSync(password, users[0].password)) { return res.json({ status: 401, message: '用户名或密码错误' }); } const user = users[0]; const token = jwt.sign({ userId: user.id }, SECRET, { expiresIn: '7d' }); res.json({ status: 200, message: '登录成功', result: { token, user: { id: user.id, nickname: user.nickname } } }); } catch { res.json({ status: 401, message: '登录失败' }); } }); app.get('/api/site', async (req, res) => { try { const [cats] = await pool.query('SELECT * FROM categories ORDER BY sort, id'); const [links] = await pool.query('SELECT l.*, c.name as categoryName FROM links l JOIN categories c ON l.category_id = c.id WHERE l.is_show = 1 ORDER BY c.sort, l.sort, l.id'); const [sets] = await pool.query('SELECT * FROM settings'); const settings = {}; sets.forEach(s => { settings[s.key] = s.value; }); const linksMapped = links.map(r => ({ ...r, categoryId: r.category_id })); res.json({ status: 200, message: '加载成功', result: { categories: cats, links: linksMapped, settings } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); app.get('/api/site/stats', async (req, res) => { try { const [cats] = await pool.query('SELECT c.*, COUNT(l.id) as linkCount FROM categories c LEFT JOIN links l ON c.id = l.category_id AND l.is_show = 1 GROUP BY c.id ORDER BY c.sort, c.id'); const [sets] = await pool.query('SELECT * FROM settings'); const settings = {}; sets.forEach(s => { settings[s.key] = s.value; }); res.json({ status: 200, result: { data: { categories: cats, settings } } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); app.post('/api/links/:id/view', async (req, res) => { try { await pool.query('UPDATE links SET views = views + 1 WHERE id = ?', [Number(req.params.id)]); res.json({ status: 200, result: { data: {} } }); } catch { res.json({ status: 500, message: '更新失败' }); } }); // ---- 后台管理接口 ---- app.get('/api/admin/profile', authenticate, async (req, res) => { try { const [users] = await pool.query('SELECT id, username, nickname, role FROM users WHERE id = ?', [req.user.id]); res.json({ status: 200, result: { data: users[0] } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); app.put('/api/admin/profile', authenticate, async (req, res) => { try { await pool.query('UPDATE users SET nickname = ? WHERE id = ?', [req.body.nickname, req.user.id]); res.json({ status: 200, message: '更新成功' }); } catch { res.json({ status: 500, message: '更新失败' }); } }); app.get('/api/admin/categories', authenticate, async (req, res) => { try { const [rows] = await pool.query('SELECT * FROM categories ORDER BY sort, id'); res.json({ status: 200, result: { data: rows } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); 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]); res.json({ status: 200, message: '创建成功', result: { data: { id: Number(result.insertId) } } }); } catch { res.json({ status: 500, message: '创建失败' }); } }); 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, Number(req.params.id)]); res.json({ status: 200, message: '更新成功' }); } catch { res.json({ status: 500, message: '更新失败' }); } }); app.delete('/api/admin/categories/:id', authenticate, async (req, res) => { try { await pool.query('DELETE FROM categories WHERE id = ?', [Number(req.params.id)]); res.json({ status: 200, message: '删除成功' }); } catch { res.json({ status: 500, message: '删除失败' }); } }); app.get('/api/admin/links', authenticate, async (req, res) => { try { const { keyword, categoryId } = req.query; let sql = 'SELECT l.*, c.name as categoryName FROM links l JOIN categories c ON l.category_id = c.id'; const params = []; if (keyword) { sql += ' WHERE l.title LIKE ? OR l.description LIKE ?'; params.push(`%${keyword}%`, `%${keyword}%`); } if (categoryId) { sql += (keyword ? ' AND' : ' WHERE') + ' l.category_id = ?'; params.push(Number(categoryId)); } sql += ' ORDER BY l.sort, l.id'; const [rows] = await pool.query(sql, params); res.json({ status: 200, result: { data: rows } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); app.post('/api/admin/links', authenticate, async (req, res) => { try { const { title, url, categoryId, description, iconUrl, sort, isShow } = req.body; const [result] = await pool.query('INSERT INTO links (title, url, category_id, description, icon_url, sort, is_show) VALUES (?, ?, ?, ?, ?, ?, ?)', [title, url, Number(categoryId), description || '', iconUrl || '', sort || 0, isShow !== false ? 1 : 0]); res.json({ status: 200, message: '创建成功', result: { data: { id: Number(result.insertId) } } }); } catch { res.json({ status: 500, message: '创建失败' }); } }); app.put('/api/admin/links/:id', authenticate, async (req, res) => { try { const { title, url, categoryId, description, iconUrl, sort, isShow } = req.body; await pool.query('UPDATE links SET title=?, url=?, category_id=?, description=?, icon_url=?, sort=?, is_show=? WHERE id=?', [title, url, Number(categoryId), description, iconUrl, sort, isShow !== false ? 1 : 0, Number(req.params.id)]); res.json({ status: 200, message: '更新成功' }); } catch { res.json({ status: 500, message: '更新失败' }); } }); app.delete('/api/admin/links/:id', authenticate, async (req, res) => { try { await pool.query('DELETE FROM links WHERE id = ?', [Number(req.params.id)]); res.json({ status: 200, message: '删除成功' }); } catch { res.json({ status: 500, message: '删除失败' }); } }); app.put('/api/admin/links/sort', authenticate, async (req, res) => { try { for (const { id, sort } of (req.body.sorts || [])) { await pool.query('UPDATE links SET sort = ? WHERE id = ?', [sort, Number(id)]); } res.json({ status: 200, message: '排序成功' }); } catch { res.json({ status: 500, message: '排序失败' }); } }); app.get('/api/admin/settings', authenticate, async (req, res) => { try { const [sets] = await pool.query('SELECT * FROM settings'); const settings = {}; sets.forEach(s => { settings[s.key] = s.value; }); res.json({ status: 200, result: { data: settings } }); } catch { res.json({ status: 500, message: '加载失败' }); } }); app.put('/api/admin/settings', authenticate, async (req, res) => { try { for (const [key, value] of Object.entries(req.body)) { await pool.query('INSERT INTO settings (key, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)', [key, value]); } res.json({ status: 200, message: '保存成功' }); } catch { res.json({ status: 500, message: '保存失败' }); } }); }; export default router;