redpacket.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package admin
  2. import (
  3. "app/commons/services"
  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. )
  11. // SendRedPacketAdmin 管理端发送红包(不需要JWT,使用签名验证)
  12. func (s *Server) SendRedPacketAdmin(ctx *gin.Context) {
  13. c := s.FromContext(ctx)
  14. type request struct {
  15. GroupID string `json:"groupId" binding:"required"` // Telegram群组ID
  16. TotalAmount decimal.Decimal `json:"totalAmount" binding:"required"` // 红包总金额
  17. TotalCount int `json:"totalCount" binding:"required"` // 红包个数
  18. PacketType int `json:"packetType" binding:"required"` // 1=普通, 2=手气
  19. Symbol string `json:"symbol"` // 币种,默认VND
  20. BlessingWords string `json:"blessingWords"` // 祝福语
  21. }
  22. req := new(request)
  23. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  24. c.Fail(fmt.Sprintf("参数错误: %v", err))
  25. return
  26. }
  27. // 参数验证
  28. if req.PacketType != 1 && req.PacketType != 2 {
  29. c.Fail("红包类型错误,必须是1(普通)或2(手气)")
  30. return
  31. }
  32. if req.TotalCount <= 0 || req.TotalCount > 100 {
  33. c.Fail("红包个数必须在1-100之间")
  34. return
  35. }
  36. if req.TotalAmount.LessThanOrEqual(decimal.Zero) {
  37. c.Fail("红包金额必须大于0")
  38. return
  39. }
  40. // 默认币种
  41. if req.Symbol == "" {
  42. req.Symbol = "VND"
  43. }
  44. // 默认祝福语
  45. if req.BlessingWords == "" {
  46. if req.PacketType == 1 {
  47. req.BlessingWords = "恭喜发财,大吉大利"
  48. } else {
  49. req.BlessingWords = "拼手气红包,快来抢!"
  50. }
  51. }
  52. // 创建红包(使用管理员ID,这里简化处理用0)
  53. redPacketService := &services.RedPacketService{}
  54. packet, err := redPacketService.CreateRedPacket(
  55. 0, // 管理员发送,userId 为 0
  56. req.GroupID,
  57. req.PacketType,
  58. req.TotalAmount,
  59. req.TotalCount,
  60. req.Symbol,
  61. req.BlessingWords,
  62. )
  63. if err != nil {
  64. core.Log.Errorf("创建红包失败: %v", err)
  65. c.Fail(fmt.Sprintf("创建红包失败: %v", err))
  66. return
  67. }
  68. // 发送到 Telegram 群组
  69. go func() {
  70. sendToTelegram(packet, "系统管理员")
  71. }()
  72. c.Resp(gin.H{
  73. "packetId": packet.Id,
  74. "packetNo": packet.PacketNo,
  75. "message": "红包发送成功",
  76. })
  77. }
  78. // sendToTelegram 发送红包到Telegram群组
  79. func sendToTelegram(packet *entity.TgRedPacket, senderName string) {
  80. if packet == nil {
  81. return
  82. }
  83. // 调用 telegram 包的发送函数
  84. telegram.SendRedPacketToGroup(
  85. packet.GroupId,
  86. packet.PacketNo,
  87. senderName,
  88. packet.TotalAmount.InexactFloat64(),
  89. packet.TotalCount,
  90. packet.PacketType,
  91. packet.BlessingWords,
  92. )
  93. core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s",
  94. packet.PacketNo, packet.GroupId)
  95. }