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

210 lines
6.3 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
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. "strings"
  13. "time"
  14. )
  15. type NoticeRedPackEvent struct {
  16. OutTradeNo string `json:"out_trade_no"`
  17. Prompt string `json:"prompt"`
  18. RedPackInfoId int64 `json:"red_pack_info_id"`
  19. ActivityId int64 `json:"activity_id"`
  20. Status int `json:"status"`
  21. }
  22. type LiveCtl struct {
  23. controllers.AuthorCtl
  24. }
  25. // 详情
  26. func (t *LiveCtl) Detail() {
  27. activityId := t.MustGetInt64("activity_id")
  28. areaId := t.MustGetInt64("area_id")
  29. userId := t.MustGetUID()
  30. err := new(models.LiveViewer).Record(userId, activityId)
  31. t.CheckErr(err)
  32. live := new(models.LiveConfig)
  33. exist, err := live.GetByActivityId(activityId)
  34. t.CheckErr(err)
  35. t.Assert(exist, code.MSG_LIVE_CONFIG_NOT_EXIST, "直播活动不存在")
  36. fs := make(map[string]int, 0)
  37. fs["is_shake"] = 0
  38. fs["is_reward"] = 0
  39. fs["is_order"] = 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. }
  52. }
  53. }
  54. config := new(models.LiveConfigArea)
  55. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  56. t.CheckErr(err)
  57. if exist {
  58. live.SharePosterImg = config.MergeSharePoster
  59. }
  60. live.AdminLiveUrl = ""
  61. live.AreaId = areaId
  62. live.Adaptation = fs
  63. t.JSON(live)
  64. }
  65. func (t *LiveCtl) Like() {
  66. activityId := t.MustGetInt64("activity_id")
  67. _, err := new(models.LiveConfig).Like(activityId)
  68. t.CheckErr(err)
  69. live := new(models.LiveConfig)
  70. exist, err := live.GetByActivityId(activityId)
  71. t.CheckErr(err)
  72. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  73. t.JSON(map[string]interface{}{
  74. "like": live.LikeNum,
  75. "watch": live.WatchNum,
  76. })
  77. }
  78. func (t *LiveCtl) LoopQuery() {
  79. activityId := t.MustGetInt64("activity_id")
  80. live := new(models.LiveConfig)
  81. exist, err := live.GetByActivityId(activityId)
  82. t.CheckErr(err)
  83. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  84. t.JSON(map[string]interface{}{
  85. "like": live.LikeNum,
  86. "watch": live.WatchNum,
  87. })
  88. }
  89. // 下单发送红包
  90. // 维护一个队列进行循环, 遍历是否付款成功
  91. func (t *LiveCtl) SendLiveRedPack() {
  92. userId := t.MustGetUID() // 用户 uid
  93. activityId := t.MustGetInt64("activity_id") // activity_id
  94. num := t.MustGetInt("num") // 红包数量
  95. prompt := t.MustGet("prompt") // 提示
  96. amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额
  97. areaId := t.MustGetInt64("area_id")
  98. if amount/float64(num) < 0.3 { // 平均每个红包不得小于0.3
  99. t.ERROR("每个红包不得小于0.3元", code.MSG_ERR)
  100. return
  101. }
  102. activity := new(models.Activity)
  103. exist, err := models.Get(activity, activityId)
  104. t.CheckErr(err)
  105. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  106. area := new(models.AreaStore)
  107. exist, err = models.Get(area, areaId)
  108. t.CheckErr(err)
  109. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  110. user := models.User{}
  111. exist, err = models.Get(&user, userId)
  112. t.CheckErr(err)
  113. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  114. res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, userId, activityId)
  115. t.CheckErr(err)
  116. rule := new(models.LiveRedEnvelopeRule)
  117. rule.OutTradeNo = res["out_trade_no"].(string)
  118. rule.ActivityId = activityId
  119. rule.RehearsalId = activity.RehearsalId
  120. rule.AreaId = areaId
  121. rule.UserId = userId
  122. rule.Amount = amount
  123. rule.Num = num
  124. rule.Prompt = filter.Replace(prompt)
  125. rule.IsDelete = false
  126. rule.Status = 0
  127. rule.UpdatedAt = time.Now()
  128. rule.CreatedAt = time.Now()
  129. _, err = models.Add(rule)
  130. t.CheckErr(err)
  131. records := red_envelope_service.GenRedPack(int(amount*100), num)
  132. for _, v := range records {
  133. record := new(models.ShakeRedEnvelopeRecord)
  134. record.ActivityId = activityId
  135. record.RehearsalId = activity.RehearsalId
  136. record.AreaId = area.Id
  137. record.ShakeRedEnvelopeType = 1
  138. record.ShakeRedEnvelopeRuleId = rule.Id
  139. record.Name = user.Nickname + "发红包"
  140. record.UserId = 0
  141. record.IsDraw = -1 // 未被提现
  142. record.Amount = utils.Float64CusDecimal(float64(v)/float64(100), 2)
  143. record.CreatedAt = time.Now()
  144. record.UpdatedAt = time.Now()
  145. _, err = models.Add(record)
  146. t.CheckErr(err)
  147. }
  148. res["rehearsal_id"] = activity.RehearsalId
  149. res["live_red_envelope_rule_id"] = rule.Id
  150. t.JSON(res)
  151. }
  152. // 领取红包
  153. func (t *LiveCtl) GetLiveRedPack() {
  154. ruleId := t.MustGetInt64("live_red_envelope_rule_id")
  155. userId := t.MustGetUID()
  156. rule := new(models.LiveRedEnvelopeRule)
  157. exist, err := models.Get(rule, ruleId)
  158. t.CheckErr(err)
  159. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
  160. t.Assert(rule.Status == 1, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则尚未生效")
  161. user := models.User{}
  162. exist, err = models.Get(&user, userId)
  163. t.CheckErr(err)
  164. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  165. record := new(models.ShakeRedEnvelopeRecord)
  166. exist, err = record.GetByRuleId(ruleId, rule.RehearsalId, 1)
  167. t.CheckErr(err)
  168. t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包领完了")
  169. // 乐观锁 ==> 防止并发
  170. record.UserId = user.Id
  171. record.IsDraw = 0
  172. row, err := record.UpdateById(record.Id, "version, user_id, is_draw")
  173. t.CheckErr(err)
  174. t.Assert(row == 1, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包被领完了")
  175. addr := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
  176. result, err := pay_service.SendRedPack("欧轩互动", user.Openid, rule.Prompt, addr[0], "直播抢红包活动",
  177. "抢的多,赚得多", int(record.Amount*100), 1, 2)
  178. t.CheckErr(err)
  179. record.MchBillno = result.MchBillno
  180. record.Version += 1
  181. record.UpdateById(record.Id, "version, mch_billno")
  182. t.JSON(record)
  183. }