package controllers import ( "encoding/json" "fmt" "io" "strconv" "strings" "time" "server/models" "server/pkg/jwtutil" "github.com/beego/beego/v2/client/orm" beego "github.com/beego/beego/v2/server/web" ) type PlatformComplaintController struct { beego.Controller } func (c *PlatformComplaintController) platformClaims() (*jwtutil.Claims, error) { auth := c.Ctx.Request.Header.Get("Authorization") if auth == "" { return nil, fmt.Errorf("未登录") } parts := strings.SplitN(auth, " ", 2) if len(parts) != 2 || parts[0] != "Bearer" { return nil, fmt.Errorf("认证信息格式错误") } claims, err := jwtutil.ParseToken(parts[1]) if err != nil { return nil, fmt.Errorf("无效的token") } if claims.UserType != "platform" { return nil, fmt.Errorf("无权访问") } return claims, nil } func (c *PlatformComplaintController) jsonErr(httpStatus, bizCode int, msg string) { c.Ctx.Output.SetStatus(httpStatus) c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg} _ = c.ServeJSON() } func (c *PlatformComplaintController) ok(data interface{}) { c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data} _ = c.ServeJSON() } func categoryNameMap(ids []uint64) map[uint64]string { m := make(map[uint64]string) if len(ids) == 0 { return m } seen := make(map[uint64]bool) var uniq []uint64 for _, id := range ids { if id > 0 && !seen[id] { seen[id] = true uniq = append(uniq, id) } } if len(uniq) == 0 { return m } var cats []models.ComplaintCategory _, _ = models.Orm.QueryTable(new(models.ComplaintCategory)). Filter("id__in", uniq). Filter("delete_time__isnull", true). All(&cats) for _, x := range cats { m[x.ID] = x.Name } return m } // List GET /platform/complaint/list?page=1&pageSize=20&categoryId=&status=&keyword= func (c *PlatformComplaintController) List() { if _, err := c.platformClaims(); err != nil { c.jsonErr(401, 401, err.Error()) return } page, _ := c.GetInt("page", 1) pageSize, _ := c.GetInt("pageSize", 20) if page < 1 { page = 1 } if pageSize < 1 { pageSize = 20 } if pageSize > 200 { pageSize = 200 } var categoryID uint64 if s := strings.TrimSpace(c.GetString("categoryId")); s != "" { if v, err := strconv.ParseUint(s, 10, 64); err == nil { categoryID = v } } statusStr := strings.TrimSpace(c.GetString("status")) keyword := strings.TrimSpace(c.GetString("keyword")) qs := models.Orm.QueryTable(new(models.PlatformComplaint)).Filter("delete_time__isnull", true) if categoryID > 0 { qs = qs.Filter("category_id", categoryID) } if statusStr != "" { if st, err := strconv.Atoi(statusStr); err == nil { qs = qs.Filter("status", st) } } if keyword != "" { cond := orm.NewCondition(). Or("title__icontains", keyword). Or("content__icontains", keyword). Or("contact_name__icontains", keyword). Or("contact_phone__icontains", keyword). Or("contact_email__icontains", keyword) qs = qs.SetCond(cond) } total, _ := qs.Count() var rows []models.PlatformComplaint _, err := qs.OrderBy("-id").Limit(pageSize, (page-1)*pageSize).All(&rows) if err != nil { c.jsonErr(500, 500, "获取失败: "+err.Error()) return } ids := make([]uint64, 0, len(rows)) for _, r := range rows { ids = append(ids, r.CategoryID) } names := categoryNameMap(ids) list := make([]map[string]interface{}, 0, len(rows)) for _, r := range rows { list = append(list, map[string]interface{}{ "id": r.ID, "categoryId": r.CategoryID, "categoryName": names[r.CategoryID], "title": r.Title, "content": r.Content, "contactName": r.ContactName, "contactPhone": r.ContactPhone, "contactEmail": r.ContactEmail, "status": r.Status, "replyContent": r.ReplyContent, "replyTime": r.ReplyTime, "tid": r.Tid, "remark": r.Remark, "createTime": r.CreateTime, "updateTime": r.UpdateTime, }) } c.ok(map[string]interface{}{ "list": list, "total": total, "page": page, "pageSize": pageSize, }) } // Detail GET /platform/complaint/:id func (c *PlatformComplaintController) Detail() { if _, err := c.platformClaims(); err != nil { c.jsonErr(401, 401, err.Error()) return } id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64) if err != nil || id == 0 { c.jsonErr(400, 400, "无效ID") return } var row models.PlatformComplaint err = models.Orm.QueryTable(new(models.PlatformComplaint)). Filter("id", id). Filter("delete_time__isnull", true). One(&row) if err != nil { c.jsonErr(404, 404, "记录不存在") return } names := categoryNameMap([]uint64{row.CategoryID}) c.ok(map[string]interface{}{ "id": row.ID, "categoryId": row.CategoryID, "categoryName": names[row.CategoryID], "title": row.Title, "content": row.Content, "contactName": row.ContactName, "contactPhone": row.ContactPhone, "contactEmail": row.ContactEmail, "status": row.Status, "replyContent": row.ReplyContent, "replyTime": row.ReplyTime, "tid": row.Tid, "remark": row.Remark, "createTime": row.CreateTime, "updateTime": row.UpdateTime, }) } type complaintPayload struct { CategoryID *uint64 `json:"categoryId"` Title *string `json:"title"` Content *string `json:"content"` ContactName *string `json:"contactName"` ContactPhone *string `json:"contactPhone"` ContactEmail *string `json:"contactEmail"` Status *int8 `json:"status"` ReplyContent *string `json:"replyContent"` Tid *uint64 `json:"tid"` Remark *string `json:"remark"` } // Create POST /platform/complaint func (c *PlatformComplaintController) Create() { if _, err := c.platformClaims(); err != nil { c.jsonErr(401, 401, err.Error()) return } body, _ := io.ReadAll(c.Ctx.Request.Body) var p complaintPayload if err := json.Unmarshal(body, &p); err != nil { c.jsonErr(400, 400, "参数错误") return } if p.CategoryID == nil || *p.CategoryID == 0 || p.Title == nil || strings.TrimSpace(*p.Title) == "" || p.Content == nil || strings.TrimSpace(*p.Content) == "" { c.jsonErr(400, 400, "分类、标题、内容不能为空") return } row := models.PlatformComplaint{ CategoryID: *p.CategoryID, Title: strings.TrimSpace(*p.Title), Content: strings.TrimSpace(*p.Content), Status: 0, } if p.ContactName != nil { row.ContactName = p.ContactName } if p.ContactPhone != nil { row.ContactPhone = p.ContactPhone } if p.ContactEmail != nil { row.ContactEmail = p.ContactEmail } if p.Tid != nil { row.Tid = p.Tid } if p.Status != nil { row.Status = *p.Status } if p.Remark != nil { row.Remark = p.Remark } id, err := models.Orm.Insert(&row) if err != nil { c.jsonErr(500, 500, "创建失败: "+err.Error()) return } c.ok(map[string]interface{}{"id": id}) } // Update POST /platform/complaint/:id func (c *PlatformComplaintController) Update() { if _, err := c.platformClaims(); err != nil { c.jsonErr(401, 401, err.Error()) return } id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64) if err != nil || id == 0 { c.jsonErr(400, 400, "无效ID") return } body, _ := io.ReadAll(c.Ctx.Request.Body) var p complaintPayload if err := json.Unmarshal(body, &p); err != nil { c.jsonErr(400, 400, "参数错误") return } up := map[string]interface{}{} if p.CategoryID != nil && *p.CategoryID > 0 { up["category_id"] = *p.CategoryID } if p.Title != nil { up["title"] = strings.TrimSpace(*p.Title) } if p.Content != nil { up["content"] = strings.TrimSpace(*p.Content) } if p.ContactName != nil { up["contact_name"] = p.ContactName } if p.ContactPhone != nil { up["contact_phone"] = p.ContactPhone } if p.ContactEmail != nil { up["contact_email"] = p.ContactEmail } if p.Status != nil { up["status"] = *p.Status } if p.ReplyContent != nil { s := strings.TrimSpace(*p.ReplyContent) up["reply_content"] = s if s != "" { now := time.Now() up["reply_time"] = now } } if p.Tid != nil { up["tid"] = p.Tid } if p.Remark != nil { up["remark"] = p.Remark } if len(up) == 0 { c.jsonErr(400, 400, "无更新字段") return } n, err := models.Orm.QueryTable(new(models.PlatformComplaint)). Filter("id", id). Filter("delete_time__isnull", true). Update(up) if err != nil { c.jsonErr(500, 500, "更新失败: "+err.Error()) return } if n == 0 { c.jsonErr(404, 404, "记录不存在") return } c.ok(nil) } // Delete DELETE /platform/complaint/:id func (c *PlatformComplaintController) Delete() { if _, err := c.platformClaims(); err != nil { c.jsonErr(401, 401, err.Error()) return } id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64) if err != nil || id == 0 { c.jsonErr(400, 400, "无效ID") return } now := time.Now() n, err := models.Orm.QueryTable(new(models.PlatformComplaint)). Filter("id", id). Filter("delete_time__isnull", true). Update(map[string]interface{}{"delete_time": now}) if err != nil { c.jsonErr(500, 500, "删除失败: "+err.Error()) return } if n == 0 { c.jsonErr(404, 404, "记录不存在") return } c.ok(nil) }