- 导航页所有文案(标题/副标题/标签/咨询区/页脚/工作室名称)均可在后台「导航页设置」页配置 - 新增公开API GET /api/v1/nav-settings,导航页加载时自动拉取配置覆盖默认值 - CustomerHandler改为从数据库读取webhook URL,不再依赖config.yaml - 新增默认企业微信webhook地址,开箱即用 - 后台新增webhook测试专用接口 POST /api/v1/admin/webhook/test - 后台新增批量配置保存接口 PUT /api/v1/admin/config-batch - 后台新增「导航页设置」管理页面,支持分组编辑保存 - 页脚添加工作室名称展示位
164 lines
3.8 KiB
Go
164 lines
3.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"gotest/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ConfigHandler 系统配置处理器
|
|
type ConfigHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewConfigHandler 创建系统配置处理器
|
|
func NewConfigHandler(db *gorm.DB) *ConfigHandler {
|
|
return &ConfigHandler{db: db}
|
|
}
|
|
|
|
// GetConfig 获取配置 GET /api/v1/admin/config/:key
|
|
func (h *ConfigHandler) GetConfig(c *gin.Context) {
|
|
key := c.Param("key")
|
|
var config model.Config
|
|
if err := h.db.Where("`key` = ?", key).First(&config).Error; err != nil {
|
|
Success(c, gin.H{"key": key, "value": "", "remark": ""})
|
|
return
|
|
}
|
|
Success(c, config)
|
|
}
|
|
|
|
// SetConfig 设置配置 PUT /api/v1/admin/config/:key
|
|
func (h *ConfigHandler) SetConfig(c *gin.Context) {
|
|
key := c.Param("key")
|
|
var req struct {
|
|
Value string `json:"value"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
FailBadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
|
|
var config model.Config
|
|
result := h.db.Where("`key` = ?", key).First(&config)
|
|
if result.Error == gorm.ErrRecordNotFound {
|
|
config = model.Config{
|
|
Key: key,
|
|
Value: req.Value,
|
|
Remark: req.Remark,
|
|
}
|
|
if err := h.db.Create(&config).Error; err != nil {
|
|
FailInternal(c, "创建失败: "+err.Error())
|
|
return
|
|
}
|
|
} else if result.Error == nil {
|
|
config.Value = req.Value
|
|
if req.Remark != "" {
|
|
config.Remark = req.Remark
|
|
}
|
|
if err := h.db.Save(&config).Error; err != nil {
|
|
FailInternal(c, "更新失败: "+err.Error())
|
|
return
|
|
}
|
|
} else {
|
|
FailInternal(c, "数据库错误: "+result.Error.Error())
|
|
return
|
|
}
|
|
|
|
Success(c, config)
|
|
}
|
|
|
|
// ListConfigs 获取所有配置 GET /api/v1/admin/config
|
|
func (h *ConfigHandler) ListConfigs(c *gin.Context) {
|
|
var configs []model.Config
|
|
if err := h.db.Order("id ASC").Find(&configs).Error; err != nil {
|
|
FailInternal(c, "查询失败: "+err.Error())
|
|
return
|
|
}
|
|
Success(c, configs)
|
|
}
|
|
|
|
// BatchSetConfigs 批量设置配置 PUT /api/v1/admin/config
|
|
func (h *ConfigHandler) BatchSetConfigs(c *gin.Context) {
|
|
var req struct {
|
|
Items []ConfigItem `json:"items"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
FailBadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
|
|
for _, item := range req.Items {
|
|
var config model.Config
|
|
result := h.db.Where("`key` = ?", item.Key).First(&config)
|
|
if result.Error == gorm.ErrRecordNotFound {
|
|
config = model.Config{
|
|
Key: item.Key,
|
|
Value: item.Value,
|
|
Remark: item.Remark,
|
|
}
|
|
h.db.Create(&config)
|
|
} else if result.Error == nil {
|
|
config.Value = item.Value
|
|
if item.Remark != "" {
|
|
config.Remark = item.Remark
|
|
}
|
|
h.db.Save(&config)
|
|
}
|
|
}
|
|
|
|
SuccessMsg(c, "保存成功")
|
|
}
|
|
|
|
// GetNavSettings 公开接口:获取导航页所有配置 GET /api/v1/nav-settings
|
|
func (h *ConfigHandler) GetNavSettings(c *gin.Context) {
|
|
// 导航页所有可配置项的 key 列表
|
|
keys := []string{
|
|
"nav_site_title",
|
|
"nav_hero_title",
|
|
"nav_hero_highlight",
|
|
"nav_hero_desc",
|
|
"nav_pill_templates",
|
|
"nav_pill_lines",
|
|
"nav_pill_industries",
|
|
"nav_pill_per_industry",
|
|
"nav_pill_responsive",
|
|
"nav_consult_title",
|
|
"nav_consult_desc",
|
|
"nav_consult_success",
|
|
"nav_footer_text",
|
|
"nav_company_name",
|
|
// 6 大行业标题
|
|
"nav_sec_enterprise_title",
|
|
"nav_sec_enterprise_sub",
|
|
"nav_sec_shop_title",
|
|
"nav_sec_shop_sub",
|
|
"nav_sec_food_title",
|
|
"nav_sec_food_sub",
|
|
"nav_sec_game_title",
|
|
"nav_sec_game_sub",
|
|
"nav_sec_app_title",
|
|
"nav_sec_app_sub",
|
|
"nav_sec_music_title",
|
|
"nav_sec_music_sub",
|
|
}
|
|
|
|
settings := map[string]string{}
|
|
for _, key := range keys {
|
|
var config model.Config
|
|
if err := h.db.Where("`key` = ?", key).First(&config).Error; err == nil {
|
|
settings[key] = config.Value
|
|
}
|
|
}
|
|
|
|
Success(c, settings)
|
|
}
|
|
|
|
// ConfigItem 配置项
|
|
type ConfigItem struct {
|
|
Key string `json:"key"`
|
|
Value string `json:"value"`
|
|
Remark string `json:"remark"`
|
|
}
|