bnj.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/web/model"
  6. accmdl "go-common/app/service/main/account/api"
  7. arcmdl "go-common/app/service/main/archive/api"
  8. coinmdl "go-common/app/service/main/coin/api"
  9. favmdl "go-common/app/service/main/favorite/model"
  10. thumbup "go-common/app/service/main/thumbup/model"
  11. "go-common/library/conf/env"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. "go-common/library/net/metadata"
  15. "go-common/library/sync/errgroup"
  16. )
  17. func (s *Service) checkBnjAccess(mid int64) bool {
  18. if s.c.Bnj2019.Open {
  19. return true
  20. }
  21. if env.DeployEnv == env.DeployEnvProd && len(s.bnjGrayUids) > 0 {
  22. if mid == 0 {
  23. return false
  24. }
  25. if _, ok := s.bnjGrayUids[mid]; !ok {
  26. return false
  27. }
  28. }
  29. return true
  30. }
  31. // Bnj2019Aids get bnj aids.3
  32. func (s Service) Bnj2019Aids(c context.Context) []int64 {
  33. aids := s.c.Bnj2019.BnjListAids
  34. aids = append(aids, s.c.Bnj2019.BnjMainAid)
  35. return aids
  36. }
  37. // Timeline get timeline.
  38. func (s *Service) Timeline(c context.Context, mid int64) (data []*model.Timeline, err error) {
  39. if !s.checkBnjAccess(mid) {
  40. err = ecode.AccessDenied
  41. return
  42. }
  43. for _, v := range s.c.Bnj2019.Timeline {
  44. data = append(data, &model.Timeline{
  45. Name: v.Name,
  46. Start: v.Start.Unix(),
  47. End: v.End.Unix(),
  48. Cover: v.Cover,
  49. H5Cover: v.H5Cover,
  50. })
  51. }
  52. return
  53. }
  54. // Bnj2019 get bnj2019 data.
  55. func (s *Service) Bnj2019(c context.Context, mid int64) (data *model.Bnj2019, err error) {
  56. if !s.checkBnjAccess(mid) {
  57. err = ecode.AccessDenied
  58. return
  59. }
  60. if s.bnj2019View == nil || !s.bnj2019View.Arc.IsNormal() {
  61. err = ecode.NothingFound
  62. return
  63. }
  64. data = &model.Bnj2019{
  65. Bnj2019View: s.bnj2019View,
  66. Elec: s.BnjElecInfo,
  67. Related: s.bnj2019List,
  68. ReqUser: &model.ReqUser{},
  69. }
  70. if len(data.Related) == 0 {
  71. data.Related = make([]*model.Bnj2019Related, 0)
  72. }
  73. if mid > 0 {
  74. authorMid := s.bnj2019View.Author.Mid
  75. aid := s.bnj2019View.Aid
  76. ip := metadata.String(c, metadata.RemoteIP)
  77. group, errCtx := errgroup.WithContext(c)
  78. // attention
  79. group.Go(func() error {
  80. if resp, e := s.accClient.Relation3(errCtx, &accmdl.RelationReq{Mid: mid, Owner: authorMid, RealIp: ip}); e != nil {
  81. log.Error("Bnj2019 s.accClient.Relation3(%d,%d,%s) error(%v)", mid, authorMid, ip, e)
  82. } else if resp != nil {
  83. data.ReqUser.Attention = resp.Following
  84. }
  85. return nil
  86. })
  87. // favorite
  88. group.Go(func() error {
  89. if resp, e := s.fav.IsFav(errCtx, &favmdl.ArgIsFav{Type: favmdl.TypeVideo, Mid: mid, Oid: aid, RealIP: ip}); e != nil {
  90. log.Error("Bnj2019 s.fav.IsFav(%d,%d,%s) error(%v)", mid, aid, ip, e)
  91. } else {
  92. data.ReqUser.Favorite = resp
  93. }
  94. return nil
  95. })
  96. // like
  97. group.Go(func() error {
  98. if resp, e := s.thumbup.HasLike(errCtx, &thumbup.ArgHasLike{Business: _businessLike, MessageIDs: []int64{aid}, Mid: mid, RealIP: ip}); e != nil {
  99. log.Error("Bnj2019 s.thumbup.HasLike(%d,%d,%s) error %v", aid, mid, ip, e)
  100. } else if resp != nil {
  101. if v, ok := resp[aid]; ok {
  102. switch v {
  103. case thumbup.StateLike:
  104. data.ReqUser.Like = true
  105. case thumbup.StateDislike:
  106. data.ReqUser.Dislike = true
  107. }
  108. }
  109. }
  110. return nil
  111. })
  112. // coin
  113. group.Go(func() error {
  114. if resp, e := s.coinClient.ItemUserCoins(errCtx, &coinmdl.ItemUserCoinsReq{Mid: mid, Aid: aid, Business: model.CoinArcBusiness}); e != nil {
  115. log.Error("Bnj2019 s.coinClient.ItemUserCoins(%d,%d,%s) error %v", mid, aid, ip, e)
  116. } else if resp != nil {
  117. data.ReqUser.Coin = resp.Number
  118. }
  119. return nil
  120. })
  121. group.Wait()
  122. }
  123. return
  124. }
  125. func (s *Service) bnj2019proc() {
  126. // bnj gray uid
  127. go func() {
  128. for {
  129. time.Sleep(time.Duration(s.c.Bnj2019.BnjTick))
  130. if mids, err := s.dao.Bnj2019Conf(context.Background()); err != nil {
  131. log.Error("bnj2019proc s.dao.Bnj2019Conf error(%v)", err)
  132. continue
  133. } else {
  134. tmp := make(map[int64]struct{}, len(mids))
  135. if len(mids) > 0 {
  136. for _, mid := range mids {
  137. tmp[mid] = struct{}{}
  138. }
  139. }
  140. s.bnjGrayUids = tmp
  141. }
  142. }
  143. }()
  144. // main arc
  145. go func() {
  146. for {
  147. time.Sleep(time.Duration(s.c.Bnj2019.BnjTick))
  148. if s.c.Bnj2019.BnjMainAid == 0 {
  149. continue
  150. }
  151. if viewReply, err := s.arcClient.View(context.Background(), &arcmdl.ViewRequest{Aid: s.c.Bnj2019.BnjMainAid}); err != nil {
  152. log.Error("bnj2019proc main s.arcClient.View(%d) error(%v)", s.c.Bnj2019.BnjMainAid, err)
  153. continue
  154. } else if viewReply != nil {
  155. s.bnj2019View = &model.Bnj2019View{Arc: viewReply.Arc, Pages: viewReply.Pages}
  156. // elec
  157. if elec, err := s.dao.ElecShow(context.Background(), viewReply.Arc.Author.Mid, viewReply.Arc.Aid, 0); err == nil {
  158. s.BnjElecInfo = elec
  159. s.BnjElecInfo.TotalCount += s.c.Bnj2019.FakeElec
  160. } else {
  161. log.Error("bnj2019proc s.dao.ElecShow(%d,%d) error(%v)", viewReply.Arc.Author.Mid, viewReply.Arc.Aid, err)
  162. }
  163. }
  164. }
  165. }()
  166. // live arc
  167. go func() {
  168. for {
  169. time.Sleep(time.Duration(s.c.Bnj2019.BnjTick))
  170. if s.c.Bnj2019.LiveAid == 0 {
  171. continue
  172. }
  173. if arcReply, err := s.arcClient.Arc(context.Background(), &arcmdl.ArcRequest{Aid: s.c.Bnj2019.LiveAid}); err != nil {
  174. log.Error("bnj2019proc live arc s.arcClient.Arc(%d) error(%v)", s.c.Bnj2019.LiveAid, err)
  175. continue
  176. } else if arcReply != nil {
  177. s.bnj2019LiveArc = arcReply
  178. }
  179. }
  180. }()
  181. // list arc
  182. go func() {
  183. for {
  184. time.Sleep(time.Duration(s.c.Bnj2019.BnjTick))
  185. if len(s.c.Bnj2019.BnjListAids) == 0 {
  186. continue
  187. }
  188. if viewsReply, err := s.arcClient.Views(context.Background(), &arcmdl.ViewsRequest{Aids: s.c.Bnj2019.BnjListAids}); err != nil {
  189. log.Error("bnj2019proc list s.arcClient.Views(%v) error(%v)", s.c.Bnj2019.BnjListAids, err)
  190. continue
  191. } else {
  192. var tmpList []*model.Bnj2019Related
  193. for _, aid := range s.c.Bnj2019.BnjListAids {
  194. if view, ok := viewsReply.Views[aid]; ok && view.Arc.IsNormal() {
  195. item := &model.Bnj2019Related{Arc: view.Arc, Pages: view.Pages}
  196. tmpList = append(tmpList, item)
  197. }
  198. }
  199. s.bnj2019List = tmpList
  200. }
  201. }
  202. }()
  203. }