authorizer_list.go 1.7KB

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