package admin import ( "net/http" "strconv" "bindbox-game/internal/code" "bindbox-game/internal/pkg/core" "bindbox-game/internal/pkg/validation" ) type listSysConfigsRequest struct { Page int `form:"page"` PageSize int `form:"page_size"` Keyword string `form:"keyword"` } type sysConfigItem struct { ID int64 `json:"id"` Key string `json:"key"` Value string `json:"value"` Remark string `json:"remark"` } type listSysConfigsResponse struct { Page int `json:"page"` PageSize int `json:"page_size"` Total int64 `json:"total"` List []sysConfigItem `json:"list"` } type upsertSysConfigRequest struct { Key string `json:"key" binding:"required"` Value string `json:"value" binding:"required"` Remark string `json:"remark"` } type upsertSysConfigResponse struct { ID int64 `json:"id"` Message string `json:"message"` } type modifySysConfigRequest struct { Value *string `json:"value"` Remark *string `json:"remark"` } func (h *handler) ListSystemConfigs() core.HandlerFunc { return func(ctx core.Context) { req := new(listSysConfigsRequest) rsp := new(listSysConfigsResponse) if err := ctx.ShouldBindForm(req); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err))) return } items, total, err := h.syscfg.List(ctx.RequestContext(), req.Page, req.PageSize, req.Keyword) if err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, validation.Error(err))) return } rsp.Page = req.Page rsp.PageSize = req.PageSize rsp.Total = total rsp.List = make([]sysConfigItem, len(items)) for i, it := range items { rsp.List[i] = sysConfigItem{ID: it.ID, Key: it.ConfigKey, Value: it.ConfigValue, Remark: it.Remark} } ctx.Payload(rsp) } } func (h *handler) UpsertSystemConfig() core.HandlerFunc { return func(ctx core.Context) { req := new(upsertSysConfigRequest) rsp := new(upsertSysConfigResponse) if err := ctx.ShouldBindJSON(req); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err))) return } if ctx.SessionUserInfo().IsSuper != 1 { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, "禁止操作")) return } m, err := h.syscfg.UpsertByKey(ctx.RequestContext(), req.Key, req.Value, req.Remark) if err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, validation.Error(err))) return } rsp.ID = m.ID rsp.Message = "操作成功" ctx.Payload(rsp) } } func (h *handler) ModifySystemConfig() core.HandlerFunc { return func(ctx core.Context) { req := new(modifySysConfigRequest) if err := ctx.ShouldBindJSON(req); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err))) return } idStr := ctx.Param("id") id, _ := strconv.ParseInt(idStr, 10, 64) if ctx.SessionUserInfo().IsSuper != 1 { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, "禁止操作")) return } if err := h.syscfg.ModifyByID(ctx.RequestContext(), id, req.Value, req.Remark); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, validation.Error(err))) return } ctx.Payload(pcSimpleMessage{Message: "操作成功"}) } } func (h *handler) DeleteSystemConfig() core.HandlerFunc { return func(ctx core.Context) { idStr := ctx.Param("id") id, _ := strconv.ParseInt(idStr, 10, 64) if ctx.SessionUserInfo().IsSuper != 1 { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, "禁止操作")) return } if err := h.syscfg.DeleteByID(ctx.RequestContext(), id); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateAdminError, validation.Error(err))) return } ctx.Payload(pcSimpleMessage{Message: "操作成功"}) } }