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

205 lines
6.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
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. fs["is_lottery"] = 0
  40. if live.AdaptationFunc != nil && len(live.AdaptationFunc) != 0 {
  41. modules := make([]*models.ModuleService, 0)
  42. err = core.GetXormAuto().Where("is_delete=0").In("id", live.AdaptationFunc).Find(&modules)
  43. t.CheckErr(err)
  44. for _, module := range modules {
  45. if module.Name == define.MODULE_ORDER {
  46. fs["is_order"] = 1
  47. } else if module.Name == define.MODULE_SHAKRB {
  48. fs["is_shake"] = 1
  49. } else if module.Name == define.MODULE_REWARD {
  50. fs["is_reward"] = 1
  51. } else if module.Name == define.MODULE_LOTTERY {
  52. fs["is_lottery"] = 1
  53. }
  54. }
  55. }
  56. config := new(models.LiveConfigArea)
  57. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  58. t.CheckErr(err)
  59. if exist {
  60. live.SharePosterImg = config.MergeSharePoster
  61. }
  62. live.AdminLiveUrl = ""
  63. live.AreaId = areaId
  64. live.Adaptation = fs
  65. t.JSON(live)
  66. }
  67. func (t *LiveCtl) Like() {
  68. activityId := t.MustGetInt64("activity_id")
  69. _, err := new(models.LiveConfig).Like(activityId)
  70. t.CheckErr(err)
  71. live := new(models.LiveConfig)
  72. exist, err := live.GetByActivityId(activityId)
  73. t.CheckErr(err)
  74. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  75. t.JSON(map[string]interface{}{
  76. "like": live.LikeNum,
  77. "watch": live.WatchNum,
  78. })
  79. }
  80. func (t *LiveCtl) LoopQuery() {
  81. activityId := t.MustGetInt64("activity_id")
  82. live := new(models.LiveConfig)
  83. exist, err := live.GetByActivityId(activityId)
  84. t.CheckErr(err)
  85. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  86. t.JSON(map[string]interface{}{
  87. "like": live.LikeNum,
  88. "watch": live.WatchNum,
  89. })
  90. }
  91. // 下单发送红包
  92. // 维护一个队列进行循环, 遍历是否付款成功
  93. func (t *LiveCtl) SendLiveRedPack() {
  94. userId := t.MustGetUID() // 用户 uid
  95. activityId := t.MustGetInt64("activity_id") // activity_id
  96. num := t.MustGetInt("num") // 红包数量
  97. prompt := t.MustGet("prompt") // 提示
  98. amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额
  99. areaId := t.MustGetInt64("area_id")
  100. if amount/float64(num) < 0.3 { // 平均每个红包不得小于0.3
  101. t.ERROR("每个红包不得小于0.3元", code.MSG_ERR)
  102. return
  103. }
  104. activity := new(models.Activity)
  105. exist, err := models.Get(activity, activityId)
  106. t.CheckErr(err)
  107. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  108. user := models.User{}
  109. exist, err = models.Get(&user, userId)
  110. t.CheckErr(err)
  111. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  112. res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100),
  113. 3, userId, activityId, time.Now().Add(1*time.Hour).Unix())
  114. t.CheckErr(err)
  115. rule := models.LiveRedEnvelopeRule{
  116. UserId: 0,
  117. ActivityId: activityId,
  118. AreaId: areaId,
  119. OutTradeNo: res["out_trade_no"].(string),
  120. RehearsalId: activity.RehearsalId,
  121. Prompt: filter.Replace(prompt),
  122. Amount: amount,
  123. Num: num,
  124. Status: 0,
  125. }
  126. _, err = models.Add(&rule)
  127. t.CheckErr(err)
  128. records := red_envelope_service.GenRedPack(int(amount*100), num)
  129. for _, v := range records {
  130. record := models.ShakeRedEnvelopeRecord{
  131. ActivityId: activityId,
  132. RehearsalId: activity.RehearsalId,
  133. ShakeRedEnvelopeType: 1,
  134. ShakeRedEnvelopeRuleId: rule.Id,
  135. AreaId: areaId,
  136. Name: user.Nickname + "发红包",
  137. UserId: user.Id,
  138. Amount: utils.Float64CusDecimal(float64(v)/float64(100), 2),
  139. IsDraw: -1,
  140. }
  141. _, err = models.Add(&record)
  142. t.CheckErr(err)
  143. }
  144. res["rehearsal_id"] = activity.RehearsalId
  145. res["live_red_envelope_rule_id"] = rule.Id
  146. t.JSON(res)
  147. }
  148. // 领取红包
  149. func (t *LiveCtl) GetLiveRedPack() {
  150. ruleId := t.MustGetInt64("live_red_envelope_rule_id")
  151. userId := t.MustGetUID()
  152. rule := new(models.LiveRedEnvelopeRule)
  153. exist, err := models.Get(rule, ruleId)
  154. t.CheckErr(err)
  155. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
  156. t.Assert(rule.Status == 1, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则尚未生效")
  157. user := models.User{}
  158. exist, err = models.Get(&user, userId)
  159. t.CheckErr(err)
  160. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  161. record := new(models.ShakeRedEnvelopeRecord)
  162. exist, err = record.GetByRuleId(ruleId, rule.RehearsalId, 1)
  163. t.CheckErr(err)
  164. t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包领完了")
  165. // 乐观锁 ==> 防止并发
  166. record.UserId = user.Id
  167. record.IsDraw = 0
  168. row, err := models.Update(record.Id, record, "user_id", "is_draw")
  169. t.CheckErr(err)
  170. t.Assert(row == 1, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包被领完了")
  171. result, err := pay_service.SendRedPack("欧轩互动", user.Openid, rule.Prompt, "直播抢红包活动",
  172. "抢的多,赚得多", int(record.Amount*100), 1, 2)
  173. t.CheckErr(err)
  174. record.MchBillno = result.MchBillno
  175. record.IsDraw = 1
  176. models.Update(record.Id, record, "mch_billno", "is_draw")
  177. t.JSON(record)
  178. }