feat: 小程序首页banner管理

This commit is contained in:
Blizzard
2026-04-27 11:26:01 +08:00
parent c5aa1c6f2c
commit 37b9055994
10 changed files with 241 additions and 0 deletions
+131
View File
@@ -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)
}
+2
View File
@@ -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
)
+1
View File
@@ -40,6 +40,7 @@ func MigrateTable() {
system.Oss{},
system.SysAiConfig{},
plant.Banner{}, //轮播图
plant.MyPlant{}, //我的植物
plant.CarePlan{}, //植物养护计划
plant.CareTask{}, //植物养护任务
+1
View File
@@ -62,6 +62,7 @@ func Routers() {
plantGroup.InitUserProfileRouter(NeedAuthGroup) //用户资料
plantGroup.InitExchangeRouter(NeedAuthGroup) //兑换中心
plantGroup.InitAiChatRouter(NeedAuthGroup) //AI聊天
plantGroup.InitBannerRouter(NeedAuthGroup) //轮播图管理
}
+20
View File
@@ -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"
}
+9
View File
@@ -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 禁用
}
+18
View File
@@ -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)
}
}
+2
View File
@@ -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
)
+56
View File
@@ -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
}
+1
View File
@@ -14,4 +14,5 @@ type ServiceGroup struct {
ExchangeService
AiRagService
AiChatHistoryService
BannerService
}