set_authorizer_option.go 1.9KB

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