| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package admin
- import (
- "app/commons/core"
- "app/commons/i18n"
- "app/commons/model/entity"
- "app/commons/services"
- "app/telegram"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/shopspring/decimal"
- )
- // SendRedPacketAdmin 管理端发送红包(不需要JWT,使用签名验证)
- func (s *Server) SendRedPacketAdmin(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"` // 祝福语
- ExpireMinutes int `json:"expireMinutes"` // 过期时间(分钟),默认10
- MaxGrabAmount decimal.Decimal `json:"maxGrabAmount"` // 单人最大可领取金额(0=不限制)
- Lang string `json:"lang"` // 消息语言: vi/id/en/zh
- }
- req := new(request)
- if err := c.ShouldBindBodyWithJSON(req); err != nil {
- c.Fail(fmt.Sprintf("参数错误: %v", err))
- return
- }
- // 参数验证
- if req.PacketType != 1 && req.PacketType != 2 {
- c.Fail("红包类型错误,必须是1(普通)或2(手气)")
- return
- }
- if req.TotalCount <= 0 || req.TotalCount > 100 {
- c.Fail("红包个数必须在1-100之间")
- return
- }
- if req.TotalAmount.LessThanOrEqual(decimal.Zero) {
- c.Fail("红包金额必须大于0")
- return
- }
- // 默认语言
- if req.Lang == "" {
- req.Lang = "vi"
- }
- // 默认币种(根据语言)
- if req.Symbol == "" {
- req.Symbol = i18n.DefaultSymbol(req.Lang)
- }
- // 默认祝福语(根据语言)
- if req.BlessingWords == "" {
- m := i18n.GetLang(req.Lang)
- if req.PacketType == 1 {
- req.BlessingWords = m.BlessingDefault1
- } else {
- req.BlessingWords = m.BlessingDefault2
- }
- }
- // 创建红包(使用管理员ID,这里简化处理用0)
- redPacketService := &services.RedPacketService{}
- packet, err := redPacketService.CreateRedPacket(
- 0, // 管理员发送,userId 为 0
- req.GroupID,
- req.PacketType,
- req.TotalAmount,
- req.TotalCount,
- req.Symbol,
- req.BlessingWords,
- req.ExpireMinutes,
- req.MaxGrabAmount,
- req.Lang,
- )
- if err != nil {
- core.Log.Errorf("创建红包失败: %v", err)
- c.Fail(fmt.Sprintf("创建红包失败: %v", err))
- return
- }
- // 发送到 Telegram 群组并保存消息ID
- adminName := i18n.GetLang(req.Lang).AdminName
- go func() {
- sendToTelegramAndSaveMsg(packet, adminName, req.ExpireMinutes, req.Lang)
- }()
- c.Resp(gin.H{
- "packetId": packet.Id,
- "packetNo": packet.PacketNo,
- "message": "红包发送成功",
- })
- }
- // sendToTelegramAndSaveMsg 发送红包到Telegram群组并保存消息ID
- func sendToTelegramAndSaveMsg(packet *entity.TgRedPacket, senderName string, expireMinutes int, lang string) {
- if packet == nil {
- return
- }
- // 调用 telegram 包的发送函数,获取返回的消息ID
- msgId := telegram.SendRedPacketToGroup(
- packet.GroupId,
- packet.PacketNo,
- senderName,
- packet.TotalAmount.InexactFloat64(),
- packet.TotalCount,
- packet.PacketType,
- packet.BlessingWords,
- expireMinutes,
- lang,
- packet.Symbol,
- )
- // 保存 Telegram 消息ID到红包记录(用于过期时编辑消息)
- if msgId > 0 {
- db := services.NewComService().DB()
- db.Model(&entity.TgRedPacket{}).Where("id = ?", packet.Id).Update("message_id", msgId)
- core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s, MessageID: %d",
- packet.PacketNo, packet.GroupId, msgId)
- } else {
- core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s (未获取到消息ID)",
- packet.PacketNo, packet.GroupId)
- }
- }
|