set_authorizer_option.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. resp, e2 := request.PostJSON(apiSetAuthorizerOption, &queryParam, data)
  54. if e2 != nil {
  55. return nil, e2
  56. }
  57. result := SetAuthorizerOptionResult{}
  58. if err := json.Unmarshal(resp, &result); err != nil {
  59. return nil, err
  60. }
  61. return &result, nil
  62. }