1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- /**
- * Copyright (c) 2022 Yansen Zhang
- * wxcomponent is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- * See the Mulan PSL v2 for more details.
- **/
-
- package authorization
-
- import (
- "encoding/json"
- "net/url"
-
- "gitee.com/yansen_zh/wxcomponent/config"
- "gitee.com/yansen_zh/wxcomponent/errors"
- "gitee.com/yansen_zh/wxcomponent/utils/request"
- )
-
- // QueryAuthResult 获取授权信息结果
- type QueryAuthResult struct {
- errors.Error
- AuthorizationInfo AuthorizationInfo `json:"authorization_info"`
- }
-
- const (
- apiQueryAuth = "https://apies.weixin.qq.com/cgi-bin/component/api_query_auth"
- )
-
- // GetQueryAuth 获取授权信息
- func GetQueryAuth(authorizationCode string) (*QueryAuthResult, error) {
- if authorizationCode == "" {
- return nil, errors.New("获取授权信息 授权码 不能为空")
- }
-
- queryParam := url.Values{}
- queryParam.Set("component_access_token", config.GetAccessToken())
-
- data := map[string]string{
- "component_appid": config.GetAppID(),
- "authorization_code": authorizationCode,
- }
-
- resp, e2 := request.PostJSON(apiQueryAuth, &queryParam, data)
- if e2 != nil {
- return nil, e2
- }
-
- result := QueryAuthResult{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- return &result, nil
- }
|