| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package redpacket
- import (
- "app/commons/constant"
- "app/commons/core"
- "app/commons/model/entity"
- "app/telegram"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/shopspring/decimal"
- "time"
- )
- // SendRedPacket 发红包
- func (s *Server) SendRedPacket(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type request struct {
- GroupID string `json:"groupId" binding:"required"` // Telegram群组ID
- TotalAmount decimal.Decimal `json:"totalAmount" binding:"required"` // 红包总金额
- TotalCount int `json:"totalCount" binding:"required"` // 红包个数
- PacketType int `json:"packetType" binding:"required"` // 1=普通, 2=手气
- Symbol string `json:"symbol"` // 币种,默认VND
- BlessingWords string `json:"blessingWords"` // 祝福语
- }
- req := new(request)
- if err := c.ShouldBindBodyWithJSON(req); err != nil {
- c.Fail(constant.ErrorParams)
- return
- }
- // 防刷控制(3秒内不能重复发)
- if !c.RepeatFilter(fmt.Sprintf("SendRedPacket:%d", c.UserId()), time.Second*3) {
- c.Fail(constant.ErrorFrequent)
- return
- }
- // 默认币种
- if req.Symbol == "" {
- req.Symbol = "VND"
- }
- // 创建红包
- packet, err := s.RedPacketService.CreateRedPacket(
- c.UserId(),
- req.GroupID,
- req.PacketType,
- req.TotalAmount,
- req.TotalCount,
- req.Symbol,
- req.BlessingWords,
- )
- if err != nil {
- c.Fail(err.Error())
- return
- }
- // 发送到 Telegram 群组
- go func() {
- // 异步发送到 Telegram
- // 使用系统发送者名称
- sendToTelegram(packet, "System")
- }()
- c.Resp(gin.H{
- "packetId": packet.Id,
- "packetNo": packet.PacketNo,
- "message": "红包发送成功",
- })
- }
- // sendToTelegram 发送红包到Telegram群组
- func sendToTelegram(packet *entity.TgRedPacket, senderName string) {
- if packet == nil {
- return
- }
- // 调用 telegram 包的发送函数
- telegram.SendRedPacketToGroup(
- packet.GroupId,
- packet.PacketNo,
- senderName,
- packet.TotalAmount.InexactFloat64(),
- packet.TotalCount,
- packet.PacketType,
- packet.BlessingWords,
- )
- core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s",
- packet.PacketNo, packet.GroupId)
- }
|