authorizer_info.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 authorization
  13. import (
  14. "encoding/json"
  15. "net/url"
  16. "gitee.com/yansen_zh/wxcomponent/config"
  17. "gitee.com/yansen_zh/wxcomponent/errors"
  18. "gitee.com/yansen_zh/wxcomponent/utils/request"
  19. )
  20. // AuthorizerInfoResult 获取授权帐号信息结果
  21. type AuthorizerInfoResult struct {
  22. errors.Error
  23. AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
  24. AuthorizerInfo AuthorizerInfo `json:"authorizer_info"`
  25. }
  26. const (
  27. apiGetAuthorizerInfo = "https://apies.weixin.qq.com/cgi-bin/component/api_get_authorizer_info"
  28. )
  29. // GetAuthorizerInfo 获取授权帐号信息
  30. func GetAuthorizerInfo(authorizerAppID string) (*AuthorizerInfoResult, error) {
  31. if authorizerAppID == "" {
  32. return nil, errors.New("获取授权帐号信息 授权方 appid 不能为空")
  33. }
  34. queryParam := url.Values{}
  35. queryParam.Set("component_access_token", config.GetAccessToken())
  36. data := map[string]string{
  37. "component_appid": config.GetAppID(),
  38. "authorizer_appid": authorizerAppID,
  39. }
  40. resp, e2 := request.PostJSON(apiGetAuthorizerInfo, &queryParam, data)
  41. if e2 != nil {
  42. return nil, e2
  43. }
  44. result := AuthorizerInfoResult{}
  45. if err := json.Unmarshal(resp, &result); err != nil {
  46. return nil, err
  47. }
  48. return &result, nil
  49. }