From 67d3821aacebd170bad129a684b4be05f072e96d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Mon, 7 Dec 2020 16:19:15 +0800 Subject: [PATCH] fnps --- platformapi/fnpsapi/fnpsapi.go | 96 +++++++++++++++++++++++++++++ platformapi/fnpsapi/fnpsapi_test.go | 18 ++++++ platformapi/jdshopapi/store_test.go | 2 +- 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 platformapi/fnpsapi/fnpsapi.go create mode 100644 platformapi/fnpsapi/fnpsapi_test.go diff --git a/platformapi/fnpsapi/fnpsapi.go b/platformapi/fnpsapi/fnpsapi.go new file mode 100644 index 00000000..eeb874c3 --- /dev/null +++ b/platformapi/fnpsapi/fnpsapi.go @@ -0,0 +1,96 @@ +package fnpsapi + +import ( + "crypto/md5" + "encoding/json" + "fmt" + "net/http" + "sort" + "strings" + + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/platformapi" + "git.rosy.net.cn/baseapi/utils" +) + +const ( + sigKey = "signature" + + TestURL = "https://exam-anubis.ele.me/anubis-webapi/v2" + URL = "https://open-anubis.ele.me/anubis-webapi/v2" +) + +type API struct { + accessToken string + appID string + appSecret string + client *http.Client + config *platformapi.APIConfig +} + +func New(accessToken, appID, appSecret string, config ...*platformapi.APIConfig) *API { + curConfig := platformapi.DefAPIConfig + if len(config) > 0 { + curConfig = *config[0] + } + return &API{ + accessToken: accessToken, + appID: appID, + appSecret: appSecret, + 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 str := fmt.Sprint(v); 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) + 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) + 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) + } + 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("ejiay AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) + } + retVal = jsonResult1 + } + return errLevel, err + }) + return retVal, err +} diff --git a/platformapi/fnpsapi/fnpsapi_test.go b/platformapi/fnpsapi/fnpsapi_test.go new file mode 100644 index 00000000..02157d42 --- /dev/null +++ b/platformapi/fnpsapi/fnpsapi_test.go @@ -0,0 +1,18 @@ +package fnpsapi + +import ( + "git.rosy.net.cn/baseapi" + "go.uber.org/zap" +) + +var ( + api *API + sugarLogger *zap.SugaredLogger +) + +func init() { + logger, _ := zap.NewDevelopment() + sugarLogger = logger.Sugar() + baseapi.Init(sugarLogger) + // api = New("9ad9cf5fc8b140b19a1dbbb700b47676kown", "D5E8352BE0786ED16F77B4548F62F09A", "71e1061ac2f246f6ac27efb900edba12") +} diff --git a/platformapi/jdshopapi/store_test.go b/platformapi/jdshopapi/store_test.go index f1734213..3a70d8cc 100644 --- a/platformapi/jdshopapi/store_test.go +++ b/platformapi/jdshopapi/store_test.go @@ -32,7 +32,7 @@ func TestCreateEntityStore(t *testing.T) { } func TestDeleteStoresByID(t *testing.T) { - err := api.DeleteStoresByID(1000063844) + err := api.DeleteStoresByID(24563933) if err != nil { t.Fatal(err) }