set_authorizer_option.go 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. wxerr "gitee.com/yansen_zh/wxcomponent/errors"
  18. "gitee.com/yansen_zh/wxcomponent/utils/request"
  19. )
  20. // SetAuthorizerOptionParam 设置授权方选项信息参数
  21. type SetAuthorizerOptionParam struct {
  22. ComponentAppId string `json:"component_appid"`
  23. AuthorizerAppId string `json:"authorizer_appid"`
  24. OptionName string `json:"option_name"`
  25. OptionValue string `json:"option_value"`
  26. }
  27. // SetAuthorizerOptionResult 设置授权方选项信息结果
  28. type SetAuthorizerOptionResult struct {
  29. wxerr.Error
  30. }
  31. const (
  32. apiSetAuthorizerOption = "https://apies.weixin.qq.com/cgi-bin/component/api_set_authorizer_option"
  33. )
  34. // SetAuthorizerOption 设置授权方选项信息
  35. func SetAuthorizerOption(componentAccessToken string, data SetAuthorizerOptionParam) (*SetAuthorizerOptionResult, 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. if data.OptionName == "" {
  46. return nil, errors.New("设置授权方选项信息 选项名称 不能为空")
  47. }
  48. if data.OptionValue == "" {
  49. return nil, errors.New("设置授权方选项信息 设置的选项值 不能为空")
  50. }
  51. queryParam := url.Values{}
  52. queryParam.Set("component_access_token", componentAccessToken)
  53. apiUrl, _ := request.ParseURL(apiSetAuthorizerOption, &queryParam)
  54. resp, e2 := request.PostJSON(apiUrl.String(), data)
  55. if e2 != nil {
  56. return nil, e2
  57. }
  58. result := SetAuthorizerOptionResult{}
  59. if err := json.Unmarshal(resp, &result); err != nil {
  60. return nil, err
  61. }
  62. if result.Code != 0 {
  63. return &result, result.Error
  64. }
  65. return &result, nil
  66. }