188 lines
5.2 KiB
Go
188 lines
5.2 KiB
Go
package send_ins_service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"message-nest/models"
|
|
"message-nest/pkg/app"
|
|
"message-nest/pkg/constant"
|
|
)
|
|
|
|
type SendTaskInsService struct {
|
|
ID string
|
|
Name string
|
|
CreatedBy string
|
|
ModifiedBy string
|
|
CreatedOn string
|
|
|
|
PageNum int
|
|
PageSize int
|
|
Enable int
|
|
}
|
|
|
|
// ValidateDiffWay 各种发信渠道具体字段校验
|
|
func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string, interface{}) {
|
|
var empty interface{}
|
|
if ins.WayType == constant.MessageTypeEmail {
|
|
var emailConfig models.InsEmailConfig
|
|
err := json.Unmarshal([]byte(ins.Config), &emailConfig)
|
|
if err != nil {
|
|
return "邮箱auth反序列化失败!", empty
|
|
}
|
|
|
|
// 检查是否为动态接收者模式
|
|
var configMap map[string]interface{}
|
|
json.Unmarshal([]byte(ins.Config), &configMap)
|
|
allowMultiRecip, exists := configMap["allowMultiRecip"].(bool)
|
|
|
|
// allowMultiRecip=true为动态模式,to_account可以为空(但如果有值也要验证格式)
|
|
if exists && allowMultiRecip {
|
|
// 动态模式:如果to_account为空,直接返回;如果有值,验证邮箱格式
|
|
if emailConfig.ToAccount == "" {
|
|
return "", emailConfig
|
|
}
|
|
// 有to_account值时,验证邮箱格式
|
|
_, Msg := app.CommonPlaygroundValid(emailConfig)
|
|
return Msg, emailConfig
|
|
}
|
|
|
|
// 固定模式:必须验证to_account
|
|
_, Msg := app.CommonPlaygroundValid(emailConfig)
|
|
return Msg, emailConfig
|
|
}
|
|
if ins.WayType == constant.MessageTypeDtalk {
|
|
var Config models.InsDtalkConfig
|
|
return "", Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeQyWeiXin {
|
|
var Config models.InsQyWeiXinConfig
|
|
return "", Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeFeishu {
|
|
var Config models.InsFeishuConfig
|
|
return "", Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeMessageNest {
|
|
var Config models.InsQyWeiXinConfig
|
|
return "", Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeCustom {
|
|
var Config models.InsCustomConfig
|
|
err := json.Unmarshal([]byte(ins.Config), &Config)
|
|
if err != nil {
|
|
return "自定义webhook反序列化失败!", empty
|
|
}
|
|
_, Msg := app.CommonPlaygroundValid(Config)
|
|
return Msg, Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeWeChatOFAccount {
|
|
var Config models.InsWeChatAccountConfig
|
|
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)
|
|
|
|
// allowMultiRecip=true为动态模式,to_account可以为空
|
|
// 历史数据(不存在allowMultiRecip字段)默认为固定模式
|
|
if exists && allowMultiRecip && Config.ToAccount == "" {
|
|
// 动态模式,不验证to_account
|
|
return "", Config
|
|
}
|
|
|
|
// 固定模式或有to_account时,进行常规验证
|
|
_, Msg := app.CommonPlaygroundValid(Config)
|
|
return Msg, Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeAliyunSMS {
|
|
var Config models.InsAliyunSMSConfig
|
|
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)
|
|
|
|
// allowMultiRecip=true为动态模式,phone_number可以为空,但template_code仍需验证
|
|
if exists && allowMultiRecip && Config.PhoneNumber == "" {
|
|
// 动态模式,只验证template_code
|
|
if Config.TemplateCode == "" {
|
|
return "短信模板CODE不能为空", empty
|
|
}
|
|
return "", Config
|
|
}
|
|
|
|
// 固定模式或有phone_number时,进行常规验证
|
|
_, Msg := app.CommonPlaygroundValid(Config)
|
|
return Msg, Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeTelegram {
|
|
var Config models.InsTelegramConfig
|
|
return "", Config
|
|
}
|
|
if ins.WayType == constant.MessageTypeBark {
|
|
var Config models.InsBarkConfig
|
|
return "", Config
|
|
}
|
|
return "未知的渠道的config校验", empty
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (st *SendTaskInsService) Update(data map[string]interface{}) error {
|
|
return models.UpdateMsgTaskIns(st.ID, data)
|
|
}
|
|
|
|
func (st *SendTaskInsService) Count() (int64, error) {
|
|
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
|
|
}
|