Files
Message-Push-Nest/service/send_ins_service/send_ins.go
T

210 lines
5.9 KiB
Go
Raw Normal View History

2023-12-30 17:40:20 +08:00
package send_ins_service
import (
"encoding/json"
"fmt"
"message-nest/models"
"message-nest/pkg/app"
2025-12-17 21:51:21 +08:00
"message-nest/pkg/constant"
2023-12-30 17:40:20 +08:00
)
type SendTaskInsService struct {
ID string
Name string
CreatedBy string
ModifiedBy string
CreatedOn string
PageNum int
PageSize int
2024-01-14 17:05:35 +08:00
Enable int
2023-12-30 17:40:20 +08:00
}
// ValidateDiffWay 各种发信渠道具体字段校验
func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string, interface{}) {
var empty interface{}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeEmail {
2023-12-30 17:40:20 +08:00
var emailConfig models.InsEmailConfig
err := json.Unmarshal([]byte(ins.Config), &emailConfig)
if err != nil {
return "邮箱auth反序列化失败!", empty
}
2026-01-15 08:52:57 +08:00
2025-12-14 12:43:09 +08:00
// 检查是否为动态接收者模式
var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
2026-01-15 08:52:57 +08:00
// allowMultiRecip=true为动态模式,to_account可以为空(但如果有值也要验证格式)
if exists && allowMultiRecip {
// 动态模式:如果to_account为空,直接返回;如果有值,验证邮箱格式
if emailConfig.ToAccount == "" {
return "", emailConfig
}
// 有to_account值时,验证邮箱格式
_, Msg := app.CommonPlaygroundValid(emailConfig)
return Msg, emailConfig
2025-12-14 12:43:09 +08:00
}
2026-01-15 08:52:57 +08:00
// 固定模式:必须验证to_account
2023-12-30 17:40:20 +08:00
_, Msg := app.CommonPlaygroundValid(emailConfig)
return Msg, emailConfig
}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeDtalk {
2024-01-06 17:44:45 +08:00
var Config models.InsDtalkConfig
return "", Config
}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeQyWeiXin {
2024-01-14 20:39:55 +08:00
var Config models.InsQyWeiXinConfig
return "", Config
}
if ins.WayType == constant.MessageTypeWeChatCorpAccount {
var Config models.InsWeChatCorpAccountConfig
err := json.Unmarshal([]byte(ins.Config), &Config)
if err != nil {
return "企业微信应用发送配置反序列化失败!", empty
}
var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
if exists && allowMultiRecip && Config.ToAccount == "" {
return "", Config
}
_, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config
}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeFeishu {
var Config models.InsFeishuConfig
return "", Config
}
if ins.WayType == constant.MessageTypeMessageNest {
2025-01-01 17:30:02 +08:00
var Config models.InsQyWeiXinConfig
return "", Config
}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeCustom {
2024-01-07 00:25:35 +08:00
var Config models.InsCustomConfig
err := json.Unmarshal([]byte(ins.Config), &Config)
if err != nil {
return "自定义webhook反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config
}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeWeChatOFAccount {
2024-03-05 14:42:12 +08:00
var Config models.InsWeChatAccountConfig
err := json.Unmarshal([]byte(ins.Config), &Config)
if err != nil {
return "微信公众号发送配置反序列化失败!", empty
}
2026-01-15 08:52:57 +08:00
2025-12-14 12:43:09 +08:00
// 检查是否为动态接收者模式
var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
2026-01-15 08:52:57 +08:00
2025-12-14 12:43:09 +08:00
// allowMultiRecip=true为动态模式,to_account可以为空
// 历史数据(不存在allowMultiRecip字段)默认为固定模式
if exists && allowMultiRecip && Config.ToAccount == "" {
// 动态模式,不验证to_account
return "", Config
}
2026-01-15 08:52:57 +08:00
2025-12-14 12:43:09 +08:00
// 固定模式或有to_account时,进行常规验证
_, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config
}
2025-12-17 21:51:21 +08:00
if ins.WayType == constant.MessageTypeAliyunSMS {
2025-12-14 12:43:09 +08:00
var Config models.InsAliyunSMSConfig
err := json.Unmarshal([]byte(ins.Config), &Config)
if err != nil {
return "阿里云短信配置反序列化失败!", empty
}
2026-01-15 08:52:57 +08:00
2025-12-17 21:51:21 +08:00
// 检查是否为动态接收者模式
var configMap map[string]interface{}
json.Unmarshal([]byte(ins.Config), &configMap)
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
2026-01-15 08:52:57 +08:00
2025-12-17 21:51:21 +08:00
// allowMultiRecip=true为动态模式,phone_number可以为空,但template_code仍需验证
if exists && allowMultiRecip && Config.PhoneNumber == "" {
// 动态模式,只验证template_code
if Config.TemplateCode == "" {
return "短信模板CODE不能为空", empty
}
return "", Config
}
2026-01-15 08:52:57 +08:00
2025-12-17 21:51:21 +08:00
// 固定模式或有phone_number时,进行常规验证
2024-03-05 14:42:12 +08:00
_, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config
}
2026-01-14 20:51:58 +08:00
if ins.WayType == constant.MessageTypeTelegram {
var Config models.InsTelegramConfig
return "", Config
}
2026-01-21 19:26:48 +08:00
if ins.WayType == constant.MessageTypeBark {
var Config models.InsBarkConfig
return "", Config
}
2026-02-04 19:30:34 +08:00
if ins.WayType == constant.MessageTypePushMe {
var Config models.InsPushMeConfig
return "", Config
}
2024-01-01 12:03:56 +08:00
return "未知的渠道的config校验", empty
2023-12-30 17:40:20 +08:00
}
func (st *SendTaskInsService) ManyAdd(taskIns []models.SendTasksIns) string {
for _, ins := range taskIns {
errStr, _ := st.ValidateDiffIns(ins)
if errStr != "" {
return errStr
}
}
err := models.ManyAddTaskIns(taskIns)
if err != nil {
return fmt.Sprintf("%s", err)
}
return ""
}
func (st *SendTaskInsService) AddOne(ins models.SendTasksIns) string {
errStr, _ := st.ValidateDiffIns(ins)
if errStr != "" {
return errStr
}
err := models.AddTaskInsOne(ins)
if err != nil {
return fmt.Sprintf("%s", err)
}
return ""
}
func (st *SendTaskInsService) Delete() error {
return models.DeleteMsgTaskIns(st.ID)
}
2024-01-14 17:05:35 +08:00
func (st *SendTaskInsService) Update(data map[string]interface{}) error {
return models.UpdateMsgTaskIns(st.ID, data)
}
2024-04-29 17:31:13 +08:00
func (st *SendTaskInsService) Count() (int64, error) {
2023-12-30 17:40:20 +08:00
return models.GetSendTasksTotal(st.Name, st.getMaps())
}
func (st *SendTaskInsService) GetAll() ([]models.SendTasks, error) {
tasks, err := models.GetSendTasks(st.PageNum, st.PageSize, st.Name, st.getMaps())
if err != nil {
return nil, err
}
return tasks, nil
}
func (st *SendTaskInsService) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
return maps
}