Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import Router from '@koa/router'; import { searchService } from './search.service'; const router = new Router(); /** * 搜索音频 * GET /api/search?q=关键词 */ router.get('/', async (ctx) => { try { const { q, limit } = ctx.query as { q?: string; limit?: string }; if (!q || q.trim() === '') { ctx.body = { code: 0, message: 'success', data: [], }; return; } const results = await searchService.searchAudio(q, parseInt(limit || '20')); ctx.body = { code: 0, message: 'success', data: results, }; } catch (error: any) { ctx.body = { code: 400, message: error.message || '搜索失败', }; } }); /** * 获取热门搜索词 * GET /api/search/hot */ router.get('/hot', async (ctx) => { try { const { limit } = ctx.query as { limit?: string }; const hotSearches = await searchService.getHotSearches(parseInt(limit || '10')); ctx.body = { code: 0, message: 'success', data: hotSearches, }; } catch (error: any) { ctx.body = { code: 400, message: error.message || '获取热门搜索失败', }; } }); /** * 获取搜索历史 * GET /api/search/history?userId=1 */ router.get('/history', async (ctx) => { try { const { userId, limit } = ctx.query as { userId?: string; limit?: string }; const userIdNum = parseInt(userId || '0') || 0; const history = await searchService.getSearchHistory(userIdNum, parseInt(limit || '5')); ctx.body = { code: 0, message: 'success', data: history, }; } catch (error: any) { ctx.body = { code: 400, message: error.message || '获取搜索历史失败', }; } }); /** * 保存搜索历史 * POST /api/search/history */ router.post('/history', async (ctx) => { try { const { userId, keyword } = ctx.request.body as { userId?: number; keyword?: string }; if (!keyword || keyword.trim() === '') { ctx.body = { code: 400, message: '关键词不能为空', }; return; } const userIdNum = userId || 0; await searchService.saveSearchHistory(userIdNum, keyword); ctx.body = { code: 0, message: 'success', }; } catch (error: any) { ctx.body = { code: 400, message: error.message || '保存搜索历史失败', }; } }); /** * 删除单条搜索历史 * DELETE /api/search/history?userId=1&keyword=关键词 */ router.delete('/history', async (ctx) => { try { const { userId, keyword } = ctx.query as { userId?: string; keyword?: string }; if (!keyword) { ctx.body = { code: 400, message: '关键词不能为空', }; return; } const userIdNum = parseInt(userId || '0') || 0; await searchService.deleteSearchHistoryItem(userIdNum, keyword); ctx.body = { code: 0, message: 'success', }; } catch (error: any) { ctx.body = { code: 400, message: error.message || '删除搜索历史失败', }; } }); /** * 清空搜索历史 * DELETE /api/search/history/all?userId=1 */ router.delete('/history/all', async (ctx) => { try { const { userId } = ctx.query as { userId?: string }; const userIdNum = parseInt(userId || '0') || 0; await searchService.clearSearchHistory(userIdNum); ctx.body = { code: 0, message: 'success', }; } catch (error: any) { ctx.body = { code: 400, message: error.message || '清空搜索历史失败', }; } }); export default router; |