package handler import ( "errors" "net/http" "strconv" "github.com/gin-gonic/gin" "wx_service/internal/marketing/repository" "wx_service/internal/marketing/service" "wx_service/internal/middleware" "wx_service/internal/model" ) type UserLogoHandler struct { svc *service.UserLogoService } func NewUserLogoHandler(svc *service.UserLogoService) *UserLogoHandler { return &UserLogoHandler{svc: svc} } func (h *UserLogoHandler) List(c *gin.Context) { user := middleware.MustCurrentUser(c) logos, err := h.svc.List(user.ID) if err != nil { c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取 Logo 列表失败")) return } c.JSON(http.StatusOK, model.Success(logos)) } func (h *UserLogoHandler) Save(c *gin.Context) { user := middleware.MustCurrentUser(c) var req service.SaveLogoRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误")) return } logo, err := h.svc.Save(user.ID, req) if err != nil { if errors.Is(err, service.ErrLogoLimitReached) || errors.Is(err, service.ErrLogoTooLarge) { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, err.Error())) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "保存 Logo 失败")) return } c.JSON(http.StatusOK, model.Success(logo)) } func (h *UserLogoHandler) Delete(c *gin.Context) { user := middleware.MustCurrentUser(c) id, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil || id == 0 { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "无效的 ID")) return } if err := h.svc.Delete(uint(id), user.ID); err != nil { if errors.Is(err, repository.ErrUserLogoNotFound) { c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "Logo 不存在")) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "删除失败")) return } c.JSON(http.StatusOK, model.Success(nil)) }