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

256 lines
7.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
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. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/libs/filter"
  6. "hudongzhuanjia/libs/im"
  7. "hudongzhuanjia/models"
  8. pay_service "hudongzhuanjia/services/pay"
  9. red_envelope_service "hudongzhuanjia/services/red_envelope"
  10. "hudongzhuanjia/utils"
  11. "hudongzhuanjia/utils/code"
  12. "hudongzhuanjia/utils/define"
  13. "sync"
  14. "time"
  15. "github.com/ouxuanserver/osmanthuswine/src/core"
  16. )
  17. // 轮询接口
  18. func init() {
  19. go utils.HandleTicker(10*time.Second, QueryOnline)
  20. }
  21. func QueryOnline() error {
  22. lives, err := models.GetLiveConfigByStatus(2, 2) // 进行中的直播, 统计峰值
  23. if err != nil {
  24. return err
  25. }
  26. liveIds := make([]int64, 0)
  27. for _, live := range lives { // 查询在线人数
  28. liveIds = append(liveIds, live.Id)
  29. }
  30. viewers, err := models.GetLiveViewerByLiveConfigIds(liveIds)
  31. if err != nil {
  32. return err
  33. }
  34. var idm = make(map[int64][]string, 0)
  35. for _, live := range lives {
  36. for _, viewer := range viewers {
  37. if live.Id == viewer.LiveConfigId {
  38. if _, ok := idm[live.Id]; !ok {
  39. idm[live.Id] = make([]string, 0)
  40. }
  41. idm[live.Id] = append(idm[live.Id], fmt.Sprint(viewer.UserId))
  42. }
  43. }
  44. }
  45. var wg sync.WaitGroup
  46. wg.Add(len(idm))
  47. var errs []error
  48. for lid, ids := range idm {
  49. go func(lid int64, ids []string) {
  50. defer func() { recover() }()
  51. defer wg.Done()
  52. res, err := im.QueryState(ids)
  53. if err != nil {
  54. errs = append(errs, err)
  55. }
  56. var count int
  57. for _, item := range res.QueryResult {
  58. if item.Status != "Offline" {
  59. count++
  60. }
  61. }
  62. _, err = models.Update(lid, &models.LiveConfig{WatchNum: count}, "watch_num")
  63. if err != nil {
  64. errs = append(errs, err)
  65. }
  66. }(lid, ids)
  67. }
  68. wg.Wait()
  69. return nil
  70. }
  71. type LiveCtl struct {
  72. controllers.AuthorCtl
  73. }
  74. // 详情
  75. func (t *LiveCtl) Detail() {
  76. activityId := t.MustGetInt64("activity_id")
  77. areaId := t.MustGetInt64("area_id")
  78. live := new(models.LiveConfig)
  79. exist, err := live.GetByActivityId(activityId)
  80. t.CheckErr(err)
  81. t.Assert(exist, code.MSG_LIVE_CONFIG_NOT_EXIST, "直播活动不存在")
  82. fs := make(map[string]int, 0)
  83. fs["is_shake"] = 0
  84. fs["is_reward"] = 0
  85. fs["is_order"] = 0
  86. fs["is_lottery"] = 0
  87. if live.AdaptationFunc != nil && len(live.AdaptationFunc) != 0 {
  88. modules := make([]*models.ModuleServiceHistory, 0)
  89. err = core.GetXormAuto().Where("is_delete=0").In("id", live.AdaptationFunc).Find(&modules)
  90. t.CheckErr(err)
  91. for _, module := range modules {
  92. if module.Name == define.MODULE_ORDER {
  93. fs["is_order"] = 1
  94. } else if module.Name == define.MODULE_SHAKRB {
  95. fs["is_shake"] = 1
  96. } else if module.Name == define.MODULE_REWARD {
  97. fs["is_reward"] = 1
  98. } else if module.Name == define.MODULE_LOTTERY {
  99. fs["is_lottery"] = 1
  100. }
  101. }
  102. }
  103. config := new(models.LiveConfigArea)
  104. exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
  105. t.CheckErr(err)
  106. if exist {
  107. live.SharePosterImg = config.MergeSharePoster
  108. }
  109. live.AdminLiveUrl = ""
  110. live.AreaId = areaId
  111. live.Adaptation = fs
  112. t.JSON(live)
  113. }
  114. func (t *LiveCtl) Like() {
  115. activityId := t.MustGetInt64("activity_id")
  116. _, err := new(models.LiveConfig).Like(activityId)
  117. t.CheckErr(err)
  118. live := new(models.LiveConfig)
  119. exist, err := live.GetByActivityId(activityId)
  120. t.CheckErr(err)
  121. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  122. t.JSON(map[string]interface{}{
  123. "like": live.LikeNum,
  124. "watch": live.WatchNum,
  125. })
  126. }
  127. func (t *LiveCtl) LoopQuery() {
  128. activityId := t.MustGetInt64("activity_id")
  129. live := new(models.LiveConfig)
  130. exist, err := live.GetByActivityId(activityId)
  131. t.CheckErr(err)
  132. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
  133. t.JSON(map[string]interface{}{
  134. "like": live.LikeNum,
  135. "watch": live.WatchNum,
  136. })
  137. }
  138. // 下单发送红包
  139. // 维护一个队列进行循环, 遍历是否付款成功
  140. func (t *LiveCtl) SendLiveRedPack() {
  141. userId := t.MustGetUID() // 用户 uid
  142. activityId := t.MustGetInt64("activity_id") // activity_id
  143. num := t.MustGetInt("num") // 红包数量
  144. prompt := t.MustGet("prompt") // 提示
  145. amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额
  146. areaId := t.MustGetInt64("area_id")
  147. if amount/float64(num) < 1 && amount/float64(num) > 200 { // 平均每个红包不得小于0.3
  148. t.ERROR("每个红包的平均金额必须在1.00元到200.00元之间", code.MSG_ERR)
  149. return
  150. }
  151. activity := new(models.Activity)
  152. exist, err := models.Get(activity, activityId)
  153. t.CheckErr(err)
  154. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  155. user := models.User{}
  156. exist, err = models.Get(&user, userId)
  157. t.CheckErr(err)
  158. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  159. res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100),
  160. 3, userId, activityId, time.Now().Add(1*time.Hour).Unix())
  161. t.CheckErr(err)
  162. rule := models.LiveRedEnvelopeRule{
  163. UserId: 0,
  164. ActivityId: activityId,
  165. AreaId: areaId,
  166. OutTradeNo: res["out_trade_no"].(string),
  167. RehearsalId: activity.RehearsalId,
  168. Prompt: filter.Replace(prompt),
  169. Amount: amount,
  170. Num: num,
  171. Status: 0,
  172. }
  173. _, err = models.Add(&rule)
  174. t.CheckErr(err)
  175. records := red_envelope_service.GenRedPack(int(amount*100), num)
  176. for _, v := range records {
  177. record := models.ShakeRedEnvelopeRecord{
  178. ActivityId: activityId,
  179. RehearsalId: activity.RehearsalId,
  180. ShakeRedEnvelopeType: 1,
  181. ShakeRedEnvelopeRuleId: rule.Id,
  182. AreaId: areaId,
  183. Name: user.Nickname + "发红包",
  184. UserId: user.Id,
  185. Amount: utils.Float64CusDecimal(float64(v)/float64(100), 2),
  186. IsDraw: -1,
  187. }
  188. _, err = models.Add(&record)
  189. t.CheckErr(err)
  190. }
  191. res["rehearsal_id"] = activity.RehearsalId
  192. res["live_red_envelope_rule_id"] = rule.Id
  193. t.JSON(res)
  194. }
  195. // 领取红包
  196. func (t *LiveCtl) GetLiveRedPack() {
  197. ruleId := t.MustGetInt64("live_red_envelope_rule_id")
  198. userId := t.MustGetUID()
  199. rule := new(models.LiveRedEnvelopeRule)
  200. exist, err := models.Get(rule, ruleId)
  201. t.CheckErr(err)
  202. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
  203. t.Assert(rule.Status == 1, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则尚未生效")
  204. user := models.User{}
  205. exist, err = models.Get(&user, userId)
  206. t.CheckErr(err)
  207. t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
  208. record := new(models.ShakeRedEnvelopeRecord)
  209. exist, err = record.GetByRuleId(ruleId, rule.RehearsalId, 1)
  210. t.CheckErr(err)
  211. t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包领完了")
  212. // 乐观锁 ==> 防止并发
  213. record.UserId = user.Id
  214. record.IsDraw = 0
  215. row, err := models.Update(record.Id, record, "user_id", "is_draw")
  216. t.CheckErr(err)
  217. t.Assert(row == 1, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包被领完了")
  218. result, err := pay_service.SendRedPack("欧轩互动", user.Openid, rule.Prompt, "直播抢红包活动",
  219. "抢的多,赚得多", int(record.Amount*100), 1, 2)
  220. t.CheckErr(err)
  221. record.MchBillno = result.MchBillno
  222. record.IsDraw = 1
  223. models.Update(record.Id, record, "mch_billno", "is_draw")
  224. t.JSON(record)
  225. }
  226. // 订单