query_auth.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 authorization
  13. import (
  14. "encoding/json"
  15. "errors"
  16. "net/url"
  17. "gitee.com/yansen_zh/wxcomponent/config"
  18. wxerr "gitee.com/yansen_zh/wxcomponent/errors"
  19. "gitee.com/yansen_zh/wxcomponent/utils/request"
  20. )
  21. // QueryAuthResult 获取授权信息结果
  22. type QueryAuthResult struct {
  23. wxerr.Error
  24. AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
  25. }
  26. const (
  27. apiQueryAuth = "https://apies.weixin.qq.com/cgi-bin/component/api_query_auth"
  28. )
  29. // GetQueryAuth 获取授权信息
  30. func GetQueryAuth(authorizationCode string) (*QueryAuthResult, error) {
  31. if authorizationCode == "" {
  32. return nil, errors.New("获取授权信息 授权码 不能为空")
  33. }
  34. queryParam := url.Values{}
  35. queryParam.Set("component_access_token", config.GetAccessToken())
  36. data := map[string]string{
  37. "component_appid": config.GetAppID(),
  38. "authorization_code": authorizationCode,
  39. }
  40. resp, e2 := request.PostJSON(apiQueryAuth, &queryParam, data)
  41. if e2 != nil {
  42. return nil, e2
  43. }
  44. result := QueryAuthResult{}
  45. if err := json.Unmarshal(resp, &result); err != nil {
  46. return nil, err
  47. }
  48. return &result, nil
  49. }