query_auth.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // QueryAuthParam 获取授权信息参数
  12. type QueryAuthParam struct {
  13. ComponentAppId string `json:"component_appid"`
  14. AuthorizationCode string `json:"authorization_code"`
  15. }
  16. // QueryAuthResult 获取授权信息结果
  17. type QueryAuthResult struct {
  18. entities.Error
  19. AuthorizationInfo entities.AuthorizationInfo `json:"authorization_info"`
  20. }
  21. // QueryAuth 获取授权信息
  22. func QueryAuth(componentAccessToken string, param QueryAuthParam) (*QueryAuthResult, error) {
  23. if componentAccessToken == "" {
  24. return nil, errors.New("获取授权信息 第三方平台的 component_access_token 不能为空")
  25. }
  26. if param.ComponentAppId == "" {
  27. return nil, errors.New("获取授权信息 第三方平台的 appid 不能为空")
  28. }
  29. if param.AuthorizationCode == "" {
  30. return nil, errors.New("获取授权信息 授权码 不能为空")
  31. }
  32. data, e1 := json.Marshal(param)
  33. if e1 != nil {
  34. return nil, e1
  35. }
  36. queryParam := url.Values{}
  37. queryParam.Set("component_access_token", componentAccessToken)
  38. apiUrl, _ := utils.ParseURL(api.QueryAuth, &queryParam)
  39. resp, e2 := request.PostJSON(apiUrl.String(), data)
  40. if e2 != nil {
  41. return nil, e2
  42. }
  43. result := QueryAuthResult{}
  44. if err := json.Unmarshal(resp, &result); err != nil {
  45. return nil, err
  46. }
  47. if result.Code != 0 {
  48. return &result, result.Error
  49. }
  50. return &result, nil
  51. }