tree.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "go-common/app/admin/ep/melloi/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _treeTokenURI = "/v1/auth"
  11. _treeNodeURI = "/v1/node/apptree"
  12. _ajSessionID = "_AJSESSIONID"
  13. _treeAdminURI = "/v1/node/role/"
  14. _treeRoleAppURI = "/v1/node/role/app"
  15. _treeRspCode = 90000
  16. )
  17. // QueryServiceTreeToken query service tree token by sessionID
  18. func (d *Dao) QueryServiceTreeToken(c context.Context, sessionID string) (token string, err error) {
  19. var (
  20. req *http.Request
  21. tokenURL = d.c.ServiceTree.Host + _treeTokenURI
  22. res struct {
  23. Code int `json:"code"`
  24. Data *model.TokenResponse `json:"data"`
  25. Message string `json:"message"`
  26. Status int `json:"status"`
  27. }
  28. )
  29. if req, err = d.newRequest(http.MethodGet, tokenURL, nil); err != nil {
  30. return
  31. }
  32. req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
  33. if err = d.httpClient.Do(c, req, &res); err != nil {
  34. log.Error("d.Token url(%s) res($s) error(%v)", tokenURL, res, err)
  35. return
  36. }
  37. if res.Code != _treeRspCode {
  38. err = ecode.MelloiTreeRequestErr
  39. log.Error("d.Tree.Response url(%s) resCode(%s) error(%v)", tokenURL, res.Code, err)
  40. return
  41. }
  42. token = res.Data.Token
  43. return
  44. }
  45. // QueryUserTree query user tree by user token
  46. func (d *Dao) QueryUserTree(c context.Context, token string) (tree *model.UserTree, err error) {
  47. var (
  48. url = d.c.ServiceTree.Host + _treeNodeURI
  49. req *http.Request
  50. res = &model.TreeResponse{}
  51. )
  52. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  53. return
  54. }
  55. req.Header.Set("Content-Type", "application/json")
  56. req.Header.Set("X-Authorization-Token", token)
  57. if err = d.httpClient.Do(c, req, &res); err != nil {
  58. log.Error("d.Token url(%s) error(%v)", url, err)
  59. err = ecode.MerlinTreeRequestErr
  60. return
  61. }
  62. if res.Code != _treeRspCode {
  63. err = ecode.MelloiTreeRequestErr
  64. log.Error("Get tree error(%v)", err)
  65. return
  66. }
  67. tree = &res.Data
  68. return
  69. }
  70. // QueryUserRoleApp query User role app
  71. func (d *Dao) QueryUserRoleApp(c context.Context, token string) (ra []*model.RoleApp, err error) {
  72. var (
  73. url = d.c.ServiceTree.Host + _treeRoleAppURI
  74. req *http.Request
  75. res = &model.TreeRoleApp{}
  76. )
  77. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  78. return
  79. }
  80. req.Header.Set("Context-Type", "application/json")
  81. req.Header.Set("X-Authorization-Token", token)
  82. if err = d.httpClient.Do(c, req, &res); err != nil {
  83. log.Error("d.Token url(%s) error(%v)", url, err)
  84. return
  85. }
  86. if res.Code != _treeRspCode {
  87. err = ecode.MelloiTreeRequestErr
  88. log.Error("get tree admin error(%v)", err)
  89. return
  90. }
  91. ra = res.Data
  92. return
  93. }
  94. //QueryTreeAdmin query tree admin
  95. func (d *Dao) QueryTreeAdmin(c context.Context, path string, token string) (ta *model.TreeAdminResponse, err error) {
  96. var (
  97. url = d.c.ServiceTree.Host + _treeAdminURI + path
  98. req *http.Request
  99. )
  100. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  101. return
  102. }
  103. req.Header.Set("Context-Type", "application/json")
  104. req.Header.Set("X-Authorization-Token", token)
  105. if err = d.httpClient.Do(c, req, &ta); err != nil {
  106. log.Error("d.Token url(%s) error(%v)", url, err)
  107. return
  108. }
  109. if ta.Code != _treeRspCode {
  110. err = ecode.MelloiTreeRequestErr
  111. log.Error("get tree admin error(%v)", err)
  112. return
  113. }
  114. return
  115. }