aa
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
var (
|
||||
api *API
|
||||
apiv3 *APIv3
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
@@ -19,6 +20,8 @@ func init() {
|
||||
baseapi.Init(sugarLogger)
|
||||
api = New("6a3e2073-1850-413b-9eb7-6c342ec36e1c", "a8248088-a742-4c33-a0db-03aeae00ca7d")
|
||||
api.SetToken("n-606c907a-5117-4c64-bc32-291c3091cabd-w")
|
||||
apiv3 = Newv3("6a3e2073-1850-413b-9eb7-6c342ec36e1c", "a8248088-a742-4c33-a0db-03aeae00ca7d")
|
||||
apiv3.SetTokenv3("n-606c907a-5117-4c64-bc32-291c3091cabd-w")
|
||||
}
|
||||
|
||||
func TestGetAccessToken(t *testing.T) {
|
||||
|
||||
139
platformapi/fnpsapi/fnpsapiv3.go
Normal file
139
platformapi/fnpsapi/fnpsapiv3.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package fnpsapi
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
merchantID = "51658"
|
||||
|
||||
URLv3 = "https://open-anubis.ele.me/anubis-webapi/v3/invoke"
|
||||
)
|
||||
|
||||
type APIv3 struct {
|
||||
accessToken string
|
||||
appID string
|
||||
appSecret string
|
||||
|
||||
locker sync.RWMutex
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func Newv3(appID, appSecret string, config ...*platformapi.APIConfig) *APIv3 {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &APIv3{
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *APIv3) SetTokenv3(token string) {
|
||||
a.locker.Lock()
|
||||
defer a.locker.Unlock()
|
||||
a.accessToken = token
|
||||
}
|
||||
|
||||
func (a *APIv3) signParamv3(params map[string]interface{}) (sig string) {
|
||||
var valueList []string
|
||||
for k, v := range params {
|
||||
if k != sigKey {
|
||||
if str := fmt.Sprint(v); str != "" {
|
||||
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(valueList))
|
||||
sig = strings.Join(valueList, "&")
|
||||
sig = a.appSecret + sig
|
||||
binSig := sha1.Sum([]byte(sig))
|
||||
sig = fmt.Sprintf("%x", binSig)
|
||||
return sig
|
||||
}
|
||||
|
||||
func (a *APIv3) AccessAPIv3(action string, url string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["timestamp"] = time.Now().UnixNano()
|
||||
params["app_id"] = a.appID
|
||||
params["merchant_id"] = merchantID
|
||||
params["version"] = "1.0"
|
||||
params["access_token"] = a.accessToken
|
||||
data, _ := json.Marshal(bizParams)
|
||||
params["business_data"] = string(data)
|
||||
signStr := a.signParamv3(params)
|
||||
params[sigKey] = signStr
|
||||
datas, _ := 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(datas)))
|
||||
} 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
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
type PreCreateOrderParam struct {
|
||||
PartnerOrderCode string `json:"partner_order_code"`
|
||||
OutShopCode string `json:"out_shop_code"`
|
||||
OrderType int `json:"order_type"`
|
||||
PositionSource int `json:"position_source"` // 3:高德地图
|
||||
ReceiverAddress string `json:"receiver_address"`
|
||||
ReceiverLongitude float64 `json:"receiver_longitude"`
|
||||
ReceiverLatitude float64 `json:"receiver_latitude"`
|
||||
GoodsTotalAmountCent float64 `json:"goods_total_amount_cent"`
|
||||
GoodsActualAmountCent float64 `json:"goods_actual_amount_cent"`
|
||||
GoodsWeight float64 `json:"goods_weight"`
|
||||
GoodsCount float64 `json:"goods_count"`
|
||||
GoodsItemList []*ItemsJSON2 `json:"goods_item_list"`
|
||||
}
|
||||
|
||||
type ItemsJSON2 struct {
|
||||
ItemName string `json:"item_name"`
|
||||
ItemQuantity int `json:"item_quantity"`
|
||||
ItemAmountCent float64 `json:"item_amount_cent"`
|
||||
ItemActualAmountCent float64 `json:"item_actual_amount_cent"`
|
||||
}
|
||||
|
||||
func (a *APIv3) PreCreateOrder(preCreateOrderParam *PreCreateOrderParam) (queryOrderResult *QueryOrderResult, err error) {
|
||||
result, err := a.AccessAPIv3("preCreateOrder", URLv3, utils.Struct2FlatMap(preCreateOrderParam), true)
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result["data"], &queryOrderResult, false)
|
||||
}
|
||||
return queryOrderResult, err
|
||||
}
|
||||
46
platformapi/fnpsapi/fnpsapiv3_test.go
Normal file
46
platformapi/fnpsapi/fnpsapiv3_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package fnpsapi
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPreCreateOrder(t *testing.T) {
|
||||
result, err := apiv3.PreCreateOrder(&PreCreateOrderParam{
|
||||
PartnerOrderCode: "2118717546000352",
|
||||
OutShopCode: "667002",
|
||||
OrderType: 1,
|
||||
PositionSource: 3,
|
||||
ReceiverAddress: "徐州市泉山区大润发(欣欣店)一楼咔啦嘟熊童装店",
|
||||
ReceiverLongitude: 117.195360,
|
||||
ReceiverLatitude: 34.201010,
|
||||
GoodsTotalAmountCent: 66.2,
|
||||
GoodsActualAmountCent: 67.2,
|
||||
GoodsWeight: 2.3,
|
||||
GoodsCount: 6,
|
||||
GoodsItemList: []*ItemsJSON2{
|
||||
&ItemsJSON2{
|
||||
ItemName: "巨峰葡萄约500g/份",
|
||||
ItemQuantity: 2,
|
||||
ItemAmountCent: 15.2,
|
||||
ItemActualAmountCent: 15.2,
|
||||
},
|
||||
&ItemsJSON2{
|
||||
ItemName: "[新鲜]苹果150g/个(约150g-250g)",
|
||||
ItemQuantity: 2,
|
||||
ItemAmountCent: 4.3,
|
||||
ItemActualAmountCent: 4.3,
|
||||
},
|
||||
&ItemsJSON2{
|
||||
ItemName: "脆桃 鲜,甜,脆约500g/份",
|
||||
ItemQuantity: 2,
|
||||
ItemAmountCent: 14.6,
|
||||
ItemActualAmountCent: 14.6,
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
Reference in New Issue
Block a user