添加蜂鸟新API
This commit is contained in:
116
platformapi/fnpsapi_v3/fnpsapi.go
Normal file
116
platformapi/fnpsapi_v3/fnpsapi.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package fnpsapi
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
func (a *API) SetToken(token string) {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.accessToken = token
|
||||
}
|
||||
|
||||
func New(appID, appSecret, merchantId string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
grantType: "authorization_code", // 授权模式,填固定值authorization_code
|
||||
code: "cod3",
|
||||
appID: appID,
|
||||
merchantId: merchantId,
|
||||
signature: "",
|
||||
timestamp: time.Now().UnixNano(),
|
||||
accessToken: "",
|
||||
appSecret: appSecret,
|
||||
locker: sync.RWMutex{},
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
// signParam2 apiV3签名算法SHA-256
|
||||
func (a *API) signParam2() (sig string) {
|
||||
sb := new(strings.Builder)
|
||||
sb.WriteString("grant_type=")
|
||||
sb.WriteString("CODE") //todo
|
||||
sb.WriteString("&code=")
|
||||
sb.WriteString(a.code) //todo
|
||||
sb.WriteString("&app_id=")
|
||||
sb.WriteString(a.appID)
|
||||
sb.WriteString("&merchant_id=")
|
||||
sb.WriteString(a.merchantId)
|
||||
sb.WriteString("×tamp=")
|
||||
sb.WriteString(utils.Int64ToStr(a.timestamp))
|
||||
sig = sb.String()
|
||||
sig = base64.StdEncoding.EncodeToString(sha256.New().Sum([]byte(sig)))
|
||||
return sig
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
// 序列化
|
||||
data, err := json.Marshal(bizParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 全路径请求参数
|
||||
fullURL := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
||||
|
||||
// 发送请求
|
||||
sendUrl := func() *http.Request {
|
||||
var request *http.Request
|
||||
if method == "POST" {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request
|
||||
}
|
||||
|
||||
// 数据解析
|
||||
dataMarshal := func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if utils.MustInterface2Int64(jsonResult1["code"]) != 200 {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"])))
|
||||
baseapi.SugarLogger.Debugf("fnps AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
return errLevel, err
|
||||
}
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
||||
result, err := a.AccessAPI(TokenURL, "", RequestPost, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := utils.Map2StructByJson(result["data"], &tokenInfo, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tokenInfo, err
|
||||
}
|
||||
Reference in New Issue
Block a user