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

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