authorizer_option.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "gitee.com/yansen_zh/wechat-oplatform-sdk/api"
  7. "gitee.com/yansen_zh/wechat-oplatform-sdk/entities"
  8. "gitee.com/yansen_zh/wechat-oplatform-sdk/utils"
  9. "gitee.com/yansen_zh/wechat-oplatform-sdk/utils/request"
  10. )
  11. // GetAuthorizerOptionParam 获取授权方选项信息参数
  12. type GetAuthorizerOptionParam struct {
  13. ComponentAppId string `json:"component_appid"`
  14. AuthorizerAppId string `json:"authorizer_appid"`
  15. OptionName string `json:"option_name"`
  16. }
  17. // GetAuthorizerOptionResult 获取授权方选项信息结果
  18. type GetAuthorizerOptionResult struct {
  19. entities.Error
  20. AuthorizerAppId string `json:"authorizer_appid"`
  21. OptionName string `json:"option_name"`
  22. OptionValue string `json:"option_value"`
  23. }
  24. // GetAuthorizerOption 获取授权方选项信息
  25. func GetAuthorizerOption(componentAccessToken string, param GetAuthorizerOptionParam) (*GetAuthorizerOptionResult, error) {
  26. if componentAccessToken == "" {
  27. return nil, errors.New("获取授权方选项信息 第三方平台的 component_access_token 不能为空")
  28. }
  29. if param.ComponentAppId == "" {
  30. return nil, errors.New("获取授权方选项信息 授权公众号或小程序的 appid 不能为空")
  31. }
  32. if param.AuthorizerAppId == "" {
  33. return nil, errors.New("获取授权方选项信息 第三方平台的 appid 不能为空")
  34. }
  35. if param.OptionName == "" {
  36. return nil, errors.New("获取授权方选项信息 选项名称 不能为空")
  37. }
  38. data, e1 := json.Marshal(param)
  39. if e1 != nil {
  40. return nil, e1
  41. }
  42. queryParam := url.Values{}
  43. queryParam.Set("component_access_token", componentAccessToken)
  44. apiUrl, _ := utils.ParseURL(api.GetAuthorizerOption, &queryParam)
  45. resp, e2 := request.PostJSON(apiUrl.String(), 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. if result.Code != 0 {
  54. return &result, result.Error
  55. }
  56. return &result, nil
  57. }
  58. // SetAuthorizerOptionParam 设置授权方选项信息参数
  59. type SetAuthorizerOptionParam struct {
  60. ComponentAppId string `json:"component_appid"`
  61. AuthorizerAppId string `json:"authorizer_appid"`
  62. OptionName string `json:"option_name"`
  63. OptionValue string `json:"option_value"`
  64. }
  65. // SetAuthorizerOptionResult 设置授权方选项信息结果
  66. type SetAuthorizerOptionResult struct {
  67. entities.Error
  68. }
  69. // SetAuthorizerOption 设置授权方选项信息
  70. func SetAuthorizerOption(componentAccessToken string, param SetAuthorizerOptionParam) (*SetAuthorizerOptionResult, error) {
  71. if componentAccessToken == "" {
  72. return nil, errors.New("设置授权方选项信息 第三方平台的 component_access_token 不能为空")
  73. }
  74. if param.ComponentAppId == "" {
  75. return nil, errors.New("设置授权方选项信息 授权公众号或小程序的 appid 不能为空")
  76. }
  77. if param.AuthorizerAppId == "" {
  78. return nil, errors.New("设置授权方选项信息 第三方平台的 appid 不能为空")
  79. }
  80. if param.OptionName == "" {
  81. return nil, errors.New("设置授权方选项信息 选项名称 不能为空")
  82. }
  83. if param.OptionValue == "" {
  84. return nil, errors.New("设置授权方选项信息 设置的选项值 不能为空")
  85. }
  86. data, e1 := json.Marshal(param)
  87. if e1 != nil {
  88. return nil, e1
  89. }
  90. queryParam := url.Values{}
  91. queryParam.Set("component_access_token", componentAccessToken)
  92. apiUrl, _ := utils.ParseURL(api.SetAuthorizerOption, &queryParam)
  93. resp, e2 := request.PostJSON(apiUrl.String(), data)
  94. if e2 != nil {
  95. return nil, e2
  96. }
  97. result := SetAuthorizerOptionResult{}
  98. if err := json.Unmarshal(resp, &result); err != nil {
  99. return nil, err
  100. }
  101. if result.Code != 0 {
  102. return &result, result.Error
  103. }
  104. return &result, nil
  105. }