From 37b90559942e569a4fb72da02976317c7fae9197 Mon Sep 17 00:00:00 2001 From: Blizzard Date: Mon, 27 Apr 2026 11:26:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=8F=E7=A8=8B=E5=BA=8F=E9=A6=96?= =?UTF-8?q?=E9=A1=B5banner=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/plant/banner.go | 131 ++++++++++++++++++++++++++++++++++ api/v1/plant/enter.go | 2 + initialize/gorm.go | 1 + initialize/router.go | 1 + model/plant/banner.go | 20 ++++++ model/plant/request/banner.go | 9 +++ router/plant/banner.go | 18 +++++ router/plant/enter.go | 2 + service/plant/banner.go | 56 +++++++++++++++ service/plant/enter.go | 1 + 10 files changed, 241 insertions(+) create mode 100644 api/v1/plant/banner.go create mode 100644 model/plant/banner.go create mode 100644 model/plant/request/banner.go create mode 100644 router/plant/banner.go create mode 100644 service/plant/banner.go diff --git a/api/v1/plant/banner.go b/api/v1/plant/banner.go new file mode 100644 index 0000000..cb44f28 --- /dev/null +++ b/api/v1/plant/banner.go @@ -0,0 +1,131 @@ +package plant + +import ( + "sundynix-go/global" + "sundynix-go/model/commom/response" + "sundynix-go/model/plant" + "sundynix-go/model/plant/request" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +type BannerApi struct{} + +// Create 创建Banner +// @Tags 轮播图管理 +// @Summary 创建Banner +// @Security BearerAuth +// @accept application/json +// @Produce application/json +// @Param data body plant.Banner true "创建Banner" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /plantBanner/create [post] +func (a *BannerApi) Create(c *gin.Context) { + var req plant.Banner + if err := c.ShouldBindJSON(&req); err != nil { + response.FailWithMsg("参数错误", c) + return + } + if err := bannerService.Create(req); err != nil { + global.Logger.Error("创建失败", zap.Error(err)) + response.FailWithMsg("创建失败", c) + return + } + response.OkWithMsg("创建成功", c) +} + +// Delete 删除Banner +// @Tags 轮播图管理 +// @Summary 删除Banner +// @Security BearerAuth +// @accept application/json +// @Produce application/json +// @Param data body object true "删除Banner" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}" +// @Router /plantBanner/delete [post] +func (a *BannerApi) Delete(c *gin.Context) { + var req struct { + Id string `json:"id"` + } + if err := c.ShouldBindJSON(&req); err != nil || req.Id == "" { + response.FailWithMsg("参数错误", c) + return + } + if err := bannerService.Delete(req.Id); err != nil { + global.Logger.Error("删除失败", zap.Error(err)) + response.FailWithMsg("删除失败", c) + return + } + response.OkWithMsg("删除成功", c) +} + +// Update 更新Banner +// @Tags 轮播图管理 +// @Summary 更新Banner +// @Security BearerAuth +// @accept application/json +// @Produce application/json +// @Param data body plant.Banner true "更新Banner" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}" +// @Router /plantBanner/update [put] +func (a *BannerApi) Update(c *gin.Context) { + var req plant.Banner + if err := c.ShouldBindJSON(&req); err != nil { + response.FailWithMsg("参数错误", c) + return + } + if err := bannerService.Update(req); err != nil { + global.Logger.Error("更新失败", zap.Error(err)) + response.FailWithMsg("更新失败", c) + return + } + response.OkWithMsg("更新成功", c) +} + +// GetList 分页获取Banner +// @Tags 轮播图管理 +// @Summary 分页获取Banner +// @Security BearerAuth +// @accept application/json +// @Produce application/json +// @Param data body request.BannerPageReq true "分页获取Banner" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" +// @Router /plantBanner/list [post] +func (a *BannerApi) GetList(c *gin.Context) { + var req request.BannerPageReq + if err := c.ShouldBindJSON(&req); err != nil { + response.FailWithMsg("参数错误", c) + return + } + list, total, err := bannerService.GetList(req) + if err != nil { + global.Logger.Error("获取失败", zap.Error(err)) + response.FailWithMsg("获取失败", c) + return + } + response.OkWithData(response.PageResult{ + List: list, + Total: total, + Page: req.Current, + PageSize: req.PageSize, + }, c) +} + +// GetActiveList 客户端获取启用的Banner +// @Tags 轮播图管理 +// @Summary 客户端获取启用的Banner +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" +// @Router /plantBanner/activeList [get] +func (a *BannerApi) GetActiveList(c *gin.Context) { + list, err := bannerService.GetActiveList() + if err != nil { + global.Logger.Error("获取失败", zap.Error(err)) + response.FailWithMsg("获取失败", c) + return + } + response.OkWithData(response.ListResult{ + List: list, + }, c) +} diff --git a/api/v1/plant/enter.go b/api/v1/plant/enter.go index 28ca710..6dd26e1 100644 --- a/api/v1/plant/enter.go +++ b/api/v1/plant/enter.go @@ -15,6 +15,7 @@ type ApiGroup struct { CallbackApi ExchangeApi AiChatApi + BannerApi } var ( @@ -32,4 +33,5 @@ var ( aiRagService = service.GroupApp.PlantServiceGroup.AiRagService chatHistoryService = service.GroupApp.PlantServiceGroup.AiChatHistoryService sysAiConfigService = service.GroupApp.SystemServiceGroup.SysAiConfigService + bannerService = service.GroupApp.PlantServiceGroup.BannerService ) diff --git a/initialize/gorm.go b/initialize/gorm.go index 3d4b748..bb06d8b 100644 --- a/initialize/gorm.go +++ b/initialize/gorm.go @@ -40,6 +40,7 @@ func MigrateTable() { system.Oss{}, system.SysAiConfig{}, + plant.Banner{}, //轮播图 plant.MyPlant{}, //我的植物 plant.CarePlan{}, //植物养护计划 plant.CareTask{}, //植物养护任务 diff --git a/initialize/router.go b/initialize/router.go index a58fee5..6048981 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -62,6 +62,7 @@ func Routers() { plantGroup.InitUserProfileRouter(NeedAuthGroup) //用户资料 plantGroup.InitExchangeRouter(NeedAuthGroup) //兑换中心 plantGroup.InitAiChatRouter(NeedAuthGroup) //AI聊天 + plantGroup.InitBannerRouter(NeedAuthGroup) //轮播图管理 } diff --git a/model/plant/banner.go b/model/plant/banner.go new file mode 100644 index 0000000..fa3510d --- /dev/null +++ b/model/plant/banner.go @@ -0,0 +1,20 @@ +package plant + +import ( + "sundynix-go/global" + "sundynix-go/model/system" +) + +type Banner struct { + global.BaseModel + Title string `json:"title" gorm:"column:title;comment:标题"` + ImageId string `json:"imageId" gorm:"column:image_id;comment:图片ID"` + Image *system.Oss `json:"image" gorm:"foreignKey:ImageId"` + Sort int `json:"sort" gorm:"column:sort;default:0;comment:排序"` + IsActive int `json:"isActive" gorm:"column:is_active;default:1;comment:状态 1:启用 2:禁用"` + TargetUrl string `json:"targetUrl" gorm:"column:target_url;comment:跳转链接"` +} + +func (Banner) TableName() string { + return "sundynix_plant_banner" +} diff --git a/model/plant/request/banner.go b/model/plant/request/banner.go new file mode 100644 index 0000000..f35d960 --- /dev/null +++ b/model/plant/request/banner.go @@ -0,0 +1,9 @@ +package request + +import "sundynix-go/model/commom/request" + +type BannerPageReq struct { + request.PageInfo + Title string `json:"title" form:"title"` + IsActive *int `json:"isActive" form:"isActive"` // 1 启用 2 禁用 +} diff --git a/router/plant/banner.go b/router/plant/banner.go new file mode 100644 index 0000000..d1a29bf --- /dev/null +++ b/router/plant/banner.go @@ -0,0 +1,18 @@ +package plant + +import ( + "github.com/gin-gonic/gin" +) + +type BannerRouter struct{} + +func (c *BannerRouter) InitBannerRouter(Router *gin.RouterGroup) { + bannerRouter := Router.Group("plantBanner") + { + bannerRouter.POST("create", bannerApi.Create) + bannerRouter.POST("delete", bannerApi.Delete) + bannerRouter.POST("update", bannerApi.Update) + bannerRouter.POST("list", bannerApi.GetList) + bannerRouter.GET("activeList", bannerApi.GetActiveList) + } +} diff --git a/router/plant/enter.go b/router/plant/enter.go index e5cd2b0..8703296 100644 --- a/router/plant/enter.go +++ b/router/plant/enter.go @@ -15,6 +15,7 @@ type RouterGroup struct { CallbackRouter ExchangeRouter AiChatRouter + BannerRouter } // 初始化路由 @@ -31,4 +32,5 @@ var ( callbackApi = v1.ApiGroupApp.PlantApiGroup.CallbackApi exchangeApi = v1.ApiGroupApp.PlantApiGroup.ExchangeApi aiChatApi = v1.ApiGroupApp.PlantApiGroup.AiChatApi + bannerApi = v1.ApiGroupApp.PlantApiGroup.BannerApi ) diff --git a/service/plant/banner.go b/service/plant/banner.go new file mode 100644 index 0000000..9236eb3 --- /dev/null +++ b/service/plant/banner.go @@ -0,0 +1,56 @@ +package plant + +import ( + "sundynix-go/global" + "sundynix-go/model/plant" + "sundynix-go/model/plant/request" +) + +type BannerService struct{} + +func (s *BannerService) Create(req plant.Banner) error { + return global.DB.Create(&req).Error +} + +func (s *BannerService) Delete(id string) error { + return global.DB.Where("id = ?", id).Delete(&plant.Banner{}).Error +} + +func (s *BannerService) Update(req plant.Banner) error { + return global.DB.Model(&plant.Banner{}).Where("id = ?", req.Id).Updates(map[string]interface{}{ + "title": req.Title, + "image_id": req.ImageId, + "sort": req.Sort, + "is_active": req.IsActive, + "target_url": req.TargetUrl, + }).Error +} + +func (s *BannerService) GetList(req request.BannerPageReq) (list interface{}, total int64, err error) { + limit := req.PageSize + offset := req.PageSize * (req.Current - 1) + db := global.DB.Model(&plant.Banner{}).Preload("Image") + if req.Title != "" { + db = db.Where("title LIKE ?", "%"+req.Title+"%") + } + if req.IsActive != nil { + db = db.Where("is_active = ?", *req.IsActive) + } + + var banners []plant.Banner + err = db.Count(&total).Error + if err != nil { + return + } + err = db.Order("sort asc, created_at desc").Limit(limit).Offset(offset).Find(&banners).Error + return banners, total, err +} + +func (s *BannerService) GetActiveList() ([]plant.Banner, error) { + var banners []plant.Banner + err := global.DB.Model(&plant.Banner{}).Preload("Image"). + Where("is_active = 1"). + Order("sort asc, created_at desc"). + Find(&banners).Error + return banners, err +} diff --git a/service/plant/enter.go b/service/plant/enter.go index 6971acf..d943ca4 100644 --- a/service/plant/enter.go +++ b/service/plant/enter.go @@ -14,4 +14,5 @@ type ServiceGroup struct { ExchangeService AiRagService AiChatHistoryService + BannerService }