get_authorizer_option.go 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package authorizer
  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. // GetAuthorizerOptionParam 获取授权方选项信息参数
  10. type GetAuthorizerOptionParam struct {
  11. ComponentAppId string `json:"component_appid"`
  12. AuthorizerAppId string `json:"authorizer_appid"`
  13. OptionName string `json:"option_name"`
  14. }
  15. // GetAuthorizerOptionResult 获取授权方选项信息结果
  16. type GetAuthorizerOptionResult struct {
  17. wxerr.Error
  18. AuthorizerAppId string `json:"authorizer_appid"`
  19. OptionName string `json:"option_name"`
  20. OptionValue string `json:"option_value"`
  21. }
  22. const (
  23. apiGetAuthorizerOption = "https://apies.weixin.qq.com/cgi-bin/component/api_get_authorizer_option"
  24. )
  25. // GetAuthorizerOption 获取授权方选项信息
  26. func GetAuthorizerOption(componentAccessToken string, data GetAuthorizerOptionParam) (*GetAuthorizerOptionResult, error) {
  27. if componentAccessToken == "" {
  28. return nil, errors.New("获取授权方选项信息 第三方平台的 component_access_token 不能为空")
  29. }
  30. if data.ComponentAppId == "" {
  31. return nil, errors.New("获取授权方选项信息 授权公众号或小程序的 appid 不能为空")
  32. }
  33. if data.AuthorizerAppId == "" {
  34. return nil, errors.New("获取授权方选项信息 第三方平台的 appid 不能为空")
  35. }
  36. if data.OptionName == "" {
  37. return nil, errors.New("获取授权方选项信息 选项名称 不能为空")
  38. }
  39. queryParam := url.Values{}
  40. queryParam.Set("component_access_token", componentAccessToken)
  41. apiUrl, _ := request.ParseURL(apiGetAuthorizerOption, &queryParam)
  42. resp, e2 := request.PostJSON(apiUrl.String(), data)
  43. if e2 != nil {
  44. return nil, e2
  45. }
  46. result := GetAuthorizerOptionResult{}
  47. if err := json.Unmarshal(resp, &result); err != nil {
  48. return nil, err
  49. }
  50. if result.Code != 0 {
  51. return &result, result.Error
  52. }
  53. return &result, nil
  54. }