fix: template message send mutli

This commit is contained in:
Your Name
2026-01-15 08:52:57 +08:00
parent 328e88e8c4
commit 18ad967df3
4 changed files with 31 additions and 15 deletions
+2
View File
@@ -18,6 +18,7 @@ type SendMessageByTemplateReq struct {
Token string `json:"token" validate:"required" label:"模板token"` Token string `json:"token" validate:"required" label:"模板token"`
Title string `json:"title" validate:"required" label:"消息标题"` Title string `json:"title" validate:"required" label:"消息标题"`
Placeholders map[string]interface{} `json:"placeholders" label:"占位符"` Placeholders map[string]interface{} `json:"placeholders" label:"占位符"`
Recipients []string `json:"recipients" label:"接收者列表"`
} }
// DoSendMessageByTemplate 使用模板发送消息 // DoSendMessageByTemplate 使用模板发送消息
@@ -111,6 +112,7 @@ func DoSendMessageByTemplate(c *gin.Context) {
AtMobiles: atMobiles, AtMobiles: atMobiles,
AtUserIds: atUserIds, AtUserIds: atUserIds,
AtAll: template.IsAtAll, AtAll: template.IsAtAll,
Recipients: req.Recipients, // 动态接收者列表
DefaultLogger: logrus.WithFields(logrus.Fields{ DefaultLogger: logrus.WithFields(logrus.Fields{
"prefix": "[Template Send]", "prefix": "[Template Send]",
}), }),
+19 -15
View File
@@ -29,20 +29,24 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
if err != nil { if err != nil {
return "邮箱auth反序列化失败!", empty return "邮箱auth反序列化失败!", empty
} }
// 检查是否为动态接收者模式 // 检查是否为动态接收者模式
var configMap map[string]interface{} var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap) json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool) allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
// allowMultiRecip=true为动态模式,to_account可以为空 // allowMultiRecip=true为动态模式,to_account可以为空(但如果有值也要验证格式)
// 历史数据(不存在allowMultiRecip字段)默认为固定模式 if exists && allowMultiRecip {
if exists && allowMultiRecip && emailConfig.ToAccount == "" { // 动态模式:如果to_account为空,直接返回;如果有值,验证邮箱格式
// 动态模式,不验证to_account if emailConfig.ToAccount == "" {
return "", emailConfig return "", emailConfig
}
// 有to_account值时,验证邮箱格式
_, Msg := app.CommonPlaygroundValid(emailConfig)
return Msg, emailConfig
} }
// 固定模式或有to_account时,进行常规验证 // 固定模式:必须验证to_account
_, Msg := app.CommonPlaygroundValid(emailConfig) _, Msg := app.CommonPlaygroundValid(emailConfig)
return Msg, emailConfig return Msg, emailConfig
} }
@@ -77,19 +81,19 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
if err != nil { if err != nil {
return "微信公众号发送配置反序列化失败!", empty return "微信公众号发送配置反序列化失败!", empty
} }
// 检查是否为动态接收者模式 // 检查是否为动态接收者模式
var configMap map[string]interface{} var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap) json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool) allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
// allowMultiRecip=true为动态模式,to_account可以为空 // allowMultiRecip=true为动态模式,to_account可以为空
// 历史数据(不存在allowMultiRecip字段)默认为固定模式 // 历史数据(不存在allowMultiRecip字段)默认为固定模式
if exists && allowMultiRecip && Config.ToAccount == "" { if exists && allowMultiRecip && Config.ToAccount == "" {
// 动态模式,不验证to_account // 动态模式,不验证to_account
return "", Config return "", Config
} }
// 固定模式或有to_account时,进行常规验证 // 固定模式或有to_account时,进行常规验证
_, Msg := app.CommonPlaygroundValid(Config) _, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config return Msg, Config
@@ -100,12 +104,12 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
if err != nil { if err != nil {
return "阿里云短信配置反序列化失败!", empty return "阿里云短信配置反序列化失败!", empty
} }
// 检查是否为动态接收者模式 // 检查是否为动态接收者模式
var configMap map[string]interface{} var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap) json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool) allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
// allowMultiRecip=true为动态模式,phone_number可以为空,但template_code仍需验证 // allowMultiRecip=true为动态模式,phone_number可以为空,但template_code仍需验证
if exists && allowMultiRecip && Config.PhoneNumber == "" { if exists && allowMultiRecip && Config.PhoneNumber == "" {
// 动态模式,只验证template_code // 动态模式,只验证template_code
@@ -114,7 +118,7 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
} }
return "", Config return "", Config
} }
// 固定模式或有phone_number时,进行常规验证 // 固定模式或有phone_number时,进行常规验证
_, Msg := app.CommonPlaygroundValid(Config) _, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config return Msg, Config
@@ -261,6 +261,11 @@ func (sm *SendMessageService) Send(task models.TaskIns) (string, error) {
// 处理动态接收者(邮箱、微信公众号等支持群发的渠道) // 处理动态接收者(邮箱、微信公众号等支持群发的渠道)
isDynamicMode := sm.isDynamicRecipientMode(ins.SendTasksIns) isDynamicMode := sm.isDynamicRecipientMode(ins.SendTasksIns)
// 检查:如果提供了recipients但实例未启用动态模式,给出提示
if !isDynamicMode && sm.supportsDynamicRecipient(way.Type) && len(sm.Recipients) > 0 {
sm.LogsAndStatusMark(fmt.Sprintf("警告:提供了 %d 个接收者,但实例未启用动态接收模式(allowMultiRecip=true),将使用实例配置的固定接收者", len(sm.Recipients)), sm.Status)
}
if isDynamicMode && sm.supportsDynamicRecipient(way.Type) && len(sm.Recipients) > 0 { if isDynamicMode && sm.supportsDynamicRecipient(way.Type) && len(sm.Recipients) > 0 {
// 动态接收模式:使用API传入的Recipients列表(群发) // 动态接收模式:使用API传入的Recipients列表(群发)
sm.LogsAndStatusMark(fmt.Sprintf("动态接收模式[共 %d 个接收者]", len(sm.Recipients)), sm.Status) sm.LogsAndStatusMark(fmt.Sprintf("动态接收模式[共 %d 个接收者]", len(sm.Recipients)), sm.Status)
@@ -28,6 +28,11 @@ func (c *EmailChannel) SendUnified(msgObj interface{}, ins models.SendTasksIns,
if !ok { if !ok {
return "", "邮箱config校验失败" return "", "邮箱config校验失败"
} }
if config.ToAccount == "" {
return "", "收件邮箱地址为空,请检查实例配置或启用动态接收模式"
}
contentType, formattedContent, err := c.FormatContent(content) contentType, formattedContent, err := c.FormatContent(content)
if err != nil { if err != nil {
return "", err.Error() return "", err.Error()