fengniao ceshi

This commit is contained in:
邹宗楠
2022-03-25 17:00:20 +08:00
parent 6682ad5f93
commit d43a076bec
5 changed files with 78 additions and 47 deletions

View File

@@ -7,8 +7,9 @@ import (
)
const (
TokenURL = "https://open-anubis.ele.me/anubis-webapi/openapi/token" // 正式环境
ApiURL = "https://open-anubis.ele.me/anubis-webapi/v3/invoke" // 正式环境
TokenURL = "https://open-anubis.ele.me/anubis-webapi/openapi/token" // 正式环境
ApiURL = "https://open-anubis.ele.me/anubis-webapi/v3/invoke" // 正式环境
RefreshTokenUrl = "https://open-anubis.ele.me/anubis-webapi/openapi/refreshToken" // 正式环境刷新token
//TokenURL = "https://exam-anubis.ele.me/anubis-webapi/openapi/token" // 沙箱环境
//ApiURL = "https://exam-anubis.ele.me/anubis-webapi/v3/invoke" // 沙箱环境
RequestPost = "POST"
@@ -17,18 +18,19 @@ const (
// 注册请求api
type API struct {
grantType string `json:"grant_type"`
code string `json:"code"`
appID string `json:"app_id"`
merchantId string `json:"merchant_id"`
signature string `json:"signature"`
timestamp int64 `json:"timestamp"`
accessToken string `json:"access_token"`
appSecret string `json:"app_secret"`
version string `json:"version"`
locker sync.RWMutex
client *http.Client
config *platformapi.APIConfig
grantType string `json:"grant_type"`
code string `json:"code"`
appID string `json:"app_id"`
merchantId string `json:"merchant_id"`
signature string `json:"signature"`
timestamp int64 `json:"timestamp"`
accessToken string `json:"access_token"`
refreshToken string `json:"refresh_token"`
appSecret string `json:"app_secret"`
version string `json:"version"`
locker sync.RWMutex
client *http.Client
config *platformapi.APIConfig
}
// 请求基础结构体

View File

@@ -30,6 +30,12 @@ func (a *API) SetToken(token string) {
a.accessToken = token
}
func (a *API) SetRefreshToken(token string) {
a.locker.Lock()
defer a.locker.Unlock()
a.refreshToken = token
}
func (a *API) MakeFnRequestHead() map[string]interface{} {
requestParam := make(map[string]interface{}, 6)
requestParam["access_token"] = a.accessToken
@@ -45,18 +51,21 @@ func New(appID, appSecret, merchantId, code string, config ...*platformapi.APICo
if len(config) > 0 {
curConfig = *config[0]
}
// 查询蜂鸟refeshToken
return &API{
grantType: "authorization_code", // 授权模式填固定值authorization_code
code: code,
appID: appID,
merchantId: merchantId,
signature: "",
accessToken: "",
version: "1.0",
appSecret: appSecret,
locker: sync.RWMutex{},
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
grantType: "authorization_code", // 授权模式填固定值authorization_code
code: code,
appID: appID,
merchantId: merchantId,
signature: "",
accessToken: "",
refreshToken: "",
version: "1.0",
appSecret: appSecret,
locker: sync.RWMutex{},
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
}
}
@@ -125,14 +134,22 @@ func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
parameter := make(map[string]interface{}, 6)
parameter["grant_type"] = a.grantType
parameter["code"] = a.code
parameter["app_id"] = a.appID
parameter["merchant_id"] = a.merchantId
result, err := a.AccessAPI(TokenURL, "", RequestPost, parameter)
if err != nil {
return nil, err
// 先去刷新token,没有的话再去获取token(code只能使用一次,生成的token管一年)
var result map[string]interface{}
parameter["refresh_token"] = a.refreshToken
if a.refreshToken == "" {
parameter["code"] = a.code
result, err = a.AccessAPI(TokenURL, "", RequestPost, parameter)
if err != nil {
return nil, err
}
} else {
result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter)
}
if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {
return nil, err
}