authorizer_list.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (c) 2022 Yansen Zhang
  3. * wxcomponent is licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. **/
  12. package authorizer
  13. import (
  14. "encoding/json"
  15. "errors"
  16. "net/url"
  17. wxerr "gitee.com/yansen_zh/wxcomponent/errors"
  18. "gitee.com/yansen_zh/wxcomponent/utils/request"
  19. )
  20. // AuthorizerListParam 拉取所有已授权的帐号信息参数
  21. type AuthorizerListParam struct {
  22. ComponentAppId string `json:"component_appid"`
  23. Offset int `json:"offset"`
  24. Count int `json:"count"`
  25. }
  26. // AuthorizerListResult 拉取所有已授权的帐号信息结果
  27. type AuthorizerListResult struct {
  28. wxerr.Error
  29. TotalCount int `json:"total_count"`
  30. List []AuthorizerBasicInfo `json:"list"`
  31. }
  32. const (
  33. apiGetAuthorizerList = "https://apies.weixin.qq.com/cgi-bin/component/api_get_authorizer_list"
  34. )
  35. // AuthorizerList 拉取所有已授权的帐号信息
  36. func AuthorizerList(componentAccessToken string, data AuthorizerListParam) (*AuthorizerListResult, error) {
  37. if componentAccessToken == "" {
  38. return nil, errors.New("拉取所有已授权的帐号信息 第三方平台的 component_access_token 不能为空")
  39. }
  40. if data.ComponentAppId == "" {
  41. return nil, errors.New("拉取所有已授权的帐号信息 第三方平台的 appid 不能为空")
  42. }
  43. if data.Offset < 0 {
  44. data.Offset = 0
  45. }
  46. if data.Count < 0 {
  47. data.Count = 100
  48. }
  49. if data.Count > 500 {
  50. data.Count = 500
  51. }
  52. queryParam := url.Values{}
  53. queryParam.Set("component_access_token", componentAccessToken)
  54. apiUrl, _ := request.ParseURL(apiGetAuthorizerList, &queryParam)
  55. resp, e2 := request.PostJSON(apiUrl.String(), data)
  56. if e2 != nil {
  57. return nil, e2
  58. }
  59. result := AuthorizerListResult{}
  60. if err := json.Unmarshal(resp, &result); err != nil {
  61. return nil, err
  62. }
  63. if result.Code != 0 {
  64. return &result, result.Error
  65. }
  66. return &result, nil
  67. }