passport.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/member/model"
  5. "net/url"
  6. "strconv"
  7. "sync"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "go-common/library/xstr"
  12. )
  13. const (
  14. _updateUname = "/intranet/acc/updateUname"
  15. _queryByMids = "/intranet/acc/queryByMids"
  16. )
  17. // UpdateUname is.
  18. func (d *Dao) UpdateUname(ctx context.Context, mid int64, name string) error {
  19. ip := metadata.String(ctx, metadata.RemoteIP)
  20. params := url.Values{}
  21. params.Set("mid", strconv.FormatInt(mid, 10))
  22. params.Set("uname", name)
  23. var res struct {
  24. Code int `json:"code"`
  25. }
  26. if err := d.passportClient.Post(ctx, d.upUnameURL, ip, params, &res); err != nil {
  27. return err
  28. }
  29. if res.Code != 0 {
  30. log.Error("Failed to update uname(%+v) code(%+v)", params, res.Code)
  31. return parsePassportEcode(res.Code)
  32. }
  33. return nil
  34. }
  35. func parsePassportEcode(pecode int) error {
  36. switch pecode {
  37. case -618:
  38. return ecode.UpdateUnameRepeated
  39. case -617:
  40. return ecode.UpdateUnameHadLocked
  41. case -601:
  42. return ecode.UpdateUnameFormat
  43. }
  44. log.Error("Unrecognized passport ecode: %d", pecode)
  45. return ecode.Int(pecode)
  46. }
  47. // PassportQueryByMids is.
  48. func (d *Dao) PassportQueryByMids(ctx context.Context, mids []int64) (map[int64]*model.PassportQueryByMidResult, error) {
  49. ip := metadata.String(ctx, metadata.RemoteIP)
  50. params := url.Values{}
  51. params.Set("mids", xstr.JoinInts(mids))
  52. var res struct {
  53. Code int `json:"code"`
  54. Cards map[string]*model.PassportQueryByMidResult `json:"cards"`
  55. }
  56. if err := d.passportClient.Get(ctx, d.queryByMidsURL, ip, params, &res); err != nil {
  57. return nil, err
  58. }
  59. if res.Code != 0 {
  60. log.Error("Failed to QueryByMid(%+v) code(%+v)", params, res.Code)
  61. return nil, ecode.Int(res.Code)
  62. }
  63. result := make(map[int64]*model.PassportQueryByMidResult, len(res.Cards))
  64. for _, card := range res.Cards {
  65. result[card.Mid] = card
  66. }
  67. return result, nil
  68. }
  69. // PassportQueryByMidsChunked is
  70. func (d *Dao) PassportQueryByMidsChunked(ctx context.Context, mids []int64, chunkSize int) (map[int64]*model.PassportQueryByMidResult, error) {
  71. chunkedMids := func() [][]int64 {
  72. divided := make([][]int64, 0)
  73. for i := 0; i < len(mids); i += chunkSize {
  74. end := i + chunkSize
  75. if end > len(mids) {
  76. end = len(mids)
  77. }
  78. divided = append(divided, mids[i:end])
  79. }
  80. return divided
  81. }()
  82. lock := sync.Mutex{}
  83. result := make(map[int64]*model.PassportQueryByMidResult, len(mids))
  84. wg := sync.WaitGroup{}
  85. for _, chunk := range chunkedMids {
  86. wg.Add(1)
  87. go func(chunk []int64) {
  88. defer wg.Done()
  89. res, err := d.PassportQueryByMids(ctx, chunk)
  90. if err != nil {
  91. log.Error("Failed to get passport query by mids: %+v: %+v", chunk, err)
  92. return
  93. }
  94. lock.Lock()
  95. for k, v := range res {
  96. result[k] = v
  97. }
  98. lock.Unlock()
  99. }(chunk)
  100. }
  101. wg.Wait()
  102. return result, nil
  103. }