1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package service
-
- import (
- "encoding/json"
- "errors"
- "net/url"
-
- "gitee.com/yansen_zh/wechat-oplatform-sdk/api"
- "gitee.com/yansen_zh/wechat-oplatform-sdk/entity"
- "gitee.com/yansen_zh/wechat-oplatform-sdk/utils"
- "gitee.com/yansen_zh/wechat-oplatform-sdk/utils/request"
- )
-
- // AuthorizerTokenParam 获取/刷新接口调用令牌参数
- type AuthorizerTokenParam struct {
- ComponentAppId string `json:"component_appid"`
- AuthorizerAppId string `json:"authorizer_appid"`
- AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
- }
-
- // AuthorizerTokenResult 获取/刷新接口调用令牌结果
- type AuthorizerTokenResult struct {
- entity.Error
- AuthorizerAccessToken string `json:"authorizer_access_token"`
- ExpiresIn float64 `json:"expires_in"`
- AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
- }
-
- // AuthorizerToken 获取/刷新接口调用令牌
- func AuthorizerToken(componentAccessToken string, param AuthorizerTokenParam) (*AuthorizerTokenResult, error) {
- if componentAccessToken == "" {
- return nil, errors.New("获取/刷新接口调用令牌 第三方平台的 component_access_token 不能为空")
- }
-
- if param.ComponentAppId == "" {
- return nil, errors.New("获取/刷新接口调用令牌 第三方平台的 appid 不能为空")
- }
-
- if param.AuthorizerAppId == "" {
- return nil, errors.New("获取/刷新接口调用令牌 授权方 appid 不能为空")
- }
-
- if param.AuthorizerRefreshToken == "" {
- 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.AuthorizerToken, &queryParam)
- resp, e2 := request.PostJSON(apiUrl.String(), data)
- if e2 != nil {
- return nil, e2
- }
-
- result := AuthorizerTokenResult{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- if result.Code != 0 {
- return &result, result.Error
- }
-
- return &result, nil
- }
|