互动
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.

188 lines
5.3 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
5 years ago
  1. package client
  2. import (
  3. "hudongzhuanjia/controllers"
  4. "hudongzhuanjia/libs/filter"
  5. "hudongzhuanjia/models"
  6. pay_service "hudongzhuanjia/services/pay"
  7. red_envelope_service "hudongzhuanjia/services/red_envelope"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/code"
  10. "time"
  11. )
  12. type NoticeRedPackEvent struct {
  13. OutTradeNo string `json:"out_trade_no"`
  14. Prompt string `json:"prompt"`
  15. RedPackInfoId int64 `json:"red_pack_info_id"`
  16. ActivityId int64 `json:"activity_id"`
  17. Status int `json:"status"`
  18. }
  19. type LiveCtl struct {
  20. controllers.AuthorCtl
  21. }
  22. // 详情
  23. func (t *LiveCtl) Detail() {
  24. activityId := t.MustGetInt64("activity_id")
  25. areaId, exist := t.GetInt64("area_id")
  26. if !exist { // 假设不存地区,那就设置为主地区
  27. area := new(models.AreaStore)
  28. exist, err := area.GetMainAreaById(activityId)
  29. t.CheckErr(err)
  30. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  31. areaId = area.Id
  32. }
  33. userId := t.MustGetUID()
  34. err := new(models.LiveViewer).Record(userId, activityId)
  35. t.CheckErr(err)
  36. live := new(models.LiveConfig)
  37. exist, err = live.GetByActivityId(activityId)
  38. t.CheckErr(err)
  39. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  40. config := new(models.LiveConfigArea)
  41. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  42. t.CheckErr(err)
  43. if exist {
  44. live.SharePosterImg = config.MergeSharePoster
  45. }
  46. live.AdminLiveUrl = ""
  47. live.AreaId = areaId
  48. t.JSON(live)
  49. }
  50. func (t *LiveCtl) Like() {
  51. activityId := t.MustGetInt64("activity_id")
  52. //userId := t.MustGetUID()
  53. _, err := new(models.LiveConfig).Like(activityId)
  54. t.CheckErr(err)
  55. live := new(models.LiveConfig)
  56. exist, err := live.GetByActivityId(activityId)
  57. t.CheckErr(err)
  58. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  59. t.JSON(map[string]interface{}{
  60. "like": live.LikeNum,
  61. "watch": live.WatchNum,
  62. })
  63. }
  64. func (t *LiveCtl) LoopQuery() {
  65. activityId := t.MustGetInt64("activity_id")
  66. live := new(models.LiveConfig)
  67. exist, err := live.GetByActivityId(activityId)
  68. t.CheckErr(err)
  69. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  70. t.JSON(map[string]interface{}{
  71. "like": live.LikeNum,
  72. "watch": live.WatchNum,
  73. })
  74. }
  75. // 下单发送红包
  76. // 维护一个队列进行循环, 遍历是否付款成功
  77. func (t *LiveCtl) SendLiveRedPack() {
  78. userId := t.MustGetUID() // 用户 uid
  79. activityId := t.MustGetInt64("activity_id") // activity_id
  80. num := t.MustGetInt("num") // 红包数量
  81. prompt := t.MustGet("prompt") // 提示
  82. amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额
  83. areaId := t.MustGetInt64("area_id")
  84. activity := new(models.Activity)
  85. exist, err := models.GetById(activity, activityId)
  86. t.CheckErr(err)
  87. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  88. area := new(models.AreaStore)
  89. exist, err = models.GetById(area, areaId)
  90. t.CheckErr(err)
  91. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  92. user := models.User{}
  93. exist, err = models.GetById(&user, userId)
  94. t.CheckErr(err)
  95. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  96. res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, activityId, userId)
  97. t.CheckErr(err)
  98. rule := models.LiveRedEnvelopeRule{}
  99. rule.OutTradeNo = res["out_trade_no"].(string)
  100. rule.Amount = amount
  101. rule.UserId = userId
  102. rule.ActivityId = activityId
  103. rule.Prompt = filter.Replace(prompt)
  104. rule.IsDelete = false
  105. rule.UpdatedAt = time.Now()
  106. rule.CreatedAt = time.Now()
  107. _, err = rule.Add()
  108. t.CheckErr(err)
  109. records := red_envelope_service.GenRedPack(int(amount*100), num)
  110. for _, v := range records {
  111. record := new(models.ShakeRedEnvelopeRecord)
  112. record.AreaId = area.Id
  113. record.AreaName = area.Name
  114. record.RedEnvelopeType = 1
  115. record.LiveRedEnvelopeRuleId = rule.Id
  116. record.ActivityId = activityId
  117. record.Name = user.Nickname + "发红包"
  118. record.UserId = 0
  119. record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2)
  120. record.CreatedAt = time.Now()
  121. record.UpdatedAt = time.Now()
  122. _, err = record.Add()
  123. t.CheckErr(err)
  124. }
  125. res["live_red_envelope_rule_id"] = rule.Id
  126. t.JSON(res)
  127. }
  128. // 领取红包
  129. func (t *LiveCtl) GetLiveRedPack() {
  130. liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id")
  131. userId := t.MustGetUID()
  132. if liveRedPackInfoId == 0 {
  133. t.ERROR("live_red_pack_info_id不为0", code.MSG_ERR_Param)
  134. return
  135. }
  136. user := models.User{}
  137. exist, err := models.GetById(&user, userId)
  138. t.CheckErr(err)
  139. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  140. redPack := new(models.ShakeRedEnvelopeRecord)
  141. exist, err = redPack.GetLiveRedPackRecord(liveRedPackInfoId)
  142. t.CheckErr(err)
  143. if !exist {
  144. // 通知其他的人
  145. t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
  146. return
  147. }
  148. // 乐观锁 ==> 防止并发
  149. redPack.UserId = user.Id
  150. redPack.TransferType = 1
  151. redPack.PartnerTradeNo = utils.RandomStr(32)
  152. row, err := redPack.UpdateAllColsById(redPack.Id)
  153. t.CheckErr(err)
  154. if row != 1 {
  155. t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
  156. return
  157. }
  158. pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60)
  159. t.JSON(redPack)
  160. }