service.go 5.3 KB

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