service.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "encoding/xml"
  7. "fmt"
  8. htemplate "html/template"
  9. "strconv"
  10. "strings"
  11. "text/template"
  12. "time"
  13. dmrpc "go-common/app/interface/main/dm2/rpc/client"
  14. hisrpc "go-common/app/interface/main/history/rpc/client"
  15. "go-common/app/interface/main/player/conf"
  16. "go-common/app/interface/main/player/dao"
  17. "go-common/app/interface/main/player/model"
  18. tagrpc "go-common/app/interface/main/tag/rpc/client"
  19. accclient "go-common/app/service/main/account/api"
  20. arcclient "go-common/app/service/main/archive/api"
  21. arcrpc "go-common/app/service/main/archive/api/gorpc"
  22. assrpc "go-common/app/service/main/assist/rpc/client"
  23. locrpc "go-common/app/service/main/location/rpc/client"
  24. resmdl "go-common/app/service/main/resource/model"
  25. resrpc "go-common/app/service/main/resource/rpc/client"
  26. ugcclient "go-common/app/service/main/ugcpay/api/grpc/v1"
  27. "go-common/library/ecode"
  28. "go-common/library/log"
  29. )
  30. const (
  31. _resourceID = 2319
  32. _bgColor = "#000000"
  33. )
  34. // Service is a service.
  35. type Service struct {
  36. // config
  37. c *conf.Config
  38. // dao
  39. dao *dao.Dao
  40. // rpc
  41. arc *arcrpc.Service2
  42. his *hisrpc.Service
  43. ass *assrpc.Service
  44. res *resrpc.Service
  45. dm2 *dmrpc.Service
  46. loc *locrpc.Service
  47. tag *tagrpc.Service
  48. // memory cache
  49. caItems []*model.Item
  50. params string
  51. icon htemplate.HTML
  52. // template
  53. tWithU *template.Template
  54. tNoU *template.Template
  55. // broadcast
  56. BrBegin time.Time
  57. BrEnd time.Time
  58. // 拜年祭相关
  59. matOn bool
  60. matTime time.Time
  61. pastView *model.View
  62. matView *model.View
  63. // vipQn
  64. vipQn map[int]int
  65. // grpc client
  66. accClient accclient.AccountClient
  67. arcClient arcclient.ArchiveClient
  68. ugcPayClient ugcclient.UGCPayClient
  69. // playurl paths
  70. playURL string
  71. playURLV3 string
  72. h5PlayURL string
  73. highQaURL string
  74. // bnj2019 view map
  75. bnj2019ViewMap map[int64]*arcclient.ViewReply
  76. }
  77. // New new and return service.
  78. func New(c *conf.Config) (s *Service) {
  79. s = &Service{
  80. c: c,
  81. dao: dao.New(c),
  82. arc: arcrpc.New2(c.ArchiveRPC),
  83. his: hisrpc.New(c.HistoryRPC),
  84. ass: assrpc.New(c.AssistRPC),
  85. res: resrpc.New(c.ResourceRPC),
  86. dm2: dmrpc.New(c.Dm2RPC),
  87. loc: locrpc.New(c.LocRPC),
  88. tag: tagrpc.New2(c.TagRPC),
  89. }
  90. s.playURL = c.Host.PlayurlCo + _playurlURI
  91. s.playURLV3 = c.Host.PlayurlCo + _playurlURIV3
  92. s.h5PlayURL = c.Host.H5Playurl + _h5PlayURI
  93. s.highQaURL = c.Host.HighPlayurl + _highQaURI
  94. var err error
  95. if s.accClient, err = accclient.NewClient(c.AccClient); err != nil {
  96. panic(err)
  97. }
  98. if s.arcClient, err = arcclient.NewClient(c.ArcClient); err != nil {
  99. panic(err)
  100. }
  101. if s.ugcPayClient, err = ugcclient.NewClient(c.UGCPayClient); err != nil {
  102. panic(err)
  103. }
  104. s.initVipQn()
  105. s.caItems = []*model.Item{}
  106. // 模板
  107. s.tWithU, _ = template.New("with_user").Parse(model.TpWithUinfo)
  108. s.tNoU, _ = template.New("no_user").Parse(model.TpWithNoUinfo)
  109. // broadcast
  110. s.BrBegin, _ = time.Parse("2006-01-02 15:04:05", c.Broadcast.Begin)
  111. s.BrEnd, _ = time.Parse("2006-01-02 15:04:05", c.Broadcast.End)
  112. // 初始化播放器灰度配置项目
  113. go s.policyproc()
  114. go s.resourceproc()
  115. go s.paramproc()
  116. go s.iconproc()
  117. // 拜年祭
  118. s.matTime, _ = time.Parse(time.RFC3339, c.Matsuri.MatTime)
  119. s.matOn = true
  120. go s.matProc()
  121. go s.bnj2019Viewproc()
  122. return
  123. }
  124. // Ping check service health
  125. func (s *Service) Ping(c context.Context) (err error) {
  126. if err = s.dao.Ping(c); err != nil {
  127. log.Error("s.dao.Ping() error(%v)", err)
  128. }
  129. return
  130. }
  131. func (s *Service) matProc() {
  132. var (
  133. ctx = context.Background()
  134. err error
  135. )
  136. for {
  137. var tmp *arcclient.ViewReply
  138. if tmp, err = s.arcClient.View(ctx, &arcclient.ViewRequest{Aid: s.c.Matsuri.PastID}); err != nil || tmp == nil {
  139. dao.PromError("View接口错误", "s.arcClientView3(%v) tmp(%v) error(%v) ", s.c.Matsuri.PastID, tmp, err)
  140. time.Sleep(time.Second)
  141. continue
  142. }
  143. s.pastView = &model.View{Arc: tmp.Arc, Pages: tmp.Pages}
  144. if tmp, err = s.arcClient.View(ctx, &arcclient.ViewRequest{Aid: s.c.Matsuri.MatID}); err != nil || tmp == nil {
  145. dao.PromError("View接口错误", "s.arcClientView3(%v) tmp(%v) error(%v)", s.c.Matsuri.MatID, tmp, err)
  146. time.Sleep(time.Second)
  147. continue
  148. }
  149. s.matView = &model.View{Arc: tmp.Arc, Pages: tmp.Pages}
  150. time.Sleep(time.Duration(s.c.Matsuri.Tick))
  151. }
  152. }
  153. func (s *Service) resourceproc() {
  154. for {
  155. var (
  156. items []*model.Item
  157. err error
  158. res *resmdl.Resource
  159. )
  160. if res, err = s.res.Resource(context.Background(), &resmdl.ArgRes{ResID: _resourceID}); err != nil {
  161. dao.PromError("Resource接口错误", "s.res.Resource(%d) error(%v)", _resourceID, err)
  162. time.Sleep(time.Second)
  163. continue
  164. }
  165. if res == nil {
  166. dao.PromError("Resource接口数据为空", "s.res.Resource(%d) is nil", _resourceID)
  167. time.Sleep(time.Second)
  168. continue
  169. }
  170. if len(res.Assignments) == 0 {
  171. dao.PromError("Resource接口Assignments数据为空", "s.res.Resource(%d) assignments is nil", _resourceID)
  172. time.Sleep(time.Second)
  173. continue
  174. }
  175. for _, v := range res.Assignments {
  176. item := &model.Item{
  177. Bgcolor: _bgColor,
  178. ResourceID: strconv.Itoa(_resourceID),
  179. SrcID: strconv.Itoa(v.ResID),
  180. ID: strconv.Itoa(v.ID),
  181. }
  182. if catalog, ok := model.Catalog[v.PlayerCategory]; ok {
  183. item.Catalog = catalog
  184. }
  185. item.Content = string(rune(10)) + string(rune(13)) + fmt.Sprintf(_content, v.URL, v.Name) + string(rune(10)) + string(rune(13))
  186. items = append(items, item)
  187. }
  188. s.caItems = items
  189. time.Sleep(time.Duration(s.c.Tick.CarouselTick))
  190. }
  191. }
  192. func (s *Service) paramproc() {
  193. for {
  194. var (
  195. params []*model.Param
  196. items []string
  197. err error
  198. )
  199. c := context.Background()
  200. if params, err = s.dao.Param(c); err != nil {
  201. log.Error("s.dao.Param() error(%v)", err)
  202. time.Sleep(time.Second)
  203. continue
  204. }
  205. if len(params) == 0 {
  206. time.Sleep(time.Duration(s.c.Tick.ParamTick))
  207. continue
  208. }
  209. for _, pa := range params {
  210. nameBy := bytes.NewBuffer(nil)
  211. valueBy := bytes.NewBuffer(nil)
  212. if err = xml.EscapeText(nameBy, []byte(pa.Name)); err != nil {
  213. log.Error("xml.EscapeText(%s) error(%v)", pa.Name, err)
  214. continue
  215. } else {
  216. pa.Name = nameBy.String()
  217. }
  218. if err = xml.EscapeText(valueBy, []byte(pa.Value)); err != nil {
  219. log.Error("xml.EscapeText(%s) error(%v)", pa.Value, err)
  220. continue
  221. } else {
  222. pa.Value = valueBy.String()
  223. }
  224. item := "<" + pa.Name + ">" + pa.Value + "</" + pa.Name + ">"
  225. items = append(items, item)
  226. }
  227. if len(items) > 0 {
  228. s.params = strings.Join(items, "\n")
  229. }
  230. time.Sleep(time.Duration(s.c.Tick.ParamTick))
  231. }
  232. }
  233. func (s *Service) policyproc() {
  234. s.c.Policy.StartTime, _ = time.Parse("2006-01-02 15:04:05", s.c.Policy.Start)
  235. s.c.Policy.EndTime, _ = time.Parse("2006-01-02 15:04:05", s.c.Policy.End)
  236. s.c.Policy.MtimeTime, _ = time.Parse("2006-01-02 15:04:05", s.c.Policy.Mtime)
  237. }
  238. func (s *Service) iconproc() {
  239. for {
  240. icon, err := s.res.PlayerIcon(context.Background())
  241. if err != nil || icon == nil {
  242. log.Error("iconproc s.res.PlayerIcon error(%v) icon(%v)", err, icon)
  243. if ecode.Cause(err) == ecode.NothingFound {
  244. s.icon = ""
  245. }
  246. time.Sleep(time.Duration(s.c.Tick.IconTick))
  247. continue
  248. }
  249. icon.URL1 = strings.Replace(icon.URL1, "http://", "//", 1)
  250. icon.URL2 = strings.Replace(icon.URL2, "http://", "//", 1)
  251. bs, err := json.Marshal(icon)
  252. if err != nil {
  253. log.Error("iconproc json.Marshal(%v) error(%v)", icon, err)
  254. time.Sleep(time.Second)
  255. continue
  256. }
  257. s.icon = htemplate.HTML(bs)
  258. time.Sleep(time.Duration(s.c.Tick.IconTick))
  259. }
  260. }
  261. func (s *Service) initVipQn() {
  262. tmp := make(map[int]int, len(s.c.Rule.VipQn))
  263. for _, qn := range s.c.Rule.VipQn {
  264. tmp[qn] = qn
  265. }
  266. s.vipQn = tmp
  267. }
  268. func (s *Service) bnj2019Viewproc() {
  269. for {
  270. time.Sleep(time.Duration(s.c.Bnj2019.BnjTick))
  271. aids := append(s.c.Bnj2019.BnjListAids, s.c.Bnj2019.BnjMainAid)
  272. if len(aids) == 0 {
  273. continue
  274. }
  275. if views, err := s.arcClient.Views(context.Background(), &arcclient.ViewsRequest{Aids: aids}); err != nil || views == nil {
  276. log.Error("bnj2019Viewproc s.arcClient.Views(%v) error(%v)", aids, err)
  277. continue
  278. } else {
  279. tmp := make(map[int64]*arcclient.ViewReply, len(aids))
  280. for _, aid := range aids {
  281. if view, ok := views.Views[aid]; ok && view.Arc.IsNormal() {
  282. tmp[aid] = view
  283. }
  284. }
  285. s.bnj2019ViewMap = tmp
  286. }
  287. }
  288. }