Files
Message-Push-Nest/service/send_way_service/send_way.go
T

220 lines
6.2 KiB
Go
Raw Normal View History

2023-12-30 17:40:20 +08:00
package send_way_service
import (
"encoding/json"
"errors"
"fmt"
"message-nest/models"
"message-nest/pkg/app"
"message-nest/pkg/message"
"strings"
)
type SendWay struct {
ID string
Name string
Type string
CreatedBy string
ModifiedBy string
Auth string
CreatedOn string
PageNum int
PageSize int
}
// WayDetailEmail 邮箱渠道明细字段
type WayDetailEmail struct {
Server string `validate:"required,max=50" label:"SMTP服务地址"`
Port int `validate:"required,max=65535" label:"SMTP服务端口"`
Account string `validate:"required,email" label:"邮箱账号"`
Passwd string `validate:"required,max=50" label:"邮箱密码"`
}
// WayDetailDTalk 钉钉渠道明细字段
type WayDetailDTalk struct {
2024-01-06 17:44:45 +08:00
AccessToken string `json:"access_token" validate:"required,max=100" label:"钉钉access_token"`
Keys string `json:"keys" validate:"max=200" label:"钉钉关键字"`
Secret string `json:"secret" validate:"max=100" label:"钉钉加签秘钥"`
2023-12-30 17:40:20 +08:00
}
2024-01-14 20:39:55 +08:00
// WayDetailQyWeiXin 企业微信渠道明细字段
type WayDetailQyWeiXin struct {
AccessToken string `json:"access_token" validate:"required,max=100" label:"企业微信access_token"`
}
2024-01-07 00:25:35 +08:00
// WayDetailCustom 自定义渠道
type WayDetailCustom struct {
Webhook string `json:"webhook" validate:"required,max=200" label:"自定义的webhook地址"`
Body string `json:"body" validate:"max=2000" label:"自定义的请求体"`
}
2024-03-05 14:42:12 +08:00
// WeChatOFAccount 微信公众号
type WeChatOFAccount struct {
AppID string `json:"appID" validate:"required,max=200" label:"微信公众号id"`
APPSecret string `json:"appsecret" validate:"max=2000" label:"微信公众号秘钥"`
TempID string `json:"tempid" validate:"max=2000" label:"模板消息id"`
}
2023-12-30 17:40:20 +08:00
func (sw *SendWay) GetByID() (interface{}, error) {
return models.GetWayByID(sw.ID)
}
2024-01-21 12:35:33 +08:00
func (sw *SendWay) NameIsExist(method string) error {
2024-01-21 00:54:02 +08:00
way, err := models.GetWayByName(sw.Name)
if err != nil {
return err
}
2024-01-21 12:35:33 +08:00
if method == "add" {
if len(way.ID) > 0 {
return errors.New(fmt.Sprintf("已经存在同名的渠道名:%s", way.Name))
}
} else {
// 只允许当前的重名
if len(way.ID) > 0 && way.ID != sw.ID {
return errors.New(fmt.Sprintf("已经存在同名的渠道名:%s", way.Name))
}
}
return nil
}
func (sw *SendWay) Add() error {
err := sw.NameIsExist("add")
if err != nil {
return err
2024-01-21 00:54:02 +08:00
}
2023-12-30 17:40:20 +08:00
return models.AddSendWay(sw.Name, sw.Auth, sw.Type, sw.CreatedBy, sw.ModifiedBy)
}
func (sw *SendWay) Edit() error {
2024-01-21 12:35:33 +08:00
err := sw.NameIsExist("edit")
if err != nil {
return err
}
2023-12-30 17:40:20 +08:00
data := make(map[string]interface{})
data["modified_by"] = sw.ModifiedBy
data["name"] = sw.Name
data["auth"] = sw.Auth
return models.EditSendWay(sw.ID, data)
}
func (sw *SendWay) Delete() error {
tasks := models.FindTaskByWayId(sw.ID)
if len(tasks) > 0 {
var names []string
for _, task := range tasks {
names = append(names, task.Name)
}
return errors.New(fmt.Sprintf("已经存在使用的任务,删除失败!任务名:%s", strings.Join(names, ", ")))
}
return models.DeleteMsgWay(sw.ID)
}
2024-04-29 17:31:13 +08:00
func (sw *SendWay) Count() (int64, error) {
2023-12-30 17:40:20 +08:00
return models.GetSendWaysTotal(sw.Name, sw.Type, sw.getMaps())
}
func (sw *SendWay) GetAll() ([]models.SendWays, error) {
tags, err := models.GetSendWays(sw.PageNum, sw.PageSize, sw.Name, sw.Type, sw.getMaps())
if err != nil {
return nil, err
}
return tags, nil
}
func (sw *SendWay) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
return maps
}
// ValidateDiffWay 各种发信渠道具体字段校验
func (sw *SendWay) ValidateDiffWay() (string, interface{}) {
var empty interface{}
if sw.Type == "Email" {
var email WayDetailEmail
err := json.Unmarshal([]byte(sw.Auth), &email)
if err != nil {
return "邮箱auth反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(email)
return Msg, email
} else if sw.Type == "Dtalk" {
var dtalk WayDetailDTalk
err := json.Unmarshal([]byte(sw.Auth), &dtalk)
if err != nil {
return "钉钉auth反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(dtalk)
return Msg, dtalk
2024-01-14 20:39:55 +08:00
} else if sw.Type == "QyWeiXin" {
var config WayDetailQyWeiXin
err := json.Unmarshal([]byte(sw.Auth), &config)
if err != nil {
return "企业微信auth反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(config)
return Msg, config
2024-01-07 00:25:35 +08:00
} else if sw.Type == "Custom" {
var custom WayDetailCustom
err := json.Unmarshal([]byte(sw.Auth), &custom)
if err != nil {
return "自定义参数反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(custom)
return Msg, custom
2024-03-05 14:42:12 +08:00
} else if sw.Type == "WeChatOFAccount" {
var wca WeChatOFAccount
err := json.Unmarshal([]byte(sw.Auth), &wca)
if err != nil {
return "微信公众号反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(wca)
return Msg, wca
2023-12-30 17:40:20 +08:00
}
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type), empty
}
// TestSendWay 尝试带发信测试连通性
2024-01-08 23:35:10 +08:00
func (sw *SendWay) TestSendWay(msgObj interface{}) (string, string) {
2024-01-06 17:44:45 +08:00
testMsg := "This is a test message from message-nest."
2023-12-30 17:40:20 +08:00
emailAuth, ok := msgObj.(WayDetailEmail)
if ok {
var emailer message.EmailMessage
emailer.Init(emailAuth.Server, 465, emailAuth.Account, emailAuth.Passwd)
2024-01-06 17:44:45 +08:00
errMsg := emailer.SendTextMessage(emailAuth.Account, "test email", testMsg)
2024-01-08 23:35:10 +08:00
return errMsg, ""
2023-12-30 17:40:20 +08:00
}
2024-01-06 17:44:45 +08:00
dtalkAuth, ok := msgObj.(WayDetailDTalk)
if ok {
2024-01-07 00:25:35 +08:00
var cli = message.Dtalk{
AccessToken: dtalkAuth.AccessToken,
Secret: dtalkAuth.Secret,
}
2024-01-08 23:35:10 +08:00
res, err := cli.SendMessageText(testMsg + dtalkAuth.Keys)
2024-01-07 00:25:35 +08:00
if err != nil {
2024-01-08 23:35:10 +08:00
return fmt.Sprintf("发送失败:%s", err), string(res)
2024-01-07 00:25:35 +08:00
}
2024-01-08 23:35:10 +08:00
return "", string(res)
2024-01-07 00:25:35 +08:00
}
2024-01-14 20:39:55 +08:00
qywxAuth, ok := msgObj.(WayDetailQyWeiXin)
if ok {
var cli = message.QyWeiXin{
AccessToken: qywxAuth.AccessToken,
}
res, err := cli.SendMessageText(testMsg)
if err != nil {
return fmt.Sprintf("发送失败:%s", err), string(res)
}
return "", string(res)
}
2024-03-05 14:42:12 +08:00
_, ok = msgObj.(WeChatOFAccount)
2024-01-07 00:25:35 +08:00
if ok {
2024-03-05 14:42:12 +08:00
//var cli = message.WeChatOFAccount{
// AppID: wca.AppID,
// AppSecret: wca.APPSecret,
//}
return "微信公众号模板消息不用测试运行,请直接添加", ""
2024-01-06 17:44:45 +08:00
}
2024-01-08 23:35:10 +08:00
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type), ""
2023-12-30 17:40:20 +08:00
}