account.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/interface/main/space/model"
  6. tagmdl "go-common/app/interface/main/tag/model"
  7. artmdl "go-common/app/interface/openplatform/article/model"
  8. accwar "go-common/app/service/main/account/api"
  9. accmdl "go-common/app/service/main/account/model"
  10. favmdl "go-common/app/service/main/favorite/model"
  11. memmdl "go-common/app/service/main/member/model"
  12. relmdl "go-common/app/service/main/relation/model"
  13. upmdl "go-common/app/service/main/up/api/v1"
  14. "go-common/library/conf/env"
  15. "go-common/library/ecode"
  16. "go-common/library/log"
  17. "go-common/library/net/metadata"
  18. "go-common/library/sync/errgroup"
  19. )
  20. const (
  21. _samplePn = 1
  22. _samplePs = 1
  23. _silenceForbid = 1
  24. _accBlockDefault = 0
  25. _accBlockDue = 1
  26. _officialNoType = -1
  27. _audioCardOn = 1
  28. _noticeForbid = 1
  29. )
  30. var (
  31. _emptyThemeList = make([]*model.ThemeDetail, 0)
  32. _emptyArcItem = make([]*model.ArcItem, 0)
  33. )
  34. // NavNum get space nav num by mid.
  35. func (s *Service) NavNum(c context.Context, mid, vmid int64) (res *model.NavNum) {
  36. ip := metadata.String(c, metadata.RemoteIP)
  37. res = new(model.NavNum)
  38. group, errCtx := errgroup.WithContext(c)
  39. group.Go(func() error {
  40. if reply, err := s.upClient.UpCount(errCtx, &upmdl.UpCountReq{Mid: vmid}); err != nil {
  41. log.Error("s.upClient.UpCount(%d) error(%v)", vmid, err)
  42. } else if reply.Count > 0 {
  43. res.Video = reply.Count
  44. }
  45. return nil
  46. })
  47. group.Go(func() error {
  48. res.Channel = new(model.Num)
  49. if chs, err := s.ChannelList(errCtx, vmid, false); err != nil {
  50. log.Error("s.ChannelList(%d) error(%v)", vmid, err)
  51. } else if chCnt := len(chs); chCnt > 0 {
  52. res.Channel.Master = chCnt
  53. for _, v := range chs {
  54. if v.Count > 0 {
  55. res.Channel.Guest++
  56. }
  57. }
  58. }
  59. return nil
  60. })
  61. group.Go(func() error {
  62. res.Favourite = new(model.Num)
  63. if favs, err := s.dao.FavFolder(errCtx, mid, vmid); err != nil {
  64. log.Error("s.dao.FavFolder(%d) error(%v)", vmid, err)
  65. } else if favCnt := len(favs); favCnt > 0 {
  66. res.Favourite.Master = favCnt
  67. for _, v := range favs {
  68. if v.IsPublic() {
  69. res.Favourite.Guest++
  70. }
  71. }
  72. }
  73. return nil
  74. })
  75. group.Go(func() error {
  76. if _, cnt, err := s.dao.BangumiList(errCtx, vmid, _samplePn, _samplePs); err != nil {
  77. log.Error("s.dao.BangumiList(%d) error(%v)", vmid, err)
  78. } else if cnt > 0 {
  79. res.Bangumi = cnt
  80. }
  81. return nil
  82. })
  83. group.Go(func() error {
  84. if tag, err := s.tag.SubTags(errCtx, &tagmdl.ArgSub{Mid: vmid, Pn: _samplePn, Ps: _samplePs, RealIP: ip}); err != nil {
  85. log.Error("s.tag.SubTags(%d) error(%v)", vmid, err)
  86. } else if tag != nil {
  87. res.Tag = tag.Total
  88. }
  89. return nil
  90. })
  91. group.Go(func() error {
  92. if art, err := s.art.UpArtMetas(errCtx, &artmdl.ArgUpArts{Mid: vmid, Pn: 1, Ps: 10, RealIP: ip}); err != nil {
  93. log.Error("s.art.UpArtMetas(%d) error(%v)", vmid, err)
  94. } else if art != nil {
  95. res.Article = art.Count
  96. }
  97. return nil
  98. })
  99. group.Go(func() error {
  100. if cnt, err := s.fav.CntUserFolders(errCtx, &favmdl.ArgCntUserFolders{Type: favmdl.TypePlayVideo, Mid: vmid, RealIP: ip}); err != nil {
  101. log.Error("s.dao.Playlist(%d) error(%v)", vmid, err)
  102. } else if cnt > 0 {
  103. res.Playlist = cnt
  104. }
  105. return nil
  106. })
  107. group.Go(func() error {
  108. if cnt, err := s.dao.AlbumCount(errCtx, vmid); err == nil && cnt > 0 {
  109. res.Album = cnt
  110. }
  111. return nil
  112. })
  113. group.Go(func() error {
  114. if cnt, err := s.dao.AudioCnt(errCtx, vmid); err != nil {
  115. log.Error("s.dao.AudioCnt(%d) error(%v)", vmid, err)
  116. } else if cnt > 0 {
  117. res.Audio = cnt
  118. }
  119. return nil
  120. })
  121. group.Wait()
  122. return
  123. }
  124. // UpStat get up stat.
  125. func (s *Service) UpStat(c context.Context, mid int64) (res *model.UpStat, err error) {
  126. var (
  127. info *accwar.InfoReply
  128. arcStat *model.UpArcStat
  129. artStat *model.UpArtStat
  130. arcErr, artErr error
  131. )
  132. if info, err = s.accClient.Info3(c, &accwar.MidReq{Mid: mid}); err != nil || info == nil {
  133. log.Error("s.accClient.Info3(%d) error(%v)", mid, err)
  134. return
  135. }
  136. res = new(model.UpStat)
  137. group, errCtx := errgroup.WithContext(c)
  138. group.Go(func() error {
  139. if arcStat, arcErr = s.UpArcStat(errCtx, mid); arcErr != nil {
  140. log.Error("s.UpArcStat(%d) error(%v)", mid, arcErr)
  141. } else if arcStat != nil {
  142. res.Archive.View = arcStat.View
  143. }
  144. return nil
  145. })
  146. group.Go(func() error {
  147. if artStat, artErr = s.UpArtStat(errCtx, mid); artErr != nil {
  148. log.Error("s.UpArtStat(%d) error(%v)", mid, artErr)
  149. } else if artStat != nil {
  150. res.Article.View = artStat.View
  151. }
  152. return nil
  153. })
  154. group.Wait()
  155. return
  156. }
  157. // MyInfo get my info.
  158. func (s *Service) MyInfo(c context.Context, mid int64) (res *accmdl.ProfileStat, err error) {
  159. var reply *accwar.ProfileStatReply
  160. if reply, err = s.accClient.ProfileWithStat3(c, &accwar.MidReq{Mid: mid}); err != nil {
  161. log.Error("s.accClient.ProfileWithStat3(%d) error(%v)", mid, err)
  162. return
  163. }
  164. level := memmdl.LevelInfo{
  165. Cur: reply.LevelInfo.Cur,
  166. Min: reply.LevelInfo.Min,
  167. NowExp: reply.LevelInfo.NowExp,
  168. NextExp: reply.LevelInfo.NextExp,
  169. }
  170. res = &accmdl.ProfileStat{
  171. Profile: reply.Profile,
  172. LevelExp: level,
  173. Coins: reply.Coins,
  174. Following: reply.Follower,
  175. Follower: reply.Follower,
  176. }
  177. return
  178. }
  179. // AccTags get account tags.
  180. func (s *Service) AccTags(c context.Context, mid int64) (res json.RawMessage, err error) {
  181. return s.dao.AccTags(c, mid)
  182. }
  183. // SetAccTags set account tags.
  184. func (s *Service) SetAccTags(c context.Context, tags, ck string) (err error) {
  185. return s.dao.SetAccTags(c, tags, ck)
  186. }
  187. // AccInfo web acc info.
  188. func (s *Service) AccInfo(c context.Context, mid, vmid int64) (res *model.AccInfo, err error) {
  189. if env.DeployEnv == env.DeployEnvProd {
  190. if _, ok := s.BlacklistValue[vmid]; ok {
  191. err = ecode.NothingFound
  192. return
  193. }
  194. }
  195. var (
  196. reply *accwar.ProfileStatReply
  197. topPhoto *model.TopPhoto
  198. topErr error
  199. )
  200. if reply, err = s.accClient.ProfileWithStat3(c, &accwar.MidReq{Mid: vmid}); err != nil || reply == nil {
  201. log.Error("s.accClient.ProfileWithStat3(%d) error(%v)", vmid, err)
  202. if ecode.Cause(err) != ecode.UserNotExist {
  203. return
  204. }
  205. reply = model.DefaultProfileStat
  206. }
  207. res = new(model.AccInfo)
  208. res.FromCard(reply)
  209. if res.Mid == 0 {
  210. res.Mid = vmid
  211. }
  212. group, errCtx := errgroup.WithContext(c)
  213. //check privacy
  214. if mid != vmid {
  215. group.Go(func() error {
  216. if privacyErr := s.privacyCheck(errCtx, vmid, model.PcyUserInfo); privacyErr != nil {
  217. res.JoinTime = 0
  218. res.Sex = "保密"
  219. res.Birthday = ""
  220. }
  221. return nil
  222. })
  223. if mid > 0 {
  224. group.Go(func() error {
  225. if relation, err := s.accClient.Relation3(errCtx, &accwar.RelationReq{Mid: mid, Owner: vmid}); err != nil {
  226. log.Error("s.accClient.Relation3(%d,%d) error (%v)", mid, vmid, err)
  227. } else if relation != nil {
  228. res.IsFollowed = relation.Following
  229. }
  230. return nil
  231. })
  232. }
  233. }
  234. //get top photo
  235. group.Go(func() error {
  236. topPhoto, topErr = s.dao.WebTopPhoto(errCtx, vmid)
  237. return nil
  238. })
  239. //get all theme
  240. group.Go(func() error {
  241. res.Theme = struct{}{}
  242. if theme, err := s.dao.Theme(errCtx, vmid); err == nil && theme != nil && len(theme.List) > 0 {
  243. for _, v := range theme.List {
  244. if v.IsActivated == 1 {
  245. res.Theme = v
  246. break
  247. }
  248. }
  249. }
  250. return nil
  251. })
  252. //get live metal
  253. group.Go(func() error {
  254. res.FansBadge, _ = s.dao.LiveMetal(errCtx, vmid)
  255. return nil
  256. })
  257. group.Wait()
  258. if topErr != nil || topPhoto == nil || topPhoto.LImg == "" {
  259. res.TopPhoto = s.c.Rule.TopPhoto
  260. } else {
  261. res.TopPhoto = topPhoto.LImg
  262. }
  263. return
  264. }
  265. // ThemeList get theme list.
  266. func (s *Service) ThemeList(c context.Context, mid int64) (data []*model.ThemeDetail, err error) {
  267. var theme *model.ThemeDetails
  268. if theme, err = s.dao.Theme(c, mid); err != nil {
  269. return
  270. }
  271. if theme == nil || len(theme.List) == 0 {
  272. data = _emptyThemeList
  273. return
  274. }
  275. data = theme.List
  276. return
  277. }
  278. // ThemeActive theme active.
  279. func (s *Service) ThemeActive(c context.Context, mid, themeID int64) (err error) {
  280. var (
  281. theme *model.ThemeDetails
  282. check bool
  283. )
  284. if theme, err = s.dao.Theme(c, mid); err != nil {
  285. return
  286. }
  287. if theme == nil || len(theme.List) == 0 {
  288. err = ecode.RequestErr
  289. return
  290. }
  291. for _, v := range theme.List {
  292. if v.ID == themeID {
  293. if v.IsActivated == 1 {
  294. err = ecode.NotModified
  295. return
  296. }
  297. check = true
  298. }
  299. }
  300. if !check {
  301. err = ecode.RequestErr
  302. return
  303. }
  304. if err = s.dao.ThemeActive(c, mid, themeID); err == nil {
  305. s.dao.DelCacheTheme(c, mid)
  306. }
  307. return
  308. }
  309. // Relation .
  310. func (s *Service) Relation(c context.Context, mid, vmid int64) (data *model.Relation) {
  311. data = &model.Relation{Relation: struct{}{}, BeRelation: struct{}{}}
  312. ip := metadata.String(c, metadata.RemoteIP)
  313. if mid == vmid {
  314. return
  315. }
  316. group, errCtx := errgroup.WithContext(c)
  317. group.Go(func() error {
  318. if relation, err := s.relation.Relation(errCtx, &relmdl.ArgRelation{Mid: mid, Fid: vmid, RealIP: ip}); err != nil {
  319. log.Error("Relation s.relation.Relation(Mid:%d,Fid:%d,%s) error %v", mid, vmid, ip, err)
  320. } else if relation != nil {
  321. data.Relation = relation
  322. }
  323. return nil
  324. })
  325. group.Go(func() error {
  326. if beRelation, err := s.relation.Relation(errCtx, &relmdl.ArgRelation{Mid: vmid, Fid: mid, RealIP: ip}); err != nil {
  327. log.Error("Relation s.relation.Relation(Mid:%d,Fid:%d,%s) error %v", vmid, mid, ip, err)
  328. } else if beRelation != nil {
  329. data.BeRelation = beRelation
  330. }
  331. return nil
  332. })
  333. group.Wait()
  334. return
  335. }
  336. // WebIndex web index.
  337. func (s *Service) WebIndex(c context.Context, mid, vmid int64, pn, ps int32) (data *model.WebIndex, err error) {
  338. data = new(model.WebIndex)
  339. group, errCtx := errgroup.WithContext(c)
  340. group.Go(func() error {
  341. info, infoErr := s.AccInfo(errCtx, mid, vmid)
  342. if infoErr != nil {
  343. return infoErr
  344. }
  345. data.Account = info
  346. return nil
  347. })
  348. group.Go(func() error {
  349. if setting, e := s.SettingInfo(errCtx, vmid); e == nil {
  350. data.Setting = setting
  351. }
  352. return nil
  353. })
  354. group.Go(func() error {
  355. if upArc, e := s.UpArcs(errCtx, vmid, pn, ps); e != nil {
  356. data.Archive = &model.WebArc{Archives: _emptyArcItem}
  357. } else {
  358. arc := &model.WebArc{
  359. Page: model.WebPage{Pn: pn, Ps: ps, Count: upArc.Count},
  360. Archives: upArc.List,
  361. }
  362. data.Archive = arc
  363. }
  364. return nil
  365. })
  366. err = group.Wait()
  367. return
  368. }