Files
2026-03-05 16:54:25 +08:00

185 lines
4.8 KiB
Go

package radio
import (
"sundynix-go/global"
common "sundynix-go/model/commom/request"
"sundynix-go/model/commom/response"
"sundynix-go/model/radio/request"
"sundynix-go/utils/auth"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type ChannelApi struct{}
// GetFreeChannelList 获取免费频道列表
// @Tags 频道管理
// @Summary 获取频道列表
// @Accept application/json
// @Produce application/json
// @Param data body request.GetChannelList true "分页查询"
// @Success 200 {object} response.Response
// @Router /radio/channel/freeList [post]
func (a *ChannelApi) GetFreeChannelList(c *gin.Context) {
var req common.PageInfo
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMsg("参数错误: "+err.Error(), c)
return
}
list, total, err := channelService.GetFreeChannelList(req)
if err != nil {
global.Logger.Error("获取频道列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(response.PageResult{
List: list,
Total: total,
Page: req.Current,
PageSize: req.PageSize,
}, c)
}
// GetChannelList 获取频道列表
// @Tags 频道管理
// @Summary 获取频道列表
// @Accept application/json
// @Produce application/json
// @Param data body request.GetChannelList true "分页查询"
// @Success 200 {object} response.Response
// @Router /radio/channel/list [post]
func (a *ChannelApi) GetChannelList(c *gin.Context) {
userId := auth.GetUserId(c)
if userId == "" || userId == "0" {
response.FailWithMsg("用户未登录", c)
return
}
var req request.GetChannelList
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMsg("参数错误: "+err.Error(), c)
return
}
list, total, err := channelService.GetChannelList(userId, req)
if err != nil {
global.Logger.Error("获取频道列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(response.PageResult{
List: list,
Total: total,
Page: req.Current,
PageSize: req.PageSize,
}, c)
}
// GetChannelDetail 获取频道详情
// @Tags 频道管理
// @Summary 获取频道详情
// @Accept application/json
// @Produce application/json
// @Param id query string true "频道ID"
// @Success 200 {object} response.Response
// @Router /radio/channel/detail [get]
func (a *ChannelApi) GetChannelDetail(c *gin.Context) {
userId := auth.GetUserId(c)
if userId == "" || userId == "0" {
response.FailWithMsg("用户未登录", c)
return
}
id := c.Query("id")
if id == "" {
response.FailWithMsg("参数错误: id不能为空", c)
return
}
channel, err := channelService.GetChannelById(userId, id)
if err != nil {
global.Logger.Error("获取频道详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(channel, c)
}
// SaveChannel 保存频道
// @Tags 频道管理
// @Summary 保存频道
// @Accept application/json
// @Produce application/json
// @Param data body request.SaveChannel true "频道信息"
// @Success 200 {object} response.Response
// @Router /radio/channel/save [post]
func (a *ChannelApi) SaveChannel(c *gin.Context) {
var req request.SaveChannel
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMsg("参数错误: "+err.Error(), c)
return
}
if err := channelService.SaveChannel(req); err != nil {
global.Logger.Error("保存频道失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("保存成功", c)
}
// UpdateChannel 更新频道
// @Tags 频道管理
// @Summary 更新频道
// @Accept application/json
// @Produce application/json
// @Param data body request.UpdateChannel true "频道信息"
// @Success 200 {object} response.Response
// @Router /radio/channel/update [post]
func (a *ChannelApi) UpdateChannel(c *gin.Context) {
var req request.UpdateChannel
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMsg("参数错误: "+err.Error(), c)
return
}
if err := channelService.UpdateChannel(req); err != nil {
global.Logger.Error("更新频道失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("更新成功", c)
}
// DeleteChannel 删除频道
// @Tags 频道管理
// @Summary 删除频道
// @Produce json
// @Param data body common.GetById true "频道ID"
// @Success 200 {object} response.Response
// @Router /radio/channel/delete [post]
func (a *ChannelApi) DeleteChannel(c *gin.Context) {
var req common.IdReq
err := c.ShouldBindJSON(&req)
if err != nil || req.Id == "" {
response.FailWithMsg("参数错误: id不能为空", c)
return
}
if err := channelService.DeleteChannel(req.Id); err != nil {
global.Logger.Error("删除频道失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("删除成功", c)
}