邹方成 45815bfb7d chore: 清理无用文件与优化代码结构
refactor(utils): 修复密码哈希比较逻辑错误
feat(user): 新增按状态筛选优惠券接口
docs: 添加虚拟发货与任务中心相关文档
fix(wechat): 修正Code2Session上下文传递问题
test: 补充订单折扣与积分转换测试用例
build: 更新配置文件与构建脚本
style: 清理多余的空行与注释
2025-12-18 17:35:55 +08:00

164 lines
4.7 KiB
Go

package admin
import (
"net/http"
"strconv"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
channelsvc "bindbox-game/internal/service/channel"
)
type createChannelRequest struct {
Name string `json:"name" binding:"required"`
Code string `json:"code" binding:"required"`
Type string `json:"type"`
Remarks string `json:"remarks"`
}
type createChannelResponse struct {
ID int64 `json:"id"`
Message string `json:"message"`
}
// CreateChannel 创建渠道
func (h *handler) CreateChannel() core.HandlerFunc {
return func(ctx core.Context) {
req := new(createChannelRequest)
res := new(createChannelResponse)
if err := ctx.ShouldBindJSON(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
item, err := h.channel.Create(ctx.RequestContext(), channelsvc.CreateInput{Name: req.Name, Code: req.Code, Type: req.Type, Remarks: req.Remarks})
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
return
}
res.ID = item.ID
res.Message = "操作成功"
ctx.Payload(res)
}
}
type channelStatsRequest struct {
Days int `form:"days"`
}
// ChannelStats 渠道数据分析
func (h *handler) ChannelStats() core.HandlerFunc {
return func(ctx core.Context) {
req := new(channelStatsRequest)
if err := ctx.ShouldBindForm(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
idStr := ctx.Param("channel_id")
id, _ := strconv.ParseInt(idStr, 10, 64)
stats, err := h.channel.GetStats(ctx.RequestContext(), id, req.Days)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
return
}
ctx.Payload(stats)
}
}
type modifyChannelRequest struct {
Name *string `json:"name"`
Type *string `json:"type"`
Remarks *string `json:"remarks"`
}
// ModifyChannel 修改渠道
func (h *handler) ModifyChannel() core.HandlerFunc {
return func(ctx core.Context) {
req := new(modifyChannelRequest)
if err := ctx.ShouldBindJSON(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
idStr := ctx.Param("channel_id")
id, _ := strconv.ParseInt(idStr, 10, 64)
if err := h.channel.Modify(ctx.RequestContext(), id, channelsvc.ModifyInput{Name: req.Name, Type: req.Type, Remarks: req.Remarks}); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
return
}
ctx.Payload(pcSimpleMessage{Message: "操作成功"})
}
}
// DeleteChannel 删除渠道
func (h *handler) DeleteChannel() core.HandlerFunc {
return func(ctx core.Context) {
idStr := ctx.Param("channel_id")
id, _ := strconv.ParseInt(idStr, 10, 64)
if err := h.channel.Delete(ctx.RequestContext(), id); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
return
}
ctx.Payload(pcSimpleMessage{Message: "操作成功"})
}
}
type listChannelsRequest struct {
Name string `form:"name"`
Page int `form:"page"`
PageSize int `form:"page_size"`
}
type channelItem struct {
ID int64 `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Type string `json:"type"`
Remarks string `json:"remarks"`
UserCount int64 `json:"user_count"`
CreatedAt string `json:"created_at"`
}
type listChannelsResponse struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int64 `json:"total"`
List []channelItem `json:"list"`
}
// ListChannels 查看渠道列表
func (h *handler) ListChannels() core.HandlerFunc {
return func(ctx core.Context) {
req := new(listChannelsRequest)
res := new(listChannelsResponse)
if err := ctx.ShouldBindForm(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
items, total, err := h.channel.List(ctx.RequestContext(), channelsvc.ListInput{Name: req.Name, Page: req.Page, PageSize: req.PageSize})
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, err.Error()))
return
}
res.Page = req.Page
res.PageSize = req.PageSize
res.Total = total
res.List = make([]channelItem, len(items))
for i, it := range items {
res.List[i] = channelItem{
ID: it.ID,
Name: it.Name,
Code: it.Code,
Type: it.Type,
Remarks: it.Remarks,
UserCount: it.UserCount,
CreatedAt: it.CreatedAt.Format("2006-01-02 15:04:05"),
}
}
ctx.Payload(res)
}
}