feat: 编辑器与帖子展示布局优化 + 新增个人空间
- 编辑器投稿页布局优化:标题与编辑器高度动态适配,工具栏与内容区视觉对齐 - 稿件展示页布局优化:MD 渲染区与评论区视觉分离,代码块主题微调 - CSRF 中间件:图像上传端点豁免,解决 Vditor 拖拽/粘贴上传 403 - Post 状态映射、GetGinUser、SaveUploadedFile 提取至 common 包,遵循审计建议 - 新增个人空间功能:/space(需登录)重定向到 /space/:uid,/space/:uid 公开访问 - 空间页模板:参考 B 站布局,展示头像、用户名、个性签名、UID、加入时间及稿件列表 - 导航栏:用户名链接指向 /space,新增「设置」入口 - 分层实现:SpaceController → SpaceService → PostRepo.FindByUserID,严格 ISP 接口隔离
This commit is contained in:
@ -144,18 +144,29 @@ func (s *PostService) GetByID(id uint) (*model.Post, error) {
|
||||
return s.findPostWithAuthor(id)
|
||||
}
|
||||
|
||||
// List 公开帖子列表(仅 approved)
|
||||
func (s *PostService) List(keyword string, page, pageSize int) ([]model.Post, int64, error) {
|
||||
// listPageable 分页查询公共逻辑:参数规范化 + nil 转空切片
|
||||
func (s *PostService) listPageable(page, pageSize int, fn func(offset, limit int) ([]model.Post, int64, error)) ([]model.Post, int64, error) {
|
||||
p := common.Pagination{Page: page, PageSize: pageSize}
|
||||
p.DefaultPagination()
|
||||
return s.repo.FindPageable(keyword, p.Offset(), p.PageSize)
|
||||
posts, total, err := fn(p.Offset(), p.PageSize)
|
||||
if posts == nil {
|
||||
posts = []model.Post{}
|
||||
}
|
||||
return posts, total, err
|
||||
}
|
||||
|
||||
// List 公开帖子列表(仅 approved)
|
||||
func (s *PostService) List(keyword string, page, pageSize int) ([]model.Post, int64, error) {
|
||||
return s.listPageable(page, pageSize, func(offset, limit int) ([]model.Post, int64, error) {
|
||||
return s.repo.FindPageable(keyword, offset, limit)
|
||||
})
|
||||
}
|
||||
|
||||
// ListAdmin 管理后台帖子列表(全状态)
|
||||
func (s *PostService) ListAdmin(keyword, status string, page, pageSize int) ([]model.Post, int64, error) {
|
||||
p := common.Pagination{Page: page, PageSize: pageSize}
|
||||
p.DefaultPagination()
|
||||
return s.repo.FindAdminPageable(keyword, status, p.Offset(), p.PageSize)
|
||||
return s.listPageable(page, pageSize, func(offset, limit int) ([]model.Post, int64, error) {
|
||||
return s.repo.FindAdminPageable(keyword, status, offset, limit)
|
||||
})
|
||||
}
|
||||
|
||||
// Update 编辑帖子(权限在 controller 层检查)
|
||||
@ -253,5 +264,17 @@ func (s *PostService) Unlock(postID uint) error {
|
||||
|
||||
// Restore 恢复软删除
|
||||
func (s *PostService) Restore(postID uint) error {
|
||||
return s.repo.Restore(postID)
|
||||
err := s.repo.Restore(postID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return common.ErrPostNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsPostAccessible 检查用户是否有权限访问/操作帖子(作者或 moderator+)
|
||||
func (s *PostService) IsPostAccessible(userID uint, role string, postUserID uint) bool {
|
||||
return userID == postUserID || model.HasMinRole(role, model.RoleModerator)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user