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

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