playurl.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/player/model"
  5. accmdl "go-common/app/service/main/account/api"
  6. arcmdl "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. ugcmdl "go-common/app/service/main/ugcpay/api/grpc/v1"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _playurlURI = "/v2/playurl"
  14. _playurlURIV3 = "/v3/playurl"
  15. _h5PlayURI = "/playurl"
  16. _highQaURI = "/v2/playurlproj"
  17. _ugcPayOtypeArc = "archive"
  18. _relationPaid = "paid"
  19. )
  20. // Playurl get playurl data.
  21. func (s *Service) Playurl(c context.Context, mid int64, arg *model.PlayurlArg) (data *model.PlayurlRes, err error) {
  22. var (
  23. token, playurl string
  24. isUGCPayArc bool
  25. viewReply *arcmdl.ViewReply
  26. )
  27. if arg.HTML5 > 0 {
  28. if arg.HighQuality > 0 {
  29. playurl = s.highQaURL
  30. } else {
  31. playurl = s.h5PlayURL
  32. }
  33. } else {
  34. if viewReply, err = s.view(c, arg.Aid); err != nil {
  35. log.Error("Playurl s.arcClient.Arc aid(%d) error(%v)", arg.Aid, err)
  36. return
  37. }
  38. arc := viewReply.Arc
  39. if !arc.IsNormal() || !hasCid(viewReply.Pages, arg.Cid) {
  40. err = ecode.NothingFound
  41. log.Warn("Playurl verifyArchive aid(%d) can not play or no cid(%d)", arg.Aid, arg.Cid)
  42. return
  43. }
  44. if arc.AttrVal(archive.AttrBitIsPGC) == archive.AttrYes || arc.AttrVal(archive.AttrBitBadgepay) == archive.AttrYes {
  45. err = ecode.NothingFound
  46. log.Warn("Playurl verifyArchive aid(%d) cid(%d) is pgc", arg.Aid, arg.Cid)
  47. return
  48. }
  49. if arc.AttrVal(archive.AttrBitUGCPay) == archive.AttrYes {
  50. if mid <= 0 {
  51. err = ecode.PlayURLNotLogin
  52. return
  53. } else if arc.Author.Mid != mid {
  54. var relation *ugcmdl.AssetRelationResp
  55. if relation, err = s.ugcPayClient.AssetRelation(c, &ugcmdl.AssetRelationReq{Mid: mid, Oid: arg.Aid, Otype: _ugcPayOtypeArc}); err != nil {
  56. log.Error("Playurl AssetRelation mid:%d aid:%d error(%+v)", mid, arg.Aid, err)
  57. err = ecode.PlayURLNotPay
  58. return
  59. } else if relation.State != _relationPaid {
  60. log.Warn("Playurl not pay aid(%d) mid(%d) state(%s)", arg.Aid, mid, relation.State)
  61. err = ecode.PlayURLNotPay
  62. return
  63. }
  64. }
  65. isUGCPayArc = true
  66. }
  67. if isUGCPayArc || arg.Aid%10 < s.c.Rule.PlayurlGray {
  68. playurl = s.playURLV3
  69. if mid > 0 {
  70. if arg.Qn == 0 {
  71. arg.Qn = s.c.Rule.AutoQn
  72. }
  73. if _, isVipQn := s.vipQn[arg.Qn]; isVipQn {
  74. if arc.Author.Mid != mid {
  75. var card *accmdl.CardReply
  76. if card, err = s.accClient.Card3(c, &accmdl.MidReq{Mid: mid}); err != nil {
  77. log.Error("Playurl s.accClient.Card3(%d) error(%+v)", mid, err)
  78. err = nil
  79. arg.Qn = s.c.Rule.MaxFreeQn
  80. } else if card.Card.Vip.Status != 1 || card.Card.Vip.Type <= 0 {
  81. arg.Qn = s.c.Rule.MaxFreeQn
  82. }
  83. }
  84. }
  85. } else {
  86. if arg.Qn > s.c.Rule.LoginQn {
  87. arg.Qn = s.c.Rule.LoginQn
  88. }
  89. }
  90. } else {
  91. playurl = s.playURL
  92. if mid > 0 {
  93. if arg.Qn == 0 {
  94. arg.Qn = s.c.Rule.AutoQn
  95. }
  96. if _, isVipQn := s.vipQn[arg.Qn]; isVipQn {
  97. if playurlToken, e := s.PlayURLToken(c, mid, arg.Aid, arg.Cid); e != nil {
  98. log.Warn("Playurl token arg(%+v) error(%v)", arg, e)
  99. } else if playurlToken != nil {
  100. token = playurlToken.Token
  101. }
  102. }
  103. } else {
  104. if arg.Qn > s.c.Rule.LoginQn {
  105. arg.Qn = s.c.Rule.LoginQn
  106. }
  107. }
  108. }
  109. }
  110. if data, err = s.dao.Playurl(c, mid, arg, playurl, token); err != nil {
  111. log.Error("s.dao.Playurl mid(%d) arg(%+v) token(%s) error(%+v)", mid, arg, token, err)
  112. // h5 high quality backup
  113. if arg.HTML5 > 0 && arg.HighQuality > 0 {
  114. err = nil
  115. playurl = s.h5PlayURL
  116. arg.HighQuality = 0
  117. if data, err = s.dao.Playurl(c, mid, arg, playurl, token); err != nil {
  118. log.Error("s.dao.Playurl h5 backup mid(%d) arg(%+v) token(%s) error(%+v)", mid, arg, token, err)
  119. }
  120. }
  121. }
  122. return
  123. }
  124. func hasCid(pages []*arcmdl.Page, cid int64) bool {
  125. for _, v := range pages {
  126. if cid == v.Cid {
  127. return true
  128. }
  129. }
  130. return false
  131. }