authorizer_list.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "gitee.com/yansen_zh/wechat-oplatform-sdk/api"
  7. "gitee.com/yansen_zh/wechat-oplatform-sdk/entity"
  8. "gitee.com/yansen_zh/wechat-oplatform-sdk/utils"
  9. "gitee.com/yansen_zh/wechat-oplatform-sdk/utils/request"
  10. )
  11. // AuthorizerListParam 拉取所有已授权的帐号信息参数
  12. type AuthorizerListParam struct {
  13. ComponentAppId string `json:"component_appid"`
  14. Offset int `json:"offset"`
  15. Count int `json:"count"`
  16. }
  17. // AuthorizerListResult 拉取所有已授权的帐号信息结果
  18. type AuthorizerListResult struct {
  19. entity.Error
  20. TotalCount float64 `json:"total_count"`
  21. List []entity.AuthorizerBasicInfo `json:"list"`
  22. }
  23. // AuthorizerList 拉取所有已授权的帐号信息
  24. func AuthorizerList(componentAccessToken string, param AuthorizerListParam) (*AuthorizerListResult, error) {
  25. if componentAccessToken == "" {
  26. return nil, errors.New("拉取所有已授权的帐号信息 第三方平台的 component_access_token 不能为空")
  27. }
  28. if param.ComponentAppId == "" {
  29. return nil, errors.New("拉取所有已授权的帐号信息 第三方平台的 appid 不能为空")
  30. }
  31. if param.Offset < 0 {
  32. param.Offset = 0
  33. }
  34. if param.Count < 0 {
  35. param.Count = 100
  36. }
  37. if param.Count > 500 {
  38. param.Count = 500
  39. }
  40. data, e1 := json.Marshal(param)
  41. if e1 != nil {
  42. return nil, e1
  43. }
  44. queryParam := url.Values{}
  45. queryParam.Set("component_access_token", componentAccessToken)
  46. apiUrl, _ := utils.ParseURL(api.GetAuthorizerList, &queryParam)
  47. resp, e2 := request.PostJSON(apiUrl.String(), data)
  48. if e2 != nil {
  49. return nil, e2
  50. }
  51. result := AuthorizerListResult{}
  52. if err := json.Unmarshal(resp, &result); err != nil {
  53. return nil, err
  54. }
  55. if result.Code != 0 {
  56. return &result, result.Error
  57. }
  58. return &result, nil
  59. }