get_authorizer_option.go 1.9KB

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