diff --git a/controllers/client/live.go b/controllers/client/live.go index 8cf17a7..e53f9a3 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -1,17 +1,13 @@ 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" ) @@ -24,95 +20,6 @@ type NoticeRedPackEvent struct { 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 @@ -216,12 +123,11 @@ func (t *LiveCtl) SendLiveRedPack() { } 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() { +func (t *LiveCtl) QueryLiveRedPack() { outTradeNo := t.MustGet("out_trade_no") res, err := pay_service.OrderQuery(outTradeNo) t.CheckErr(err) @@ -245,7 +151,7 @@ func (t *LiveCtl) QueryRedPack() { } // 领取红包 -func (t *LiveCtl) GetRedPack() { +func (t *LiveCtl) GetLiveRedPack() { liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id") userId := t.MustGetUID()