dao.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package account
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-interface/conf"
  7. account "go-common/app/service/main/account/model"
  8. accrpc "go-common/app/service/main/account/rpc/client"
  9. memberrpc "go-common/app/service/main/member/api/gorpc"
  10. blockmodel "go-common/app/service/main/member/model/block"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/metadata"
  15. "github.com/pkg/errors"
  16. )
  17. // Dao is account dao.
  18. type Dao struct {
  19. client *bm.Client
  20. // rpc
  21. accRPC *accrpc.Service3
  22. memberRPC *memberrpc.Service
  23. }
  24. // New account dao.
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: bm.NewClient(c.HTTPClient),
  28. accRPC: accrpc.New3(c.AccountRPC),
  29. memberRPC: memberrpc.New(c.MemberRPC),
  30. }
  31. return
  32. }
  33. // BlockTime get user blocktime
  34. func (d *Dao) BlockTime(c context.Context, mid int64) (blockTime int64, err error) {
  35. info, err := d.memberRPC.BlockInfo(c, &blockmodel.RPCArgInfo{MID: mid})
  36. if err != nil {
  37. err = errors.Wrapf(err, "%v", mid)
  38. return
  39. }
  40. if info.EndTime > 0 {
  41. blockTime = info.EndTime
  42. }
  43. return
  44. }
  45. // Profile3 get profile
  46. func (d *Dao) Profile3(c context.Context, mid int64) (card *account.ProfileStat, err error) {
  47. arg := &account.ArgMid{Mid: mid}
  48. if card, err = d.accRPC.ProfileWithStat3(c, arg); err != nil {
  49. err = errors.Wrapf(err, "%v", arg)
  50. }
  51. return
  52. }
  53. // Card get card
  54. func (d *Dao) Card(c context.Context, mid int64) (card *account.Card, err error) {
  55. if card, err = d.accRPC.Card3(c, &account.ArgMid{Mid: mid}); err != nil {
  56. err = errors.Wrapf(err, "%v", mid)
  57. }
  58. return
  59. }
  60. // ProfileByName3 rpc card get by name
  61. func (d *Dao) ProfileByName3(c context.Context, name string) (card *account.ProfileStat, err error) {
  62. infos, err := d.accRPC.InfosByName3(c, &account.ArgNames{Names: []string{name}})
  63. if err != nil {
  64. err = errors.Wrapf(err, "%v", name)
  65. return
  66. }
  67. if len(infos) == 0 {
  68. err = ecode.NothingFound
  69. return
  70. }
  71. for mid := range infos {
  72. card, err = d.Profile3(c, mid)
  73. break
  74. }
  75. return
  76. }
  77. // Infos3 rpc info get by mids .
  78. func (d *Dao) Infos3(c context.Context, mids []int64) (res map[int64]*account.Info, err error) {
  79. arg := &account.ArgMids{Mids: mids}
  80. if res, err = d.accRPC.Infos3(c, arg); err != nil {
  81. err = errors.Wrapf(err, "%v", arg)
  82. }
  83. return
  84. }
  85. // Relations3 relations.
  86. func (d *Dao) Relations3(c context.Context, owners []int64, mid int64) (follows map[int64]bool) {
  87. if len(owners) == 0 {
  88. return nil
  89. }
  90. follows = make(map[int64]bool, len(owners))
  91. for _, owner := range owners {
  92. follows[owner] = false
  93. }
  94. var (
  95. am map[int64]*account.Relation
  96. err error
  97. ip = metadata.String(c, metadata.RemoteIP)
  98. )
  99. arg := &account.ArgRelations{Owners: owners, Mid: mid, RealIP: ip}
  100. if am, err = d.accRPC.Relations3(c, arg); err != nil {
  101. log.Error("d.accRPC.Relations2(%v) error(%v)", arg, err)
  102. return
  103. }
  104. for i, a := range am {
  105. if _, ok := follows[i]; ok {
  106. follows[i] = a.Following
  107. }
  108. }
  109. return
  110. }
  111. // RichRelations3 rich relations.
  112. func (d *Dao) RichRelations3(c context.Context, owner, mid int64) (rel int, err error) {
  113. var (
  114. res map[int64]int
  115. ip = metadata.String(c, metadata.RemoteIP)
  116. )
  117. arg := &account.ArgRichRelation{Mids: []int64{mid}, Owner: owner, RealIP: ip}
  118. if res, err = d.accRPC.RichRelations3(c, arg); err != nil {
  119. err = errors.Wrapf(err, "%v", arg)
  120. return
  121. }
  122. if r, ok := res[mid]; ok {
  123. rel = r
  124. }
  125. return
  126. }
  127. // Cards3 is
  128. func (d *Dao) Cards3(c context.Context, mids []int64) (res map[int64]*account.Card, err error) {
  129. arg := &account.ArgMids{Mids: mids}
  130. if res, err = d.accRPC.Cards3(c, arg); err != nil {
  131. err = errors.Wrapf(err, "%v")
  132. }
  133. return
  134. }
  135. // UserCheck 各种入口白名单
  136. // https://www.tapd.cn/20055921/prong/stories/view/1120055921001066980 动态互推TAPD在此!!
  137. func (d *Dao) UserCheck(c context.Context, mid int64, checkURL string) (ok bool, err error) {
  138. params := url.Values{}
  139. params.Set("uid", strconv.FormatInt(mid, 10))
  140. var res struct {
  141. Code int `json:"code"`
  142. Data struct {
  143. Status int `json:"status"`
  144. } `json:"data"`
  145. }
  146. if err = d.client.Get(c, checkURL, "", params, &res); err != nil {
  147. return
  148. }
  149. if res.Code != ecode.OK.Code() {
  150. err = errors.Wrap(ecode.Int(res.Code), checkURL+"?"+params.Encode())
  151. return
  152. }
  153. if res.Data.Status == 1 {
  154. ok = true
  155. }
  156. return
  157. }
  158. // RedDot 我的页小红点逻辑
  159. func (d *Dao) RedDot(c context.Context, mid int64, redDotURL string) (ok bool, err error) {
  160. params := url.Values{}
  161. params.Set("mid", strconv.FormatInt(mid, 10))
  162. var res struct {
  163. Code int `json:"code"`
  164. Data struct {
  165. RedDot bool `json:"red_dot"`
  166. } `json:"data"`
  167. }
  168. if err = d.client.Get(c, redDotURL, "", params, &res); err != nil {
  169. return
  170. }
  171. if res.Code != ecode.OK.Code() {
  172. err = errors.Wrap(ecode.Int(res.Code), redDotURL+"?"+params.Encode())
  173. return
  174. }
  175. log.Warn("reddot response mid(%d) url(%s) res(%t)", mid, redDotURL+"?"+params.Encode(), res.Data.RedDot)
  176. ok = res.Data.RedDot
  177. return
  178. }