Files
Message-Push-Nest/routers/api/v1/send_message.go
T

98 lines
2.8 KiB
Go
Raw Normal View History

2023-12-30 17:40:20 +08:00
package v1
import (
2024-01-29 19:17:42 +08:00
"fmt"
2023-12-30 17:40:20 +08:00
"message-nest/pkg/app"
"message-nest/pkg/e"
2025-10-08 13:16:27 +08:00
utilpkg "message-nest/pkg/util"
2023-12-30 17:40:20 +08:00
"message-nest/service/send_message_service"
"net/http"
2025-10-08 13:16:27 +08:00
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
2023-12-30 17:40:20 +08:00
)
type SendMessageReq struct {
2025-10-08 13:16:27 +08:00
// 兼容旧参数:task_id;不再必填
TaskID string `json:"task_id" validate:"omitempty,len=12" label:"任务id"`
// 新参数:token(与task_id互斥,优先使用task_id
Token string `json:"token" label:"任务token"`
2023-12-30 17:40:20 +08:00
Text string `json:"text" validate:"required" label:"文本内容"`
2024-01-06 17:44:45 +08:00
Title string `json:"title" label:"消息标题"`
2023-12-30 17:40:20 +08:00
HTML string `json:"html" label:"html内容"`
2024-03-05 14:42:12 +08:00
URL string `json:"url" label:"消息详情url地址"`
2023-12-30 17:40:20 +08:00
MarkDown string `json:"markdown" label:"markdown内容"`
2024-01-03 19:55:38 +08:00
Mode string `json:"mode" label:"是否异步发送"`
2025-12-04 21:16:32 +08:00
// @提及相关参数
AtMobiles []string `json:"at_mobiles" label:"@的手机号列表"`
AtUserIds []string `json:"at_user_ids" label:"@的用户ID列表"`
AtAll bool `json:"at_all" label:"是否@所有人"`
2023-12-30 17:40:20 +08:00
}
// DoSendMassage 外部调用发信接口
func DoSendMassage(c *gin.Context) {
var (
appG = app.Gin{C: c}
req SendMessageReq
)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
2025-10-08 13:16:27 +08:00
// 解析token为task_id(如提供)
taskID := req.TaskID
// 如果使用了token就使用token解析出task_id
if req.Token != "" {
dec, err := utilpkg.DecryptTokenHex(req.Token, 71) // 71 为简单对称密钥
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("token解析失败:%v", err), nil)
return
}
taskID = dec
}
if taskID == "" {
appG.CResponse(http.StatusBadRequest, "参数缺失:token 或 task_id 必须提供其一", nil)
return
}
2023-12-30 17:40:20 +08:00
msgService := send_message_service.SendMessageService{
2025-12-06 00:28:18 +08:00
SendMode: send_message_service.SendModeTask, // 明确标记为任务模式
2025-12-04 21:16:32 +08:00
TaskID: taskID,
Title: req.Title,
Text: req.Text,
HTML: req.HTML,
URL: req.URL,
MarkDown: req.MarkDown,
CallerIp: c.ClientIP(),
AtMobiles: req.AtMobiles,
AtUserIds: req.AtUserIds,
AtAll: req.AtAll,
2024-01-29 19:17:42 +08:00
DefaultLogger: logrus.WithFields(logrus.Fields{
2025-12-04 21:16:32 +08:00
"prefix": "[Send Instance]",
2024-01-29 19:17:42 +08:00
}),
}
task, err := msgService.SendPreCheck()
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("发送检查不通过:%s", err), nil)
return
2023-12-30 17:40:20 +08:00
}
2024-01-06 17:44:45 +08:00
if req.Mode == "sync" {
2024-01-03 19:55:38 +08:00
// 同步发送
2024-01-29 19:17:42 +08:00
_, err := msgService.Send(task)
if err != nil {
2024-01-03 19:55:38 +08:00
appG.CResponse(http.StatusBadRequest, "发送失败!", nil)
return
}
appG.CResponse(http.StatusOK, "发送成功!", nil)
} else {
// 异步发送
2024-01-29 19:17:42 +08:00
msgService.AsyncSend(task)
2024-01-03 19:55:38 +08:00
appG.CResponse(http.StatusOK, "提交成功!", nil)
2023-12-30 17:40:20 +08:00
}
}