添加蜂鸟配送
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package fnpsapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -25,126 +24,125 @@ const (
|
||||
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(appID, appSecret string, config ...*platformapi.APIConfig) *API {
|
||||
func (a *API) MakeFnRequestHead() map[string]interface{} {
|
||||
requestParam := make(map[string]interface{}, 6)
|
||||
requestParam["access_token"] = a.accessToken
|
||||
requestParam["signature"] = a.signature
|
||||
requestParam["merchant_id"] = a.merchantId
|
||||
requestParam["version"] = a.version
|
||||
requestParam["app_id"] = a.appID
|
||||
requestParam["timestamp"] = a.timestamp
|
||||
return requestParam
|
||||
}
|
||||
func New(appID, appSecret, merchantId, code string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
grantType: "authorization_code", // 授权模式,填固定值authorization_code
|
||||
code: code,
|
||||
appID: appID,
|
||||
merchantId: merchantId,
|
||||
signature: "",
|
||||
timestamp: time.Now().UnixNano(),
|
||||
accessToken: "",
|
||||
version: "1.0",
|
||||
appSecret: appSecret,
|
||||
locker: sync.RWMutex{},
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) signParam(params map[string]interface{}) (sig string) {
|
||||
var valueList []string
|
||||
for k, v := range params {
|
||||
if k != sigKey {
|
||||
if k != "signature" {
|
||||
if str := fmt.Sprint(v); str != "" {
|
||||
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(valueList))
|
||||
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
|
||||
sig = a.appSecret + sig
|
||||
signature := sha256.Sum256([]byte(sig))
|
||||
return hex.EncodeToString(signature[:])
|
||||
}
|
||||
|
||||
func (a *API) signParam2(params map[string]interface{}) (sig string) {
|
||||
sb := new(strings.Builder)
|
||||
sb.WriteString("app_id=")
|
||||
sb.WriteString(a.appID)
|
||||
sb.WriteString("&access_token=")
|
||||
sb.WriteString(a.accessToken)
|
||||
sb.WriteString("&data=")
|
||||
sb.WriteString(params["data"].(string))
|
||||
sb.WriteString("&salt=")
|
||||
sb.WriteString(utils.Int64ToStr(utils.MustInterface2Int64(params["salt"])))
|
||||
sig = sb.String()
|
||||
binSig := md5.Sum([]byte(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) {
|
||||
params := make(map[string]interface{})
|
||||
params["salt"] = GetSalt()
|
||||
params["app_id"] = a.appID
|
||||
if action != tokenAction {
|
||||
data, _ := json.Marshal(bizParams)
|
||||
params["data"] = string(data)
|
||||
signStr := a.signParam2(params)
|
||||
params[sigKey] = signStr
|
||||
} else {
|
||||
signStr := a.signParam(params)
|
||||
params[sigKey] = signStr
|
||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
a.signature = a.signParam(bizParams)
|
||||
bizParams["signature"] = a.signature
|
||||
// 序列化
|
||||
data, err := json.Marshal(bizParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, params), nil)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
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 {
|
||||
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
|
||||
})
|
||||
|
||||
// 全路径请求参数
|
||||
fullURL := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
||||
|
||||
// 发送请求
|
||||
sendUrl := func() *http.Request {
|
||||
var request *http.Request
|
||||
if RequestPost == method {
|
||||
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
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
||||
result, err := a.AccessAPI(tokenAction, URL, nil, false)
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result["data"], &tokenInfo, false)
|
||||
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
|
||||
parameter["timestamp"] = utils.Int64ToStr(a.timestamp)
|
||||
|
||||
result, err := a.AccessAPI(TokenURL, "", RequestPost, parameter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
businessData := &BusinessData{}
|
||||
if err := json.Unmarshal([]byte(utils.Interface2String(result["business_data"])), businessData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokenInfo.BusinessDataObj = businessData
|
||||
return tokenInfo, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user