feat: 增加博客评论模块
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { IBasicBlog, IBlog } from '@/types/blog.ts'
|
||||
import { IBasicBlog, IBlog, IBlogComment, IBlogNestedComment } from '@/types/blog.ts'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
/**
|
||||
@@ -172,3 +172,24 @@ export const getBasicBlog = (blog: IBlog): IBasicBlog => {
|
||||
}
|
||||
}
|
||||
|
||||
// 扁平化数据转两层嵌套结构
|
||||
export function flattenToTwoLevel(comments: IBlogComment[]): IBlogNestedComment[] {
|
||||
// 创建评论映射表,快速查找评论
|
||||
const commentMap = new Map<number, IBlogComment>()
|
||||
comments.forEach((comment) => commentMap.set(comment.id, comment))
|
||||
|
||||
// 结果数组
|
||||
const result: IBlogNestedComment[] = []
|
||||
|
||||
// 遍历所有评论,构建两层结构
|
||||
comments.forEach((comment) => {
|
||||
if (comment.parentId === 0) {
|
||||
// 顶层评论
|
||||
const replies = comments.filter((reply) => reply.parentId === comment.id)
|
||||
result.push({ comment, replies })
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user