From 8fc1f675ff1cfd1e68d1882c0ba90c6c3a9edf83 Mon Sep 17 00:00:00 2001 From: RichZDS <3388214266@qq.com> Date: Thu, 5 Feb 2026 22:03:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=BF=E5=9D=97=E5=88=9B=E5=BB=BA=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/dao/internal/forum_comments.go | 2 +- Backend/internal/logic/Forum/boards.go | 143 +++++++++++++++++- Backend/internal/logic/Forum/post.go | 53 ++++++- Backend/internal/logic/logic.go | 1 + Backend/internal/logic/user/user.go | 44 +++--- Backend/internal/model/do/forum_comments.go | 2 +- .../internal/model/entity/forum_comments.go | 22 +-- Backend/internal/model/forum.go | 20 +-- Backend/internal/service/chat.go | 8 + Backend/main.go | 3 +- 10 files changed, 240 insertions(+), 58 deletions(-) create mode 100644 Backend/internal/service/chat.go diff --git a/Backend/internal/dao/internal/forum_comments.go b/Backend/internal/dao/internal/forum_comments.go index 0dcc799..d200598 100644 --- a/Backend/internal/dao/internal/forum_comments.go +++ b/Backend/internal/dao/internal/forum_comments.go @@ -27,7 +27,7 @@ type ForumCommentsColumns struct { ParentId string // 父评论ID:NULL=一级评论;非NULL=二级评论(指向一级评论ID,无外键) ReplyToUserId string // 回复的用户ID(可选,用于展示“回复@xxx”,无外键) Content string // 评论内容(支持emoji) - Status string // 状态:normal=正常 deleted=软删 audit=审核中 reject=驳回 + Status string // 状态:1=正常 2=软删 3=审核中 4=驳回 LikeCount string // 点赞数(冗余) CreatedAt string // 创建时间 UpdatedAt string // 更新时间 diff --git a/Backend/internal/logic/Forum/boards.go b/Backend/internal/logic/Forum/boards.go index 3399c93..4fb49c3 100644 --- a/Backend/internal/logic/Forum/boards.go +++ b/Backend/internal/logic/Forum/boards.go @@ -2,7 +2,15 @@ package Forum import ( v1 "TrangleAgent/api/forum/v1" + "TrangleAgent/internal/dao" + "TrangleAgent/internal/model" + "TrangleAgent/internal/model/entity" + "TrangleAgent/internal/model/response" + "TrangleAgent/internal/service" "context" + + "github.com/gogf/gf/v2/errors/gerror" + "github.com/gogf/gf/v2/os/gtime" ) type sForumBoards struct{} @@ -11,21 +19,146 @@ func NewForumBoards() *sForumBoards { return &sForumBoards{} } +func init() { + service.RegisterForumBoards(NewForumBoards()) +} + func (s *sForumBoards) Create(ctx context.Context, req *v1.ForumBoardsCreateReq) (res *v1.ForumBoardsCreateRes, err error) { - return nil, nil + // 校验参数 + if req.Name == "" { + return nil, gerror.New("版块名称不能为空") + } + + // 创建版块 + insert := entity.ForumBoards{ + SectionId: req.SectionId, + Name: req.Name, + Description: req.Description, + CoverImage: req.CoverImage, + Status: req.Status, + DisplayOrder: req.DisplayOrder, + CreatedAt: gtime.Now(), + UpdatedAt: gtime.Now(), + } + + // 如果 Status 为空,默认为 normal + if insert.Status == "" { + insert.Status = "normal" + } + + result, err := dao.ForumBoards.Ctx(ctx).Data(insert).Insert() + if err != nil { + return nil, gerror.Wrap(err, "创建版块失败") + } + + id, err := result.LastInsertId() + if err != nil { + return nil, gerror.Wrap(err, "获取版块ID失败") + } + + return &v1.ForumBoardsCreateRes{Id: uint64(id)}, nil } func (s *sForumBoards) Update(ctx context.Context, req *v1.ForumBoardsUpdateReq) (res *v1.ForumBoardsUpdateRes, err error) { - return nil, nil + if req.Id == 0 { + return nil, gerror.New("版块ID不能为空") + } + + // 只能修改部分字段 + + data := entity.ForumBoards{ + Id: req.Id, + SectionId: req.SectionId, + Name: req.Name, + Description: req.Description, + CoverImage: req.CoverImage, + Status: req.Status, + DisplayOrder: req.DisplayOrder, + UpdatedAt: gtime.Now(), + } + + _, err = dao.ForumBoards.Ctx(ctx).Data(data).OmitEmpty().Where(dao.ForumBoards.Columns().Id, req.Id).Update() + if err != nil { + return nil, gerror.Wrap(err, "更新版块失败") + } + + return &v1.ForumBoardsUpdateRes{Id: req.Id}, nil } func (s *sForumBoards) Delete(ctx context.Context, req *v1.ForumBoardsDeleteReq) (res *v1.ForumBoardsDeleteRes, err error) { - return nil, nil + if req.Id == 0 { + return nil, gerror.New("版块ID不能为空") + } + + // 物理删除 + _, err = dao.ForumBoards.Ctx(ctx).Where(dao.ForumBoards.Columns().Id, req.Id).Delete() + if err != nil { + return nil, gerror.Wrap(err, "删除版块失败") + } + + return &v1.ForumBoardsDeleteRes{}, nil } func (s *sForumBoards) View(ctx context.Context, req *v1.ForumBoardsViewReq) (res *v1.ForumBoardsViewRes, err error) { - return nil, nil + if req.Id == 0 { + return nil, gerror.New("版块ID不能为空") + } + + var board model.ForumBoardViewParams + err = dao.ForumBoards.Ctx(ctx).Where(dao.ForumBoards.Columns().Id, req.Id).Scan(&board) + if err != nil { + return nil, gerror.Wrap(err, "查询版块失败") + } + + return &v1.ForumBoardsViewRes{ + ForumBoardViewParams: board, + }, nil } + func (s *sForumBoards) List(ctx context.Context, req *v1.ForumBoardsListReq) (res *v1.ForumBoardsListRes, err error) { - return nil, nil + m := dao.ForumBoards.Ctx(ctx) + + if req.SectionId != 0 { + m = m.Where(dao.ForumBoards.Columns().SectionId, req.SectionId) + } + if req.Name != "" { + m = m.WhereLike(dao.ForumBoards.Columns().Name, "%"+req.Name+"%") + } + if req.Status != "" { + m = m.Where(dao.ForumBoards.Columns().Status, req.Status) + } + + // 默认按 DisplayOrder 降序,Id 升序 + m = m.OrderDesc(dao.ForumBoards.Columns().DisplayOrder).OrderAsc(dao.ForumBoards.Columns().Id) + + // 分页 + total, err := m.Count() + if err != nil { + return nil, gerror.Wrap(err, "获取版块总数失败") + } + + var list []*model.ForumBoardViewParams + + page := req.Page + if page <= 0 { + page = 1 + } + size := req.PageSize + if size <= 0 { + size = 10 + } + + err = m.Page(page, size).Scan(&list) + if err != nil { + return nil, gerror.Wrap(err, "查询版块列表失败") + } + + return &v1.ForumBoardsListRes{ + PageResult: response.PageResult{ + Total: total, + Page: page, + PageSize: size, + }, + List: list, + }, nil } diff --git a/Backend/internal/logic/Forum/post.go b/Backend/internal/logic/Forum/post.go index aa9b6bf..0987efd 100644 --- a/Backend/internal/logic/Forum/post.go +++ b/Backend/internal/logic/Forum/post.go @@ -4,6 +4,8 @@ import ( v1 "TrangleAgent/api/forum/v1" "TrangleAgent/internal/dao" "TrangleAgent/internal/model" + "TrangleAgent/internal/model/entity" + "TrangleAgent/internal/model/response" "TrangleAgent/internal/service" "context" @@ -36,7 +38,7 @@ func (s *sForumPosts) Create(ctx context.Context, req *v1.ForumPostsCreateReq) ( } //创建帖子 - insert := model.ForumPost{ + insert := entity.ForumPosts{ BoardId: req.BoardId, UserId: req.UserId, Title: req.Title, @@ -46,6 +48,11 @@ func (s *sForumPosts) Create(ctx context.Context, req *v1.ForumPostsCreateReq) ( CreatedAt: gtime.Now(), UpdatedAt: gtime.Now(), } + + if insert.Status == "" { + insert.Status = "normal" + } + result, err := dao.ForumPosts.Ctx(ctx).Data(insert).Insert() if err != nil { return nil, gerror.Wrap(err, "创建帖子失败") @@ -69,14 +76,14 @@ func (s *sForumPosts) Update(ctx context.Context, req *v1.ForumPostsUpdateReq) ( return nil, gerror.New("内容长度要在5-10000之间") } //只能修改 帖子标题 帖子正文 帖子封面图URL - fields := []string{"title", "content", "cover_image"} //更新帖子 - _, err = dao.ForumPosts.Ctx(ctx).Data(model.ForumPost{ + _, err = dao.ForumPosts.Ctx(ctx).Data(entity.ForumPosts{ Id: req.Id, Title: req.Title, Content: req.Content, CoverImage: req.CoverImage, - }).Fields(fields).OmitEmpty().Update() + UpdatedAt: gtime.Now(), + }).Fields("title", "content", "cover_image", "updated_at").OmitEmpty().Update() if err != nil { return nil, gerror.Wrap(err, "更新帖子失败") } @@ -116,12 +123,48 @@ func (s *sForumPosts) List(ctx context.Context, req *v1.ForumPostsListReq) (res if req.BoardId != 0 { mod = mod.Where(dao.ForumPosts.Columns().BoardId, req.BoardId) } + if req.UserId != 0 { + mod = mod.Where(dao.ForumPosts.Columns().UserId, req.UserId) + } + if req.Status != "" { + mod = mod.Where(dao.ForumPosts.Columns().Status, req.Status) + } + if req.IsPinned != 0 { + mod = mod.Where(dao.ForumPosts.Columns().IsPinned, req.IsPinned) + } + if req.IsEssence != 0 { + mod = mod.Where(dao.ForumPosts.Columns().IsEssence, req.IsEssence) + } + + // 排序:优先置顶,然后按创建时间倒序 + mod = mod.OrderDesc(dao.ForumPosts.Columns().IsPinned).OrderDesc(dao.ForumPosts.Columns().CreatedAt) + + // 分页 + page := req.Page + if page <= 0 { + page = 1 + } + size := req.PageSize + if size <= 0 { + size = 10 + } + + total, err := mod.Count() + if err != nil { + return nil, gerror.Wrap(err, "获取帖子总数失败") + } + var list []*model.ForumPostViewParams - err = mod.Scan(&list) + err = mod.Page(page, size).Scan(&list) if err != nil { return nil, gerror.Wrap(err, "查询帖子列表失败") } return &v1.ForumPostsListRes{ + PageResult: response.PageResult{ + Total: total, + Page: page, + PageSize: size, + }, List: list, }, nil } diff --git a/Backend/internal/logic/logic.go b/Backend/internal/logic/logic.go index 5d913a1..ea036c4 100644 --- a/Backend/internal/logic/logic.go +++ b/Backend/internal/logic/logic.go @@ -6,6 +6,7 @@ package logic import ( _ "TrangleAgent/internal/logic/Forum" + _ "TrangleAgent/internal/logic/chat" _ "TrangleAgent/internal/logic/containment" _ "TrangleAgent/internal/logic/department" _ "TrangleAgent/internal/logic/login" diff --git a/Backend/internal/logic/user/user.go b/Backend/internal/logic/user/user.go index 9c443fd..df63e27 100644 --- a/Backend/internal/logic/user/user.go +++ b/Backend/internal/logic/user/user.go @@ -1,12 +1,12 @@ package user import ( - "context" v1 "TrangleAgent/api/user/v1" "TrangleAgent/internal/dao" "TrangleAgent/internal/model" "TrangleAgent/internal/model/entity" "TrangleAgent/internal/service" + "context" "github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gerror" @@ -77,7 +77,7 @@ func (s *sUser) UserList(ctx context.Context, req *v1.UserListReq) (res *v1.User req.PageResult.Page = req.Page req.PageResult.PageSize = req.PageSize - return + return res, nil } func (s *sUser) UserView(ctx context.Context, req *v1.UserViewReq) (res *v1.UserViewRes, err error) { @@ -92,10 +92,10 @@ func (s *sUser) UserView(ctx context.Context, req *v1.UserViewReq) (res *v1.User res = &v1.UserViewRes{} err = m.Scan(&res.UserViewParams) if err != nil { - return nil, err + return nil, gerror.Wrap(err, "查询用户信息失败") } - return + return res, nil } func (s *sUser) UserUpdate(ctx context.Context, req *v1.UserUpdateReq) (res *v1.UserUpdateRes, err error) { @@ -105,28 +105,28 @@ func (s *sUser) UserUpdate(ctx context.Context, req *v1.UserUpdateReq) (res *v1. // 根据账号更新用户信息 _, err = m.Data(req).Where(dao.Users.Columns().Account, req.Account).Update() if err != nil { - return nil, err + return nil, gerror.Wrap(err, "更新用户信息失败") } res = &v1.UserUpdateRes{} // 获取更新后的用户信息 err = m.Where(dao.Users.Columns().Account, req.Account).Scan(&res) if err != nil { - return nil, err + return nil, gerror.Wrap(err, "查询用户信息失败") } - return + return res, nil } func (s *sUser) UserDelete(ctx context.Context, req *v1.UserDeleteReq) (res *v1.UserDeleteRes, err error) { // 根据账号删除用户 _, err = dao.Users.Ctx(ctx).Where(dao.Users.Columns().Account, req.Account).Delete() if err != nil { - return nil, err + return nil, gerror.Wrap(err, "删除用户失败") } res = &v1.UserDeleteRes{} - return + return res, nil } // RoleCreate 创建角色 @@ -145,13 +145,13 @@ func (s *sUser) RoleCreate(ctx context.Context, req *v1.RoleCreateReq) (res *v1. id, err := dao.RoleCards.Ctx(ctx).Data(data).InsertAndGetId() if err != nil { - return nil, err + return nil, gerror.Wrap(err, "创建角色失败") } res = &v1.RoleCreateRes{ Id: uint64(id), } - return + return res, nil } // RoleUpdate 更新角色 @@ -174,13 +174,13 @@ func (s *sUser) RoleUpdate(ctx context.Context, req *v1.RoleUpdateReq) (res *v1. _, err = dao.RoleCards.Ctx(ctx).Data(data).Where(dao.RoleCards.Columns().Id, req.Id).Update() if err != nil { - return nil, err + return nil, gerror.Wrap(err, "更新角色失败") } res = &v1.RoleUpdateRes{ Id: req.Id, } - return + return res, nil } // RoleView 查看角色详情 @@ -189,7 +189,7 @@ func (s *sUser) RoleView(ctx context.Context, req *v1.RoleViewReq) (res *v1.Role var role entity.RoleCards err = dao.RoleCards.Ctx(ctx).Where(dao.RoleCards.Columns().Id, req.Id).Scan(&role) if err != nil { - return nil, err + return nil, gerror.Wrap(err, "查询角色详情失败") } res = &v1.RoleViewRes{ @@ -219,7 +219,7 @@ func (s *sUser) RoleView(ctx context.Context, req *v1.RoleViewReq) (res *v1.Role QaDiscretion: role.QaDiscretion, }, } - return + return res, nil } // RoleList 获取角色列表 @@ -293,7 +293,7 @@ func (s *sUser) RoleList(ctx context.Context, req *v1.RoleListReq) (res *v1.Role req.PageResult.Page = req.Page req.PageResult.PageSize = req.PageSize - return + return res, nil } // RoleDelete 删除角色 @@ -301,13 +301,13 @@ func (s *sUser) RoleDelete(ctx context.Context, req *v1.RoleDeleteReq) (res *v1. // 根据ID删除角色 _, err = dao.RoleCards.Ctx(ctx).Where(dao.RoleCards.Columns().Id, req.Id).Delete() if err != nil { - return nil, err + return nil, gerror.Wrap(err, "删除角色失败") } res = &v1.RoleDeleteRes{ Id: req.Id, } - return + return res, nil } // RolePermissionCheck 权限查询 @@ -316,7 +316,7 @@ func (s *sUser) RolePermissionCheck(ctx context.Context, req *v1.RolePermissionC var role entity.RoleCards err = dao.RoleCards.Ctx(ctx).Where(dao.RoleCards.Columns().Id, req.RoleId).Scan(&role) if err != nil { - return nil, err + return nil, gerror.Wrap(err, "查询角色信息失败") } // 根据轨道类型获取对应的轨道值 @@ -343,5 +343,9 @@ func (s *sUser) RolePermissionCheck(ctx context.Context, req *v1.RolePermissionC } // 权限不足,返回错误 - return nil, gerror.New("权限不足") + res = &v1.RolePermissionCheckRes{ + Code: 403, + Mes: "权限不足", + } + return res, nil } diff --git a/Backend/internal/model/do/forum_comments.go b/Backend/internal/model/do/forum_comments.go index c0de881..38d216a 100644 --- a/Backend/internal/model/do/forum_comments.go +++ b/Backend/internal/model/do/forum_comments.go @@ -18,7 +18,7 @@ type ForumComments struct { ParentId any // 父评论ID:NULL=一级评论;非NULL=二级评论(指向一级评论ID,无外键) ReplyToUserId any // 回复的用户ID(可选,用于展示“回复@xxx”,无外键) Content any // 评论内容(支持emoji) - Status any // 状态:normal=正常 deleted=软删 audit=审核中 reject=驳回 + Status any // 状态:1=正常 2=软删 3=审核中 4=驳回 LikeCount any // 点赞数(冗余) CreatedAt *gtime.Time // 创建时间 UpdatedAt *gtime.Time // 更新时间 diff --git a/Backend/internal/model/entity/forum_comments.go b/Backend/internal/model/entity/forum_comments.go index d87757a..7d80b1c 100644 --- a/Backend/internal/model/entity/forum_comments.go +++ b/Backend/internal/model/entity/forum_comments.go @@ -10,15 +10,15 @@ import ( // ForumComments is the golang structure for table forum_comments. type ForumComments struct { - Id uint64 `json:"id" orm:"id" description:"评论ID(主键)"` // 评论ID(主键) - PostId uint64 `json:"postId" orm:"post_id" description:"所属帖子ID(关联 forum_posts.id,无外键)"` // 所属帖子ID(关联 forum_posts.id,无外键) - UserId uint64 `json:"userId" orm:"user_id" description:"评论发布者ID(关联 users.id,无外键)"` // 评论发布者ID(关联 users.id,无外键) - ParentId uint64 `json:"parentId" orm:"parent_id" description:"父评论ID:NULL=一级评论;非NULL=二级评论(指向一级评论ID,无外键)"` // 父评论ID:NULL=一级评论;非NULL=二级评论(指向一级评论ID,无外键) - ReplyToUserId uint64 `json:"replyToUserId" orm:"reply_to_user_id" description:"回复的用户ID(可选,用于展示“回复@xxx”,无外键)"` // 回复的用户ID(可选,用于展示“回复@xxx”,无外键) - Content string `json:"content" orm:"content" description:"评论内容(支持emoji)"` // 评论内容(支持emoji) - Status string `json:"status" orm:"status" description:"状态:normal=正常 deleted=软删 audit=审核中 reject=驳回"` // 状态:normal=正常 deleted=软删 audit=审核中 reject=驳回 - LikeCount uint `json:"likeCount" orm:"like_count" description:"点赞数(冗余)"` // 点赞数(冗余) - CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"创建时间"` // 创建时间 - UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"` // 更新时间 - DeletedAt *gtime.Time `json:"deletedAt" orm:"deleted_at" description:"软删除时间"` // 软删除时间 + Id uint64 `json:"id" orm:"id" description:"评论ID(主键)"` // 评论ID(主键) + PostId uint64 `json:"postId" orm:"post_id" description:"所属帖子ID(关联 forum_posts.id,无外键)"` // 所属帖子ID(关联 forum_posts.id,无外键) + UserId uint64 `json:"userId" orm:"user_id" description:"评论发布者ID(关联 users.id,无外键)"` // 评论发布者ID(关联 users.id,无外键) + ParentId uint64 `json:"parentId" orm:"parent_id" description:"父评论ID:NULL=一级评论;非NULL=二级评论(指向一级评论ID,无外键)"` // 父评论ID:NULL=一级评论;非NULL=二级评论(指向一级评论ID,无外键) + ReplyToUserId uint64 `json:"replyToUserId" orm:"reply_to_user_id" description:"回复的用户ID(可选,用于展示“回复@xxx”,无外键)"` // 回复的用户ID(可选,用于展示“回复@xxx”,无外键) + Content string `json:"content" orm:"content" description:"评论内容(支持emoji)"` // 评论内容(支持emoji) + Status string `json:"status" orm:"status" description:"状态:1=正常 2=软删 3=审核中 4=驳回"` // 状态:1=正常 2=软删 3=审核中 4=驳回 + LikeCount uint `json:"likeCount" orm:"like_count" description:"点赞数(冗余)"` // 点赞数(冗余) + CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"创建时间"` // 创建时间 + UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"` // 更新时间 + DeletedAt *gtime.Time `json:"deletedAt" orm:"deleted_at" description:"软删除时间"` // 软删除时间 } diff --git a/Backend/internal/model/forum.go b/Backend/internal/model/forum.go index 124a977..bab18d1 100644 --- a/Backend/internal/model/forum.go +++ b/Backend/internal/model/forum.go @@ -6,19 +6,13 @@ import ( // ForumBoard 论坛版块 type ForumBoard struct { - Id uint64 `json:"id" orm:"id" description:"版块ID"` - SectionId uint64 `json:"sectionId" orm:"section_id" description:"所属频道ID"` - Name string `json:"name" orm:"name" description:"版块名称"` - Description string `json:"description" orm:"description" description:"版块简介"` - CoverImage string `json:"coverImage" orm:"cover_image" description:"版块封面图URL"` - Status string `json:"status" orm:"status" description:"版块状态"` - DisplayOrder int `json:"displayOrder" orm:"display_order" description:"排序值"` - PostCount uint `json:"postCount" orm:"post_count" description:"帖子总数"` - TodayPostCount uint `json:"todayPostCount" orm:"today_post_count" description:"今日发帖数"` - LastPostId uint64 `json:"lastPostId" orm:"last_post_id" description:"最后一篇帖子ID"` - LastPostAt *gtime.Time `json:"lastPostAt" orm:"last_post_at" description:"最后发帖时间"` - CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"创建时间"` - UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"` + Id uint64 `json:"id" orm:"id" description:"版块ID"` + SectionId uint64 `json:"sectionId" orm:"section_id" description:"所属频道ID"` + Name string `json:"name" orm:"name" description:"版块名称"` + Description string `json:"description" orm:"description" description:"版块简介"` + CoverImage string `json:"coverImage" orm:"cover_image" description:"版块封面图URL"` + Status string `json:"status" orm:"status" description:"版块状态"` + DisplayOrder int `json:"displayOrder" orm:"display_order" description:"排序值"` } // ForumBoardViewParams 版块查看参数 diff --git a/Backend/internal/service/chat.go b/Backend/internal/service/chat.go new file mode 100644 index 0000000..3d70438 --- /dev/null +++ b/Backend/internal/service/chat.go @@ -0,0 +1,8 @@ +// ================================================================================ +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// You can delete these comments if you wish manually maintain this interface file. +// ================================================================================ + +package service + +type () diff --git a/Backend/main.go b/Backend/main.go index a454925..907641b 100644 --- a/Backend/main.go +++ b/Backend/main.go @@ -11,7 +11,6 @@ import ( "github.com/gogf/gf/v2/os/gctx" ) -func mai2n() { - +func main() { cmd.Main.Run(gctx.GetInitCtx()) }