get_authorizer_option.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 authorizer
  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. // GetAuthorizerOptionResult 获取授权方选项信息结果
  21. type GetAuthorizerOptionResult struct {
  22. errors.Error
  23. AuthorizerAppID string `json:"authorizer_appid"`
  24. OptionName string `json:"option_name"`
  25. OptionValue string `json:"option_value"`
  26. }
  27. const (
  28. apiGetAuthorizerOption = "https://apies.weixin.qq.com/cgi-bin/component/api_get_authorizer_option"
  29. )
  30. // GetAuthorizerOption 获取授权方选项信息
  31. func GetAuthorizerOption(authorizerAppID string, optionName string) (*GetAuthorizerOptionResult, error) {
  32. if authorizerAppID == "" {
  33. return nil, errors.New("获取授权方选项信息 第三方平台的 appid 不能为空")
  34. }
  35. if optionName == "" {
  36. return nil, errors.New("获取授权方选项信息 选项名称 不能为空")
  37. }
  38. queryParam := url.Values{}
  39. queryParam.Set("component_access_token", config.GetAccessToken())
  40. data := map[string]string{
  41. "component_appid": config.GetAppID(),
  42. "authorizer_appid": authorizerAppID,
  43. "option_name": optionName,
  44. }
  45. resp, e2 := request.PostJSON(apiGetAuthorizerOption, &queryParam, data)
  46. if e2 != nil {
  47. return nil, e2
  48. }
  49. result := GetAuthorizerOptionResult{}
  50. if err := json.Unmarshal(resp, &result); err != nil {
  51. return nil, err
  52. }
  53. return &result, nil
  54. }