| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package admin
- import (
- "app/commons/services"
- "app/commons/core"
- "app/commons/model/entity"
- "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"` // 祝福语
- }
- 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.Symbol == "" {
- req.Symbol = "VND"
- }
- // 默认祝福语
- if req.BlessingWords == "" {
- if req.PacketType == 1 {
- req.BlessingWords = "恭喜发财,大吉大利"
- } else {
- req.BlessingWords = "拼手气红包,快来抢!"
- }
- }
- // 创建红包(使用管理员ID,这里简化处理用0)
- redPacketService := &services.RedPacketService{}
- packet, err := redPacketService.CreateRedPacket(
- 0, // 管理员发送,userId 为 0
- req.GroupID,
- req.PacketType,
- req.TotalAmount,
- req.TotalCount,
- req.Symbol,
- req.BlessingWords,
- )
- if err != nil {
- core.Log.Errorf("创建红包失败: %v", err)
- c.Fail(fmt.Sprintf("创建红包失败: %v", err))
- return
- }
- // 发送到 Telegram 群组
- go func() {
- sendToTelegram(packet, "系统管理员")
- }()
- 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)
- }
|