feat: 第二次优化迭代
- Links 后端分页+前端分页器 - 搜索关键词高亮(mark标签) - 搜索防抖200ms,支持描述搜索 - 移除不可用的 type-check 脚本 - 更新 README 项目文档
This commit is contained in:
@@ -120,14 +120,21 @@ const router = (app) => {
|
||||
|
||||
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 { keyword, categoryId, page = '1', pageSize = '20' } = req.query;
|
||||
const p = Math.max(1, Number(page));
|
||||
const ps = Math.min(100, Math.max(1, Number(pageSize)));
|
||||
const offset = (p - 1) * ps;
|
||||
|
||||
let where = '';
|
||||
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 } });
|
||||
if (keyword) { where += ' WHERE (l.title LIKE ? OR l.description LIKE ?)'; params.push(`%${keyword}%`, `%${keyword}%`); }
|
||||
if (categoryId) { where += (where ? ' AND' : ' WHERE') + ' l.category_id = ?'; params.push(Number(categoryId)); }
|
||||
|
||||
const [countRows] = await pool.query(`SELECT COUNT(*) as total FROM links l${where}`, params);
|
||||
const total = Number(countRows[0].total);
|
||||
|
||||
const [rows] = await pool.query(`SELECT l.*, c.name as categoryName FROM links l JOIN categories c ON l.category_id = c.id${where} ORDER BY l.sort, l.id LIMIT ? OFFSET ?`, [...params, ps, offset]);
|
||||
res.json({ status: 200, result: { data: { list: rows, total, page: p, pageSize: ps } } });
|
||||
} catch {
|
||||
res.json({ status: 500, message: '加载失败' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user