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

157 lines
4.1 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
  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/code"
  9. "strings"
  10. "time"
  11. )
  12. type LiveCtl struct {
  13. controllers.AuthorCtl
  14. //controllers.BaseCtl
  15. }
  16. // 详情
  17. func (t *LiveCtl) Detail() {
  18. activityId := t.MustGetInt64("activity_id")
  19. areaId := t.MustGetInt64("area_id")
  20. //var userId int64 = 0
  21. userId := t.MustGetUID()
  22. err := new(models.LiveViewer).Record(userId, activityId)
  23. t.CheckErr(err)
  24. live := new(models.LiveConfig)
  25. exist, err := live.GetByActivityId(activityId)
  26. t.CheckErr(err)
  27. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  28. config := new(models.LiveConfigArea)
  29. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  30. t.CheckErr(err)
  31. if exist {
  32. live.SharePosterImg = config.MergeSharePoster
  33. }
  34. live.AdminLiveUrl = ""
  35. t.JSON(live)
  36. }
  37. func (t *LiveCtl) Like() {
  38. activityId := t.MustGetInt64("activity_id")
  39. //userId := t.MustGetUID()
  40. _, err := new(models.LiveConfig).Like(activityId)
  41. t.CheckErr(err)
  42. live := new(models.LiveConfig)
  43. exist, err := live.GetByActivityId(activityId)
  44. t.CheckErr(err)
  45. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  46. t.JSON(map[string]interface{}{
  47. "like": live.LikeNum,
  48. "watch": live.WatchNum,
  49. })
  50. }
  51. func (t *LiveCtl) LoopQuery() {
  52. activityId := t.MustGetInt64("activity_id")
  53. live := new(models.LiveConfig)
  54. exist, err := live.GetByActivityId(activityId)
  55. t.CheckErr(err)
  56. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  57. t.JSON(map[string]interface{}{
  58. "like": live.LikeNum,
  59. "watch": live.WatchNum,
  60. })
  61. }
  62. // 发送红包
  63. func (t *LiveCtl) SendLiveRedPack() {
  64. userId := t.MustGetUID() // 用户 uid
  65. activityId := t.MustGetInt64("activity_id") // activity_id
  66. num := t.MustGetInt("num") // 红包数量
  67. amount := t.MustGetDouble("amount") // 金额
  68. prompt := t.MustGet("prompt") // 提示
  69. user := models.User{}
  70. exist, err := models.GetById(&user, userId)
  71. t.CheckErr(err)
  72. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  73. ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
  74. res, err := pay_service.Order("欧轩互动-直播红包", ip[0], user.Openid, int(amount*100), 3, userId, activityId)
  75. t.CheckErr(err)
  76. info := models.LiveRedPackInfo{}
  77. info.UserOrderId = res["user_order_id"].(int64)
  78. info.Amount = amount
  79. info.UserId = userId
  80. info.ActivityId = activityId
  81. info.Prompt = prompt
  82. info.IsDelete = false
  83. info.UpdatedAt = time.Now()
  84. info.CreatedAt = time.Now()
  85. _, err = info.Add()
  86. t.CheckErr(err)
  87. redPacks := red_envelope_service.GenRedPack(int(amount*100), num)
  88. for _, v := range redPacks {
  89. redPack := new(models.LiveRedPack)
  90. redPack.LiveRedPackInfoId = info.Id
  91. redPack.ActivityId = activityId
  92. redPack.Receiver = 0
  93. redPack.Amount = v
  94. redPack.CreatedAt = time.Now()
  95. redPack.UpdatedAt = time.Now()
  96. _, err = redPack.Add()
  97. t.CheckErr(err)
  98. }
  99. info.Prompt = filter.Replace(info.Prompt)
  100. t.JSON(info)
  101. }
  102. func (t *LiveCtl) QueryRedPack() {
  103. }
  104. // 领取红包
  105. func (t *LiveCtl) GetRedPack() {
  106. liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id")
  107. userId := t.MustGetUID()
  108. user := models.User{}
  109. exist, err := models.GetById(&user, userId)
  110. t.CheckErr(err)
  111. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  112. redPack := new(models.LiveRedPack)
  113. exist, err = redPack.GetByInfoId(liveRedPackInfoId)
  114. t.CheckErr(err)
  115. if !exist {
  116. // 通知其他的人
  117. t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
  118. return
  119. }
  120. // 乐观锁 ==> 防止并发
  121. row, err := redPack.Receive(userId)
  122. t.CheckErr(err)
  123. if row != 1 {
  124. t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
  125. return
  126. }
  127. ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
  128. res, err := pay_service.Transfer("欧轩互动-红包活动", ip[0], user.Openid, redPack.Amount)
  129. redPack.TransferType = 1
  130. redPack.TransferNo = res.PartnerTradeNo
  131. _, err = redPack.UpdateStatusById(redPack, 2)
  132. t.CheckErr(err)
  133. t.JSON(redPack)
  134. }