|
|
package client
import ( "go.uber.org/zap" "hudongzhuanjia/controllers" "hudongzhuanjia/libs/filter" "hudongzhuanjia/logger" "hudongzhuanjia/models" pay_service "hudongzhuanjia/services/pay" red_envelope_service "hudongzhuanjia/services/red_envelope" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "strings" "time" )
var MaxQueueSize = 10000 var RequestLimit = 60 * time.Second var GetRedPackQueue = make(chan int64, MaxQueueSize)
func loopGetRedPack() { redPacks, err := models.GetRedPacksByStatus(1) if err != nil { panic(err) }
// 保证容量足够
GetRedPackQueue = make(chan int64, MaxQueueSize+len(redPacks))
// 初始化
for _, redPack := range redPacks { GetRedPackQueue <- redPack.Id }
defer func() { if errRec := recover(); errRec != nil { logger.Error("用户转账轮询发生错误", zap.String("函数", "loopGetRedPack"), zap.Any("错误原因", errRec)) } time.Sleep(5 * time.Second) loopGetRedPack() }() for { select { case redPackId, ok := <-GetRedPackQueue: if !ok { panic("GetRedPackQueue通道关闭") } time.Sleep(RequestLimit) // 请求频率
redPack := new(models.LiveRedPack) exist, err := models.GetById(redPack, redPackId) if err != nil { GetRedPackQueue <- redPackId logger.Error("获取LiveRedPack表数据出现错误", zap.Any("错误原因", err), zap.Int64("LiveRedPack表的id", redPack.Id)) continue } if !exist { logger.Error("不存在LiveRedPack表数据", zap.Int64("LiveRedPack表的id", redPack.Id)) continue }
// todo: 增加一个查询机制 --查询此次付款是否成功
// todo: 是否需要发放系统通知
resp, err := pay_service.TransferInfo(redPack.TransferNo) if err != nil { GetRedPackQueue <- redPackId logger.Error("微信企业转账查询API", zap.Any("错误原因", err), zap.Int64("LiveRedPack表的id", redPack.Id)) continue } if resp.Status == pay_service.CODE_FAIL { // 转账失败
_, err = pay_service.Transfer("欧轩互动-红包活动", redPack.OpenId, redPack.TransferNo, redPack.Amount) if err != nil { GetRedPackQueue <- redPackId logger.Error("微信企业转账API", zap.Any("错误原因", err), zap.Int64("LiveRedPack表的id", redPack.Id)) continue } _, err = redPack.UpdateStatusById(redPack.Id, 2) if err != nil { GetRedPackQueue <- redPackId logger.Error("微信企业转账,LiveRedPack表更新错误", zap.Any("错误原因", err), zap.Int64("LiveRedPack表id", redPack.Id)) continue } } else if resp.Status == pay_service.CODE_SUCCESS { _, err = redPack.UpdateStatusById(redPack.Id, 2) if err != nil { logger.Error("微信企业查询,转账成功", zap.Any("错误原因", err), zap.Int64("LiveRedPack表id", redPack.Id)) continue } } } } }
var SendRedPackQueue = make(chan int64, MaxQueueSize)
func loopSendRedPack() { redPackInfo := defer func() { if err := recover(); err != nil { logger.Error("用户发送红包轮询出现错误", zap.String("函数", "loopSendRedPack"), zap.Any("错误", err)) } time.Sleep(5 * time.Second) loopSendRedPack() }() for { select { case redPackInfoId, ok := <-SendRedPackQueue: if !ok { panic("SendRedPackQueue通道异常关闭") }
} } }
type LiveCtl struct { controllers.AuthorCtl //controllers.BaseCtl
}
// 详情
func (t *LiveCtl) Detail() { activityId := t.MustGetInt64("activity_id") areaId := t.MustGetInt64("area_id") userId := t.MustGetUID()
err := new(models.LiveViewer).Record(userId, activityId) t.CheckErr(err)
live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
config := new(models.LiveConfigArea) exist, err = config.GetByActivityIdAndAreaId(activityId, areaId) t.CheckErr(err) if exist { live.SharePosterImg = config.MergeSharePoster }
live.AdminLiveUrl = "" t.JSON(live) }
func (t *LiveCtl) Like() { activityId := t.MustGetInt64("activity_id") //userId := t.MustGetUID()
_, err := new(models.LiveConfig).Like(activityId) t.CheckErr(err)
live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在") t.JSON(map[string]interface{}{ "like": live.LikeNum, "watch": live.WatchNum, }) }
func (t *LiveCtl) LoopQuery() { activityId := t.MustGetInt64("activity_id") live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在") t.JSON(map[string]interface{}{ "like": live.LikeNum, "watch": live.WatchNum, }) }
// 发送红包
// 维护一个队列进行循环, 遍历是否付款成功
func (t *LiveCtl) SendLiveRedPack() { userId := t.MustGetUID() // 用户 uid
activityId := t.MustGetInt64("activity_id") // activity_id
num := t.MustGetInt("num") // 红包数量
amount := t.MustGetInt64("amount") // 金额
prompt := t.MustGet("prompt") // 提示
user := models.User{} exist, err := models.GetById(&user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", ip[0], user.Openid, amount) t.CheckErr(err)
info := models.LiveRedPackInfo{} info.Amount = amount info.UserId = userId info.ActivityId = activityId info.Prompt = prompt info.IsDelete = false info.UpdatedAt = time.Now() info.CreatedAt = time.Now() _, err = info.Add() t.CheckErr(err)
redPacks := red_envelope_service.GenRedPack(int(amount*100), num) for _, v := range redPacks { redPack := new(models.LiveRedPack) redPack.LiveRedPackInfoId = info.Id redPack.ActivityId = activityId redPack.Receiver = 0 redPack.Amount = v redPack.CreatedAt = time.Now() redPack.UpdatedAt = time.Now() _, err = redPack.Add() t.CheckErr(err) } info.Prompt = filter.Replace(info.Prompt) t.JSON(info) }
// 领取红包
func (t *LiveCtl) GetRedPack() { liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id") userId := t.MustGetUID()
user := models.User{} exist, err := models.GetById(&user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
redPack := new(models.LiveRedPack) exist, err = redPack.GetByInfoId(liveRedPackInfoId) t.CheckErr(err) if !exist { // 通知其他的人
t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST) return }
// 乐观锁 ==> 防止并发
redPack.OpenId = user.Openid redPack.Receiver = user.Id redPack.TransferType = 1 redPack.TransferNo = utils.RandomStr(32) row, err := redPack.UpdateStatusById(redPack.Id, 1) t.CheckErr(err) if row != 1 { t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST) return } GetRedPackQueue <- redPack.Id // 进入队列
t.JSON(redPack) }
|