Files
urldb/db/converter/wechat_bot_converter.go

89 lines
2.5 KiB
Go
Raw Normal View History

2025-10-31 13:36:07 +08:00
package converter
import (
2025-10-31 23:32:57 +08:00
"strconv"
2025-10-31 13:36:07 +08:00
"github.com/ctwj/urldb/db/dto"
"github.com/ctwj/urldb/db/entity"
)
// WechatBotConfigRequestToSystemConfigs 将微信机器人配置请求转换为系统配置实体
func WechatBotConfigRequestToSystemConfigs(req dto.WechatBotConfigRequest) []entity.SystemConfig {
configs := []entity.SystemConfig{
{Key: entity.ConfigKeyWechatBotEnabled, Value: wechatBoolToString(req.Enabled)},
{Key: entity.ConfigKeyWechatAppId, Value: req.AppID},
{Key: entity.ConfigKeyWechatAppSecret, Value: req.AppSecret},
{Key: entity.ConfigKeyWechatToken, Value: req.Token},
{Key: entity.ConfigKeyWechatEncodingAesKey, Value: req.EncodingAesKey},
{Key: entity.ConfigKeyWechatWelcomeMessage, Value: req.WelcomeMessage},
{Key: entity.ConfigKeyWechatAutoReplyEnabled, Value: wechatBoolToString(req.AutoReplyEnabled)},
{Key: entity.ConfigKeyWechatSearchLimit, Value: wechatIntToString(req.SearchLimit)},
}
return configs
}
// SystemConfigToWechatBotConfig 将系统配置转换为微信机器人配置响应
func SystemConfigToWechatBotConfig(configs []entity.SystemConfig) dto.WechatBotConfigResponse {
resp := dto.WechatBotConfigResponse{
2025-10-31 20:14:17 +08:00
Enabled: false,
AppID: "",
AppSecret: "",
Token: "",
EncodingAesKey: "",
WelcomeMessage: "欢迎关注老九网盘资源库!发送关键词即可搜索资源。",
2025-10-31 13:36:07 +08:00
AutoReplyEnabled: true,
2025-10-31 20:14:17 +08:00
SearchLimit: 5,
2025-10-31 13:36:07 +08:00
}
for _, config := range configs {
switch config.Key {
case entity.ConfigKeyWechatBotEnabled:
resp.Enabled = config.Value == "true"
case entity.ConfigKeyWechatAppId:
resp.AppID = config.Value
2025-10-31 20:14:17 +08:00
case entity.ConfigKeyWechatAppSecret:
resp.AppSecret = config.Value
2025-10-31 13:36:07 +08:00
case entity.ConfigKeyWechatToken:
resp.Token = config.Value
case entity.ConfigKeyWechatEncodingAesKey:
resp.EncodingAesKey = config.Value
case entity.ConfigKeyWechatWelcomeMessage:
if config.Value != "" {
resp.WelcomeMessage = config.Value
}
case entity.ConfigKeyWechatAutoReplyEnabled:
resp.AutoReplyEnabled = config.Value == "true"
case entity.ConfigKeyWechatSearchLimit:
if config.Value != "" {
resp.SearchLimit = wechatStringToInt(config.Value)
}
}
}
return resp
}
// 辅助函数 - 使用大写名称避免与其他文件中的函数冲突
func wechatBoolToString(b bool) string {
if b {
return "true"
}
return "false"
}
func wechatIntToString(i int) string {
2025-10-31 23:32:57 +08:00
return strconv.Itoa(i)
2025-10-31 13:36:07 +08:00
}
func wechatStringToInt(s string) int {
if s == "" {
return 0
}
2025-10-31 23:32:57 +08:00
i, err := strconv.Atoi(s)
if err != nil {
return 0
2025-10-31 13:36:07 +08:00
}
2025-10-31 23:32:57 +08:00
return i
2025-10-31 20:14:17 +08:00
}