package controllers import ( "strings" "time" "server/models" beego "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/client/orm" ) // ============================================================= // 平台官网 - 标签管理 /platform/website/tag/* // - 评论管理 /platform/website/comment/* // 表:yz_platform_website_tag / yz_platform_website_comment // ============================================================= type PlatformWebsiteTagController struct { beego.Controller } type platformWebsiteTagPayload struct { Name *string `json:"name"` Sort *int `json:"sort"` Status *int8 `json:"status"` } // List GET /platform/website/tag/list — 分页 func (c *PlatformWebsiteTagController) List() { if _, ok := websiteAuth(&c.Controller); !ok { return } page, pageSize := websitePaging(&c.Controller) qs := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("delete_time__isnull", true) if kw := strings.TrimSpace(c.GetString("keyword")); kw != "" { qs = qs.Filter("name__icontains", kw) } if st := websiteStatusParam(&c.Controller); st >= 0 { qs = qs.Filter("status", st) } total, err := qs.Count() if err != nil { jsonErr(&c.Controller, 500, 500, "获取失败: "+err.Error()) return } var rows []models.PlatformWebsiteTag if _, err = qs.OrderBy("sort", "id").Limit(pageSize, (page-1)*pageSize).All(&rows); err != nil { jsonErr(&c.Controller, 500, 500, "获取失败: "+err.Error()) return } websiteOK(&c.Controller, map[string]interface{}{"list": rows, "total": total}) } // All GET /platform/website/tag/all — 全量返回,供文章编辑选择标签 func (c *PlatformWebsiteTagController) All() { if _, ok := websiteAuth(&c.Controller); !ok { return } var rows []models.PlatformWebsiteTag if _, err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("delete_time__isnull", true). OrderBy("sort", "id"). All(&rows); err != nil { jsonErr(&c.Controller, 500, 500, "获取失败: "+err.Error()) return } websiteOK(&c.Controller, rows) } // Get GET /platform/website/tag/:id func (c *PlatformWebsiteTagController) Get() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } var row models.PlatformWebsiteTag if err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("id", id). Filter("delete_time__isnull", true). One(&row); err != nil { jsonErr(&c.Controller, 404, 404, "标签不存在") return } websiteOK(&c.Controller, row) } // Create POST /platform/website/tag func (c *PlatformWebsiteTagController) Create() { if _, ok := websiteAuth(&c.Controller); !ok { return } var p platformWebsiteTagPayload if !websiteBind(&c.Controller, &p) { return } if p.Name == nil || strings.TrimSpace(*p.Name) == "" { jsonErr(&c.Controller, 400, 400, "标签名称不能为空") return } name := strings.TrimSpace(*p.Name) // 同名标签去重,避免前台聚合出现重复项 exist, err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("name", name). Filter("delete_time__isnull", true). Count() if err != nil { jsonErr(&c.Controller, 500, 500, "创建失败: "+err.Error()) return } if exist > 0 { jsonErr(&c.Controller, 400, 400, "标签名称已存在") return } row := models.PlatformWebsiteTag{Name: name, Status: 1} if p.Sort != nil { row.Sort = *p.Sort } if p.Status != nil { row.Status = *p.Status } id, err := models.Orm.Insert(&row) if err != nil { jsonErr(&c.Controller, 500, 500, "创建失败: "+err.Error()) return } websiteOK(&c.Controller, map[string]interface{}{"id": id}) } // Update POST /platform/website/tag/:id func (c *PlatformWebsiteTagController) Update() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } var p platformWebsiteTagPayload if !websiteBind(&c.Controller, &p) { return } up := map[string]interface{}{} if p.Name != nil { name := strings.TrimSpace(*p.Name) if name == "" { jsonErr(&c.Controller, 400, 400, "标签名称不能为空") return } dup, err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("name", name). Filter("delete_time__isnull", true). Exclude("id", id). Count() if err != nil { jsonErr(&c.Controller, 500, 500, "更新失败: "+err.Error()) return } if dup > 0 { jsonErr(&c.Controller, 400, 400, "标签名称已存在") return } up["name"] = name } if p.Sort != nil { up["sort"] = *p.Sort } if p.Status != nil { up["status"] = *p.Status } if len(up) == 0 { jsonErr(&c.Controller, 400, 400, "无更新字段") return } up["update_time"] = time.Now() n, err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("id", id). Filter("delete_time__isnull", true). Update(up) if err != nil { jsonErr(&c.Controller, 500, 500, "更新失败: "+err.Error()) return } if n == 0 { jsonErr(&c.Controller, 404, 404, "标签不存在") return } websiteOK(&c.Controller, nil) } // UpdateStatus PATCH /platform/website/tag/:id/status — body { status } func (c *PlatformWebsiteTagController) UpdateStatus() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } var p struct { Status *int8 `json:"status"` } if !websiteBind(&c.Controller, &p) || p.Status == nil { jsonErr(&c.Controller, 400, 400, "状态参数错误") return } n, err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("id", id). Filter("delete_time__isnull", true). Update(map[string]interface{}{"status": *p.Status, "update_time": time.Now()}) if err != nil { jsonErr(&c.Controller, 500, 500, "更新失败: "+err.Error()) return } if n == 0 { jsonErr(&c.Controller, 404, 404, "标签不存在") return } websiteOK(&c.Controller, nil) } // Delete DELETE /platform/website/tag/:id func (c *PlatformWebsiteTagController) Delete() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } now := time.Now() n, err := models.Orm.QueryTable(new(models.PlatformWebsiteTag)). Filter("id", id). Filter("delete_time__isnull", true). Update(map[string]interface{}{"delete_time": now, "update_time": now}) if err != nil { jsonErr(&c.Controller, 500, 500, "删除失败: "+err.Error()) return } if n == 0 { jsonErr(&c.Controller, 404, 404, "标签不存在") return } // 同步清理文章关联,避免前台聚合到已删除标签 _, _ = models.Orm.QueryTable(new(models.PlatformWebsiteArticleTag)). Filter("tag_id", id). Delete() websiteOK(&c.Controller, nil) } // ============================================================= // 评论管理 // ============================================================= type PlatformWebsiteCommentController struct { beego.Controller } // List GET /platform/website/comment/list // query: keyword(内容/昵称)、status、article_id、page、pageSize func (c *PlatformWebsiteCommentController) List() { if _, ok := websiteAuth(&c.Controller); !ok { return } page, pageSize := websitePaging(&c.Controller) cond := orm.NewCondition().And("delete_time__isnull", true) if kw := strings.TrimSpace(c.GetString("keyword")); kw != "" { cond = cond.AndCond(orm.NewCondition(). Or("content__icontains", kw). Or("nickname__icontains", kw)) } if st := websiteStatusParam(&c.Controller); st >= 0 { cond = cond.And("status", st) } if articleID, err := c.GetInt64("article_id"); err == nil && articleID > 0 { cond = cond.And("article_id", articleID) } qs := models.Orm.QueryTable(new(models.PlatformWebsiteComment)).SetCond(cond) total, err := qs.Count() if err != nil { jsonErr(&c.Controller, 500, 500, "获取失败: "+err.Error()) return } var rows []models.PlatformWebsiteComment if _, err = qs.OrderBy("-id").Limit(pageSize, (page-1)*pageSize).All(&rows); err != nil { jsonErr(&c.Controller, 500, 500, "获取失败: "+err.Error()) return } articleIDs := make([]uint64, 0, len(rows)) for _, r := range rows { if r.ArticleID > 0 { articleIDs = append(articleIDs, r.ArticleID) } } titleMap := models.PlatformWebsiteArticleTitleMap(articleIDs) list := make([]map[string]interface{}, 0, len(rows)) for _, r := range rows { list = append(list, map[string]interface{}{ "id": r.ID, "article_id": r.ArticleID, "article_title": titleMap[r.ArticleID], "nickname": r.Nickname, "email": r.Email, "content": r.Content, "reply": r.Reply, "status": r.Status, "ip": r.IP, "create_time": r.CreateTime, "update_time": r.UpdateTime, }) } websiteOK(&c.Controller, map[string]interface{}{"list": list, "total": total}) } // Get GET /platform/website/comment/:id func (c *PlatformWebsiteCommentController) Get() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } var row models.PlatformWebsiteComment if err := models.Orm.QueryTable(new(models.PlatformWebsiteComment)). Filter("id", id). Filter("delete_time__isnull", true). One(&row); err != nil { jsonErr(&c.Controller, 404, 404, "评论不存在") return } websiteOK(&c.Controller, row) } // Audit POST /platform/website/comment/:id/audit — body { status: 1 通过 / 2 拒绝 } func (c *PlatformWebsiteCommentController) Audit() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } var p struct { Status *int8 `json:"status"` } if !websiteBind(&c.Controller, &p) || p.Status == nil { jsonErr(&c.Controller, 400, 400, "状态参数错误") return } if *p.Status != 1 && *p.Status != 2 { jsonErr(&c.Controller, 400, 400, "仅支持通过(1)或拒绝(2)") return } n, err := models.Orm.QueryTable(new(models.PlatformWebsiteComment)). Filter("id", id). Filter("delete_time__isnull", true). Update(map[string]interface{}{"status": *p.Status, "update_time": time.Now()}) if err != nil { jsonErr(&c.Controller, 500, 500, "审核失败: "+err.Error()) return } if n == 0 { jsonErr(&c.Controller, 404, 404, "评论不存在") return } websiteOK(&c.Controller, nil) } // Reply POST /platform/website/comment/:id/reply — body { reply } func (c *PlatformWebsiteCommentController) Reply() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } var p struct { Reply *string `json:"reply"` } if !websiteBind(&c.Controller, &p) || p.Reply == nil || strings.TrimSpace(*p.Reply) == "" { jsonErr(&c.Controller, 400, 400, "回复内容不能为空") return } n, err := models.Orm.QueryTable(new(models.PlatformWebsiteComment)). Filter("id", id). Filter("delete_time__isnull", true). Update(map[string]interface{}{ "reply": strings.TrimSpace(*p.Reply), "update_time": time.Now(), }) if err != nil { jsonErr(&c.Controller, 500, 500, "回复失败: "+err.Error()) return } if n == 0 { jsonErr(&c.Controller, 404, 404, "评论不存在") return } websiteOK(&c.Controller, nil) } // Delete DELETE /platform/website/comment/:id func (c *PlatformWebsiteCommentController) Delete() { if _, ok := websiteAuth(&c.Controller); !ok { return } id, ok := websiteID(&c.Controller) if !ok { return } now := time.Now() n, err := models.Orm.QueryTable(new(models.PlatformWebsiteComment)). Filter("id", id). Filter("delete_time__isnull", true). Update(map[string]interface{}{"delete_time": now, "update_time": now}) if err != nil { jsonErr(&c.Controller, 500, 500, "删除失败: "+err.Error()) return } if n == 0 { jsonErr(&c.Controller, 404, 404, "评论不存在") return } websiteOK(&c.Controller, nil) } // BatchDelete POST /platform/website/comment/batchdelete — body { ids: [] } func (c *PlatformWebsiteCommentController) BatchDelete() { if _, ok := websiteAuth(&c.Controller); !ok { return } var raw struct { IDs []uint64 `json:"ids"` } if !websiteBind(&c.Controller, &raw) || len(raw.IDs) == 0 { jsonErr(&c.Controller, 400, 400, "请选择要删除的评论") return } now := time.Now() if _, err := models.Orm.QueryTable(new(models.PlatformWebsiteComment)). Filter("id__in", raw.IDs). Filter("delete_time__isnull", true). Update(map[string]interface{}{"delete_time": now, "update_time": now}); err != nil { jsonErr(&c.Controller, 500, 500, "删除失败: "+err.Error()) return } websiteOK(&c.Controller, nil) }