send.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package redpacket
  2. import (
  3. "app/commons/constant"
  4. "app/commons/core"
  5. "app/commons/model/entity"
  6. "app/telegram"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/shopspring/decimal"
  10. "time"
  11. )
  12. // SendRedPacket 发红包
  13. func (s *Server) SendRedPacket(ctx *gin.Context) {
  14. c := s.FromContext(ctx)
  15. type request struct {
  16. GroupID string `json:"groupId" binding:"required"` // Telegram群组ID
  17. TotalAmount decimal.Decimal `json:"totalAmount" binding:"required"` // 红包总金额
  18. TotalCount int `json:"totalCount" binding:"required"` // 红包个数
  19. PacketType int `json:"packetType" binding:"required"` // 1=普通, 2=手气
  20. Symbol string `json:"symbol"` // 币种,默认VND
  21. BlessingWords string `json:"blessingWords"` // 祝福语
  22. }
  23. req := new(request)
  24. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  25. c.Fail(constant.ErrorParams)
  26. return
  27. }
  28. // 防刷控制(3秒内不能重复发)
  29. if !c.RepeatFilter(fmt.Sprintf("SendRedPacket:%d", c.UserId()), time.Second*3) {
  30. c.Fail(constant.ErrorFrequent)
  31. return
  32. }
  33. // 默认币种
  34. if req.Symbol == "" {
  35. req.Symbol = "VND"
  36. }
  37. // 创建红包
  38. packet, err := s.RedPacketService.CreateRedPacket(
  39. c.UserId(),
  40. req.GroupID,
  41. req.PacketType,
  42. req.TotalAmount,
  43. req.TotalCount,
  44. req.Symbol,
  45. req.BlessingWords,
  46. )
  47. if err != nil {
  48. c.Fail(err.Error())
  49. return
  50. }
  51. // 发送到 Telegram 群组
  52. go func() {
  53. // 异步发送到 Telegram
  54. // 使用系统发送者名称
  55. sendToTelegram(packet, "System")
  56. }()
  57. c.Resp(gin.H{
  58. "packetId": packet.Id,
  59. "packetNo": packet.PacketNo,
  60. "message": "红包发送成功",
  61. })
  62. }
  63. // sendToTelegram 发送红包到Telegram群组
  64. func sendToTelegram(packet *entity.TgRedPacket, senderName string) {
  65. if packet == nil {
  66. return
  67. }
  68. // 调用 telegram 包的发送函数
  69. telegram.SendRedPacketToGroup(
  70. packet.GroupId,
  71. packet.PacketNo,
  72. senderName,
  73. packet.TotalAmount.InexactFloat64(),
  74. packet.TotalCount,
  75. packet.PacketType,
  76. packet.BlessingWords,
  77. )
  78. core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s",
  79. packet.PacketNo, packet.GroupId)
  80. }