feat: 小程序首页banner管理
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ type ApiGroup struct {
|
|||||||
CallbackApi
|
CallbackApi
|
||||||
ExchangeApi
|
ExchangeApi
|
||||||
AiChatApi
|
AiChatApi
|
||||||
|
BannerApi
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -32,4 +33,5 @@ var (
|
|||||||
aiRagService = service.GroupApp.PlantServiceGroup.AiRagService
|
aiRagService = service.GroupApp.PlantServiceGroup.AiRagService
|
||||||
chatHistoryService = service.GroupApp.PlantServiceGroup.AiChatHistoryService
|
chatHistoryService = service.GroupApp.PlantServiceGroup.AiChatHistoryService
|
||||||
sysAiConfigService = service.GroupApp.SystemServiceGroup.SysAiConfigService
|
sysAiConfigService = service.GroupApp.SystemServiceGroup.SysAiConfigService
|
||||||
|
bannerService = service.GroupApp.PlantServiceGroup.BannerService
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ func MigrateTable() {
|
|||||||
system.Oss{},
|
system.Oss{},
|
||||||
system.SysAiConfig{},
|
system.SysAiConfig{},
|
||||||
|
|
||||||
|
plant.Banner{}, //轮播图
|
||||||
plant.MyPlant{}, //我的植物
|
plant.MyPlant{}, //我的植物
|
||||||
plant.CarePlan{}, //植物养护计划
|
plant.CarePlan{}, //植物养护计划
|
||||||
plant.CareTask{}, //植物养护任务
|
plant.CareTask{}, //植物养护任务
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ func Routers() {
|
|||||||
plantGroup.InitUserProfileRouter(NeedAuthGroup) //用户资料
|
plantGroup.InitUserProfileRouter(NeedAuthGroup) //用户资料
|
||||||
plantGroup.InitExchangeRouter(NeedAuthGroup) //兑换中心
|
plantGroup.InitExchangeRouter(NeedAuthGroup) //兑换中心
|
||||||
plantGroup.InitAiChatRouter(NeedAuthGroup) //AI聊天
|
plantGroup.InitAiChatRouter(NeedAuthGroup) //AI聊天
|
||||||
|
plantGroup.InitBannerRouter(NeedAuthGroup) //轮播图管理
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -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 禁用
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ type RouterGroup struct {
|
|||||||
CallbackRouter
|
CallbackRouter
|
||||||
ExchangeRouter
|
ExchangeRouter
|
||||||
AiChatRouter
|
AiChatRouter
|
||||||
|
BannerRouter
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化路由
|
// 初始化路由
|
||||||
@@ -31,4 +32,5 @@ var (
|
|||||||
callbackApi = v1.ApiGroupApp.PlantApiGroup.CallbackApi
|
callbackApi = v1.ApiGroupApp.PlantApiGroup.CallbackApi
|
||||||
exchangeApi = v1.ApiGroupApp.PlantApiGroup.ExchangeApi
|
exchangeApi = v1.ApiGroupApp.PlantApiGroup.ExchangeApi
|
||||||
aiChatApi = v1.ApiGroupApp.PlantApiGroup.AiChatApi
|
aiChatApi = v1.ApiGroupApp.PlantApiGroup.AiChatApi
|
||||||
|
bannerApi = v1.ApiGroupApp.PlantApiGroup.BannerApi
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -14,4 +14,5 @@ type ServiceGroup struct {
|
|||||||
ExchangeService
|
ExchangeService
|
||||||
AiRagService
|
AiRagService
|
||||||
AiChatHistoryService
|
AiChatHistoryService
|
||||||
|
BannerService
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user