201 lines
5.4 KiB
TypeScript
201 lines
5.4 KiB
TypeScript
import type { Request, Response } from 'express';
|
|
import type {
|
|
CommentItem,
|
|
CommentStatus,
|
|
CommentStatusUpdatePayload,
|
|
} from '@/pages/CommentManagement/data';
|
|
|
|
const now = () => new Date().toISOString();
|
|
|
|
const parseString = (value: unknown): string | undefined => {
|
|
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
};
|
|
|
|
const COMMENT_STATUS_LIST: CommentStatus[] = [
|
|
'pending',
|
|
'approved',
|
|
'rejected',
|
|
'spam',
|
|
];
|
|
|
|
const isCommentStatus = (value: unknown): value is CommentStatus => {
|
|
return (
|
|
typeof value === 'string' &&
|
|
COMMENT_STATUS_LIST.includes(value as CommentStatus)
|
|
);
|
|
};
|
|
|
|
let commentList: CommentItem[] = [
|
|
{
|
|
id: 'cmt-1001',
|
|
content: '这篇文章写得很实用,收藏了。',
|
|
articleId: '1',
|
|
articleTitle: '深入浅出 UmiJS 4 核心实战',
|
|
userId: 'u-101',
|
|
userName: '张伟',
|
|
userAvatar: 'https://i.pravatar.cc/80?img=12',
|
|
ipAddress: '10.12.3.8',
|
|
status: 'approved',
|
|
likeCount: 12,
|
|
replyCount: 1,
|
|
createdAt: '2024-02-14T10:18:00.000Z',
|
|
updatedAt: '2024-02-14T10:18:00.000Z',
|
|
},
|
|
{
|
|
id: 'cmt-1002',
|
|
content: '能不能补一段关于路由权限控制的例子?',
|
|
articleId: '1',
|
|
articleTitle: '深入浅出 UmiJS 4 核心实战',
|
|
userId: 'u-102',
|
|
userName: '李娜',
|
|
userAvatar: 'https://i.pravatar.cc/80?img=32',
|
|
ipAddress: '10.12.9.21',
|
|
status: 'pending',
|
|
likeCount: 3,
|
|
replyCount: 0,
|
|
createdAt: '2024-02-14T10:45:00.000Z',
|
|
updatedAt: '2024-02-14T10:45:00.000Z',
|
|
},
|
|
{
|
|
id: 'cmt-1003',
|
|
content: '文章不错,但封面图有点糊。',
|
|
articleId: '2',
|
|
articleTitle: 'Ant Design 5.0 设计趋势解析',
|
|
userId: 'u-103',
|
|
userName: '王强',
|
|
userAvatar: 'https://i.pravatar.cc/80?img=7',
|
|
ipAddress: '10.12.1.66',
|
|
status: 'approved',
|
|
likeCount: 1,
|
|
replyCount: 0,
|
|
createdAt: '2024-02-14T11:05:00.000Z',
|
|
updatedAt: '2024-02-14T11:05:00.000Z',
|
|
},
|
|
{
|
|
id: 'cmt-1004',
|
|
content: '点这里领红包 http://example.com',
|
|
articleId: '2',
|
|
articleTitle: 'Ant Design 5.0 设计趋势解析',
|
|
userId: 'u-104',
|
|
userName: '匿名用户',
|
|
ipAddress: '10.9.20.11',
|
|
status: 'spam',
|
|
likeCount: 0,
|
|
replyCount: 0,
|
|
createdAt: '2024-02-14T11:12:00.000Z',
|
|
updatedAt: '2024-02-14T11:12:00.000Z',
|
|
},
|
|
{
|
|
id: 'cmt-1005',
|
|
content: '这条评论包含不当内容。',
|
|
articleId: '1',
|
|
articleTitle: '深入浅出 UmiJS 4 核心实战',
|
|
userId: 'u-105',
|
|
userName: '赵敏',
|
|
userAvatar: 'https://i.pravatar.cc/80?img=19',
|
|
ipAddress: '10.12.4.2',
|
|
status: 'rejected',
|
|
rejectReason: '包含不当词汇',
|
|
likeCount: 0,
|
|
replyCount: 0,
|
|
createdAt: '2024-02-14T12:02:00.000Z',
|
|
updatedAt: '2024-02-14T12:10:00.000Z',
|
|
},
|
|
];
|
|
|
|
export default {
|
|
'GET /api/comments': (req: Request, res: Response) => {
|
|
const current = Number(req.query.current) || 1;
|
|
const pageSize = Number(req.query.pageSize) || 10;
|
|
const keyword = parseString(req.query.keyword);
|
|
const status = parseString(req.query.status);
|
|
const articleTitle = parseString(req.query.articleTitle);
|
|
const userName = parseString(req.query.userName);
|
|
|
|
let filtered = [...commentList];
|
|
|
|
if (keyword) {
|
|
filtered = filtered.filter(
|
|
(item) =>
|
|
item.content.includes(keyword) ||
|
|
item.articleTitle.includes(keyword) ||
|
|
item.userName.includes(keyword),
|
|
);
|
|
}
|
|
|
|
if (status && isCommentStatus(status)) {
|
|
filtered = filtered.filter((item) => item.status === status);
|
|
}
|
|
|
|
if (articleTitle) {
|
|
filtered = filtered.filter((item) => item.articleTitle.includes(articleTitle));
|
|
}
|
|
|
|
if (userName) {
|
|
filtered = filtered.filter((item) => item.userName.includes(userName));
|
|
}
|
|
|
|
const start = (current - 1) * pageSize;
|
|
const data = filtered.slice(start, start + pageSize);
|
|
|
|
res.json({
|
|
success: true,
|
|
data,
|
|
total: filtered.length,
|
|
});
|
|
},
|
|
|
|
'GET /api/comments/:id': (req: Request, res: Response) => {
|
|
const { id } = req.params;
|
|
const item = commentList.find((comment) => comment.id === id);
|
|
|
|
if (!item) {
|
|
res.status(404).json({ success: false, message: '评论不存在' });
|
|
return;
|
|
}
|
|
|
|
res.json({ success: true, data: item });
|
|
},
|
|
|
|
'PUT /api/comments/:id/status': (req: Request, res: Response) => {
|
|
const { id } = req.params;
|
|
const status = (req.body as any)?.status as unknown;
|
|
const rejectReason = (req.body as any)?.rejectReason as unknown;
|
|
const item = commentList.find((comment) => comment.id === id);
|
|
|
|
if (!item) {
|
|
res.status(404).json({ success: false, message: '评论不存在' });
|
|
return;
|
|
}
|
|
|
|
if (!isCommentStatus(status) || status === 'pending') {
|
|
res.status(400).json({ success: false, message: '状态非法' });
|
|
return;
|
|
}
|
|
|
|
item.status = status;
|
|
item.updatedAt = now();
|
|
|
|
if (status === 'rejected') {
|
|
item.rejectReason = parseString(rejectReason) || '未填写原因';
|
|
} else {
|
|
item.rejectReason = undefined;
|
|
}
|
|
|
|
res.json({ success: true });
|
|
},
|
|
|
|
'DELETE /api/comments/:id': (req: Request, res: Response) => {
|
|
const { id } = req.params;
|
|
const index = commentList.findIndex((comment) => comment.id === id);
|
|
|
|
if (index === -1) {
|
|
res.status(404).json({ success: false, message: '评论不存在' });
|
|
return;
|
|
}
|
|
|
|
commentList.splice(index, 1);
|
|
res.json({ success: true });
|
|
},
|
|
};
|