favorite.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package favorite
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-interface/model"
  5. "go-common/app/interface/main/app-interface/model/favorite"
  6. fav "go-common/app/service/main/favorite/api"
  7. "go-common/library/log"
  8. "go-common/library/sync/errgroup"
  9. )
  10. const (
  11. _av = "av" //视频(ipad没有播单还是视频)
  12. _playlist = "playlist" // 播单
  13. _bangumi = "bangumi" // 追番
  14. _cinema = "cinema" // 追剧
  15. _topic = "topic" // 话题
  16. _article = "article" // 专栏
  17. _menu = "menu" // 歌单
  18. _pgcMenu = "pgc_menu" // 专辑
  19. _clips = "clips" // 小视频
  20. _albums = "albums" // 相簿
  21. _product = "product" // 商品
  22. _ticket = "ticket" // 展演
  23. _favorite = "favorite"
  24. )
  25. var tabMap = map[string]*favorite.TabItem{
  26. _av: {Name: "视频", Uri: "bilibili://main/favorite/video", Tab: _favorite},
  27. _playlist: {Name: "播单", Uri: "bilibili://main/favorite/playlist", Tab: _favorite},
  28. _bangumi: {Name: "追番", Uri: "bilibili://pgc/favorite/bangumi", Tab: _bangumi},
  29. _cinema: {Name: "追剧", Uri: "bilibili://pgc/favorite/cinema", Tab: _cinema},
  30. _topic: {Name: "话题", Uri: "bilibili://main/favorite/topic", Tab: _topic},
  31. _article: {Name: "专栏", Uri: "bilibili://column/favorite/article", Tab: _article},
  32. _menu: {Name: "歌单", Uri: "bilibili://music/favorite/menu", Tab: _menu},
  33. _pgcMenu: {Name: "专辑", Uri: "bilibili://music/favorite/album", Tab: _pgcMenu},
  34. _clips: {Name: "小视频", Uri: "bilibili://clip/favorite", Tab: _clips},
  35. _albums: {Name: "相簿", Uri: "bilibili://pictureshow/favorite", Tab: _albums},
  36. _product: {Name: "商品", Uri: "bilibili://mall/favorite/goods", Tab: _product},
  37. _ticket: {Name: "展演", Uri: "bilibili://mall/favorite/ticket", Tab: _ticket},
  38. }
  39. var tabArr = []string{_av, _playlist, _bangumi, _cinema, _topic, _article, _menu, _pgcMenu, _clips, _albums, _product, _ticket}
  40. // Folder get my favorite.
  41. func (s *Service) Folder(c context.Context, accessKey, actionKey, device, mobiApp, platform string, build int, aid, vmid, mid int64) (rs *favorite.MyFavorite, err error) {
  42. var pn, ps int = 1, 5
  43. rs = &favorite.MyFavorite{
  44. Tab: &favorite.Tab{
  45. Fav: true,
  46. },
  47. }
  48. g, ctx := errgroup.WithContext(c)
  49. g.Go(func() (err error) {
  50. var (
  51. mediaList bool
  52. folders []*favorite.Folder
  53. )
  54. plat := model.Plat(mobiApp, device)
  55. // 双端版本号限制,符合此条件显示为“默认收藏夹”:
  56. // iPhone <5.36.1(8300) 或iPhone>5.36.1(8300)
  57. // Android <5360001或Android>5361000
  58. // 双端版本号限制,符合此条件显示为“默认播单”:
  59. // iPhone=5.36.1(8300)
  60. // 5360001 <=Android <=5361000
  61. if (plat == model.PlatIPhone && build == 8300) || (plat == model.PlatAndroid && build >= 5360001 && build <= 5361000) {
  62. mediaList = true
  63. }
  64. if folders, err = s.favDao.Folders(ctx, mid, vmid, mobiApp, build, mediaList); err != nil {
  65. log.Error("%+v", err)
  66. return
  67. }
  68. if len(folders) != 0 {
  69. rs.Favorite = &favorite.FavList{
  70. Count: len(folders),
  71. Items: make([]*favorite.FavItem, 0, len(folders)),
  72. }
  73. for _, v := range folders {
  74. fi := &favorite.FavItem{}
  75. fi.FromFav(v)
  76. rs.Favorite.Items = append(rs.Favorite.Items, fi)
  77. }
  78. }
  79. return
  80. })
  81. g.Go(func() (err error) {
  82. var topic *fav.UserFolderReply
  83. if topic, err = s.topicDao.UserFolder(ctx, mid, 4); err != nil {
  84. log.Error("%+v", err)
  85. err = nil
  86. return
  87. }
  88. if topic != nil && topic.Res != nil && topic.Res.Count > 0 {
  89. rs.Tab.Topic = true
  90. }
  91. return
  92. })
  93. g.Go(func() error {
  94. article := s.Article(ctx, mid, pn, ps)
  95. if article != nil && article.Count > 0 {
  96. rs.Tab.Article = true
  97. }
  98. return nil
  99. })
  100. g.Go(func() error {
  101. clips := s.Clips(ctx, mid, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  102. if clips != nil && clips.PageInfo != nil && clips.Count > 0 {
  103. rs.Tab.Clips = true
  104. }
  105. return nil
  106. })
  107. g.Go(func() error {
  108. albums := s.Albums(ctx, mid, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  109. if albums != nil && albums.PageInfo != nil && albums.Count > 0 {
  110. rs.Tab.Albums = true
  111. }
  112. return nil
  113. })
  114. g.Go(func() error {
  115. specil := s.Specil(ctx, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  116. if specil != nil && specil.Count > 0 {
  117. rs.Tab.Specil = true
  118. }
  119. return nil
  120. })
  121. g.Go(func() (err error) {
  122. var has bool
  123. if has, err = s.bangumiDao.HasFollows(ctx, mid); err != nil {
  124. log.Error("%+v", err)
  125. err = nil
  126. return
  127. }
  128. rs.Tab.Cinema = has
  129. return
  130. })
  131. g.Go(func() (err error) {
  132. fav, err := s.audioDao.Fav(ctx, mid)
  133. if err != nil {
  134. log.Error("%+v", err)
  135. err = nil
  136. return
  137. }
  138. if fav != nil {
  139. rs.Tab.Menu = fav.Menu
  140. rs.Tab.PGCMenu = fav.PGCMenu
  141. rs.Tab.Audios = fav.Song
  142. }
  143. return
  144. })
  145. g.Go(func() (err error) {
  146. var ticket int32
  147. if ticket, err = s.ticketDao.FavCount(ctx, mid); err != nil {
  148. log.Error("%+v", err)
  149. err = nil
  150. return
  151. }
  152. if ticket > 0 {
  153. rs.Tab.Ticket = true
  154. }
  155. return
  156. })
  157. g.Go(func() (err error) {
  158. var product int32
  159. if product, err = s.mallDao.FavCount(ctx, mid); err != nil {
  160. log.Error("%+v", err)
  161. err = nil
  162. return
  163. }
  164. if product > 0 {
  165. rs.Tab.Product = true
  166. }
  167. return
  168. })
  169. if err = g.Wait(); err != nil {
  170. log.Error("%+v", err)
  171. }
  172. return
  173. }
  174. func (s *Service) FolderVideo(c context.Context, accessKey, actionKey, device, mobiApp, platform, keyword, order string, build, tid, pn, ps int, mid, fid, vmid int64) (folder *favorite.FavideoList) {
  175. video, err := s.favDao.FolderVideo(c, accessKey, actionKey, device, mobiApp, platform, keyword, order, build, tid, pn, ps, mid, fid, vmid)
  176. if err != nil {
  177. folder = &favorite.FavideoList{Items: []*favorite.FavideoItem{}}
  178. log.Error("%+v", err)
  179. return
  180. }
  181. folder = &favorite.FavideoList{
  182. Count: video.Total,
  183. Items: make([]*favorite.FavideoItem, 0, len(video.Archives)),
  184. }
  185. if video != nil {
  186. for _, v := range video.Archives {
  187. fi := &favorite.FavideoItem{}
  188. fi.FromFavideo(v)
  189. folder.Items = append(folder.Items, fi)
  190. }
  191. }
  192. return
  193. }
  194. func (s *Service) Topic(c context.Context, accessKey, actionKey, device, mobiApp, platform string, build, ps, pn int, mid int64) (topic *favorite.TopicList) {
  195. topics, err := s.topicDao.Topic(c, accessKey, actionKey, device, mobiApp, platform, build, ps, pn, mid)
  196. if err != nil {
  197. topic = &favorite.TopicList{Items: []*favorite.TopicItem{}}
  198. log.Error("%+v", err)
  199. return
  200. }
  201. topic = &favorite.TopicList{
  202. Count: topics.Total,
  203. Items: make([]*favorite.TopicItem, 0, len(topics.Lists)),
  204. }
  205. if topics != nil {
  206. for _, v := range topics.Lists {
  207. fi := &favorite.TopicItem{}
  208. fi.FromTopic(v)
  209. topic.Items = append(topic.Items, fi)
  210. }
  211. }
  212. return
  213. }
  214. func (s *Service) Article(c context.Context, mid int64, pn, ps int) (article *favorite.ArticleList) {
  215. articleTmp, err := s.artDao.Favorites(c, mid, pn, ps)
  216. if err != nil {
  217. article = &favorite.ArticleList{Items: []*favorite.ArticleItem{}}
  218. log.Error("%+v", err)
  219. return
  220. }
  221. article = &favorite.ArticleList{
  222. Count: len(articleTmp),
  223. Items: make([]*favorite.ArticleItem, 0, len(articleTmp)),
  224. }
  225. if len(articleTmp) != 0 {
  226. for _, v := range articleTmp {
  227. fi := &favorite.ArticleItem{}
  228. fi.FromArticle(v)
  229. article.Items = append(article.Items, fi)
  230. }
  231. }
  232. return
  233. }
  234. // Clips
  235. func (s *Service) Clips(c context.Context, mid int64, accessKey, actionKey, device, mobiApp, platform string, build, pn, ps int) (clips *favorite.ClipsList) {
  236. clipsTmp, err := s.bplusDao.FavClips(c, mid, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  237. if err != nil {
  238. clips = &favorite.ClipsList{Items: []*favorite.ClipsItem{}}
  239. log.Error("%+v", err)
  240. return
  241. }
  242. clips = &favorite.ClipsList{
  243. PageInfo: clipsTmp.PageInfo,
  244. Items: make([]*favorite.ClipsItem, 0, len(clipsTmp.List)),
  245. }
  246. if clipsTmp != nil {
  247. for _, v := range clipsTmp.List {
  248. fi := &favorite.ClipsItem{}
  249. fi.FromClips(v)
  250. clips.Items = append(clips.Items, fi)
  251. }
  252. }
  253. return
  254. }
  255. func (s *Service) Albums(c context.Context, mid int64, accessKey, actionKey, device, mobiApp, platform string, build, pn, ps int) (albums *favorite.AlbumsList) {
  256. albumsTmp, err := s.bplusDao.FavAlbums(c, mid, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  257. if err != nil {
  258. albums = &favorite.AlbumsList{Items: []*favorite.AlbumItem{}}
  259. log.Error("%+v", err)
  260. return
  261. }
  262. albums = &favorite.AlbumsList{
  263. PageInfo: albumsTmp.PageInfo,
  264. Items: make([]*favorite.AlbumItem, 0, len(albumsTmp.List)),
  265. }
  266. if albumsTmp != nil {
  267. for _, v := range albumsTmp.List {
  268. fi := &favorite.AlbumItem{}
  269. fi.FromAlbum(v)
  270. albums.Items = append(albums.Items, fi)
  271. }
  272. }
  273. return
  274. }
  275. func (s *Service) Specil(c context.Context, accessKey, actionKey, device, mobiApp, platform string, build, pn, ps int) (specil *favorite.SpList) {
  276. specilTmp, err := s.spDao.Specil(c, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  277. if err != nil {
  278. specil = &favorite.SpList{Items: []*favorite.SpItem{}}
  279. log.Error("%+v", err)
  280. return
  281. }
  282. specil = &favorite.SpList{
  283. Count: len(specilTmp.Items),
  284. Items: make([]*favorite.SpItem, 0, len(specilTmp.Items)),
  285. }
  286. if specilTmp != nil {
  287. for _, v := range specilTmp.Items {
  288. fi := &favorite.SpItem{}
  289. fi.FromSp(v)
  290. specil.Items = append(specil.Items, fi)
  291. }
  292. }
  293. return
  294. }
  295. func (s *Service) Audio(c context.Context, accessKey string, mid int64, pn, ps int) (audio *favorite.AudioList) {
  296. audioTmp, err := s.audioDao.FavAudio(c, accessKey, mid, pn, ps)
  297. if err != nil {
  298. audio = &favorite.AudioList{Items: []*favorite.AudioItem{}}
  299. log.Error("%+v", err)
  300. return
  301. }
  302. audio = &favorite.AudioList{
  303. Count: len(audioTmp),
  304. Items: make([]*favorite.AudioItem, 0, len(audioTmp)),
  305. }
  306. for _, v := range audioTmp {
  307. fi := &favorite.AudioItem{}
  308. fi.FromAudio(v)
  309. audio.Items = append(audio.Items, fi)
  310. }
  311. return
  312. }
  313. // Tab fav tab.
  314. func (s *Service) Tab(c context.Context, accessKey, actionKey, device, mobiApp, platform, filtered string, build int, mid int64) (tab []*favorite.TabItem, err error) {
  315. var (
  316. pn, ps = 1, 5
  317. tabDisplay = []string{_playlist}
  318. )
  319. plat := model.Plat(mobiApp, device)
  320. if model.IsIPad(plat) {
  321. tabDisplay = []string{_av}
  322. }
  323. g, ctx := errgroup.WithContext(c)
  324. g.Go(func() (err error) {
  325. var bangumiFav, cinemaFav int
  326. if bangumiFav, cinemaFav, err = s.bangumiDao.FavDisplay(ctx, mid); err != nil {
  327. log.Error("%+v", err)
  328. err = nil
  329. return
  330. }
  331. if bangumiFav == 1 {
  332. tabDisplay = append(tabDisplay, _bangumi)
  333. }
  334. if cinemaFav == 1 {
  335. tabDisplay = append(tabDisplay, _cinema)
  336. }
  337. return
  338. })
  339. if !model.IsIPad(plat) {
  340. if filtered != "1" {
  341. g.Go(func() (err error) {
  342. var topic *fav.UserFolderReply
  343. if topic, err = s.topicDao.UserFolder(ctx, mid, 4); err != nil {
  344. log.Error("%+v", err)
  345. err = nil
  346. return
  347. }
  348. if topic != nil && topic.Res != nil && topic.Res.Count > 0 {
  349. tabDisplay = append(tabDisplay, _topic)
  350. }
  351. return
  352. })
  353. }
  354. g.Go(func() error {
  355. article := s.Article(ctx, mid, pn, ps)
  356. if article != nil && article.Count > 0 {
  357. tabDisplay = append(tabDisplay, _article)
  358. }
  359. return nil
  360. })
  361. g.Go(func() error {
  362. clips := s.Clips(ctx, mid, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  363. if clips != nil && clips.PageInfo != nil && clips.Count > 0 {
  364. tabDisplay = append(tabDisplay, _clips)
  365. }
  366. return nil
  367. })
  368. g.Go(func() error {
  369. albums := s.Albums(ctx, mid, accessKey, actionKey, device, mobiApp, platform, build, pn, ps)
  370. if albums != nil && albums.PageInfo != nil && albums.Count > 0 {
  371. tabDisplay = append(tabDisplay, _albums)
  372. }
  373. return nil
  374. })
  375. g.Go(func() (err error) {
  376. fav, err := s.audioDao.Fav(ctx, mid)
  377. if err != nil {
  378. log.Error("%+v", err)
  379. err = nil
  380. return
  381. }
  382. if fav != nil {
  383. tabDisplay = append(tabDisplay, _menu)
  384. tabDisplay = append(tabDisplay, _pgcMenu)
  385. }
  386. return
  387. })
  388. g.Go(func() (err error) {
  389. var ticket int32
  390. if ticket, err = s.ticketDao.FavCount(ctx, mid); err != nil {
  391. log.Error("%+v", err)
  392. err = nil
  393. return
  394. }
  395. if ticket > 0 {
  396. tabDisplay = append(tabDisplay, _ticket)
  397. }
  398. return
  399. })
  400. g.Go(func() (err error) {
  401. var product int32
  402. if product, err = s.mallDao.FavCount(ctx, mid); err != nil {
  403. log.Error("%+v", err)
  404. err = nil
  405. return
  406. }
  407. if product > 0 {
  408. tabDisplay = append(tabDisplay, _product)
  409. }
  410. return
  411. })
  412. }
  413. g.Wait()
  414. for _, t := range tabArr {
  415. for _, dt := range tabDisplay {
  416. if t == dt {
  417. tab = append(tab, tabMap[t])
  418. }
  419. }
  420. }
  421. return
  422. }