service.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/app-player/conf"
  6. "go-common/app/interface/main/app-player/dao"
  7. accdao "go-common/app/interface/main/app-player/dao/account"
  8. arcdao "go-common/app/interface/main/app-player/dao/archive"
  9. resdao "go-common/app/interface/main/app-player/dao/resource"
  10. ugcpaydao "go-common/app/interface/main/app-player/dao/ugcpay"
  11. "go-common/app/interface/main/app-player/model"
  12. arcmdl "go-common/app/interface/main/app-player/model/archive"
  13. accrpc "go-common/app/service/main/account/api"
  14. ugcpaymdl "go-common/app/service/main/ugcpay/api/grpc/v1"
  15. "go-common/library/ecode"
  16. "go-common/library/log"
  17. )
  18. const (
  19. _qn1080 = 80
  20. _qn480 = 32
  21. _relationPaid = "paid"
  22. _playURLV2 = "/v2/playurl"
  23. _playURLV3 = "/v3/playurl"
  24. _androidBuild = 5340000
  25. _iosBuild = 8230
  26. _ipadHDBuild = 12070
  27. )
  28. // Service is space service
  29. type Service struct {
  30. c *conf.Config
  31. dao *dao.Dao
  32. arcDao *arcdao.Dao
  33. accDao *accdao.Dao
  34. ugcpayDao *ugcpaydao.Dao
  35. resDao *resdao.Dao
  36. bnjArcs map[int64]*arcmdl.Info
  37. // paster
  38. pasterCache map[int64]int64
  39. }
  40. // New new space
  41. func New(c *conf.Config) (s *Service) {
  42. s = &Service{
  43. c: c,
  44. dao: dao.New(c),
  45. arcDao: arcdao.New(c),
  46. accDao: accdao.New(c),
  47. ugcpayDao: ugcpaydao.New(c),
  48. resDao: resdao.New(c),
  49. // paster
  50. pasterCache: make(map[int64]int64),
  51. }
  52. s.loadPasterCID()
  53. s.loadBnjArc()
  54. go s.bnjTickproc()
  55. go s.reloadproc()
  56. return
  57. }
  58. func (s *Service) loadPasterCID() (err error) {
  59. var tmpPaster map[int64]int64
  60. if tmpPaster, err = s.resDao.PasterCID(context.Background()); err != nil {
  61. log.Error("%v", err)
  62. return
  63. }
  64. s.pasterCache = tmpPaster
  65. return
  66. }
  67. // reloadproc reload data.
  68. func (s *Service) reloadproc() {
  69. for {
  70. time.Sleep(time.Minute * 10)
  71. s.loadPasterCID()
  72. }
  73. }
  74. // Playurl is
  75. func (s *Service) Playurl(c context.Context, mid int64, params *model.Param, plat int8, buvid, fp string) (playurl *model.Playurl, err error) {
  76. var (
  77. code, isSp int
  78. reqPath = _playURLV2
  79. aid = params.AID
  80. cid = params.CID
  81. build = params.Build
  82. qn = params.Qn
  83. )
  84. _, ok := s.pasterCache[cid]
  85. if aid > 0 && aid%10 < s.c.AidGray && !ok {
  86. reqPath = _playURLV3
  87. var (
  88. arc *arcmdl.Info
  89. relation *ugcpaymdl.AssetRelationResp
  90. )
  91. if arc, ok = s.bnjArcs[aid]; !ok {
  92. if arc, err = s.arcDao.ArchiveCache(c, aid); err != nil {
  93. log.Error("verifyArchive %+v", err)
  94. return
  95. }
  96. }
  97. if !arc.IsNormal() || !arc.HasCid(cid) {
  98. err = ecode.NothingFound
  99. log.Warn("verifyArchive aid(%d) can not play or no cid(%d)", aid, cid)
  100. return
  101. }
  102. // TODO 历史老坑 纪录片之类的会请求UGC的playurl,先打日志记一记
  103. if arc.IsPGC() && arc.AttrVal(arcmdl.AttrBitBadgepay) == arcmdl.AttrYes {
  104. log.Warn("verifyArchive aid(%d) pay(%d) cid(%d) is pgc!!!!", aid, arc.AttrVal(arcmdl.AttrBitBadgepay), cid)
  105. err = ecode.NothingFound
  106. return
  107. }
  108. if arc.AttrVal(arcmdl.AttrBitUGCPay) == 1 {
  109. if mid <= 0 {
  110. aid, cid = s.validBuild(plat, build)
  111. if aid == 0 {
  112. err = ecode.PlayURLNotLogin
  113. return
  114. }
  115. } else if arc.Mid != mid {
  116. if relation, err = s.ugcpayDao.AssetRelation(c, aid, mid); err != nil {
  117. log.Error("verifyArchive %+v", err)
  118. err = ecode.PlayURLNotPay
  119. return
  120. } else if relation.State != _relationPaid {
  121. log.Warn("verifyArchive not pay aid(%d) mid(%d) state(%s)", aid, mid, relation.State)
  122. aid, cid = s.validBuild(plat, build)
  123. if aid == 0 {
  124. err = ecode.PlayURLNotPay
  125. return
  126. }
  127. }
  128. }
  129. }
  130. if mid > 0 {
  131. if arc.Mid == mid {
  132. isSp = 1
  133. } else {
  134. var card *accrpc.CardReply
  135. if card, err = s.accDao.Card(c, mid); err != nil {
  136. log.Error("verifyArchive %+v", err)
  137. err = nil
  138. } else if card.Card != nil && card.Card.Vip.IsValid() {
  139. isSp = 1
  140. }
  141. }
  142. } else if qn > _qn480 { //未登录最高清晰度 480
  143. qn = _qn480
  144. }
  145. }
  146. reqURL := s.c.Host.Playurl + reqPath
  147. playurl, code, err = s.dao.Playurl(c, mid, aid, cid, qn, params.Npcybs, params.Fnver, params.Fnval, params.ForceHost, isSp, params.Otype, params.MobiApp, buvid, fp, params.Session, reqURL)
  148. if err != nil {
  149. log.Error("%+v", err)
  150. reqURL = s.c.Host.PlayurlBk + _playURLV2
  151. playurl, code, err = s.dao.Playurl(c, mid, aid, cid, qn, params.Npcybs, params.Fnver, params.Fnval, params.ForceHost, isSp, params.Otype, params.MobiApp, buvid, fp, params.Session, reqURL)
  152. if err != nil {
  153. log.Error("%+v", err)
  154. return
  155. }
  156. }
  157. if code != ecode.OK.Code() {
  158. log.Error("playurl aid(%d) cid(%d) code(%d)", aid, cid, code)
  159. err = ecode.NothingFound
  160. playurl = nil
  161. }
  162. return
  163. }
  164. func (s *Service) validBuild(plat int8, build int) (aid, cid int64) {
  165. if (model.IsIphone(plat) && build < _iosBuild) || (model.IsAndroid(plat) && build < _androidBuild) {
  166. aid = s.c.PhoneAid
  167. cid = s.c.PhoneCid
  168. } else if model.IsIPad(plat) && build <= _iosBuild {
  169. aid = s.c.PadAid
  170. cid = s.c.PadCid
  171. } else if model.IsIPadHD(plat) && build <= _ipadHDBuild {
  172. aid = s.c.PadHDAid
  173. cid = s.c.PadHDCid
  174. }
  175. return
  176. }