authorizer_info.go 2.2KB

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