send.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package redpacket
  2. import (
  3. "app/commons/constant"
  4. "app/commons/core"
  5. "app/commons/i18n"
  6. "app/commons/model/entity"
  7. "app/commons/services"
  8. "app/telegram"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/shopspring/decimal"
  12. "time"
  13. )
  14. // SendRedPacket 发红包
  15. func (s *Server) SendRedPacket(ctx *gin.Context) {
  16. c := s.FromContext(ctx)
  17. type request struct {
  18. GroupID string `json:"groupId" binding:"required"` // Telegram群组ID
  19. TotalAmount decimal.Decimal `json:"totalAmount" binding:"required"` // 红包总金额
  20. TotalCount int `json:"totalCount" binding:"required"` // 红包个数
  21. PacketType int `json:"packetType" binding:"required"` // 1=普通, 2=手气
  22. Symbol string `json:"symbol"` // 币种,默认VND
  23. BlessingWords string `json:"blessingWords"` // 祝福语
  24. ExpireMinutes int `json:"expireMinutes"` // 过期时间(分钟),默认10
  25. MaxGrabAmount decimal.Decimal `json:"maxGrabAmount"` // 单人最大可领取金额(0=不限制)
  26. Lang string `json:"lang"` // 消息语言: vi/id/en/zh
  27. }
  28. req := new(request)
  29. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  30. c.Fail(constant.ErrorParams)
  31. return
  32. }
  33. // 防刷控制(3秒内不能重复发)
  34. if !c.RepeatFilter(fmt.Sprintf("SendRedPacket:%d", c.UserId()), time.Second*3) {
  35. c.Fail(constant.ErrorFrequent)
  36. return
  37. }
  38. // 默认语言
  39. if req.Lang == "" {
  40. req.Lang = "vi"
  41. }
  42. // 默认币种(根据语言)
  43. if req.Symbol == "" {
  44. req.Symbol = i18n.DefaultSymbol(req.Lang)
  45. }
  46. // 创建红包
  47. packet, err := s.RedPacketService.CreateRedPacket(
  48. c.UserId(),
  49. req.GroupID,
  50. req.PacketType,
  51. req.TotalAmount,
  52. req.TotalCount,
  53. req.Symbol,
  54. req.BlessingWords,
  55. req.ExpireMinutes,
  56. req.MaxGrabAmount,
  57. req.Lang,
  58. )
  59. if err != nil {
  60. c.Fail(err.Error())
  61. return
  62. }
  63. // 发送到 Telegram 群组并保存消息ID
  64. go func() {
  65. userSendToTelegram(packet, "System", req.ExpireMinutes, req.Lang)
  66. }()
  67. c.Resp(gin.H{
  68. "packetId": packet.Id,
  69. "packetNo": packet.PacketNo,
  70. "message": "红包发送成功",
  71. })
  72. }
  73. // userSendToTelegram 用户发红包到Telegram群组并保存消息ID
  74. func userSendToTelegram(packet *entity.TgRedPacket, senderName string, expireMinutes int, lang string) {
  75. if packet == nil {
  76. return
  77. }
  78. // 调用 telegram 包的发送函数,获取返回的消息ID
  79. msgId := telegram.SendRedPacketToGroup(
  80. packet.GroupId,
  81. packet.PacketNo,
  82. senderName,
  83. packet.TotalAmount.InexactFloat64(),
  84. packet.TotalCount,
  85. packet.PacketType,
  86. packet.BlessingWords,
  87. expireMinutes,
  88. lang,
  89. packet.Symbol,
  90. )
  91. // 保存 Telegram 消息ID到红包记录
  92. if msgId > 0 {
  93. db := services.NewComService().DB()
  94. db.Model(&entity.TgRedPacket{}).Where("id = ?", packet.Id).Update("message_id", msgId)
  95. }
  96. core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s, MessageID: %d",
  97. packet.PacketNo, packet.GroupId, msgId)
  98. }