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

206 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
  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.Get(activity, activityId)
  103. t.CheckErr(err)
  104. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  105. //area := new(models.AreaStore)
  106. //exist, err = models.Get(area, areaId)
  107. //t.CheckErr(err)
  108. //t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  109. user := models.User{}
  110. exist, err = models.Get(&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),
  114. 3, userId, activityId, time.Now().Add(24*time.Hour).Unix())
  115. t.CheckErr(err)
  116. rule := models.LiveRedEnvelopeRule{
  117. UserId: 0,
  118. ActivityId: activityId,
  119. AreaId: areaId,
  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. }