91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
|
package smdl
|
|||
|
|
|||
|
import (
|
|||
|
"admin/apps/game/domain/entity"
|
|||
|
"admin/apps/game/model"
|
|||
|
"admin/apps/game/model/dto"
|
|||
|
"admin/internal/errcode"
|
|||
|
"admin/lib/httpclient"
|
|||
|
"net/url"
|
|||
|
"strconv"
|
|||
|
"strings"
|
|||
|
)
|
|||
|
|
|||
|
type MailGlobalHook struct {
|
|||
|
}
|
|||
|
|
|||
|
func (hook *MailGlobalHook) Create(projectInfo *entity.Project, resource string, dtoObj dto.CommonDtoValues) error {
|
|||
|
alisrvAddr := projectInfo.GetApiAddr()
|
|||
|
if alisrvAddr == "" {
|
|||
|
return errcode.New(errcode.ServerError, "项目%v没有配置api服务器地址", projectInfo.ProjectPo.Name)
|
|||
|
}
|
|||
|
|
|||
|
et := (&entity.CommonResource{}).FromPo(&model.GlobalMail{}).FromDto(dtoObj)
|
|||
|
mailInfo := et.ToPo().(*model.GlobalMail)
|
|||
|
|
|||
|
itemIdStr := ""
|
|||
|
itemNumStr := ""
|
|||
|
if len(mailInfo.Attach) == 1 {
|
|||
|
itemIdStr = strconv.FormatInt(int64(mailInfo.Attach[0].ID), 10)
|
|||
|
itemNumStr = strconv.FormatInt(mailInfo.Attach[0].Num, 10)
|
|||
|
} else if len(mailInfo.Attach) > 1 {
|
|||
|
// 多个道具附件,itemid格式就是id@num@id@num@id@num...
|
|||
|
itemWordList := make([]string, 0, len(mailInfo.Attach)*2)
|
|||
|
for _, v := range mailInfo.Attach {
|
|||
|
itemWordList = append(itemWordList, strconv.FormatInt(int64(v.ID), 10), strconv.FormatInt(v.Num, 10))
|
|||
|
}
|
|||
|
itemIdStr = strings.Join(itemWordList, "@")
|
|||
|
itemNumStr = "-1"
|
|||
|
} else {
|
|||
|
itemIdStr = "0"
|
|||
|
}
|
|||
|
|
|||
|
var serverIds []string
|
|||
|
|
|||
|
serverIdsLen := len(mailInfo.ServerIDs)
|
|||
|
switch {
|
|||
|
case serverIdsLen == 0:
|
|||
|
// todo 所有服
|
|||
|
case serverIdsLen > 0:
|
|||
|
for _, v := range mailInfo.ServerIDs {
|
|||
|
serverIds = append(serverIds, v)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
params := &url.Values{}
|
|||
|
params.Add("itemid", itemIdStr)
|
|||
|
params.Add("itemnum", itemNumStr)
|
|||
|
params.Add("mailtitle", mailInfo.Title)
|
|||
|
params.Add("mailcontent", mailInfo.Content)
|
|||
|
|
|||
|
// 通知对应服务器
|
|||
|
for _, serverId := range serverIds {
|
|||
|
params.Set("server", serverId)
|
|||
|
|
|||
|
rsp := make(map[string]any)
|
|||
|
err := httpclient.Request(alisrvAddr+"/mail_global", "get", params, &rsp)
|
|||
|
if err != nil {
|
|||
|
return errcode.New(errcode.ServerError, "发送全服邮件通知服务器%v发生错误:%v", serverId, err)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
func (hook *MailGlobalHook) Delete(projectInfo *entity.Project, resource string, dtoObj dto.CommonDtoValues) error {
|
|||
|
alisrvAddr := projectInfo.GetApiAddr()
|
|||
|
if alisrvAddr == "" {
|
|||
|
return errcode.New(errcode.ServerError, "项目%v没有配置api服务器地址", projectInfo.ProjectPo.Name)
|
|||
|
}
|
|||
|
|
|||
|
et := (&entity.CommonResource{}).FromPo(&model.GlobalMail{}).FromDto(dtoObj)
|
|||
|
mailInfo := et.ToPo().(*model.GlobalMail)
|
|||
|
if mailInfo != nil {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// todo 撤回邮件
|
|||
|
|
|||
|
return nil
|
|||
|
}
|