manager.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strings"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. )
  9. const (
  10. _uidsURL = "/x/admin/manager/users/uids"
  11. _unamesURL = "/x/admin/manager/users/unames"
  12. )
  13. // Unames get unames by uid
  14. func (d *Dao) Unames(c context.Context, uids []int64) (res map[int64]string, err error) {
  15. var (
  16. param = url.Values{}
  17. uidStr = xstr.JoinInts(uids)
  18. unameURI = d.c.Host.Manager + _unamesURL
  19. )
  20. param.Set("uids", uidStr)
  21. var httpRes struct {
  22. Code int `json:"code"`
  23. Data map[int64]string `json:"data"`
  24. Message string `json:"message"`
  25. }
  26. err = d.hclient.Get(c, unameURI, "", param, &httpRes)
  27. if err != nil {
  28. log.Error("d.client.Get(%s) error(%v)", unameURI+"?"+param.Encode(), err)
  29. return
  30. }
  31. if httpRes.Code != 0 {
  32. log.Error("url(%s) error(%v), code(%d), message(%s)", unameURI+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
  33. }
  34. res = httpRes.Data
  35. return
  36. }
  37. // Uids get uids by unames
  38. func (d *Dao) Uids(c context.Context, names []string) (res map[string]int64, err error) {
  39. var (
  40. param = url.Values{}
  41. namesStr = strings.Join(names, ",")
  42. uidURI = d.c.Host.Manager + _uidsURL
  43. )
  44. param.Set("unames", namesStr)
  45. var httpRes struct {
  46. Code int `json:"code"`
  47. Data map[string]int64 `json:"data"`
  48. Message string `json:"message"`
  49. }
  50. err = d.hclient.Get(c, uidURI, "", param, &httpRes)
  51. if err != nil {
  52. log.Error("d.client.Get(%s) error(%v)", uidURI+"?"+param.Encode(), err)
  53. return
  54. }
  55. if httpRes.Code != 0 {
  56. log.Error("url(%s) error(%v), code(%d), message(%s)", uidURI+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
  57. }
  58. res = httpRes.Data
  59. return
  60. }
  61. // GetUIDByName 获取uid
  62. func (d *Dao) GetUIDByName(c context.Context, name string) (uid int64, err error) {
  63. var res map[string]int64
  64. if res, err = d.Uids(c, []string{name}); err != nil {
  65. return
  66. }
  67. if uid, ok := res[name]; ok {
  68. return uid, nil
  69. }
  70. return
  71. }
  72. // GetNameByUID 获取用户名
  73. func (d *Dao) GetNameByUID(c context.Context, uids []int64) (mcases map[int64][]interface{}, err error) {
  74. var res map[int64]string
  75. if res, err = d.Unames(c, uids); err != nil {
  76. return
  77. }
  78. mcases = make(map[int64][]interface{})
  79. for uid, uname := range res {
  80. mcases[uid] = []interface{}{uname}
  81. }
  82. return
  83. }