Files
ai-agent/internal/controllers/api/channel_controller.go
T

78 lines
2.0 KiB
Go
Raw Normal View History

package api
2026-04-09 10:01:23 +08:00
import (
"cs-agent/internal/pkg/dto/response"
"cs-agent/internal/pkg/enums"
"cs-agent/internal/pkg/errorsx"
"cs-agent/internal/services"
2026-04-09 10:01:23 +08:00
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple/web"
)
type ChannelController struct {
2026-04-09 10:01:23 +08:00
Ctx iris.Context
}
func (c *ChannelController) AnyConfig() *web.JsonResult {
channel := services.ChannelService.GetEnabledChannel(c.Ctx)
2026-04-09 10:01:23 +08:00
if channel == nil {
return web.JsonErrorMsg("接入渠道未初始化")
}
cfg, externalSource, err := resolveWidgetConfig(channel.ChannelType, channel.ConfigJSON)
if err != nil {
return web.JsonError(err)
}
2026-04-09 10:01:23 +08:00
ret := response.WidgetConfigResponse{
ChannelID: channel.ChannelID,
ChannelType: channel.ChannelType,
ExternalSource: externalSource,
Title: cfg.Title,
Subtitle: cfg.Subtitle,
ThemeColor: cfg.ThemeColor,
Position: cfg.Position,
Width: cfg.Width,
2026-04-09 10:01:23 +08:00
}
return web.JsonData(ret)
}
type webLikeWidgetConfig struct {
Title string
Subtitle string
ThemeColor string
Position string
Width string
}
func resolveWidgetConfig(channelType, rawConfig string) (*webLikeWidgetConfig, string, error) {
switch channelType {
case enums.ChannelTypeWeb:
cfg, err := services.ChannelService.ParseWebChannelConfig(rawConfig)
if err != nil {
return nil, "", err
}
return &webLikeWidgetConfig{
Title: cfg.Title,
Subtitle: cfg.Subtitle,
ThemeColor: cfg.ThemeColor,
Position: cfg.Position,
Width: cfg.Width,
}, string(enums.ExternalSourceWebChat), nil
case enums.ChannelTypeWechatMP:
cfg, err := services.ChannelService.ParseWechatMPChannelConfig(rawConfig)
if err != nil {
return nil, "", err
}
return &webLikeWidgetConfig{
Title: cfg.Title,
Subtitle: cfg.Subtitle,
ThemeColor: cfg.ThemeColor,
Position: "right",
Width: "100%",
}, string(enums.ExternalSourceWechatMP), nil
default:
return nil, "", errorsx.InvalidParam("该渠道不支持开放客服配置")
}
}