互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
7.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "go.uber.org/zap"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/libs/filter"
  6. "hudongzhuanjia/logger"
  7. "hudongzhuanjia/models"
  8. im_service "hudongzhuanjia/services/im"
  9. pay_service "hudongzhuanjia/services/pay"
  10. red_envelope_service "hudongzhuanjia/services/red_envelope"
  11. "hudongzhuanjia/utils"
  12. "hudongzhuanjia/utils/code"
  13. "math"
  14. "strings"
  15. "time"
  16. )
  17. type NoticeRedPackEvent struct {
  18. OutTradeNo string `json:"out_trade_no"`
  19. Prompt string `json:"prompt"`
  20. RedPackInfoId int64 `json:"red_pack_info_id"`
  21. ActivityId int64 `json:"activity_id"`
  22. Status int `json:"status"`
  23. }
  24. var SendRedPackQueue = make(chan *NoticeRedPackEvent, math.MaxInt8)
  25. func PutSendRedPackQueue(outTradeNo, prompt string, redPackInfoId, activityId int64, status int) {
  26. SendRedPackQueue <- &NoticeRedPackEvent{
  27. OutTradeNo: outTradeNo,
  28. Prompt: prompt,
  29. RedPackInfoId: redPackInfoId,
  30. ActivityId: activityId,
  31. Status: status,
  32. }
  33. }
  34. func loopSendRedPack() {
  35. redPackInfos, err := models.GetLiveRedPackInfos(0)
  36. if err != nil {
  37. panic(err)
  38. }
  39. SendRedPackQueue = make(chan *NoticeRedPackEvent, math.MaxInt8)
  40. for _, redPackInfo := range redPackInfos {
  41. SendRedPackQueue <- &NoticeRedPackEvent{
  42. OutTradeNo: redPackInfo.OutTradeNo,
  43. Prompt: redPackInfo.Prompt,
  44. RedPackInfoId: redPackInfo.Id,
  45. ActivityId: redPackInfo.ActivityId,
  46. Status: redPackInfo.Status,
  47. }
  48. }
  49. defer func() {
  50. if err := recover(); err != nil {
  51. logger.Error("用户发送红包轮询出现错误",
  52. zap.String("函数", "loopSendRedPack"), zap.Any("错误", err))
  53. }
  54. time.Sleep(5 * time.Second)
  55. loopSendRedPack()
  56. }()
  57. for {
  58. select {
  59. case param, ok := <-SendRedPackQueue:
  60. if !ok {
  61. panic("SendRedPackQueue通道异常关闭")
  62. }
  63. if param.Status != 0 && param.Status != 1 { // 已推送和已作废过滤掉
  64. continue
  65. }
  66. userOrder := new(models.UserOrder)
  67. exist, err := userOrder.GetByOutTradeNo(param.OutTradeNo)
  68. if err != nil || !exist {
  69. logger.Error("通过out_trade_no获取user_order订单信息", zap.String("错误原因", err.Error()),
  70. zap.Bool("是否存在", exist), zap.String("交易单号", param.OutTradeNo))
  71. continue
  72. }
  73. if userOrder.Status == 0 { // NOPAY
  74. SendRedPackQueue <- param
  75. continue
  76. } else if userOrder.Status == 1 {
  77. param.Status = 1
  78. } else {
  79. param.Status = 2 // 作废订单
  80. }
  81. err = im_service.SendNoticeByActivityId(param.ActivityId, map[string]interface{}{
  82. "prompt": param.Prompt,
  83. "timestamp": time.Now().Unix(),
  84. "red_pack_info_id": param.RedPackInfoId,
  85. })
  86. if err != nil {
  87. logger.Error("red_pack_info推送通知出现错误", zap.String("错误原因", err.Error()),
  88. zap.String("交易单号", param.Prompt))
  89. continue
  90. }
  91. _, err = new(models.LiveRedPackInfo).UpdateStatusByOutTradeNo(param.OutTradeNo, param.Status)
  92. if err != nil {
  93. logger.Error("red_pack_info状态更新出现错误", zap.String("错误原因", err.Error()),
  94. zap.String("交易单号", param.OutTradeNo))
  95. continue
  96. }
  97. }
  98. }
  99. }
  100. func init() {
  101. go loopSendRedPack()
  102. }
  103. type LiveCtl struct {
  104. controllers.AuthorCtl
  105. //controllers.BaseCtl
  106. }
  107. // 详情
  108. func (t *LiveCtl) Detail() {
  109. activityId := t.MustGetInt64("activity_id")
  110. areaId := t.MustGetInt64("area_id")
  111. userId := t.MustGetUID()
  112. err := new(models.LiveViewer).Record(userId, activityId)
  113. t.CheckErr(err)
  114. live := new(models.LiveConfig)
  115. exist, err := live.GetByActivityId(activityId)
  116. t.CheckErr(err)
  117. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  118. config := new(models.LiveConfigArea)
  119. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  120. t.CheckErr(err)
  121. if exist {
  122. live.SharePosterImg = config.MergeSharePoster
  123. }
  124. live.AdminLiveUrl = ""
  125. t.JSON(live)
  126. }
  127. func (t *LiveCtl) Like() {
  128. activityId := t.MustGetInt64("activity_id")
  129. //userId := t.MustGetUID()
  130. _, err := new(models.LiveConfig).Like(activityId)
  131. t.CheckErr(err)
  132. live := new(models.LiveConfig)
  133. exist, err := live.GetByActivityId(activityId)
  134. t.CheckErr(err)
  135. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  136. t.JSON(map[string]interface{}{
  137. "like": live.LikeNum,
  138. "watch": live.WatchNum,
  139. })
  140. }
  141. func (t *LiveCtl) LoopQuery() {
  142. activityId := t.MustGetInt64("activity_id")
  143. live := new(models.LiveConfig)
  144. exist, err := live.GetByActivityId(activityId)
  145. t.CheckErr(err)
  146. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  147. t.JSON(map[string]interface{}{
  148. "like": live.LikeNum,
  149. "watch": live.WatchNum,
  150. })
  151. }
  152. // 下单发送红包
  153. // 维护一个队列进行循环, 遍历是否付款成功
  154. func (t *LiveCtl) SendLiveRedPack() {
  155. userId := t.MustGetUID() // 用户 uid
  156. activityId := t.MustGetInt64("activity_id") // activity_id
  157. num := t.MustGetInt("num") // 红包数量
  158. amount := t.MustGetInt64("amount") // 金额
  159. prompt := t.MustGet("prompt") // 提示
  160. user := models.User{}
  161. exist, err := models.GetById(&user, userId)
  162. t.CheckErr(err)
  163. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  164. ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
  165. res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", ip[0], user.Openid, amount, 3, activityId, userId)
  166. t.CheckErr(err)
  167. info := models.LiveRedPackInfo{}
  168. info.OutTradeNo = res["out_trade_no"].(string)
  169. info.Amount = amount
  170. info.UserId = userId
  171. info.ActivityId = activityId
  172. info.Prompt = filter.Replace(prompt)
  173. info.IsDelete = false
  174. info.UpdatedAt = time.Now()
  175. info.CreatedAt = time.Now()
  176. _, err = info.Add()
  177. t.CheckErr(err)
  178. redPacks := red_envelope_service.GenRedPack(int(amount), num)
  179. for _, v := range redPacks {
  180. redPack := new(models.LiveRedPack)
  181. redPack.LiveRedPackInfoId = info.Id
  182. redPack.ActivityId = activityId
  183. redPack.Receiver = 0
  184. redPack.Amount = v
  185. redPack.CreatedAt = time.Now()
  186. redPack.UpdatedAt = time.Now()
  187. _, err = redPack.Add()
  188. t.CheckErr(err)
  189. }
  190. res["red_pack_info_id"] = info.Id
  191. go PutSendRedPackQueue(info.OutTradeNo, info.Prompt, info.Id, info.ActivityId, info.Status) // 加入发送队列
  192. t.JSON(res)
  193. }
  194. // 支付之后可以遍历查询是否成功 -- 前端发送消息
  195. func (t *LiveCtl) QueryRedPack() {
  196. outTradeNo := t.MustGet("out_trade_no")
  197. res, err := pay_service.OrderQuery(outTradeNo)
  198. t.CheckErr(err)
  199. info := new(models.LiveRedPackInfo)
  200. exist, err := info.GetByOutTradeNo(outTradeNo)
  201. t.CheckErr(err)
  202. t.Assert(exist, code.MSG_LIVE_RED_PACK_INFO_NOT_EXIST, "直播红包信息不存在")
  203. if res.TradeState == pay_service.CODE_TRADE_SUCCESS {
  204. info.Status = 1
  205. info.UpdateStatusById(info.Id, info.Status)
  206. t.JSON(map[string]interface{}{
  207. "red_pack_info_id": info.Id,
  208. "status": info.Status,
  209. })
  210. } else {
  211. t.JSON(map[string]interface{}{
  212. "red_pack_info_id": info.Id,
  213. "status": info.Status,
  214. })
  215. }
  216. }
  217. // 领取红包
  218. func (t *LiveCtl) GetRedPack() {
  219. liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id")
  220. userId := t.MustGetUID()
  221. user := models.User{}
  222. exist, err := models.GetById(&user, userId)
  223. t.CheckErr(err)
  224. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  225. redPack := new(models.LiveRedPack)
  226. exist, err = redPack.GetByInfoId(liveRedPackInfoId)
  227. t.CheckErr(err)
  228. if !exist {
  229. // 通知其他的人
  230. t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
  231. return
  232. }
  233. // 乐观锁 ==> 防止并发
  234. redPack.OpenId = user.Openid
  235. redPack.Receiver = user.Id
  236. redPack.TransferType = 1
  237. redPack.PartnerTradeNo = utils.RandomStr(32)
  238. row, err := redPack.UpdateStatusById(redPack.Id, 1)
  239. t.CheckErr(err)
  240. if row != 1 {
  241. t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
  242. return
  243. }
  244. pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, redPack.Amount, 5, 5*60)
  245. t.JSON(redPack)
  246. }