- 新增收藏夹系统 (Folder/FolderItem 模型、CRUD API、前端管理页) - 新增文章详情页收藏按钮 (书签图标、ToggleFavorite 一键收藏) - 新增 Studio 趋势图 (赋能值/评论/点赞/收藏 4 线图, 7d/30d/90d) - 新增通知偏好设置页 (评论/回复/点赞/关注 4 类开关) - 后端通知列表支持按偏好过滤 (排除已关闭的通知类型) - 下载 Chart.js 到本地静态资源 (static/js/chart.umd.min.js) - 补充收藏夹卡片、开关滑块、趋势图网格等 CSS 样式
456 lines
12 KiB
Go
456 lines
12 KiB
Go
package controller
|
||
|
||
import (
|
||
"log"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/model"
|
||
"metazone.cc/metalab/internal/service"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// trendsUseCase StudioController 对趋势服务的最小依赖(ISP)
|
||
type trendsUseCase interface {
|
||
GetTrends(userID uint, since string) (*service.TrendResult, error)
|
||
}
|
||
|
||
// StudioController 创作中心控制器(SSR 页面 + API)
|
||
type StudioController struct {
|
||
postService studioUseCase
|
||
energySvc energyDeducter
|
||
trendsSvc trendsUseCase
|
||
}
|
||
|
||
// NewStudioController 构造函数
|
||
func NewStudioController(ps studioUseCase) *StudioController {
|
||
return &StudioController{postService: ps}
|
||
}
|
||
|
||
// WithEnergyService 链式注入域能服务
|
||
func (ctrl *StudioController) WithEnergyService(svc energyDeducter) *StudioController {
|
||
ctrl.energySvc = svc
|
||
return ctrl
|
||
}
|
||
|
||
// WithTrendsService 链式注入趋势服务
|
||
func (ctrl *StudioController) WithTrendsService(svc trendsUseCase) *StudioController {
|
||
ctrl.trendsSvc = svc
|
||
return ctrl
|
||
}
|
||
|
||
// ==============================
|
||
// SSR 页面方法
|
||
// ==============================
|
||
|
||
// OverviewPage 创作中心首页(数据概览)
|
||
func (ctrl *StudioController) OverviewPage(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.RedirectToLogin(c)
|
||
return
|
||
}
|
||
|
||
overview, err := ctrl.postService.GetOverview(uid)
|
||
if err != nil {
|
||
c.HTML(http.StatusOK, "studio/overview.html", common.BuildPageData(c, gin.H{
|
||
"Title": "创作中心",
|
||
"Error": "加载数据失败",
|
||
"ActiveTab": "overview",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
return
|
||
}
|
||
|
||
c.HTML(http.StatusOK, "studio/overview.html", common.BuildPageData(c, gin.H{
|
||
"Title": "创作中心",
|
||
"Overview": overview,
|
||
"StatusNames": common.PostStatusDisplayNames,
|
||
"ActiveTab": "overview",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
}
|
||
|
||
// PostsPage 内容管理页
|
||
func (ctrl *StudioController) PostsPage(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.RedirectToLogin(c)
|
||
return
|
||
}
|
||
|
||
status := c.Query("status")
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||
|
||
posts, total, err := ctrl.postService.ListByUser(uid, status, page, pageSize)
|
||
if err != nil {
|
||
c.HTML(http.StatusOK, "studio/posts.html", common.BuildPageData(c, gin.H{
|
||
"Title": "内容管理 - 创作中心",
|
||
"Error": "加载失败",
|
||
"ActiveTab": "posts",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
return
|
||
}
|
||
|
||
overview, _ := ctrl.postService.GetOverview(uid)
|
||
|
||
c.HTML(http.StatusOK, "studio/posts.html", common.BuildPageData(c, gin.H{
|
||
"Title": "内容管理 - 创作中心",
|
||
"Posts": posts,
|
||
"Total": total,
|
||
"Page": page,
|
||
"PageSize": pageSize,
|
||
"TotalPages": common.PageCount(total, pageSize),
|
||
"CurrentStatus": status,
|
||
"Overview": overview,
|
||
"StatusNames": common.PostStatusDisplayNames,
|
||
"ActiveTab": "posts",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
}
|
||
|
||
// DraftsPage 草稿箱页面
|
||
func (ctrl *StudioController) DraftsPage(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.RedirectToLogin(c)
|
||
return
|
||
}
|
||
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||
|
||
posts, total, err := ctrl.postService.ListByUser(uid, model.PostStatusDraft, page, pageSize)
|
||
if err != nil {
|
||
c.HTML(http.StatusOK, "studio/drafts.html", common.BuildPageData(c, gin.H{
|
||
"Title": "草稿箱 - 创作中心",
|
||
"Error": "加载失败",
|
||
"ActiveTab": "drafts",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
return
|
||
}
|
||
|
||
c.HTML(http.StatusOK, "studio/drafts.html", common.BuildPageData(c, gin.H{
|
||
"Title": "草稿箱 - 创作中心",
|
||
"Posts": posts,
|
||
"Total": total,
|
||
"Page": page,
|
||
"PageSize": pageSize,
|
||
"TotalPages": common.PageCount(total, pageSize),
|
||
"StatusNames": common.PostStatusDisplayNames,
|
||
"ActiveTab": "drafts",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
}
|
||
|
||
// WritePage 写文章页面
|
||
func (ctrl *StudioController) WritePage(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.RedirectToLogin(c)
|
||
return
|
||
}
|
||
|
||
// 支持编辑模式:传入 post_id 参数
|
||
postIDStr := c.Query("id")
|
||
if postIDStr != "" {
|
||
id, err := strconv.ParseUint(postIDStr, 10, 64)
|
||
if err == nil {
|
||
post, err := ctrl.postService.GetByID(uint(id))
|
||
if err == nil {
|
||
_, role, _ := common.GetGinUser(c)
|
||
if !ctrl.postService.IsPostAccessible(uid, role, post.UserID) {
|
||
c.HTML(http.StatusNotFound, "posts/404.html", common.BuildPageData(c, gin.H{
|
||
"Title": "未找到",
|
||
}))
|
||
return
|
||
}
|
||
c.HTML(http.StatusOK, "studio/write.html", common.BuildPageData(c, gin.H{
|
||
"Title": "编辑文章 - 创作中心",
|
||
"Post": post,
|
||
"ActiveTab": "write",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
c.HTML(http.StatusOK, "studio/write.html", common.BuildPageData(c, gin.H{
|
||
"Title": "写文章 - 创作中心",
|
||
"ActiveTab": "write",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
}
|
||
|
||
// AnalyticsPage 数据分析页面
|
||
func (ctrl *StudioController) AnalyticsPage(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.RedirectToLogin(c)
|
||
return
|
||
}
|
||
|
||
overview, err := ctrl.postService.GetOverview(uid)
|
||
if err != nil {
|
||
c.HTML(http.StatusOK, "studio/analytics.html", common.BuildPageData(c, gin.H{
|
||
"Title": "数据分析 - 创作中心",
|
||
"Error": "加载失败",
|
||
"ActiveTab": "analytics",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
return
|
||
}
|
||
|
||
c.HTML(http.StatusOK, "studio/analytics.html", common.BuildPageData(c, gin.H{
|
||
"Title": "数据分析 - 创作中心",
|
||
"Overview": overview,
|
||
"ActiveTab": "analytics",
|
||
"ExtraCSS": "/static/css/studio.css",
|
||
}))
|
||
}
|
||
|
||
// ==============================
|
||
// API 方法
|
||
// ==============================
|
||
|
||
// OverviewAPI 创作中心数据概览
|
||
func (ctrl *StudioController) OverviewAPI(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
overview, err := ctrl.postService.GetOverview(uid)
|
||
if err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "获取数据失败")
|
||
return
|
||
}
|
||
|
||
common.Ok(c, overview)
|
||
}
|
||
|
||
// ListPostsAPI 我的帖子列表(支持状态筛选)
|
||
func (ctrl *StudioController) ListPostsAPI(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
status := c.Query("status")
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||
|
||
posts, total, err := ctrl.postService.ListByUser(uid, status, page, pageSize)
|
||
if err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "获取列表失败")
|
||
return
|
||
}
|
||
|
||
common.Ok(c, gin.H{
|
||
"items": posts,
|
||
"total": total,
|
||
"page": page,
|
||
"page_size": pageSize,
|
||
"total_pages": common.PageCount(total, pageSize),
|
||
})
|
||
}
|
||
|
||
// Create 创建/发布帖子
|
||
func (ctrl *StudioController) Create(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
// LV0 用户不允许投稿
|
||
if expVal, exists := c.Get("exp"); exists {
|
||
if exp, ok := expVal.(int); ok && exp < 1 {
|
||
common.Error(c, http.StatusForbidden, "经验不足,Lv1 解锁投稿")
|
||
return
|
||
}
|
||
}
|
||
|
||
var req model.PostCreateRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
common.Error(c, http.StatusBadRequest, "标题和正文不能为空")
|
||
return
|
||
}
|
||
|
||
post, err := ctrl.postService.Create(uid, req.Title, req.Body)
|
||
if err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "发布失败")
|
||
return
|
||
}
|
||
|
||
common.OkWithMessage(c, post, "发布成功")
|
||
}
|
||
|
||
// Update 编辑帖子
|
||
func (ctrl *StudioController) Update(c *gin.Context) {
|
||
uid, role, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
||
return
|
||
}
|
||
|
||
var req model.PostUpdateRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
common.Error(c, http.StatusBadRequest, "标题和正文不能为空")
|
||
return
|
||
}
|
||
|
||
post, getErr := ctrl.postService.GetByID(uint(id))
|
||
if getErr != nil {
|
||
common.Error(c, http.StatusNotFound, "帖子不存在")
|
||
return
|
||
}
|
||
if !ctrl.postService.IsPostAccessible(uid, role, post.UserID) {
|
||
common.Error(c, http.StatusForbidden, "无权操作此帖子")
|
||
return
|
||
}
|
||
|
||
if err := ctrl.postService.Update(uint(id), req.Title, req.Body); err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "编辑失败")
|
||
return
|
||
}
|
||
|
||
common.OkMessage(c, "编辑成功")
|
||
}
|
||
|
||
// Delete 删除帖子
|
||
func (ctrl *StudioController) Delete(c *gin.Context) {
|
||
uid, role, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
||
return
|
||
}
|
||
|
||
post, getErr := ctrl.postService.GetByID(uint(id))
|
||
if getErr != nil {
|
||
common.Error(c, http.StatusNotFound, "帖子不存在")
|
||
return
|
||
}
|
||
if !ctrl.postService.IsPostAccessible(uid, role, post.UserID) {
|
||
common.Error(c, http.StatusForbidden, "无权操作此帖子")
|
||
return
|
||
}
|
||
|
||
authorID := post.UserID
|
||
|
||
if err := ctrl.postService.Delete(uint(id)); err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "删除失败")
|
||
return
|
||
}
|
||
|
||
// 删稿扣作者域能(无视负数,有限重试后静默忽略)
|
||
if ctrl.energySvc != nil {
|
||
const maxRetries = 3
|
||
backoff := 100 * time.Millisecond
|
||
for i := 0; i < maxRetries; i++ {
|
||
if err := ctrl.energySvc.DeductOnDeletePost(authorID, uint(id)); err == nil {
|
||
break
|
||
}
|
||
if i < maxRetries-1 {
|
||
time.Sleep(backoff)
|
||
backoff *= 2
|
||
} else {
|
||
log.Printf("删稿扣域能失败(user=%d, post=%d): %v", authorID, id, err)
|
||
}
|
||
}
|
||
}
|
||
|
||
common.OkMessage(c, "删除成功")
|
||
}
|
||
|
||
// Submit 提交审核
|
||
func (ctrl *StudioController) Submit(c *gin.Context) {
|
||
uid, role, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||
if err != nil {
|
||
common.Error(c, http.StatusBadRequest, "无效的帖子 ID")
|
||
return
|
||
}
|
||
|
||
post, getErr := ctrl.postService.GetByID(uint(id))
|
||
if getErr != nil {
|
||
common.Error(c, http.StatusNotFound, "帖子不存在")
|
||
return
|
||
}
|
||
if !ctrl.postService.IsPostAccessible(uid, role, post.UserID) {
|
||
common.Error(c, http.StatusForbidden, "无权操作此帖子")
|
||
return
|
||
}
|
||
|
||
if err := ctrl.postService.SubmitForAudit(uint(id)); err != nil {
|
||
common.Error(c, http.StatusBadRequest, err.Error())
|
||
return
|
||
}
|
||
|
||
common.OkMessage(c, "已提交审核")
|
||
}
|
||
|
||
// TrendsAPI 创作中心趋势数据 API GET /api/studio/trends?period=7d|30d|90d
|
||
func (ctrl *StudioController) TrendsAPI(c *gin.Context) {
|
||
uid, _, ok := common.GetGinUser(c)
|
||
if !ok {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
if ctrl.trendsSvc == nil {
|
||
common.Error(c, http.StatusInternalServerError, "趋势服务不可用")
|
||
return
|
||
}
|
||
|
||
period := c.DefaultQuery("period", "7d")
|
||
var days int
|
||
switch period {
|
||
case "7d":
|
||
days = 7
|
||
case "30d":
|
||
days = 30
|
||
case "90d":
|
||
days = 90
|
||
default:
|
||
common.Error(c, http.StatusBadRequest, "无效的时间范围,支持 7d/30d/90d")
|
||
return
|
||
}
|
||
|
||
since := time.Now().AddDate(0, 0, -days).Format("2006-01-02")
|
||
|
||
result, err := ctrl.trendsSvc.GetTrends(uid, since)
|
||
if err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "获取趋势数据失败")
|
||
return
|
||
}
|
||
|
||
common.Ok(c, result)
|
||
}
|