query_auth.go 1.6KB

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