|
|
package client
import ( "go.uber.org/zap" "hudongzhuanjia/controllers" "hudongzhuanjia/libs/filter" "hudongzhuanjia/logger" "hudongzhuanjia/models" im_service "hudongzhuanjia/services/im" pay_service "hudongzhuanjia/services/pay" red_envelope_service "hudongzhuanjia/services/red_envelope" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "math" "strings" "time" )
type NoticeRedPackEvent struct { OutTradeNo string `json:"out_trade_no"` Prompt string `json:"prompt"` RedPackInfoId int64 `json:"red_pack_info_id"` ActivityId int64 `json:"activity_id"` Status int `json:"status"` }
var SendRedPackQueue = make(chan *NoticeRedPackEvent, math.MaxInt8)
func PutSendRedPackQueue(outTradeNo, prompt string, redPackInfoId, activityId int64, status int) { SendRedPackQueue <- &NoticeRedPackEvent{ OutTradeNo: outTradeNo, Prompt: prompt, RedPackInfoId: redPackInfoId, ActivityId: activityId, Status: status, } }
func loopSendRedPack() { redPackInfos, err := models.GetLiveRedPackInfos(0) if err != nil { panic(err) } SendRedPackQueue = make(chan *NoticeRedPackEvent, math.MaxInt8) for _, redPackInfo := range redPackInfos { SendRedPackQueue <- &NoticeRedPackEvent{ OutTradeNo: redPackInfo.OutTradeNo, Prompt: redPackInfo.Prompt, RedPackInfoId: redPackInfo.Id, ActivityId: redPackInfo.ActivityId, Status: redPackInfo.Status, } }
defer func() { if err := recover(); err != nil { logger.Error("用户发送红包轮询出现错误", zap.String("函数", "loopSendRedPack"), zap.Any("错误", err)) } time.Sleep(5 * time.Second) loopSendRedPack() }()
for { select { case param, ok := <-SendRedPackQueue: if !ok { panic("SendRedPackQueue通道异常关闭") }
if param.Status != 0 && param.Status != 1 { // 已推送和已作废过滤掉
continue }
userOrder := new(models.UserOrder) exist, err := userOrder.GetByOutTradeNo(param.OutTradeNo) if err != nil || !exist { logger.Error("通过out_trade_no获取user_order订单信息", zap.String("错误原因", err.Error()), zap.Bool("是否存在", exist), zap.String("交易单号", param.OutTradeNo)) continue } if userOrder.Status == 0 { // NOPAY
SendRedPackQueue <- param continue } else if userOrder.Status == 1 { param.Status = 1 } else { param.Status = 2 // 作废订单
}
err = im_service.SendNoticeByActivityId(param.ActivityId, map[string]interface{}{ "prompt": param.Prompt, "timestamp": time.Now().Unix(), "red_pack_info_id": param.RedPackInfoId, }) if err != nil { logger.Error("red_pack_info推送通知出现错误", zap.String("错误原因", err.Error()), zap.String("交易单号", param.Prompt)) continue }
_, err = new(models.LiveRedPackInfo).UpdateStatusByOutTradeNo(param.OutTradeNo, param.Status) if err != nil { logger.Error("red_pack_info状态更新出现错误", zap.String("错误原因", err.Error()), zap.String("交易单号", param.OutTradeNo)) continue } } } }
func init() { go loopSendRedPack() }
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, 3, activityId, userId) t.CheckErr(err)
info := models.LiveRedPackInfo{} info.OutTradeNo = res["out_trade_no"].(string) info.Amount = amount info.UserId = userId info.ActivityId = activityId info.Prompt = filter.Replace(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), 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) }
res["red_pack_info_id"] = info.Id go PutSendRedPackQueue(info.OutTradeNo, info.Prompt, info.Id, info.ActivityId, info.Status) // 加入发送队列
t.JSON(res) }
// 支付之后可以遍历查询是否成功 -- 前端发送消息
func (t *LiveCtl) QueryRedPack() { outTradeNo := t.MustGet("out_trade_no") res, err := pay_service.OrderQuery(outTradeNo) t.CheckErr(err) info := new(models.LiveRedPackInfo) exist, err := info.GetByOutTradeNo(outTradeNo) t.CheckErr(err) t.Assert(exist, code.MSG_LIVE_RED_PACK_INFO_NOT_EXIST, "直播红包信息不存在") if res.TradeState == pay_service.CODE_TRADE_SUCCESS { info.Status = 1 info.UpdateStatusById(info.Id, info.Status) t.JSON(map[string]interface{}{ "red_pack_info_id": info.Id, "status": info.Status, }) } else { t.JSON(map[string]interface{}{ "red_pack_info_id": info.Id, "status": info.Status, }) } }
// 领取红包
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.PartnerTradeNo = 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 } pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, redPack.Amount, 5, 5*60)
t.JSON(redPack) }
|