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

227 lines
6.4 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
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. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/libs/filter"
  6. "hudongzhuanjia/libs/im"
  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. "time"
  14. )
  15. type NoticeRedPackEvent struct {
  16. OutTradeNo string `json:"out_trade_no"`
  17. Prompt string `json:"prompt"`
  18. RedPackInfoId int64 `json:"red_pack_info_id"`
  19. ActivityId int64 `json:"activity_id"`
  20. Status int `json:"status"`
  21. }
  22. type LiveCtl struct {
  23. controllers.AuthorCtl
  24. }
  25. // 详情
  26. func (t *LiveCtl) Detail() {
  27. activityId := t.MustGetInt64("activity_id")
  28. areaId, exist := t.GetInt64("area_id")
  29. if !exist { // 假设不存地区,那就设置为主地区
  30. area := new(models.AreaStore)
  31. exist, err := area.GetMainAreaById(activityId)
  32. t.CheckErr(err)
  33. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  34. areaId = area.Id
  35. }
  36. userId := t.MustGetUID()
  37. err := new(models.LiveViewer).Record(userId, activityId)
  38. t.CheckErr(err)
  39. live := new(models.LiveConfig)
  40. exist, err = live.GetByActivityId(activityId)
  41. t.CheckErr(err)
  42. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  43. config := new(models.LiveConfigArea)
  44. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  45. t.CheckErr(err)
  46. if exist {
  47. live.SharePosterImg = config.MergeSharePoster
  48. }
  49. live.AdminLiveUrl = ""
  50. live.AreaId = areaId
  51. t.JSON(live)
  52. }
  53. func (t *LiveCtl) Like() {
  54. activityId := t.MustGetInt64("activity_id")
  55. //userId := t.MustGetUID()
  56. _, err := new(models.LiveConfig).Like(activityId)
  57. t.CheckErr(err)
  58. live := new(models.LiveConfig)
  59. exist, err := live.GetByActivityId(activityId)
  60. t.CheckErr(err)
  61. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  62. t.JSON(map[string]interface{}{
  63. "like": live.LikeNum,
  64. "watch": live.WatchNum,
  65. })
  66. }
  67. func (t *LiveCtl) LoopQuery() {
  68. activityId := t.MustGetInt64("activity_id")
  69. live := new(models.LiveConfig)
  70. exist, err := live.GetByActivityId(activityId)
  71. t.CheckErr(err)
  72. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  73. t.JSON(map[string]interface{}{
  74. "like": live.LikeNum,
  75. "watch": live.WatchNum,
  76. })
  77. }
  78. // 下单发送红包
  79. // 维护一个队列进行循环, 遍历是否付款成功
  80. func (t *LiveCtl) SendLiveRedPack() {
  81. userId := t.MustGetUID() // 用户 uid
  82. activityId := t.MustGetInt64("activity_id") // activity_id
  83. num := t.MustGetInt("num") // 红包数量
  84. prompt := t.MustGet("prompt") // 提示
  85. amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额
  86. areaId := t.MustGetInt64("area_id")
  87. if amount/float64(num) < 0.3 { // 平均每个红包不得小于0.3
  88. t.ERROR("每个红包不得小于0.3元", code.MSG_ERR)
  89. return
  90. }
  91. activity := new(models.Activity)
  92. exist, err := models.GetById(activity, activityId)
  93. t.CheckErr(err)
  94. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  95. area := new(models.AreaStore)
  96. exist, err = models.GetById(area, areaId)
  97. t.CheckErr(err)
  98. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  99. user := models.User{}
  100. exist, err = models.GetById(&user, userId)
  101. t.CheckErr(err)
  102. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  103. var res = make(map[string]interface{}, 0)
  104. res["out_trade_no"] = ""
  105. if activity.RehearsalId == 0 {
  106. res, err = pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, userId, activityId)
  107. t.CheckErr(err)
  108. }
  109. rule := new(models.LiveRedEnvelopeRule)
  110. rule.OutTradeNo = res["out_trade_no"].(string) // 彩排为空
  111. rule.ActivityId = activityId
  112. rule.RehearsalId = activity.RehearsalId
  113. rule.AreaId = areaId
  114. rule.UserId = userId
  115. rule.Amount = amount
  116. rule.Num = num
  117. rule.Prompt = filter.Replace(prompt)
  118. rule.IsDelete = false
  119. if activity.RehearsalId == 0 {
  120. rule.Status = 0
  121. } else {
  122. rule.Status = 1 // 彩排
  123. }
  124. rule.UpdatedAt = time.Now()
  125. rule.CreatedAt = time.Now()
  126. _, err = models.Add(rule)
  127. t.CheckErr(err)
  128. if activity.RehearsalId != 0 { // 彩排不过钱
  129. err = im_service.SendGroupCustomMessage(fmt.Sprint(user.Id), activityId, im.NoticeLiveRedPackStart,
  130. map[string]interface{}{
  131. "live_red_envelope_rule_id": rule.Id,
  132. "prompt": rule.Prompt,
  133. "timestamp": time.Now().Unix(),
  134. })
  135. t.CheckErr(err)
  136. }
  137. records := red_envelope_service.GenRedPack(int(amount*100), num)
  138. for _, v := range records {
  139. record := new(models.ShakeRedEnvelopeRecord)
  140. record.ActivityId = activityId
  141. record.RehearsalId = activity.RehearsalId
  142. record.AreaId = area.Id
  143. record.AreaName = area.Name
  144. record.RedEnvelopeType = 1
  145. record.LiveRedEnvelopeRuleId = rule.Id
  146. record.Name = user.Nickname + "发红包"
  147. record.UserId = 0
  148. record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2)
  149. record.CreatedAt = time.Now()
  150. record.UpdatedAt = time.Now()
  151. _, err = models.Add(record)
  152. t.CheckErr(err)
  153. }
  154. res["rehearsal_id"] = activity.RehearsalId
  155. res["live_red_envelope_rule_id"] = rule.Id
  156. t.JSON(res)
  157. }
  158. // 领取红包
  159. func (t *LiveCtl) GetLiveRedPack() {
  160. ruleId := t.MustGetInt64("live_red_envelope_rule_id")
  161. userId := t.MustGetUID()
  162. rule := new(models.LiveRedEnvelopeRule)
  163. exist, err := models.GetById(rule, ruleId)
  164. t.CheckErr(err)
  165. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
  166. if rule.Status != 1 {
  167. t.ERROR("红包规则尚未生效", code.MSG_SHAKERB_RULE_NOT_EXIST)
  168. return
  169. }
  170. user := models.User{}
  171. exist, err = models.GetById(&user, userId)
  172. t.CheckErr(err)
  173. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  174. redPack := new(models.ShakeRedEnvelopeRecord)
  175. exist, err = redPack.GetLiveRedPackRecord(ruleId)
  176. t.CheckErr(err)
  177. if !exist {
  178. // 通知其他的人
  179. t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT)
  180. return
  181. }
  182. // 乐观锁 ==> 防止并发
  183. redPack.UserId = user.Id
  184. redPack.TransferType = 1
  185. redPack.PartnerTradeNo = utils.RandomStr(32)
  186. row, err := redPack.UpdateAllColsById(redPack.Id)
  187. t.CheckErr(err)
  188. if row != 1 {
  189. t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT)
  190. return
  191. }
  192. if rule.RehearsalId == 0 { // 非彩排
  193. go pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60)
  194. }
  195. t.JSON(redPack)
  196. }