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:
20
internal/common/context.go
Normal file
20
internal/common/context.go
Normal file
@ -0,0 +1,20 @@
|
||||
package common
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// GetGinUser 从 Gin context 中提取当前登录用户的 uid 和 role
|
||||
// 如果用户未登录,ok 返回 false;role 可能为空字符串
|
||||
func GetGinUser(c *gin.Context) (uid uint, role string, ok bool) {
|
||||
if v, exists := c.Get("uid"); exists {
|
||||
if u, isUint := v.(uint); isUint {
|
||||
uid = u
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
if v, exists := c.Get("role"); exists {
|
||||
if r, isStr := v.(string); isStr {
|
||||
role = r
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
29
internal/common/file.go
Normal file
29
internal/common/file.go
Normal file
@ -0,0 +1,29 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"mime/multipart"
|
||||
"os"
|
||||
)
|
||||
|
||||
// SaveUploadedFile 将 multipart.File 内容写入目标路径
|
||||
func SaveUploadedFile(file multipart.File, dst string) error {
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
buf := make([]byte, 32*1024)
|
||||
for {
|
||||
n, err := file.Read(buf)
|
||||
if n > 0 {
|
||||
if _, writeErr := out.Write(buf[:n]); writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -1,11 +1,25 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"metazone.cc/metalab/internal/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// mdImageRe 匹配 Markdown 图片语法 
|
||||
var mdImageRe = regexp.MustCompile(`!\[.*?\]\((.*?)\)`)
|
||||
|
||||
// ExtractFirstImage 从 Markdown 正文提取第一张图片 URL
|
||||
func ExtractFirstImage(md string) string {
|
||||
match := mdImageRe.FindStringSubmatch(md)
|
||||
if len(match) > 1 {
|
||||
return match[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// BuildPageData 构建页面模板数据,自动注入登录状态、站点信息与 CSRF token
|
||||
func BuildPageData(c *gin.Context, extra gin.H) gin.H {
|
||||
data := gin.H{}
|
||||
|
||||
@ -49,5 +49,13 @@ func (p *Pagination) NextPage(totalPages int) int {
|
||||
return next
|
||||
}
|
||||
|
||||
// PageCount 根据 total 和 pageSize 计算总页数(无需创建 Pagination 实例)
|
||||
func PageCount(total int64, pageSize int) int {
|
||||
if total == 0 {
|
||||
return 0
|
||||
}
|
||||
return int((total + int64(pageSize) - 1) / int64(pageSize))
|
||||
}
|
||||
|
||||
// 固定时间格式,前后端统一
|
||||
const TimeFormat = time.RFC3339
|
||||
|
||||
12
internal/common/post_helpers.go
Normal file
12
internal/common/post_helpers.go
Normal file
@ -0,0 +1,12 @@
|
||||
package common
|
||||
|
||||
import "metazone.cc/metalab/internal/model"
|
||||
|
||||
// PostStatusDisplayNames 帖子状态 → 中文名称映射(供模板渲染使用)
|
||||
var PostStatusDisplayNames = map[string]string{
|
||||
model.PostStatusDraft: "草稿",
|
||||
model.PostStatusPending: "待审核",
|
||||
model.PostStatusApproved: "已发布",
|
||||
model.PostStatusRejected: "已退回",
|
||||
model.PostStatusLocked: "已锁定",
|
||||
}
|
||||
Reference in New Issue
Block a user