fnapi
This commit is contained in:
@@ -4,9 +4,13 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
@@ -16,18 +20,26 @@ import (
|
||||
const (
|
||||
sigKey = "signature"
|
||||
|
||||
TestURL = "https://exam-anubis.ele.me/anubis-webapi/v2"
|
||||
URL = "https://open-anubis.ele.me/anubis-webapi/v2"
|
||||
TestURL = "https://exam-anubis.ele.me/anubis-webapi"
|
||||
URL = "https://open-anubis.ele.me/anubis-webapi"
|
||||
tokenAction = "get_access_token"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
accessToken string
|
||||
appID string
|
||||
appSecret string
|
||||
locker sync.RWMutex
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func (a *API) SetToken(token string) {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.accessToken = token
|
||||
}
|
||||
|
||||
func New(accessToken, appID, appSecret string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
@@ -47,34 +59,42 @@ func (a *API) signParam(params map[string]interface{}) (sig string) {
|
||||
for k, v := range params {
|
||||
if k != sigKey {
|
||||
if str := fmt.Sprint(v); str != "" {
|
||||
valueList = append(valueList, fmt.Sprintf("%s%s", k, str))
|
||||
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(valueList))
|
||||
valueList = append(valueList, fmt.Sprintf("%s", a.appSecret))
|
||||
var valueList2 = make([]string, len(valueList)+1)
|
||||
at := copy(valueList2, valueList[:0])
|
||||
at += copy(valueList2[at:], []string{a.appSecret})
|
||||
copy(valueList2[at:], valueList[0:])
|
||||
sig = strings.Join(valueList2, "")
|
||||
binSig := md5.Sum([]byte(sig))
|
||||
sig = fmt.Sprintf("%X", binSig)
|
||||
valueList = append(valueList, fmt.Sprintf("secret_key=%s", a.appSecret))
|
||||
sig = strings.Join(valueList, "&")
|
||||
binSig := md5.Sum([]byte(url.QueryEscape(sig)))
|
||||
sig = fmt.Sprintf("%x", binSig)
|
||||
return sig
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||
data, _ := json.Marshal(bizParams)
|
||||
params := make(map[string]interface{})
|
||||
params["salt"] = GetSalt()
|
||||
params["app_id"] = a.appID
|
||||
if action != tokenAction {
|
||||
if a.accessToken == "" {
|
||||
params["access_token"] = a.accessToken
|
||||
}
|
||||
data, _ := json.Marshal(bizParams)
|
||||
params["data"] = string(data)
|
||||
}
|
||||
signStr := a.signParam(params)
|
||||
params[sigKey] = signStr
|
||||
data, _ := json.Marshal(params)
|
||||
fullURL := utils.GenerateGetURL(url, action, nil)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
var request *http.Request
|
||||
if isPost {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, bizParams), nil)
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, params), nil)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
@@ -86,7 +106,7 @@ func (a *API) AccessAPI(action string, url string, bizParams map[string]interfac
|
||||
if utils.MustInterface2Int64(jsonResult1["code"]) != 200 {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"])))
|
||||
baseapi.SugarLogger.Debugf("ejiay AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
baseapi.SugarLogger.Debugf("fnps AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
@@ -94,3 +114,22 @@ func (a *API) AccessAPI(action string, url string, bizParams map[string]interfac
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func GetSalt() (salt int) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return rand.Intn(8999) + 1000
|
||||
}
|
||||
|
||||
type TokenInfo struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
AppID string `json:"app_id"`
|
||||
ExpireTime int64 `json:"expire_time"`
|
||||
}
|
||||
|
||||
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
||||
result, err := a.AccessAPI(tokenAction, TestURL, nil, false)
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result["data"], &tokenInfo, false)
|
||||
}
|
||||
return tokenInfo, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user