第一版完成
This commit is contained in:
71
internal/controller/config.go
Normal file
71
internal/controller/config.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"cms-api/internal/model"
|
||||
"cms-api/internal/service"
|
||||
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// 网站配置控制器
|
||||
type cConfig struct{}
|
||||
|
||||
var Config = &cConfig{}
|
||||
|
||||
// List 获取配置列表
|
||||
func (c *cConfig) List(r *ghttp.Request) {
|
||||
var req *model.ConfigListRequest
|
||||
if err := r.Parse(&req); err != nil {
|
||||
r.Response.WriteJson(&model.Response{Code: 400, Message: "参数解析失败", Data: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 调用配置服务获取列表
|
||||
configService := service.Config()
|
||||
result, err := configService.List(r.Context(), req)
|
||||
if err != nil {
|
||||
r.Response.WriteJson(&model.Response{Code: 500, Message: "获取配置列表失败", Data: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
r.Response.WriteJson(&model.Response{Code: 200, Message: "获取成功", Data: result})
|
||||
}
|
||||
|
||||
// Update 更新配置
|
||||
func (c *cConfig) Update(r *ghttp.Request) {
|
||||
id := gconv.Int(r.Get("id"))
|
||||
if id <= 0 {
|
||||
r.Response.WriteJson(&model.Response{Code: 400, Message: "配置ID无效"})
|
||||
return
|
||||
}
|
||||
|
||||
var req *model.UpdateConfigRequest
|
||||
if err := r.Parse(&req); err != nil {
|
||||
r.Response.WriteJson(&model.Response{Code: 400, Message: "参数解析失败", Data: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 调用配置服务更新配置
|
||||
configService := service.Config()
|
||||
err := configService.Update(r.Context(), gconv.String(id), req)
|
||||
if err != nil {
|
||||
r.Response.WriteJson(&model.Response{Code: 500, Message: "更新配置失败", Data: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
r.Response.WriteJson(&model.Response{Code: 200, Message: "更新配置成功"})
|
||||
}
|
||||
|
||||
// Public 获取公开配置
|
||||
func (c *cConfig) Public(r *ghttp.Request) {
|
||||
// 调用配置服务获取公开配置
|
||||
configService := service.Config()
|
||||
result, err := configService.GetPublicConfigs(r.Context())
|
||||
if err != nil {
|
||||
r.Response.WriteJson(&model.Response{Code: 500, Message: "获取公开配置失败", Data: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
r.Response.WriteJson(&model.Response{Code: 200, Message: "获取成功", Data: result})
|
||||
}
|
||||
Reference in New Issue
Block a user