redpacket.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package telegram
  2. import (
  3. "app/commons/core"
  4. "app/commons/services"
  5. "fmt"
  6. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  7. "strings"
  8. )
  9. // handleCallbackQuery 处理回调查询
  10. func handleCallbackQuery(callback *tgbotapi.CallbackQuery) {
  11. data := callback.Data
  12. // 处理抢红包
  13. if strings.HasPrefix(data, "grab_") {
  14. packetNo := strings.TrimPrefix(data, "grab_")
  15. grabRedPacket(callback, packetNo)
  16. return
  17. }
  18. // 其他回调
  19. answerCallback(callback.ID, "未知操作")
  20. }
  21. // grabRedPacket 抢红包
  22. func grabRedPacket(callback *tgbotapi.CallbackQuery, packetNo string) {
  23. telegramID := callback.From.ID
  24. username := callback.From.UserName
  25. // 检查绑定状态
  26. bindService := &services.TgBindService{}
  27. bound, userId := bindService.IsUserBound(telegramID, username)
  28. if !bound {
  29. answerCallback(callback.ID, "⚠️ 请先绑定账户!私聊我发送 /bind")
  30. // 私聊发送绑定提示
  31. sendTextMessage(telegramID, "⚠️ 您尚未绑定平台账户,无法抢红包。\n\n请发送 /bind 获取绑定链接。")
  32. core.Log.Infof("抢红包 - 用户: %s, 红包: %s, 结果: 未绑定", username, packetNo)
  33. return
  34. }
  35. // 用真实 userId 抢红包
  36. redPacketService := &services.RedPacketService{}
  37. record, err := redPacketService.GrabRedPacket(
  38. packetNo,
  39. userId,
  40. telegramID,
  41. username,
  42. )
  43. if err != nil {
  44. answerCallback(callback.ID, err.Error())
  45. core.Log.Infof("抢红包 - 用户: %s(userId:%d), 红包: %s, 结果: %s",
  46. username, userId, packetNo, err.Error())
  47. return
  48. }
  49. // 抢到红包
  50. amount := record.Amount.InexactFloat64()
  51. answerCallback(callback.ID, fmt.Sprintf("🎉 恭喜抢到 %.2f VND!", amount))
  52. // 从数据库查询完整的红包信息和所有领取记录,构建实时消息
  53. packet, records, _ := redPacketService.GetRedPacketDetail(packetNo)
  54. if packet == nil {
  55. return
  56. }
  57. // 构建红包头部信息
  58. var packetTypeText string
  59. if packet.PacketType == 1 {
  60. packetTypeText = "普通红包"
  61. } else {
  62. packetTypeText = "手气红包"
  63. }
  64. editText := fmt.Sprintf("🧧 红包\n💰 %s\n\n总金额: %.2f VND | 个数: %d | %s",
  65. packet.BlessingWords,
  66. packet.TotalAmount.InexactFloat64(),
  67. packet.TotalCount,
  68. packetTypeText)
  69. // 追加每个人的领取记录
  70. editText += fmt.Sprintf("\n\n📋 领取明细 (%d/%d):", packet.GrabbedCount, packet.TotalCount)
  71. for _, r := range records {
  72. displayName := r.TelegramUsername
  73. if displayName != "" {
  74. displayName = "@" + displayName
  75. } else {
  76. displayName = fmt.Sprintf("用户%d", r.TelegramId)
  77. }
  78. editText += fmt.Sprintf("\n✅ %s — %.2f VND", displayName, r.Amount.InexactFloat64())
  79. }
  80. // 根据剩余情况决定是否保留按钮
  81. if packet.RemainCount > 0 {
  82. keyboard := tgbotapi.NewInlineKeyboardMarkup(
  83. tgbotapi.NewInlineKeyboardRow(
  84. tgbotapi.NewInlineKeyboardButtonData(
  85. fmt.Sprintf("🎁 抢红包 (%d/%d)", packet.GrabbedCount, packet.TotalCount),
  86. fmt.Sprintf("grab_%s", packetNo),
  87. ),
  88. ),
  89. )
  90. editMessageWithKeyboard(callback.Message.Chat.ID, callback.Message.MessageID, editText, keyboard)
  91. } else {
  92. editText += "\n\n🎊 红包已抢完!"
  93. editMessage(callback.Message.Chat.ID, callback.Message.MessageID, editText)
  94. }
  95. core.Log.Infof("抢红包 - 用户: %s(userId:%d), 红包: %s, 金额: %s, 结果: 成功",
  96. username, userId, packetNo, record.Amount.String())
  97. }