golang 的 rabbitmq 消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 3.6 KiB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package arkid
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "applet/app/cfg"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "applet/app/utils/logx"
  10. )
  11. func arkidLogin(args map[string]interface{}) ([]byte, error) {
  12. url := cfg.ArkID.Url + "/siteapi/v1/ucenter/login/"
  13. b, err := json.Marshal(args)
  14. if err != nil {
  15. return nil, logx.Error(err)
  16. }
  17. var d []byte
  18. d, err = utils.CurlPost(url, b, nil)
  19. if err != nil {
  20. return nil, logx.Error(err)
  21. }
  22. return d, nil
  23. }
  24. func arkidLogout(token string) ([]byte, error) {
  25. // fmt.Println(cfg.ArkID.Url)
  26. url := cfg.ArkID.Url + "/siteapi/v1/revoke/token/"
  27. h := map[string]string{"authorization": fmt.Sprintf("token %s", token)}
  28. d, err := utils.CurlPost(url, "", h)
  29. if err != nil {
  30. return nil, logx.Error(err)
  31. }
  32. return d, nil
  33. }
  34. func arkidUserInfo(token string) ([]byte, error) {
  35. url := cfg.ArkID.Url + "/siteapi/v1/auth/token/"
  36. h := map[string]string{"authorization": fmt.Sprintf("token %s", token)}
  37. d, err := utils.CurlGet(url, h)
  38. if err != nil {
  39. return nil, logx.Error(err)
  40. }
  41. return d, nil
  42. }
  43. func arkidRegister(args map[string]interface{}) ([]byte, error) {
  44. url := cfg.ArkID.Url + "/siteapi/oneid/user/"
  45. b, err := json.Marshal(args)
  46. if err != nil {
  47. return nil, logx.Error(err)
  48. }
  49. admin, err := getArkIDAdmin()
  50. if err != nil {
  51. return nil, logx.Error(err)
  52. }
  53. h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
  54. var d []byte
  55. d, err = utils.CurlPost(url, b, h)
  56. if err != nil {
  57. return nil, logx.Error(err)
  58. }
  59. return d, nil
  60. }
  61. func arkidAppAccessWhiteList(args map[string]interface{}, permName string) ([]byte, error) {
  62. if permName == "" {
  63. return nil, errors.New("The perm_name arg must required")
  64. }
  65. path := fmt.Sprintf("/siteapi/oneid/perm/%s/owner/", permName)
  66. url := cfg.ArkID.Url + path
  67. b, err := json.Marshal(args)
  68. if err != nil {
  69. return nil, logx.Error(err)
  70. }
  71. admin, err := getArkIDAdmin()
  72. if err != nil {
  73. return nil, logx.Error(err)
  74. }
  75. // fmt.Println(admin.Token)
  76. h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
  77. var d []byte
  78. d, err = utils.CurlPatch(url, b, h)
  79. if err != nil {
  80. return nil, logx.Error(err)
  81. }
  82. return d, nil
  83. }
  84. func arkidUserDelete(username string) ([]byte, error) {
  85. if username == "" {
  86. return nil, errors.New("The username arg must required")
  87. }
  88. path := fmt.Sprintf("/siteapi/oneid/user/%s/", username)
  89. url := cfg.ArkID.Url + path
  90. admin, err := getArkIDAdmin()
  91. if err != nil {
  92. return nil, logx.Error(err)
  93. }
  94. // fmt.Println(admin.Token)
  95. h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
  96. var d []byte
  97. d, err = utils.CurlDelete(url, nil, h)
  98. if err != nil {
  99. return nil, logx.Error(err)
  100. }
  101. return d, nil
  102. }
  103. func arkidUserUpdate(username string, args map[string]interface{}) ([]byte, error) {
  104. if username == "" {
  105. return nil, errors.New("The username arg must required")
  106. }
  107. b, err := json.Marshal(args)
  108. if err != nil {
  109. return nil, logx.Error(err)
  110. }
  111. path := fmt.Sprintf("/siteapi/oneid/user/%s/", username)
  112. url := cfg.ArkID.Url + path
  113. var admin *ArkIDUser
  114. admin, err = getArkIDAdmin()
  115. if err != nil {
  116. return nil, logx.Error(err)
  117. }
  118. h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
  119. d, err := utils.CurlPatch(url, b, h)
  120. if err != nil {
  121. return nil, logx.Error(err)
  122. }
  123. return d, nil
  124. }
  125. func getArkIDAdmin() (*ArkIDUser, error) {
  126. c, err := cache.Bytes(cache.Get(ARKID_ADMIN_TOKEN))
  127. if err != nil {
  128. logx.Error(err)
  129. }
  130. if c != nil && err == nil {
  131. admin := new(ArkIDUser)
  132. if err = json.Unmarshal(c, admin); err != nil {
  133. return admin, err
  134. }
  135. return admin, nil
  136. }
  137. return Init()
  138. }