Files
Message-Push-Nest/routers/api/v1/send_message.go
T
2025-12-06 00:28:18 +08:00

98 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package v1
import (
"fmt"
"message-nest/pkg/app"
"message-nest/pkg/e"
utilpkg "message-nest/pkg/util"
"message-nest/service/send_message_service"
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
type SendMessageReq struct {
// 兼容旧参数:task_id;不再必填
TaskID string `json:"task_id" validate:"omitempty,len=12" label:"任务id"`
// 新参数:token(与task_id互斥,优先使用task_id
Token string `json:"token" label:"任务token"`
Text string `json:"text" validate:"required" label:"文本内容"`
Title string `json:"title" label:"消息标题"`
HTML string `json:"html" label:"html内容"`
URL string `json:"url" label:"消息详情url地址"`
MarkDown string `json:"markdown" label:"markdown内容"`
Mode string `json:"mode" label:"是否异步发送"`
// @提及相关参数
AtMobiles []string `json:"at_mobiles" label:"@的手机号列表"`
AtUserIds []string `json:"at_user_ids" label:"@的用户ID列表"`
AtAll bool `json:"at_all" label:"是否@所有人"`
}
// 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
}
// 解析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
}
msgService := send_message_service.SendMessageService{
SendMode: send_message_service.SendModeTask, // 明确标记为任务模式
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,
DefaultLogger: logrus.WithFields(logrus.Fields{
"prefix": "[Send Instance]",
}),
}
task, err := msgService.SendPreCheck()
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("发送检查不通过:%s", err), nil)
return
}
if req.Mode == "sync" {
// 同步发送
_, err := msgService.Send(task)
if err != nil {
appG.CResponse(http.StatusBadRequest, "发送失败!", nil)
return
}
appG.CResponse(http.StatusOK, "发送成功!", nil)
} else {
// 异步发送
msgService.AsyncSend(task)
appG.CResponse(http.StatusOK, "提交成功!", nil)
}
}