From fdc4403cd71fbf1dbb8878c544a8f10ce9331da1 Mon Sep 17 00:00:00 2001 From: SunWuyuan <1847261658@qq.com> Date: Sat, 5 Oct 2024 20:51:15 +0800 Subject: [PATCH] 1 --- .vscode/settings.json | 5 + server/router_comment.js | 216 +++++++++++++++++---------------------- 2 files changed, 98 insertions(+), 123 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c02512e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "aide.convertLanguagePairs": { + "javascript": "typescript" + } +} \ No newline at end of file diff --git a/server/router_comment.js b/server/router_comment.js index f5db8bb..a799562 100644 --- a/server/router_comment.js +++ b/server/router_comment.js @@ -8,87 +8,95 @@ router.all("*", (req, res, next) => next()); // 统一的错误处理函数 const handleError = (res, err, message) => { console.error(err); - res.status(500).send({ errno: 1, errmsg: message, data: err }); }; +// 检查登录状态 +const checkLogin = (res) => { + if (!res.locals.login) { + return res.status(404).send({ status: "0", msg: "请先登录" }); + } +}; + +// 获取排序条件 +const getSortCondition = (req) => { + const sortBy = req.query.sortBy; + if (sortBy == "insertedAt_desc") return { id: "desc" }; + if (sortBy == "insertedAt_asc") return { id: "asc" }; + if (sortBy == "like_desc") return { like: "desc" }; + return {}; +}; + +// 转换评论数据 +const transformComment = (comments) => { + return comments.map((comment) => { + const time = new Date(comment.insertedAt).getTime(); + const objectId = comment.id; + const browser = (comment.ua ? comment.ua.match(/(Edge|Chrome|Firefox|Safari|Opera)/) : null || ["未知"])[0]; + const os = (comment.ua ? comment.ua.match(/(Windows|Mac|Linux)/) : null || ["未知"])[0]; + const type = comment.id == 1 ? "administrator" : "guest"; + const avatar = "https://owcdn.190823.xyz/user/fcd939e653195bb6d057e8c2519f5cc7"; + const orig = comment.comment; + const ip = comment.ip; + const addr = ""; + + return { + ...comment, + time, + objectId, + browser, + os, + type, + avatar, + orig, + ip, + addr, + }; + }); +}; + // 读取评论 router.get("/api/comment", async (req, res) => { - //if (!res.locals.login) {return res.status(404).send({ status: "0", msg: "请先登录" });} - try { - console.log(req.query.path); - console.log(req.query.page); - console.log(req.query.pageSize); - if (req.query.sortBy == "insertedAt_desc") { - var sort = { - id: "desc", - }; - } else if (req.query.sortBy == "insertedAt_asc") { - var sort = { - id: "asc", - }; - } else if (req.query.sortBy == "like_desc") { - var sort = { - like: "desc", - }; - } + const { path, page, pageSize } = req.query; + const sort = getSortCondition(req); - var comment = await I.prisma.ow_comment.findMany({ - where: { - url: req.query.path, - pid: null, - rid: null, - }, + const comments = await I.prisma.ow_comment.findMany({ + where: { url: path, pid: null, rid: null }, orderBy: sort, - take: Number(req.query.pageSize) || 10, // 每页条数 - skip: (req.query.page - 1) * req.query.pageSize, // 跳过条数 + take: Number(pageSize) || 10, + skip: (page - 1) * pageSize, }); - comment = transformComment(comment); - const ids = comment.map((item) => item.id); - console.log(ids); - - var childrencomment = await I.prisma.ow_comment.findMany({ - where: { - url: req.query.path, - rid: { in: ids }, - }, + + const transformedComments = transformComment(comments); + const ids = transformedComments.map((comment) => comment.id); + + const childrenComments = await I.prisma.ow_comment.findMany({ + where: { url: path, rid: { in: ids } }, }); - childrencomment = transformComment(childrencomment); - //console.log(childrencomment); - const result1 = comment.map((item) => { - // 从 arr2 中筛选 rid 等于 item.id 的项,组成 children 数组 - const children = childrencomment.filter((child) => child.rid === item.id); + const transformedChildrenComments = transformComment(childrenComments); - // 返回包含 children 的新对象 - return { ...item, children }; + const result = transformedComments.map((comment) => { + const children = transformedChildrenComments.filter((child) => child.rid == comment.id); + return { ...comment, children }; }); - console.log(result1); const count = await I.prisma.ow_comment.count({ - where: { - url: req.query.path, - pid: null, - rid: null, - }, + where: { url: path, pid: null, rid: null }, }); - console.log(count); - //console.log(comment); - var result = { + res.status(200).send({ errno: 0, errmsg: "", data: { - page: req.query.page, - totalPages: Math.ceil(count / req.query.pageSize), - pageSize: req.query.pageSize, - count: count, - data: result1, + page, + totalPages: Math.ceil(count / pageSize), + pageSize, + count, + data: result, }, - }; - - res.status(200).send(result); + }); } catch (err) { handleError(res, err, "保存失败"); } @@ -96,31 +104,30 @@ router.get("/api/comment", async (req, res) => { // 创建评论 router.post("/api/comment", async (req, res) => { - if (!res.locals.login) { - return res.status(404).send({ status: "0", msg: "请先登录" }); - } + checkLogin(res); try { - var comment = await I.prisma.ow_comment.create({ + const { url, comment, pid, rid } = req.body; + const { userid, display_name } = res.locals; + const ua = req.headers['user-agent'] || ""; + + const newComment = await I.prisma.ow_comment.create({ data: { - url: req.body.url, - comment: req.body.comment, - link: "/user/" + res.locals.userid, - mail: res.locals.userid + "@zerocat.wuyuan.dev", - nick: res.locals.display_name, - ua: req.headers['user-agent']||"", - //at: req.body.at||"", - pid: req.body.pid || null, - rid: req.body.rid || null, + url, + comment, + link: `/user/${userid}`, + mail: `${userid}@zerocat.wuyuan.dev`, + nick: display_name, + ua, + pid: pid || null, + rid: rid || null, }, }); - console.log(comment); - res.status(200).send({ errno: 0, errmsg: "", - data: transformComment([comment])[0], + data: transformComment([newComment])[0], }); } catch (err) { handleError(res, err, "保存失败"); @@ -129,19 +136,17 @@ router.post("/api/comment", async (req, res) => { // 删除评论 router.delete("/api/comment/:id", async (req, res) => { - //if (!res.locals.login) {return res.status(404).send({ status: "0", msg: "请先登录" });} - try { - var comment = await I.prisma.ow_comment.findFirst({ - where: { - id: Number(req.params.id), - }, + const { id } = req.params; + const { user_id } = res.locals; + + const comment = await I.prisma.ow_comment.findFirst({ + where: { id: Number(id) }, }); - if (comment.user_id == res.locals.user_id || true) { - var comment = await I.prisma.ow_comment.delete({ - where: { - id: Number(req.params.id), - }, + + if (comment.user_id == user_id || true) { + await I.prisma.ow_comment.delete({ + where: { id: Number(id) }, }); } @@ -151,39 +156,4 @@ router.delete("/api/comment/:id", async (req, res) => { } }); -function transformComment(input) { - var output = input.map((item) => { - // 将时间戳转换为毫秒数 - const time = new Date(item.insertedAt).getTime(); - const objectId = item.id; - const browser = (item.ua ? item.ua.match(/(Edge|Chrome|Firefox|Safari|Opera)/) : null || ["未知"])[0]; - - const os = (item.ua ? item.ua.match(/(Windows|Mac|Linux)/) : null || ["未知"])[0]; - const type = item.id == 1 ? "administrator" : "guest"; - const label = null; - const avatar = - "https://owcdn.190823.xyz/user/fcd939e653195bb6d057e8c2519f5cc7"; - const orig = item.comment; - const ip = item.ip; - const addr = ""; - - // 转换后的对象 - return { - ...item, - time, - objectId, - browser, - os, - type, - label, - avatar, - orig, - ip, - addr, - }; - }); - - return output; -} - -module.exports = router; +module.exports = router; \ No newline at end of file