meisiyu | 1d4aade | 2025-06-02 20:10:36 +0800 | [diff] [blame] | 1 | import { Request, Response } from 'express'; |
| 2 | |
| 3 | // Mock数据 |
| 4 | const mockPosts = [ |
| 5 | { |
| 6 | postId: 1, |
| 7 | title: '【日剧推荐】献给刚刚接触日剧的你', |
| 8 | content: ` |
| 9 | <div class="post-content"> |
| 10 | <p>在日本,并没有四大台柱的称谓,这一说法的由来是贴吧的一个投票。</p> |
| 11 | <h3>校园剧</h3> |
| 12 | <p>龙樱 - 校园青春励志剧,如果你还没参加高考,推荐你提前看看,相信你会有所收获。</p> |
| 13 | <p>野猪大改造 - 两个J家美少年和两个台柱,除第一集节奏稍慢外,无硬伤,有友情有效笑有泪水。</p> |
| 14 | <h3>职场剧</h3> |
| 15 | <p>麻辣教师 - 小栗旬还是个弱小男生。</p> |
| 16 | <p>极道鲜师 - 学生上课搞乱不听讲怎么办,小栗来教你。</p> |
| 17 | </div> |
| 18 | `, |
| 19 | summary: '这是一篇关于日剧推荐的帖子,包含了校园剧和职场剧的推荐。', |
| 20 | coverImage: '/images/flower.jpg', |
| 21 | authorId: 1, |
| 22 | author: 'Ryo', |
| 23 | views: 87951, |
| 24 | comments: 15, |
| 25 | favorites: 256, |
| 26 | likes: 1024, |
| 27 | status: '1', |
| 28 | publishTime: '2017年8月26日', |
| 29 | tags: '日剧,嘉宾专栏,作品合集', |
| 30 | promotionPlanId: 1, |
| 31 | createTime: '2017-08-26 10:30:00', |
| 32 | updateTime: '2017-08-26 10:30:00', |
| 33 | }, |
| 34 | { |
| 35 | postId: 2, |
| 36 | title: '【2025年4月档】对岸的家务事 高清1080P 新番双语+中文字幕 百度网盘 更新07', |
| 37 | content: '<p>对岸的家务事是一部关于家庭生活的日剧。</p>', |
| 38 | summary: '对岸的家务事是一部关于家庭生活的日剧。', |
| 39 | coverImage: '/images/flower.jpg', |
| 40 | authorId: 2, |
| 41 | author: '日剧翻译组', |
| 42 | views: 3245, |
| 43 | comments: 156, |
| 44 | favorites: 89, |
| 45 | likes: 345, |
| 46 | status: '1', |
| 47 | publishTime: '2025-5-14', |
| 48 | tags: '日剧,2025,家务', |
| 49 | promotionPlanId: null, |
| 50 | createTime: '2025-05-14 10:30:00', |
| 51 | updateTime: '2025-05-14 10:30:00', |
| 52 | }, |
| 53 | { |
| 54 | postId: 3, |
| 55 | title: '【2025年4月档】天久鹰央的推理历表 高清1080P 中文字幕 百度网盘 更新04', |
| 56 | content: '<p>天久鹰央的推理历表是一部推理题材的日剧。</p>', |
| 57 | summary: '天久鹰央的推理历表是一部推理题材的日剧。', |
| 58 | coverImage: '/images/flower.jpg', |
| 59 | authorId: 2, |
| 60 | author: '日剧翻译组', |
| 61 | views: 4589, |
| 62 | comments: 267, |
| 63 | favorites: 123, |
| 64 | likes: 567, |
| 65 | status: '1', |
| 66 | publishTime: '2025-5-14', |
| 67 | tags: '日剧,2025,推理', |
| 68 | promotionPlanId: null, |
| 69 | createTime: '2025-05-14 10:30:00', |
| 70 | updateTime: '2025-05-14 10:30:00', |
| 71 | } |
| 72 | ]; |
| 73 | |
| 74 | const mockComments = [ |
| 75 | { |
| 76 | commentId: 1, |
| 77 | postId: 1, |
| 78 | content: '早期的堀北真希真是太美了', |
| 79 | userId: 10, |
| 80 | userName: 'Inchou', |
| 81 | userAvatar: 'https://via.placeholder.com/40', |
| 82 | parentId: 0, |
| 83 | status: '1', |
| 84 | likes: 5, |
| 85 | createTime: '2025-05-15 05:19:00', |
| 86 | }, |
| 87 | { |
| 88 | commentId: 2, |
| 89 | postId: 1, |
| 90 | content: '谢谢分享 😚', |
| 91 | userId: 11, |
| 92 | userName: '紫毛球', |
| 93 | userAvatar: 'https://via.placeholder.com/40', |
| 94 | parentId: 0, |
| 95 | status: '1', |
| 96 | likes: 3, |
| 97 | createTime: '2025-05-02 10:35:00', |
| 98 | }, |
| 99 | { |
| 100 | commentId: 3, |
| 101 | postId: 1, |
| 102 | content: '感谢分享', |
| 103 | userId: 12, |
| 104 | userName: 'JJxMM666', |
| 105 | userAvatar: 'https://via.placeholder.com/40', |
| 106 | parentId: 0, |
| 107 | status: '1', |
| 108 | likes: 2, |
| 109 | createTime: '2025-05-01 18:54:00', |
| 110 | } |
| 111 | ]; |
| 112 | |
| 113 | export default { |
| 114 | // 获取帖子列表 |
| 115 | 'GET /api/post/list': (req: Request, res: Response) => { |
| 116 | const { pageNum = 1, pageSize = 10, title, status = '1' } = req.query; |
| 117 | |
| 118 | let filteredPosts = mockPosts.filter(post => post.status === status); |
| 119 | |
| 120 | if (title) { |
| 121 | filteredPosts = filteredPosts.filter(post => |
| 122 | post.title.toLowerCase().includes((title as string).toLowerCase()) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | const startIndex = (Number(pageNum) - 1) * Number(pageSize); |
| 127 | const endIndex = startIndex + Number(pageSize); |
| 128 | const paginatedPosts = filteredPosts.slice(startIndex, endIndex); |
| 129 | |
| 130 | res.json({ |
| 131 | code: 200, |
| 132 | rows: paginatedPosts, |
| 133 | total: filteredPosts.length, |
| 134 | msg: '查询成功', |
| 135 | }); |
| 136 | }, |
| 137 | |
| 138 | // 获取帖子详情 |
| 139 | 'GET /api/post/:id': (req: Request, res: Response) => { |
| 140 | const { id } = req.params; |
| 141 | const post = mockPosts.find(p => p.postId === Number(id)); |
| 142 | |
| 143 | if (!post) { |
| 144 | return res.json({ |
| 145 | code: 404, |
| 146 | msg: '帖子不存在', |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | const postComments = mockComments |
| 151 | .filter(c => c.postId === Number(id) && c.parentId === 0) |
| 152 | .map(comment => ({ |
| 153 | comment, |
| 154 | replies: mockComments.filter(c => c.parentId === comment.commentId) |
| 155 | })); |
| 156 | |
| 157 | const authorPosts = mockPosts |
| 158 | .filter(p => p.authorId === post.authorId && p.postId !== post.postId) |
| 159 | .slice(0, 3); |
| 160 | |
| 161 | const similarPosts = mockPosts |
| 162 | .filter(p => { |
| 163 | if (p.postId === post.postId) return false; |
| 164 | const postTags = post.tags.split(','); |
| 165 | const pTags = p.tags.split(','); |
| 166 | return postTags.some(tag => pTags.includes(tag)); |
| 167 | }) |
| 168 | .slice(0, 3); |
| 169 | |
| 170 | res.json({ |
| 171 | code: 200, |
| 172 | data: { |
| 173 | post, |
| 174 | tags: post.tags.split(',').map((tag, index) => ({ |
| 175 | tagId: index + 1, |
| 176 | tagName: tag, |
| 177 | tagColor: 'blue', |
| 178 | postCount: Math.floor(Math.random() * 100) + 1, |
| 179 | status: '0' |
| 180 | })), |
| 181 | comments: postComments, |
| 182 | authorPosts, |
| 183 | similarPosts, |
| 184 | favorited: false |
| 185 | }, |
| 186 | msg: '查询成功', |
| 187 | }); |
| 188 | }, |
| 189 | |
| 190 | // 添加评论 |
| 191 | 'POST /api/post/comment': (req: Request, res: Response) => { |
| 192 | const { postId, content, parentId = 0 } = req.body; |
| 193 | |
| 194 | if (!postId || !content) { |
| 195 | return res.json({ |
| 196 | code: 400, |
| 197 | msg: '参数不完整', |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | const newComment = { |
| 202 | commentId: Date.now(), |
| 203 | postId: Number(postId), |
| 204 | content, |
| 205 | userId: 100, |
| 206 | userName: '当前用户', |
| 207 | userAvatar: 'https://via.placeholder.com/40', |
| 208 | parentId: Number(parentId), |
| 209 | status: '1', |
| 210 | likes: 0, |
| 211 | createTime: new Date().toLocaleString('zh-CN'), |
| 212 | }; |
| 213 | |
| 214 | mockComments.push(newComment); |
| 215 | |
| 216 | // 更新帖子评论数 |
| 217 | const post = mockPosts.find(p => p.postId === Number(postId)); |
| 218 | if (post) { |
| 219 | post.comments += 1; |
| 220 | } |
| 221 | |
| 222 | res.json({ |
| 223 | code: 200, |
| 224 | data: newComment, |
| 225 | msg: '评论成功', |
| 226 | }); |
| 227 | }, |
| 228 | |
| 229 | // 收藏/取消收藏帖子 |
| 230 | 'POST /api/post/favorite/:id': (req: Request, res: Response) => { |
| 231 | const { id } = req.params; |
| 232 | const { favorite } = req.query; |
| 233 | |
| 234 | const post = mockPosts.find(p => p.postId === Number(id)); |
| 235 | if (!post) { |
| 236 | return res.json({ |
| 237 | code: 404, |
| 238 | msg: '帖子不存在', |
| 239 | }); |
| 240 | } |
| 241 | |
| 242 | if (favorite === 'true') { |
| 243 | post.favorites += 1; |
| 244 | res.json({ |
| 245 | code: 200, |
| 246 | msg: '收藏成功', |
| 247 | }); |
| 248 | } else { |
| 249 | post.favorites = Math.max(0, post.favorites - 1); |
| 250 | res.json({ |
| 251 | code: 200, |
| 252 | msg: '取消收藏成功', |
| 253 | }); |
| 254 | } |
| 255 | }, |
| 256 | |
| 257 | // 获取热门标签 |
| 258 | 'GET /api/post/tags/hot': (req: Request, res: Response) => { |
| 259 | const mockTags = [ |
| 260 | { tagId: 1, tagName: '日剧', tagColor: 'blue', postCount: 150, status: '0' }, |
| 261 | { tagId: 2, tagName: '电影', tagColor: 'green', postCount: 120, status: '0' }, |
| 262 | { tagId: 3, tagName: '音乐', tagColor: 'orange', postCount: 80, status: '0' }, |
| 263 | { tagId: 4, tagName: '推理', tagColor: 'purple', postCount: 65, status: '0' }, |
| 264 | { tagId: 5, tagName: '爱情', tagColor: 'pink', postCount: 45, status: '0' }, |
| 265 | ]; |
| 266 | |
| 267 | res.json({ |
| 268 | code: 200, |
| 269 | data: mockTags, |
| 270 | msg: '查询成功', |
| 271 | }); |
| 272 | }, |
| 273 | |
| 274 | // 根据标签获取帖子 |
| 275 | 'GET /api/post/bytag/:tagId': (req: Request, res: Response) => { |
| 276 | const { tagId } = req.params; |
| 277 | |
| 278 | // 简单的mock实现,实际应该根据tagId查询相关帖子 |
| 279 | const filteredPosts = mockPosts.filter(post => |
| 280 | post.tags.includes('日剧') // 假设查询日剧标签 |
| 281 | ); |
| 282 | |
| 283 | res.json({ |
| 284 | code: 200, |
| 285 | rows: filteredPosts, |
| 286 | total: filteredPosts.length, |
| 287 | msg: '查询成功', |
| 288 | }); |
| 289 | }, |
| 290 | }; |