authorizer_info.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package authorization
  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. // AuthorizerInfoParam 获取授权帐号信息参数
  10. type AuthorizerInfoParam struct {
  11. ComponentAppId string `json:"component_appid"`
  12. AuthorizerAppId string `json:"authorizer_appid"`
  13. }
  14. // AuthorizerInfoResult 获取授权帐号信息结果
  15. type AuthorizerInfoResult struct {
  16. wxerr.Error
  17. AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
  18. AuthorizerInfo AuthorizerInfo `json:"authorizer_info"`
  19. }
  20. const (
  21. apiGetAuthorizerInfo = "https://apies.weixin.qq.com/cgi-bin/component/api_get_authorizer_info"
  22. )
  23. // GetAuthorizerInfo 获取授权帐号信息
  24. func GetAuthorizerInfo(componentAccessToken string, data AuthorizerInfoParam) (*AuthorizerInfoResult, error) {
  25. if componentAccessToken == "" {
  26. return nil, errors.New("获取授权帐号信息 第三方平台的 component_access_token 不能为空")
  27. }
  28. if data.ComponentAppId == "" {
  29. return nil, errors.New("获取授权帐号信息 第三方平台的 appid 不能为空")
  30. }
  31. if data.AuthorizerAppId == "" {
  32. return nil, errors.New("获取授权帐号信息 授权方 appid 不能为空")
  33. }
  34. queryParam := url.Values{}
  35. queryParam.Set("component_access_token", componentAccessToken)
  36. apiUrl, _ := request.ParseURL(apiGetAuthorizerInfo, &queryParam)
  37. resp, e2 := request.PostJSON(apiUrl.String(), data)
  38. if e2 != nil {
  39. return nil, e2
  40. }
  41. result := AuthorizerInfoResult{}
  42. if err := json.Unmarshal(resp, &result); err != nil {
  43. return nil, err
  44. }
  45. if result.Code != 0 {
  46. return &result, result.Error
  47. }
  48. return &result, nil
  49. }