12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package service
-
- import (
- "encoding/json"
- "errors"
- "net/url"
-
- "gitee.com/yansen_zh/wechat-oplatform-sdk/api"
- "gitee.com/yansen_zh/wechat-oplatform-sdk/entities"
- "gitee.com/yansen_zh/wechat-oplatform-sdk/utils"
- "gitee.com/yansen_zh/wechat-oplatform-sdk/utils/request"
- )
-
- // QueryAuthParam 获取授权信息参数
- type QueryAuthParam struct {
- ComponentAppId string `json:"component_appid"`
- AuthorizationCode string `json:"authorization_code"`
- }
-
- // QueryAuthResult 获取授权信息结果
- type QueryAuthResult struct {
- entities.Error
- AuthorizationInfo entities.AuthorizationInfo `json:"authorization_info"`
- }
-
- // QueryAuth 获取授权信息
- func QueryAuth(componentAccessToken string, param QueryAuthParam) (*QueryAuthResult, error) {
- if componentAccessToken == "" {
- return nil, errors.New("获取授权信息 第三方平台的 component_access_token 不能为空")
- }
-
- if param.ComponentAppId == "" {
- return nil, errors.New("获取授权信息 第三方平台的 appid 不能为空")
- }
-
- if param.AuthorizationCode == "" {
- return nil, errors.New("获取授权信息 授权码 不能为空")
- }
-
- data, e1 := json.Marshal(param)
- if e1 != nil {
- return nil, e1
- }
-
- queryParam := url.Values{}
- queryParam.Set("component_access_token", componentAccessToken)
-
- apiUrl, _ := utils.ParseURL(api.QueryAuth, &queryParam)
- resp, e2 := request.PostJSON(apiUrl.String(), data)
- if e2 != nil {
- return nil, e2
- }
-
- result := QueryAuthResult{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- if result.Code != 0 {
- return &result, result.Error
- }
-
- return &result, nil
- }
|