Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Oct 6, 2024
1 parent fdc4403 commit 6e818c4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 67 deletions.
16 changes: 8 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ model ow_config {
model ow_comment {
id Int @id @default(autoincrement()) @db.UnsignedInt
user_id Int?
comment String? @db.Text
type String? @default("comment") @db.VarChar(64)
text String? @db.Text
insertedAt DateTime? @default(now()) @db.Timestamp(0)
ip String? @default("") @db.VarChar(100)
link String? @db.VarChar(255)
mail String? @db.VarChar(255)
nick String? @db.VarChar(255)
user_ip String? @default("") @db.VarChar(100)
link String? @db.VarChar(128)
pid Int?
rid Int?
sticky Boolean?
status String @default("") @db.VarChar(50)
like Int?
ua String? @db.Text
user_ua String? @db.Text
url String? @db.VarChar(255)
page_type String? @db.VarChar(32)
page_id Int?
createdAt DateTime? @default(now()) @db.Timestamp(0)
updatedAt DateTime? @default(now()) @db.Timestamp(0)
page_key String? @db.VarChar(128)
}

model ow_counter {
Expand Down
43 changes: 22 additions & 21 deletions server/lib/method/projectlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ async function getProjectlist(listid, userid) {
}

// 获取用户的所有项目列表
async function getUserProjectlist(userid) {
async function getUserProjectlist(userid,state) {
try {
return await prisma.ow_projects_lists.findMany({
where: { authorid: Number(userid) },
where: { authorid: Number(userid) ,state:{in:state}},
select: {
id: true,
authorid: true,
Expand All @@ -118,25 +118,6 @@ async function getUserProjectlist(userid) {
}
}

// 检查项目是否在用户列表中
async function checkProjectlistWithUser(info) {
try {
if (!info.listid && !info.userid) return "参数错误";

const projects = await prisma.ow_projects_lists.findMany({
where: { authorid: Number(info.userid) },
});

return projects.map((item) => {
const listArray = cleanAndDeduplicateList(item.list.split(","));
item.include = listArray.includes(String(info.projectid));
return item;
});
} catch (error) {
handleError(error);
}
}

// 获取用户的公开项目列表
async function getUserPublicProjectlist(userid) {
try {
Expand All @@ -158,6 +139,26 @@ async function getUserPublicProjectlist(userid) {
}
}

// 检查项目是否在用户列表中
async function checkProjectlistWithUser(info) {
try {
if (!info.listid && !info.userid) return "参数错误";

const projects = await prisma.ow_projects_lists.findMany({
where: { authorid: Number(info.userid) },
});

return projects.map((item) => {
const listArray = cleanAndDeduplicateList(item.list.split(","));
item.include = listArray.includes(String(info.projectid));
return item;
});
} catch (error) {
handleError(error);
}
}


// 更新项目列表
async function updateProjectlist(listid, info) {
try {
Expand Down
25 changes: 2 additions & 23 deletions server/lib/method/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();

const { getUsersByList } = require("./users.js");

async function getProjectsByList(list, userid) {
var select = {
id: true,
Expand Down Expand Up @@ -32,29 +34,6 @@ async function getProjectsByList(list, userid) {

return projects;
}
async function getUsersByList(list) {
var select = {
id: true,
username: true,
display_name: true,
state: true,
regTime: true,
motto: true,
images: true,
};

// 获取每个用户信息
var users = await prisma.ow_users.findMany({
where: {
id: { in: list.map((item) => parseInt(item)) },
},
select: select,
});

//console.log(users);

return users;
}
async function getProjectsAndUsersByProjectsList(list, userid) {
var projects = await getProjectsByList(list, userid);
var userslist = [...new Set(projects.map((project) => project.authorid))];
Expand Down
28 changes: 28 additions & 0 deletions server/lib/method/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//prisma client
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();

async function getUsersByList(list) {
var select = {
id: true,
username: true,
display_name: true,
state: true,
regTime: true,
motto: true,
images: true,
};

// 获取每个用户信息
var users = await prisma.ow_users.findMany({
where: {
id: { in: list.map((item) => parseInt(item)) },
},
select: select,
});

//console.log(users);

return users;
}
module.exports = { getUsersByList };
35 changes: 25 additions & 10 deletions server/router_comment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require("express");
const router = express.Router();
const I = require("./lib/global.js");

const {getUsersByList} = require("./lib/method/users.js");
// 中间件,确保所有请求均经过该处理
router.all("*", (req, res, next) => next());

Expand Down Expand Up @@ -30,6 +30,7 @@ const getSortCondition = (req) => {
// 转换评论数据
const transformComment = (comments) => {
return comments.map((comment) => {
return 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];
Expand Down Expand Up @@ -62,28 +63,39 @@ router.get("/api/comment", async (req, res) => {
const sort = getSortCondition(req);

const comments = await I.prisma.ow_comment.findMany({
where: { url: path, pid: null, rid: null },
where: { page_key: path, pid: null, rid: null,type: "comment" },
orderBy: sort,
take: Number(pageSize) || 10,
skip: (page - 1) * pageSize,
});

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 } },
where: { page_key: path, rid: { in: ids } ,type: "comment" },
});

const transformedChildrenComments = transformComment(childrenComments);
// 获取评论的用户id

var user_ids = transformedComments.map((comment) => comment.user_id);
user_ids = user_ids.concat(transformedChildrenComments.map((comment) => comment.user_id));
//去重
user_ids = Array.from(new Set(user_ids));

console.log(user_ids)
const users = await getUsersByList(user_ids);
const result = transformedComments.map((comment) => {
const children = transformedChildrenComments.filter((child) => child.rid == comment.id);
return { ...comment, children };
});

const count = await I.prisma.ow_comment.count({
where: { url: path, pid: null, rid: null },
where: { page_key: path, pid: null, rid: null,type: "comment" },
});

res.status(200).send({
Expand All @@ -96,6 +108,7 @@ router.get("/api/comment", async (req, res) => {
count,
data: result,
},
users,
});
} catch (err) {
handleError(res, err, "保存失败");
Expand All @@ -109,16 +122,18 @@ router.post("/api/comment", async (req, res) => {
try {
const { url, comment, pid, rid } = req.body;
const { userid, display_name } = res.locals;
const ua = req.headers['user-agent'] || "";
const user_ua = req.headers['user-agent'] || "";

const newComment = await I.prisma.ow_comment.create({
data: {
url,
comment,
user_id: userid,
type: "comment",
user_ip: req.ip,
page_type:url.split("-")[0],
page_id:Number(url.split("-")[1])||null,
text:comment,
link: `/user/${userid}`,
mail: `${userid}@zerocat.wuyuan.dev`,
nick: display_name,
ua,
user_ua,
pid: pid || null,
rid: rid || null,
},
Expand Down
14 changes: 9 additions & 5 deletions server/router_projectlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ router.get("/user/:id/:state?", async (req, res) => {
let info;

if (state === "private" && res.locals.userid == id) {
info = await getUserProjectlist(id);
console.log("1");
info = await getUserProjectlist(id,["private"]);
} else if (state === "public") {
info = await getUserPublicProjectlist(id);
console.log("2");
info = await getUserProjectlist(id,["public"]);
} else if (!state || ["all", "undefined", "null", ""].includes(state)) {
console.log("3");
info = res.locals.userid == id
? await getUserProjectlist(id)
: await getUserPublicProjectlist(id);
? await getUserProjectlist(id,["private", "public"])
: await getUserProjectlist(id,["public"]);
} else {
info = await getUserPublicProjectlist(id);
console.log("4");
info = await getUserProjectlist(id,"public");
}

res.status(200).send({ status: "1", message: "获取成功", data: info });
Expand Down

0 comments on commit 6e818c4

Please sign in to comment.