authorizer_info.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "gitee.com/yansen_zh/wechat-oplatform-sdk/api"
  7. "gitee.com/yansen_zh/wechat-oplatform-sdk/entity"
  8. "gitee.com/yansen_zh/wechat-oplatform-sdk/utils"
  9. "gitee.com/yansen_zh/wechat-oplatform-sdk/utils/request"
  10. )
  11. // AuthorizerInfoParam 获取授权帐号信息参数
  12. type AuthorizerInfoParam struct {
  13. ComponentAppId string `json:"component_appid"`
  14. AuthorizerAppId string `json:"authorizer_appid"`
  15. }
  16. // AuthorizerInfoResult 获取授权帐号信息结果
  17. type AuthorizerInfoResult struct {
  18. entity.Error
  19. AuthorizationInfo entity.AuthorizationInfo `json:"authorization_info"`
  20. AuthorizerInfo entity.AuthorizerInfo `json:"authorizer_info"`
  21. }
  22. // GetAuthorizerInfo 获取授权帐号信息
  23. func GetAuthorizerInfo(componentAccessToken string, param AuthorizerInfoParam) (*AuthorizerInfoResult, error) {
  24. if componentAccessToken == "" {
  25. return nil, errors.New("获取授权帐号信息 第三方平台的 component_access_token 不能为空")
  26. }
  27. if param.ComponentAppId == "" {
  28. return nil, errors.New("获取授权帐号信息 第三方平台的 appid 不能为空")
  29. }
  30. if param.AuthorizerAppId == "" {
  31. return nil, errors.New("获取授权帐号信息 授权方 appid 不能为空")
  32. }
  33. data, e1 := json.Marshal(param)
  34. if e1 != nil {
  35. return nil, e1
  36. }
  37. queryParam := url.Values{}
  38. queryParam.Set("component_access_token", componentAccessToken)
  39. apiUrl, _ := utils.ParseURL(api.GetAuthorizerInfo, &queryParam)
  40. resp, e2 := request.PostJSON(apiUrl.String(), 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. if result.Code != 0 {
  49. return &result, result.Error
  50. }
  51. return &result, nil
  52. }