From 23b58f605a3457b5a46895f42ef61e4dd4f1f55e Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 14 Jan 2020 17:34:14 +0800 Subject: [PATCH] jd fake api --- platformapi/jdapi/fake_jdapi.go | 92 +++++++++++++++++++++++ platformapi/jdapi/fake_jdapi_test.go | 105 +++++++++++++++++++++++++++ platformapi/jdapi/jdapi.go | 12 ++- 3 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 platformapi/jdapi/fake_jdapi.go create mode 100644 platformapi/jdapi/fake_jdapi_test.go diff --git a/platformapi/jdapi/fake_jdapi.go b/platformapi/jdapi/fake_jdapi.go new file mode 100644 index 00000000..c04da037 --- /dev/null +++ b/platformapi/jdapi/fake_jdapi.go @@ -0,0 +1,92 @@ +package jdapi + +import ( + "errors" + + "git.rosy.net.cn/baseapi/utils" +) + +func (a *API) FakeOrderQuery(jdParams map[string]interface{}) (retVal []interface{}, totalCount int, err error) { + retVal, totalCount, err = a.AccessAPIHavePage("order/orderQuery", jdParams, nil, nil, nil) + return retVal, totalCount, err +} + +func (a *API) FakeQuerySingleOrder(orderId, deliveryStationNo string) (map[string]interface{}, error) { + jdParams := make(map[string]interface{}) + jdParams["orderId"] = orderId + jdParams["deliveryStationNo "] = deliveryStationNo + result, _, err := a.FakeOrderQuery(jdParams) + if err != nil { + return nil, err + } + if len(result) == 0 { + return nil, ErrCanNotFindOrder + } + return result[0].(map[string]interface{}), nil +} + +func (a *API) FakeBatchUpdateCurrentQtys(trackInfo, outStationNo, stationNo string, skuStockList []*SkuStock, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) { + if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") { + return nil, errors.New("outStationNo and stationNo can not all be empty or have value") + } + jdParams := map[string]interface{}{ + "skuStockList": skuStockList, + "userPin": utils.GetAPIOperator(userPin), + } + if outStationNo != "" { + jdParams["outStationNo"] = outStationNo + } else { + jdParams["stationNo"] = stationNo + } + result, err := a.AccessAPINoPage2("stock/batchUpdateCurrentQtys", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo) + if result != nil { + var err2 error + if responseList, err2 = a.handleBatchOpResult(outStationNo, stationNo, len(skuStockList), err, result, ""); err2 != nil && err == nil { + err = err2 + } + } + return responseList, err +} + +func (a *API) FakeBatchUpdateVendibility(trackInfo, outStationNo, stationNo string, stockVendibilityList []*StockVendibility, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) { + if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") { + return nil, errors.New("outStationNo and stationNo can not all be empty or have value") + } + jdParams := map[string]interface{}{ + "stockVendibilityList": stockVendibilityList, + "userPin": utils.GetAPIOperator(userPin), + } + if outStationNo != "" { + jdParams["outStationNo"] = outStationNo + } else { + jdParams["stationNo"] = stationNo + } + // 此函数在全部失败时,err仍然返回成功 + result, err := a.AccessAPINoPage2("stock/batchUpdateVendibility", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo) + if result != nil { + var err2 error + if responseList, err2 = a.handleBatchOpResult(outStationNo, stationNo, len(stockVendibilityList), err, result, ""); err2 != nil && err == nil { + err = err2 + } + } + return responseList, err +} + +func (a *API) FakeUpdateVendorStationPrice(trackInfo string, outStationNo, stationNo string, skuPriceInfoList []*SkuPriceInfo) (responseList []*StoreSkuBatchUpdateResponse, err error) { + jdParams := map[string]interface{}{ + "skuPriceInfoList": skuPriceInfoList, + } + if outStationNo != "" { + jdParams["outStationNo"] = outStationNo + } else { + jdParams["stationNo"] = stationNo + } + result, err := a.AccessAPINoPage2("price/batchUpdateStationPrice", jdParams, nil, nil, genNoPageResultParser("code", "msg", "result", "0"), trackInfo) + if result != nil { + var err2 error + if responseList, err2 = a.handleBatchOpResult(outStationNo, stationNo, len(skuPriceInfoList), err, result, "json2"); err2 != nil && err == nil { + err = err2 + } + } + return responseList, err +} diff --git a/platformapi/jdapi/fake_jdapi_test.go b/platformapi/jdapi/fake_jdapi_test.go new file mode 100644 index 00000000..8b712d0b --- /dev/null +++ b/platformapi/jdapi/fake_jdapi_test.go @@ -0,0 +1,105 @@ +package jdapi + +import ( + "testing" + "time" + + "git.rosy.net.cn/baseapi/utils" +) + +func TestFakeOrderQuery(t *testing.T) { + api := NewFakeJD("ndslkv9asl@djf_n7askdjfk$", "http://test.jxc4.com/qqqq") + + retVal, err := api.FakeQuerySingleOrder("2000483691000741", "11866256") + t.Log(utils.Format4Output(retVal, false)) + if err != nil { + t.Error(err) + } + if false { + t.Log(utils.Format4Output(retVal, false)) + } +} + +func TestFakeBatchUpdateCurrentQtys(t *testing.T) { + api := NewFakeJD("ndslkv9asl@djf_n7askdjfk$", "http://test.jxc4.com/qqqq") + + retVal, err := api.FakeBatchUpdateCurrentQtys("", "", "11943257", []*SkuStock{ + &SkuStock{ + OutSkuId: "2029937911", + StockQty: 88, + }, + }, "test") + t.Log(utils.Format4Output(retVal, false)) + if err != nil { + t.Error(err) + } + if false { + t.Log(utils.Format4Output(retVal, false)) + } +} + +func TestFakeBatchUpdateVendibility(t *testing.T) { + api := NewFakeJD("ndslkv9asl@djf_n7askdjfk$", "http://test.jxc4.com/qqqq") + + retVal, err := api.FakeBatchUpdateVendibility("", "", "11943257", []*StockVendibility{ + &StockVendibility{ + OutSkuId: "2029937911", + DoSale: true, + }, + }, "test") + t.Log(utils.Format4Output(retVal, false)) + if err != nil { + t.Error(err) + } + if false { + t.Log(utils.Format4Output(retVal, false)) + } +} + +func TestFakeUpdateVendorStationPrice(t *testing.T) { + api := NewFakeJD("ndslkv9asl@djf_n7askdjfk$", "http://test.jxc4.com/qqqq") + + retVal, err := api.FakeUpdateVendorStationPrice("", "", "11943257", []*SkuPriceInfo{ + &SkuPriceInfo{ + OutSkuId: "2029937911", + Price: 4567, + }, + }) + t.Log(utils.Format4Output(retVal, false)) + if err != nil { + t.Error(err) + } + if false { + t.Log(utils.Format4Output(retVal, false)) + } +} + +func TestFakeCreatePromotionSingle(t *testing.T) { + api := NewFakeJD("ndslkv9asl@djf_n7askdjfk$", "http://test.jxc4.com/qqqq") + + infoId, err := api.CreatePromotionInfosSingle("测试1", time.Now(), time.Now().Add(24*time.Hour), "", "", "") + if err != nil { + t.Fatal(err) + } + t.Log(infoId) + err = api.CreatePromotionRulesSingle(infoId, "", 1, 1, 1, 1, "") + if err != nil { + t.Fatal(err) + } + skuInfos, err := api.CreatePromotionSkuSingle(infoId, "", []*PromotionSku{ + &PromotionSku{ + OutSkuID: "2216", + StationNo: 11943257, + PromotionPrice: 500, + LimitSkuCount: 2, + }, + }, "") + if err != nil { + t.Fatal(err) + } + t.Log(skuInfos) + err = api.ConfirmPromotionSingle(infoId, "", "") + if err != nil { + t.Fatal(err) + } +} diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 16a2c999..7868c064 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -61,6 +61,7 @@ const ( type API struct { platformapi.APICookie + baseURL string token string appKey string appSecret string @@ -134,6 +135,7 @@ func New(token, appKey, appSecret string, config ...*platformapi.APIConfig) *API curConfig = *config[0] } return &API{ + baseURL: prodURL, token: token, appKey: appKey, appSecret: appSecret, @@ -142,6 +144,12 @@ func New(token, appKey, appSecret string, config ...*platformapi.APIConfig) *API } } +func NewFakeJD(token, baseURL string, config ...*platformapi.APIConfig) *API { + a := New(token, "", "", config...) + a.baseURL = baseURL + return a +} + func NewPageOnly(cookie string, config ...*platformapi.APIConfig) *API { api := New("", "", "", config...) api.SetCookie(accessStorePageCookieName, cookie) @@ -186,11 +194,11 @@ func (a *API) AccessAPI2(apiStr string, jdParams map[string]interface{}, traceIn params[signKey] = sign var request *http.Request if userGet { - fullURL := utils.GenerateGetURL(prodURL, apiStr, params) + fullURL := utils.GenerateGetURL(a.baseURL, apiStr, params) // baseapi.SugarLogger.Debug(fullURL) request, _ = http.NewRequest(http.MethodGet, fullURL, nil) } else { - fullURL := prodURL + "/" + apiStr + fullURL := utils.GenerateGetURL(a.baseURL, apiStr, nil) // baseapi.SugarLogger.Debug(utils.Map2URLValues(params).Encode()) request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode())) request.Header.Set("charset", "UTF-8")