role.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package http
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/admin/main/aegis/model/task"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _getUIDs = "/x/admin/manager/users/uids"
  12. _getUname = "/x/admin/manager/users/unames"
  13. _getUdepartment = "/x/admin/manager/users/udepts"
  14. _getRole = "/x/admin/manager/internal/user/role"
  15. _getRoles = "/x/admin/manager/internal/user/roles"
  16. )
  17. // GetRole 获取用户对应业务下的角色
  18. func (d *Dao) GetRole(c context.Context, bid, uid int64) (roles []*task.Role, err error) {
  19. roles = []*task.Role{}
  20. params := url.Values{}
  21. params.Set("bid", strconv.FormatInt(bid, 10))
  22. params.Set("uid", strconv.FormatInt(uid, 10))
  23. res := new(struct {
  24. Code int `json:"code"`
  25. Data []*task.Role `json:"data"`
  26. })
  27. if err = d.clientR.Get(c, d.c.Host.Manager+_getRole, "", params, res); err != nil {
  28. log.Error("GetRole error(%v) url(%s) params(%s)", err, d.c.Host.Manager+_getRole, params.Encode())
  29. return
  30. }
  31. if res.Code != 0 {
  32. log.Error("GetRole request failed res(%+v) url(%s) params(%s)", res, d.c.Host.Manager+_getRole, params.Encode())
  33. return
  34. }
  35. if len(res.Data) == 0 {
  36. return
  37. }
  38. roles = res.Data
  39. return
  40. }
  41. // GetUserRoles 获取用户在所有业务下的角色
  42. func (d *Dao) GetUserRoles(c context.Context, uid int64) (roles []*task.Role, err error) {
  43. roles = []*task.Role{}
  44. params := url.Values{}
  45. params.Set("uid", strconv.FormatInt(uid, 10))
  46. res := new(struct {
  47. Code int `json:"code"`
  48. Data []*task.Role `json:"data"`
  49. })
  50. if err = d.clientR.Get(c, d.c.Host.Manager+_getRoles, "", params, res); err != nil {
  51. log.Error("GetUserRoles error(%v) url(%s) params(%s)", err, d.c.Host.Manager+_getRole, params.Encode())
  52. return
  53. }
  54. if res.Code != 0 {
  55. log.Error("GetUserRoles request failed res(%+v) url(%s) params(%s)", res, d.c.Host.Manager+_getRole, params.Encode())
  56. return
  57. }
  58. if len(res.Data) == 0 {
  59. return
  60. }
  61. roles = res.Data
  62. return
  63. }
  64. // GetUnames .
  65. func (d *Dao) GetUnames(c context.Context, uids []int64) (unames map[int64]string, err error) {
  66. unames = map[int64]string{}
  67. if len(uids) == 0 {
  68. return
  69. }
  70. params := url.Values{}
  71. params.Set("uids", xstr.JoinInts(uids))
  72. res := new(struct {
  73. Code int `json:"code"`
  74. Data map[int64]string `json:"data"`
  75. })
  76. if err = d.clientR.Get(c, d.c.Host.Manager+_getUname, "", params, res); err != nil {
  77. log.Error("GetUnames error(%v) url(%s) params(%s)", err, d.c.Host.Manager+_getUname, params.Encode())
  78. return
  79. }
  80. if res.Code != 0 {
  81. log.Error("GetUnames request failed res(%+v) url(%s) params(%s)", res, d.c.Host.Manager+_getUname, params.Encode())
  82. return
  83. }
  84. if len(res.Data) == 0 {
  85. return
  86. }
  87. unames = res.Data
  88. return
  89. }
  90. // GetUIDs .
  91. func (d *Dao) GetUIDs(c context.Context, unames string) (uids map[string]int64, err error) {
  92. uids = map[string]int64{}
  93. params := url.Values{}
  94. params.Set("unames", unames)
  95. res := new(struct {
  96. Code int `json:"code"`
  97. Data map[string]int64 `json:"data"`
  98. })
  99. if err = d.clientR.Get(c, d.c.Host.Manager+_getUIDs, "", params, res); err != nil {
  100. log.Error("GetUIDs error(%v) url(%s) params(%s)", err, d.c.Host.Manager+_getUname, params.Encode())
  101. return
  102. }
  103. if res.Code != 0 {
  104. log.Error("GetUIDs request failed res(%+v) url(%s) params(%s)", res, d.c.Host.Manager+_getUname, params.Encode())
  105. return
  106. }
  107. if len(res.Data) == 0 {
  108. return
  109. }
  110. uids = res.Data
  111. return
  112. }
  113. // GetUdepartment .
  114. func (d *Dao) GetUdepartment(c context.Context, uids []int64) (udepartment map[int64]string, err error) {
  115. udepartment = map[int64]string{}
  116. if len(uids) == 0 {
  117. return
  118. }
  119. params := url.Values{}
  120. params.Set("uids", xstr.JoinInts(uids))
  121. res := new(struct {
  122. Code int `json:"code"`
  123. Data map[int64]string `json:"data"`
  124. })
  125. if err = d.clientR.Get(c, d.c.Host.Manager+_getUdepartment, "", params, res); err != nil {
  126. log.Error("GetUdepartment error(%v) url(%s) params(%s)", err, d.c.Host.Manager+_getUdepartment, params.Encode())
  127. return
  128. }
  129. if res.Code != 0 {
  130. log.Error("GetUdepartment request failed res(%+v) url(%s) params(%s)", res, d.c.Host.Manager+_getUname, params.Encode())
  131. return
  132. }
  133. if len(res.Data) == 0 {
  134. return
  135. }
  136. udepartment = res.Data
  137. return
  138. }