package system import ( "encoding/json" "net/http" "strings" "management/internal/db/model/dto" db "management/internal/db/sqlc" "management/internal/global/pearadmin" "management/internal/pkg/redis" "management/internal/router/manage/util" systemservice "management/internal/service/system" "management/internal/tpl" ) type SysConfigHandler struct{} func NewSysConfigHandler() *SysConfigHandler { return &SysConfigHandler{} } func (h *SysConfigHandler) Pear(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pear, err := systemservice.PearConfig(ctx) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tpl.JSON(w, pear) } func (h *SysConfigHandler) List(w http.ResponseWriter, r *http.Request) { tpl.HTML(w, r, "config/list.tmpl", nil) } func (h *SysConfigHandler) PostList(w http.ResponseWriter, r *http.Request) { var q dto.SearchDto q.SearchKey = r.PostFormValue("SearchKey") q.Page = util.ConvertInt(r.PostFormValue("page"), 1) q.Rows = util.ConvertInt(r.PostFormValue("rows"), 10) ctx := r.Context() res, count, err := systemservice.ListSysConfigCondition(ctx, q) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data := tpl.ResponseList{ Code: 0, Message: "ok", Count: count, Data: res, } tpl.JSON(w, data) } type EditSysConfig struct { *db.SysConfig Result string } func (h *SysConfigHandler) Add(w http.ResponseWriter, r *http.Request) { tpl.HTML(w, r, "config/edit.tmpl", map[string]any{ "Item": &db.SysConfig{}, "Result": "", }) } func (h *SysConfigHandler) Edit(w http.ResponseWriter, r *http.Request) { vars := r.URL.Query() id := util.DefaultInt(vars, "id", 0) vm := &EditSysConfig{} if id > 0 { ctx := r.Context() if conf, err := systemservice.GetSysConfig(ctx, int32(id)); err == nil { vm.SysConfig = conf vm.Result = string(conf.Value) } } tpl.HTML(w, r, "config/edit.tmpl", map[string]any{ "Item": vm.SysConfig, "Result": vm.Result, }) } func (h *SysConfigHandler) Save(w http.ResponseWriter, r *http.Request) { id := util.ConvertInt(r.PostFormValue("ID"), 0) key := r.PostFormValue("Key") value := r.PostFormValue("Value") if len(key) == 0 { tpl.JSON(w, tpl.Response{Success: false, Message: "Key不能为空"}) return } if len(value) == 0 { tpl.JSON(w, tpl.Response{Success: false, Message: "Value不能为空"}) return } ctx := r.Context() if id == 0 { arg := &db.CreateSysConfigParams{ Key: key, Value: []byte(value), } err := systemservice.CreateSysConfig(ctx, arg) if err != nil { if db.IsUniqueViolation(err) { tpl.JSON(w, tpl.Response{Success: false, Message: "数据已存在"}) return } tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()}) return } tpl.JSON(w, tpl.Response{Success: true, Message: "添加成功"}) } else { res, err := systemservice.GetSysConfig(ctx, int32(id)) if err != nil { tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()}) return } arg := &db.UpdateSysConfigByKeyParams{ Key: res.Key, Value: []byte(value), } err = systemservice.UpdateSysConfigByKey(ctx, arg) if err != nil { tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()}) return } tpl.JSON(w, tpl.Response{Success: true, Message: "更新成功"}) } } func (h *SysConfigHandler) ResetPear(w http.ResponseWriter, r *http.Request) { b, err := json.Marshal(pearadmin.PearJson) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } ctx := r.Context() err = systemservice.UpdateSysConfigByKey(ctx, &db.UpdateSysConfigByKeyParams{ Key: pearadmin.PearKey, Value: b, }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tpl.JSON(w, tpl.Response{Success: true, Message: "重置成功"}) } func (h *SysConfigHandler) Refresh(w http.ResponseWriter, r *http.Request) { key := r.FormValue("key") err := redis.Del(r.Context(), strings.ToLower(key)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tpl.JSON(w, tpl.Response{Success: true, Message: "刷新成功"}) }