- 重要修改相关的函数新增trackInfo参数,追踪修改人.接口类型改变

This commit is contained in:
gazebo
2019-07-28 12:34:52 +08:00
parent 437711eb38
commit da9c8a11c2
13 changed files with 152 additions and 100 deletions

View File

@@ -91,7 +91,7 @@ func (a *API) getShopID(body map[string]interface{}) (shopID string) {
return "" return ""
} }
func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *ResponseResult, err error) { func (a *API) AccessAPI2(cmd string, body map[string]interface{}, trackInfo string) (retVal *ResponseResult, err error) {
baseapi.SugarLogger.Debugf("ebai AccessAPI cmd:%s", cmd) baseapi.SugarLogger.Debugf("ebai AccessAPI cmd:%s", cmd)
// a.speedLimiter.AccessAPI(allAPI) // a.speedLimiter.AccessAPI(allAPI)
a.speedLimiter.AccessAPI(cmd, a.getShopID(body)) a.speedLimiter.AccessAPI(cmd, a.getShopID(body))
@@ -114,6 +114,9 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
request, _ := http.NewRequest(http.MethodPost, prodURL, strings.NewReader(encodedParams)) request, _ := http.NewRequest(http.MethodPost, prodURL, strings.NewReader(encodedParams))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("User-Agent", "ebai-golang-api") request.Header.Set("User-Agent", "ebai-golang-api")
if trackInfo != "" {
request.Header.Set(platformapi.KeyTrackInfo, trackInfo)
}
return request return request
}, },
a.config, a.config,
@@ -147,6 +150,10 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
return retVal, err return retVal, err
} }
func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *ResponseResult, err error) {
return a.AccessAPI2(cmd, body, "")
}
func (a *API) GetSupplierID() (supplierID int64) { func (a *API) GetSupplierID() (supplierID int64) {
a.locker.RLock() a.locker.RLock()
supplierID = a.supplierID supplierID = a.supplierID

View File

@@ -291,7 +291,7 @@ func (a *API) SkuList(shopID string, params *SkuListParams) (skuInfo *PageDataIn
} }
// 饿百商品名可以相同,不会报错 // 饿百商品名可以相同,不会报错
func (a *API) SkuCreate(shopID string, customSkuID int, params map[string]interface{}) (skuID int64, err error) { func (a *API) SkuCreate(trackInfo, shopID string, customSkuID int, params map[string]interface{}) (skuID int64, err error) {
defParams := map[string]interface{}{ defParams := map[string]interface{}{
KeyShopID: shopID, KeyShopID: shopID,
KeyCustomSkuID: customSkuID, KeyCustomSkuID: customSkuID,
@@ -308,19 +308,19 @@ func (a *API) SkuCreate(shopID string, customSkuID int, params map[string]interf
} }
params = utils.MergeMaps(params, defParams) params = utils.MergeMaps(params, defParams)
// baseapi.SugarLogger.Debugf(utils.Format4Output(params, false)) // baseapi.SugarLogger.Debugf(utils.Format4Output(params, false))
result, err := a.AccessAPI("sku.create", params) result, err := a.AccessAPI2("sku.create", params, trackInfo)
if err == nil { if err == nil {
return utils.Str2Int64(utils.Interface2String(result.Data.(map[string]interface{})[KeySkuID])), nil return utils.Str2Int64(utils.Interface2String(result.Data.(map[string]interface{})[KeySkuID])), nil
} }
return 0, err return 0, err
} }
func (a *API) SkuUpdate(shopID string, ebaiSkuID int64, params map[string]interface{}) (skuID int64, err error) { func (a *API) SkuUpdate(trackInfo, shopID string, ebaiSkuID int64, params map[string]interface{}) (skuID int64, err error) {
defParams := map[string]interface{}{ defParams := map[string]interface{}{
KeyShopID: shopID, KeyShopID: shopID,
KeySkuID: ebaiSkuID, KeySkuID: ebaiSkuID,
} }
result, err := a.AccessAPI("sku.update", utils.MergeMaps(params, defParams)) result, err := a.AccessAPI2("sku.update", utils.MergeMaps(params, defParams), trackInfo)
if err == nil { if err == nil {
return utils.Str2Int64(utils.Interface2String(result.Data.(map[string]interface{})[KeySkuID])), nil return utils.Str2Int64(utils.Interface2String(result.Data.(map[string]interface{})[KeySkuID])), nil
} }
@@ -383,10 +383,10 @@ func handleShopSkuBatchErr(err error) (opResult *BatchOpResult, outErr error) {
// 另外需要注意的是SkuPriceUpdateBatch的失败明细中的skuID是int64但其它几个是string... // 另外需要注意的是SkuPriceUpdateBatch的失败明细中的skuID是int64但其它几个是string...
// 文档上说支持custom_sku_id但实际好像只支持skuid // 文档上说支持custom_sku_id但实际好像只支持skuid
func (a *API) SkuDelete(shopID string, skuIDs []int64, customSkuDs []string) (opResult *BatchOpResult, err error) { func (a *API) SkuDelete(trackInfo, shopID string, skuIDs []int64, customSkuDs []string) (opResult *BatchOpResult, err error) {
params := genSkuIDParams(intIDs2Str(skuIDs), strIDs2Str(customSkuDs), "") params := genSkuIDParams(intIDs2Str(skuIDs), strIDs2Str(customSkuDs), "")
params[KeyShopID] = shopID params[KeyShopID] = shopID
result, err := a.AccessAPI("sku.delete", params) result, err := a.AccessAPI2("sku.delete", params, trackInfo)
if err == nil { if err == nil {
opResult, err = handleShopSkuBatchResult(result) opResult, err = handleShopSkuBatchResult(result)
} else { } else {
@@ -395,10 +395,10 @@ func (a *API) SkuDelete(shopID string, skuIDs []int64, customSkuDs []string) (op
return opResult, err return opResult, err
} }
func (a *API) SkuOnline(shopID string, skuIDs []int64, customSkuDs, upcs []string) (opResult *BatchOpResult, err error) { func (a *API) SkuOnline(trackInfo, shopID string, skuIDs []int64, customSkuDs, upcs []string) (opResult *BatchOpResult, err error) {
params := genSkuIDParams(intIDs2Str(skuIDs), strIDs2Str(customSkuDs), strIDs2Str(upcs)) params := genSkuIDParams(intIDs2Str(skuIDs), strIDs2Str(customSkuDs), strIDs2Str(upcs))
params[KeyShopID] = shopID params[KeyShopID] = shopID
result, err := a.AccessAPI("sku.online", params) result, err := a.AccessAPI2("sku.online", params, trackInfo)
if err == nil { if err == nil {
opResult, err = handleShopSkuBatchResult(result) opResult, err = handleShopSkuBatchResult(result)
} else { } else {
@@ -407,17 +407,17 @@ func (a *API) SkuOnline(shopID string, skuIDs []int64, customSkuDs, upcs []strin
return opResult, err return opResult, err
} }
func (a *API) SkuOnlineOne(shopID string, skuID int64, customSkuID, upc string) (err error) { func (a *API) SkuOnlineOne(trackInfo, shopID string, skuID int64, customSkuID, upc string) (err error) {
params := genSkuIDParams(utils.Int64ToStrNoZero(skuID), customSkuID, upc) params := genSkuIDParams(utils.Int64ToStrNoZero(skuID), customSkuID, upc)
params[KeyShopID] = shopID params[KeyShopID] = shopID
_, err = a.AccessAPI("sku.online.one", params) _, err = a.AccessAPI2("sku.online.one", params, trackInfo)
return err return err
} }
func (a *API) SkuOffline(shopID string, skuIDs []int64, customSkuDs, upcs []string) (opResult *BatchOpResult, err error) { func (a *API) SkuOffline(trackInfo, shopID string, skuIDs []int64, customSkuDs, upcs []string) (opResult *BatchOpResult, err error) {
params := genSkuIDParams(intIDs2Str(skuIDs), strIDs2Str(customSkuDs), strIDs2Str(upcs)) params := genSkuIDParams(intIDs2Str(skuIDs), strIDs2Str(customSkuDs), strIDs2Str(upcs))
params[KeyShopID] = shopID params[KeyShopID] = shopID
result, err := a.AccessAPI("sku.offline", params) result, err := a.AccessAPI2("sku.offline", params, trackInfo)
if err == nil { if err == nil {
opResult, err = handleShopSkuBatchResult(result) opResult, err = handleShopSkuBatchResult(result)
} else { } else {
@@ -426,19 +426,19 @@ func (a *API) SkuOffline(shopID string, skuIDs []int64, customSkuDs, upcs []stri
return opResult, err return opResult, err
} }
func (a *API) SkuOfflineOne(shopID string, skuID int64, customSkuID, upc string) (err error) { func (a *API) SkuOfflineOne(trackInfo, shopID string, skuID int64, customSkuID, upc string) (err error) {
params := genSkuIDParams(utils.Int64ToStrNoZero(skuID), customSkuID, upc) params := genSkuIDParams(utils.Int64ToStrNoZero(skuID), customSkuID, upc)
params[KeyShopID] = shopID params[KeyShopID] = shopID
_, err = a.AccessAPI("sku.offline.one", params) _, err = a.AccessAPI2("sku.offline.one", params, trackInfo)
return err return err
} }
func (a *API) SkuPriceUpdateBatch(shopID string, priceList ShopSkuInfoList, skuIDType int) (opResult *BatchOpResult, err error) { func (a *API) SkuPriceUpdateBatch(trackInfo, shopID string, priceList ShopSkuInfoList, skuIDType int) (opResult *BatchOpResult, err error) {
params := map[string]interface{}{ params := map[string]interface{}{
KeyShopID: shopID, KeyShopID: shopID,
priceUpdateKeyIDMap[skuIDType]: priceList.PriceString(skuIDType), priceUpdateKeyIDMap[skuIDType]: priceList.PriceString(skuIDType),
} }
result, err := a.AccessAPI("sku.price.update.batch", params) result, err := a.AccessAPI2("sku.price.update.batch", params, trackInfo)
if err == nil { if err == nil {
opResult, err = handleShopSkuBatchResult(result) opResult, err = handleShopSkuBatchResult(result)
} else { } else {
@@ -447,22 +447,22 @@ func (a *API) SkuPriceUpdateBatch(shopID string, priceList ShopSkuInfoList, skuI
return opResult, err return opResult, err
} }
func (a *API) SkuPriceUpdateOne(shopID string, priceInfo *ShopSkuInfo) (err error) { func (a *API) SkuPriceUpdateOne(trackInfo, shopID string, priceInfo *ShopSkuInfo) (err error) {
skuIDType := priceInfo.GuessIDType() skuIDType := priceInfo.GuessIDType()
params := map[string]interface{}{ params := map[string]interface{}{
KeyShopID: shopID, KeyShopID: shopID,
priceUpdateKeyIDMap[skuIDType]: priceInfo.PriceString(skuIDType), priceUpdateKeyIDMap[skuIDType]: priceInfo.PriceString(skuIDType),
} }
_, err = a.AccessAPI("sku.price.update.one", params) _, err = a.AccessAPI2("sku.price.update.one", params, trackInfo)
return err return err
} }
func (a *API) SkuStockUpdateBatch(shopID string, stockList ShopSkuInfoList, skuIDType int) (opResult *BatchOpResult, err error) { func (a *API) SkuStockUpdateBatch(trackInfo, shopID string, stockList ShopSkuInfoList, skuIDType int) (opResult *BatchOpResult, err error) {
params := map[string]interface{}{ params := map[string]interface{}{
KeyShopID: shopID, KeyShopID: shopID,
stockUpdateKeyIDMap[skuIDType]: stockList.StockString(skuIDType), stockUpdateKeyIDMap[skuIDType]: stockList.StockString(skuIDType),
} }
result, err := a.AccessAPI("sku.stock.update.batch", params) result, err := a.AccessAPI2("sku.stock.update.batch", params, trackInfo)
if err == nil { if err == nil {
opResult, err = handleShopSkuBatchResult(result) opResult, err = handleShopSkuBatchResult(result)
} else { } else {
@@ -471,13 +471,13 @@ func (a *API) SkuStockUpdateBatch(shopID string, stockList ShopSkuInfoList, skuI
return opResult, err return opResult, err
} }
func (a *API) SkuStockUpdateOne(shopID string, stockInfo *ShopSkuInfo) (err error) { func (a *API) SkuStockUpdateOne(trackInfo, shopID string, stockInfo *ShopSkuInfo) (err error) {
skuIDType := stockInfo.GuessIDType() skuIDType := stockInfo.GuessIDType()
params := map[string]interface{}{ params := map[string]interface{}{
KeyShopID: shopID, KeyShopID: shopID,
stockUpdateKeyIDMap[skuIDType]: stockInfo.StockString(skuIDType), stockUpdateKeyIDMap[skuIDType]: stockInfo.StockString(skuIDType),
} }
_, err = a.AccessAPI("sku.stock.update.one", params) _, err = a.AccessAPI2("sku.stock.update.one", params, trackInfo)
return err return err
} }

View File

@@ -95,7 +95,7 @@ func TestSkuCreate(t *testing.T) {
func TestSkuUpdate(t *testing.T) { func TestSkuUpdate(t *testing.T) {
// 15579787500720732 高级 // 15579787500720732 高级
result, err := api.SkuUpdate("2", 1557043939079105, map[string]interface{}{ result, err := api.SkuUpdate(utils.GetUUID(), "2", 1557043939079105, map[string]interface{}{
// "name": "高级商品2015a333约1100g/份", // "name": "高级商品2015a333约1100g/份",
// "rtf": "http://www.rosy.net.cn/rtf.html", // "rtf": "http://www.rosy.net.cn/rtf.html",
"shelf_number": 12, "shelf_number": 12,
@@ -119,7 +119,7 @@ func TestSkuDelete(t *testing.T) {
notExistSkuID = 12345678 notExistSkuID = 12345678
existSkuID = 156406677407848 existSkuID = 156406677407848
) )
opResult, err := api.SkuDelete(testShopID, []int64{notExistSkuID}, nil) opResult, err := api.SkuDelete(utils.GetUUID(), testShopID, []int64{notExistSkuID}, nil)
t.Log(utils.Format4Output(opResult, false)) t.Log(utils.Format4Output(opResult, false))
if err == nil { if err == nil {
t.Log("应该要报错") t.Log("应该要报错")
@@ -127,7 +127,7 @@ func TestSkuDelete(t *testing.T) {
if opResult == nil || len(opResult.FailedList) != 1 || opResult.FailedList[0].SkuID != notExistSkuID { if opResult == nil || len(opResult.FailedList) != 1 || opResult.FailedList[0].SkuID != notExistSkuID {
t.Logf("错误结果中应该要包含:%d", notExistSkuID) t.Logf("错误结果中应该要包含:%d", notExistSkuID)
} }
opResult, err = api.SkuDelete(testShopID, []int64{existSkuID}, nil) opResult, err = api.SkuDelete(utils.GetUUID(), testShopID, []int64{existSkuID}, nil)
t.Log(utils.Format4Output(opResult, false)) t.Log(utils.Format4Output(opResult, false))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -143,8 +143,9 @@ func TestSkuOnline(t *testing.T) {
existSkuID = 156406688807623 existSkuID = 156406688807623
) )
opResult, err := api.SkuOnline(testShopID, []int64{notExistSkuID, existSkuID}, nil, nil) opResult, err := api.SkuOnline(utils.GetUUID(), testShopID, []int64{notExistSkuID, existSkuID}, nil, nil)
t.Log(utils.Format4Output(opResult, false)) t.Log(utils.Format4Output(opResult, false))
t.Log(err.Error())
if err == nil { if err == nil {
t.Log("应该要报错") t.Log("应该要报错")
} }
@@ -157,14 +158,14 @@ func TestSkuOnline(t *testing.T) {
} }
func TestSkuOnlineOne(t *testing.T) { func TestSkuOnlineOne(t *testing.T) {
err := api.SkuOnlineOne(testShopID, 13211, "", "") err := api.SkuOnlineOne(utils.GetUUID(), testShopID, 13211, "", "")
if err == nil { if err == nil {
t.Fatal("应该要报错才对") t.Fatal("应该要报错才对")
} }
} }
func TestSkuOffline(t *testing.T) { func TestSkuOffline(t *testing.T) {
opResult, err := api.SkuOffline(testShopID, []int64{1564049914071288, 156389470507185}, nil, nil) opResult, err := api.SkuOffline(utils.GetUUID(), testShopID, []int64{1564049914071288, 156389470507185}, nil, nil)
t.Log(utils.Format4Output(opResult, false)) t.Log(utils.Format4Output(opResult, false))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -172,7 +173,7 @@ func TestSkuOffline(t *testing.T) {
} }
func TestSkuPriceUpdateBatch(t *testing.T) { func TestSkuPriceUpdateBatch(t *testing.T) {
opResult, err := api.SkuPriceUpdateBatch(testShopID, ShopSkuInfoList{ opResult, err := api.SkuPriceUpdateBatch(utils.GetUUID(), testShopID, ShopSkuInfoList{
&ShopSkuInfo{ &ShopSkuInfo{
SkuID: 156369111807787, SkuID: 156369111807787,
SalePrice: 100, SalePrice: 100,
@@ -203,7 +204,7 @@ func TestSkuStockUpdateBatch(t *testing.T) {
leftNum = 123 leftNum = 123
) )
opResult, err := api.SkuStockUpdateBatch(testShopID, ShopSkuInfoList{ opResult, err := api.SkuStockUpdateBatch(utils.GetUUID(), testShopID, ShopSkuInfoList{
&ShopSkuInfo{ &ShopSkuInfo{
SkuID: notExistSkuID, SkuID: notExistSkuID,
Stock: leftNum, Stock: leftNum,
@@ -239,7 +240,7 @@ func TestSkuStockUpdateOne(t *testing.T) {
leftNum = 234 leftNum = 234
) )
err := api.SkuStockUpdateOne(testShopID, &ShopSkuInfo{ err := api.SkuStockUpdateOne(utils.GetUUID(), testShopID, &ShopSkuInfo{
SkuID: existSkuID, SkuID: existSkuID,
Stock: leftNum, Stock: leftNum,
}) })

View File

@@ -143,7 +143,7 @@ func New(token, appKey, appSecret string, config ...*platformapi.APIConfig) *API
} }
} }
func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal map[string]interface{}, err error) { func (a *API) AccessAPI2(apiStr string, jdParams map[string]interface{}, traceInfo string) (retVal map[string]interface{}, err error) {
params := make(map[string]interface{}) params := make(map[string]interface{})
params["v"] = "1.0" params["v"] = "1.0"
params["format"] = "json" params["format"] = "json"
@@ -182,6 +182,9 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
request.Header.Set("charset", "UTF-8") request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} }
if traceInfo != "" {
request.Header.Set(platformapi.KeyTrackInfo, traceInfo)
}
// request.Close = true //todo 为了性能考虑还是不要关闭 // request.Close = true //todo 为了性能考虑还是不要关闭
return request return request
}, },
@@ -208,6 +211,10 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
return retVal, platformapi.RebuildError(err, jdParams, watchKeys) return retVal, platformapi.RebuildError(err, jdParams, watchKeys)
} }
func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal map[string]interface{}, err error) {
return a.AccessAPI2(apiStr, jdParams, "")
}
func genNoPageResultParser(codeKey, msgKey, resultKey, okCode string) func(data map[string]interface{}) (interface{}, error) { func genNoPageResultParser(codeKey, msgKey, resultKey, okCode string) func(data map[string]interface{}) (interface{}, error) {
return func(data map[string]interface{}) (innerData interface{}, err error) { return func(data map[string]interface{}) (innerData interface{}, err error) {
rawInnerCode, ok := data[codeKey] rawInnerCode, ok := data[codeKey]
@@ -236,11 +243,11 @@ func formatErrorMsg(msg interface{}) (strMsg string) {
return strMsg return strMsg
} }
func (a *API) AccessAPINoPage(apiStr string, jdParams map[string]interface{}, keyToRemove, keyToKeep []string, resultParser func(data map[string]interface{}) (interface{}, error)) (interface{}, error) { func (a *API) AccessAPINoPage2(apiStr string, jdParams map[string]interface{}, keyToRemove, keyToKeep []string, resultParser func(data map[string]interface{}) (interface{}, error), traceInfo string) (interface{}, error) {
if resultParser == nil { if resultParser == nil {
resultParser = genNoPageResultParser("code", "msg", "result", "0") resultParser = genNoPageResultParser("code", "msg", "result", "0")
} }
jsonResult, err := a.AccessAPI(apiStr, jdParams) jsonResult, err := a.AccessAPI2(apiStr, jdParams, traceInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -257,6 +264,10 @@ func (a *API) AccessAPINoPage(apiStr string, jdParams map[string]interface{}, ke
return result, err return result, err
} }
func (a *API) AccessAPINoPage(apiStr string, jdParams map[string]interface{}, keyToRemove, keyToKeep []string, resultParser func(data map[string]interface{}) (interface{}, error)) (interface{}, error) {
return a.AccessAPINoPage2(apiStr, jdParams, keyToRemove, keyToKeep, resultParser, "")
}
func normalHavePageResultParser(data map[string]interface{}, totalCount int) ([]interface{}, int, error) { func normalHavePageResultParser(data map[string]interface{}, totalCount int) ([]interface{}, int, error) {
var result map[string]interface{} var result map[string]interface{}
var retVal []interface{} var retVal []interface{}

View File

@@ -622,3 +622,15 @@ func ProcessQuestionPic(questionPic string) (outQuestionPic string) {
} }
return outQuestionPic return outQuestionPic
} }
// 订单商家加小费接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=169&apiid=ed9e3ca7325c4d4d8ceaf959ed0e7a62
func (a *API) OrderAddTips(OrderID string, tips int, pin string) (err error) {
jdParams := map[string]interface{}{
"orderId": OrderID,
"tips": tips,
"pin": utils.GetAPIOperator(pin),
}
_, err = a.AccessAPINoPage("order/addTips", jdParams, nil, nil, nullResultParser)
return err
}

View File

@@ -97,7 +97,7 @@ type StoreSkuBatchUpdateResponse struct {
// 根据商家商品编码和商家门店编码批量修改门店价格接口 // 根据商家商品编码和商家门店编码批量修改门店价格接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=205&apiid=fcbf346648a54d03b92dec8fa62ea643 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=205&apiid=fcbf346648a54d03b92dec8fa62ea643
func (a *API) UpdateVendorStationPrice(outStationNo, stationNo string, skuPriceInfoList []*SkuPriceInfo) (responseList []*StoreSkuBatchUpdateResponse, err error) { func (a *API) UpdateVendorStationPrice(trackInfo string, outStationNo, stationNo string, skuPriceInfoList []*SkuPriceInfo) (responseList []*StoreSkuBatchUpdateResponse, err error) {
jdParams := map[string]interface{}{ jdParams := map[string]interface{}{
"skuPriceInfoList": skuPriceInfoList, "skuPriceInfoList": skuPriceInfoList,
} }
@@ -106,7 +106,7 @@ func (a *API) UpdateVendorStationPrice(outStationNo, stationNo string, skuPriceI
} else { } else {
jdParams["stationNo"] = stationNo jdParams["stationNo"] = stationNo
} }
result, err := a.AccessAPINoPage("venderprice/updateStationPrice", jdParams, nil, nil, genNoPageResultParser("code", "msg", "result", "0")) result, err := a.AccessAPINoPage2("venderprice/updateStationPrice", jdParams, nil, nil, genNoPageResultParser("code", "msg", "result", "0"), trackInfo)
if result != nil { if result != nil {
var err2 error var err2 error
if responseList, err2 = a.handleBatchOpResult(len(skuPriceInfoList), result, "json2"); err2 != nil && err == nil { if responseList, err2 = a.handleBatchOpResult(len(skuPriceInfoList), result, "json2"); err2 != nil && err == nil {
@@ -119,13 +119,13 @@ func (a *API) UpdateVendorStationPrice(outStationNo, stationNo string, skuPriceI
// 根据到家商品编码和到家门店编码修改门店价格接口 // 根据到家商品编码和到家门店编码修改门店价格接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=205&apiid=45f83ef7c6e74dad94b6b68d3c50b673 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=205&apiid=45f83ef7c6e74dad94b6b68d3c50b673
// 单商品用此接口 // 单商品用此接口
func (a *API) UpdateStationPrice(skuId int64, stationNo string, price int) (string, error) { func (a *API) UpdateStationPrice(trackInfo string, skuId int64, stationNo string, price int) (string, error) {
jdParams := map[string]interface{}{ jdParams := map[string]interface{}{
"skuId": skuId, "skuId": skuId,
"stationNo": stationNo, "stationNo": stationNo,
"price": price, "price": price,
} }
result, err := a.AccessAPINoPage("price/updateStationPrice", jdParams, nil, nil, nil) result, err := a.AccessAPINoPage2("price/updateStationPrice", jdParams, nil, nil, nil, trackInfo)
if err == nil && result != nil { if err == nil && result != nil {
return utils.Interface2String(result), nil return utils.Interface2String(result), nil
} }
@@ -165,7 +165,7 @@ func (a *API) handleBatchOpResult(batchCount int, result interface{}, tagName st
// 根据商家商品编码和商家门店编码批量修改现货库存接口 // 根据商家商品编码和商家门店编码批量修改现货库存接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=10812f9fc7ee4564b552f19270a7e92e // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=10812f9fc7ee4564b552f19270a7e92e
func (a *API) BatchUpdateCurrentQtys(outStationNo, stationNo string, skuStockList []*SkuStock, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) { func (a *API) BatchUpdateCurrentQtys(trackInfo, outStationNo, stationNo string, skuStockList []*SkuStock, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) {
if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") { if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") {
return nil, errors.New("outStationNo and stationNo can not all be empty or have value") return nil, errors.New("outStationNo and stationNo can not all be empty or have value")
} }
@@ -178,7 +178,7 @@ func (a *API) BatchUpdateCurrentQtys(outStationNo, stationNo string, skuStockLis
} else { } else {
jdParams["stationNo"] = stationNo jdParams["stationNo"] = stationNo
} }
result, err := a.AccessAPINoPage("stock/batchUpdateCurrentQtys", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0")) result, err := a.AccessAPINoPage2("stock/batchUpdateCurrentQtys", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo)
if result != nil { if result != nil {
var err2 error var err2 error
if responseList, err2 = a.handleBatchOpResult(len(skuStockList), result, ""); err2 != nil && err == nil { if responseList, err2 = a.handleBatchOpResult(len(skuStockList), result, ""); err2 != nil && err == nil {
@@ -191,14 +191,14 @@ func (a *API) BatchUpdateCurrentQtys(outStationNo, stationNo string, skuStockLis
// 根据商家商品编码和商家门店编码更新门店现货库存接口 // 根据商家商品编码和商家门店编码更新门店现货库存接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=a78664d4ead349da95d2f4576ed18d7f // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=a78664d4ead349da95d2f4576ed18d7f
// 此接口基本可以不用 // 此接口基本可以不用
func (a *API) StockUpdate(outStationNo string, outSkuID string, currentQty int) error { func (a *API) StockUpdate(trackInfo string, outStationNo string, outSkuID string, currentQty int) error {
// !这个接口的stationNo与skuId好像本身就写错了的 // !这个接口的stationNo与skuId好像本身就写错了的
jdParams := map[string]interface{}{ jdParams := map[string]interface{}{
"stationNo": outStationNo, "stationNo": outStationNo,
"skuId": outSkuID, "skuId": outSkuID,
"currentQty": currentQty, "currentQty": currentQty,
} }
_, err := a.AccessAPINoPage("stock/update", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "", "0")) _, err := a.AccessAPINoPage2("stock/update", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "", "0"), trackInfo)
if err == nil { if err == nil {
return nil return nil
} }
@@ -208,13 +208,13 @@ func (a *API) StockUpdate(outStationNo string, outSkuID string, currentQty int)
// 根据到家商品编码和到家门店编码更新门店现货库存 // 根据到家商品编码和到家门店编码更新门店现货库存
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=200&apiid=af70e699d4974e1683128742018f6381 // https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=200&apiid=af70e699d4974e1683128742018f6381
// 单商品用此接口 // 单商品用此接口
func (a *API) UpdateCurrentQty(stationNo string, skuID int64, currentQty int) error { func (a *API) UpdateCurrentQty(trackInfo string, stationNo string, skuID int64, currentQty int) error {
jdParams := map[string]interface{}{ jdParams := map[string]interface{}{
"stationNo": stationNo, "stationNo": stationNo,
"skuId": skuID, "skuId": skuID,
"currentQty": currentQty, "currentQty": currentQty,
} }
_, err := a.AccessAPINoPage("update/currentQty", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "", "0")) _, err := a.AccessAPINoPage2("update/currentQty", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "", "0"), trackInfo)
if err == nil { if err == nil {
return nil return nil
} }
@@ -224,11 +224,11 @@ func (a *API) UpdateCurrentQty(stationNo string, skuID int64, currentQty int) er
// 根据到家商品编码和到家门店编码批量修改门店商品可售状态接口 // 根据到家商品编码和到家门店编码批量修改门店商品可售状态接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=b783a508e2cf4aca94681e4eed9af5bc // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=b783a508e2cf4aca94681e4eed9af5bc
// 尽量不用这个接口用下面那个原因是这个不支持设置操作人BatchUpdateVendibility可以 // 尽量不用这个接口用下面那个原因是这个不支持设置操作人BatchUpdateVendibility可以
func (a *API) UpdateVendibility(listBaseStockCenterRequest []*QueryStockRequest) (responseList []*StoreSkuBatchUpdateResponse, err error) { func (a *API) UpdateVendibility(trackInfo string, listBaseStockCenterRequest []*QueryStockRequest) (responseList []*StoreSkuBatchUpdateResponse, err error) {
jdParams := map[string]interface{}{ jdParams := map[string]interface{}{
"listBaseStockCenterRequest": listBaseStockCenterRequest, "listBaseStockCenterRequest": listBaseStockCenterRequest,
} }
result, err := a.AccessAPINoPage("stock/updateVendibility", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0")) result, err := a.AccessAPINoPage2("stock/updateVendibility", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo)
if result != nil { if result != nil {
var err2 error var err2 error
if responseList, err2 = a.handleBatchOpResult(len(listBaseStockCenterRequest), result, ""); err2 != nil && err == nil { if responseList, err2 = a.handleBatchOpResult(len(listBaseStockCenterRequest), result, ""); err2 != nil && err == nil {
@@ -240,7 +240,7 @@ func (a *API) UpdateVendibility(listBaseStockCenterRequest []*QueryStockRequest)
// 根据商家商品编码和门店编码批量修改门店商品可售状态接口 // 根据商家商品编码和门店编码批量修改门店商品可售状态接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=ac6f559ebabf4b70bc423687638e07c1 // https://opendj.jd.com/staticnew/widgets/resources.html?groupid=200&apiid=ac6f559ebabf4b70bc423687638e07c1
func (a *API) BatchUpdateVendibility(outStationNo, stationNo string, stockVendibilityList []*StockVendibility, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) { func (a *API) BatchUpdateVendibility(trackInfo, outStationNo, stationNo string, stockVendibilityList []*StockVendibility, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) {
if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") { if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") {
return nil, errors.New("outStationNo and stationNo can not all be empty or have value") return nil, errors.New("outStationNo and stationNo can not all be empty or have value")
} }
@@ -253,7 +253,7 @@ func (a *API) BatchUpdateVendibility(outStationNo, stationNo string, stockVendib
} else { } else {
jdParams["stationNo"] = stationNo jdParams["stationNo"] = stationNo
} }
result, err := a.AccessAPINoPage("stock/batchUpdateVendibility", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0")) result, err := a.AccessAPINoPage2("stock/batchUpdateVendibility", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo)
if result != nil { if result != nil {
var err2 error var err2 error
if responseList, err2 = a.handleBatchOpResult(len(stockVendibilityList), result, ""); err2 != nil && err == nil { if responseList, err2 = a.handleBatchOpResult(len(stockVendibilityList), result, ""); err2 != nil && err == nil {

View File

@@ -46,7 +46,7 @@ func TestQueryStockCenter(t *testing.T) {
} }
func TestBatchUpdateVendibility(t *testing.T) { func TestBatchUpdateVendibility(t *testing.T) {
result, err := api.BatchUpdateVendibility("100130", "", []*StockVendibility{ result, err := api.BatchUpdateVendibility("", "100130", "", []*StockVendibility{
&StockVendibility{ &StockVendibility{
OutSkuId: "26919", OutSkuId: "26919",
DoSale: true, DoSale: true,
@@ -59,7 +59,7 @@ func TestBatchUpdateVendibility(t *testing.T) {
} }
func TestUpdateVendibility(t *testing.T) { func TestUpdateVendibility(t *testing.T) {
result, err := api.UpdateVendibility([]*QueryStockRequest{ result, err := api.UpdateVendibility("", []*QueryStockRequest{
&QueryStockRequest{ &QueryStockRequest{
StationNo: "11053496", StationNo: "11053496",
SkuId: 2012224772, SkuId: 2012224772,

View File

@@ -186,7 +186,7 @@ func (a *API) FullDiscountBatchSave(poiCode string, actInfo *FullDiscountActInfo
if actInfo.ActType == ActTypeSkuFullDiscount { if actInfo.ActType == ActTypeSkuFullDiscount {
params["app_foods"] = string(utils.MustMarshal(actSkuList)) params["app_foods"] = string(utils.MustMarshal(actSkuList))
} }
result, err := a.AccessAPI2("act/full/discount/batchsave", false, params, resultKeySuccessMsg) result, err := a.AccessAPI2("act/full/discount/batchsave", false, params, resultKeySuccessMsg, "")
if err == nil { if err == nil {
err = utils.UnmarshalUseNumber([]byte(result.(string)), &actIDList) err = utils.UnmarshalUseNumber([]byte(result.(string)), &actIDList)
} }
@@ -322,7 +322,7 @@ func (a *API) RetailDiscountBatchSave(poiCode string, actData []*RetailDiscountA
result, err := a.AccessAPI2("act/retail/discount/batchsave", false, map[string]interface{}{ result, err := a.AccessAPI2("act/retail/discount/batchsave", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
"act_data": string(utils.MustMarshal(actData)), "act_data": string(utils.MustMarshal(actData)),
}, resultKeySuccessMsg) }, resultKeySuccessMsg, "")
if err == nil { if err == nil {
err = utils.UnmarshalUseNumber([]byte(result.(string)), &actResult) err = utils.UnmarshalUseNumber([]byte(result.(string)), &actResult)
} }
@@ -394,7 +394,7 @@ func (a *API) InStoreCouponBatchSave(poiCode string, limitTime *LimitTime, coupo
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
"limit_time": string(utils.MustMarshal(limitTime)), "limit_time": string(utils.MustMarshal(limitTime)),
"act_data": string(utils.MustMarshal(couponInfoList)), "act_data": string(utils.MustMarshal(couponInfoList)),
}, resultKeySuccessMsg) }, resultKeySuccessMsg, "")
if err == nil { if err == nil {
err = utils.UnmarshalUseNumber([]byte(result.(string)), &couponResultList) err = utils.UnmarshalUseNumber([]byte(result.(string)), &couponResultList)
} }

View File

@@ -112,7 +112,7 @@ func (a *API) signParams(signURL string, params map[string]interface{}) string {
return fmt.Sprintf("%x", md5.Sum([]byte(finalStr))) return fmt.Sprintf("%x", md5.Sum([]byte(finalStr)))
} }
func (a *API) AccessAPI2(cmd string, isGet bool, bizParams map[string]interface{}, resultKey string) (retVal interface{}, err error) { func (a *API) AccessAPI2(cmd string, isGet bool, bizParams map[string]interface{}, resultKey, trackInfo string) (retVal interface{}, err error) {
params := make(map[string]interface{}) params := make(map[string]interface{})
params["timestamp"] = time.Now().Unix() params["timestamp"] = time.Now().Unix()
params["app_id"] = a.appID params["app_id"] = a.appID
@@ -155,6 +155,9 @@ func (a *API) AccessAPI2(cmd string, isGet bool, bizParams map[string]interface{
} }
request.Header.Set("charset", "UTF-8") request.Header.Set("charset", "UTF-8")
} }
if trackInfo != "" {
request.Header.Set(platformapi.KeyTrackInfo, trackInfo)
}
// request.Close = true //todo 为了性能考虑还是不要关闭 // request.Close = true //todo 为了性能考虑还是不要关闭
return request return request
}, },
@@ -184,5 +187,5 @@ func (a *API) AccessAPI2(cmd string, isGet bool, bizParams map[string]interface{
} }
func (a *API) AccessAPI(cmd string, isGet bool, bizParams map[string]interface{}) (retVal interface{}, err error) { func (a *API) AccessAPI(cmd string, isGet bool, bizParams map[string]interface{}) (retVal interface{}, err error) {
return a.AccessAPI2(cmd, isGet, bizParams, resultKeyData) return a.AccessAPI2(cmd, isGet, bizParams, resultKeyData, "")
} }

View File

@@ -550,3 +550,14 @@ func (a *API) GetOrderIdByDaySeq(poiCode string, dateTime time.Time, seqStart, s
} }
return vendorOrderIDs, err return vendorOrderIDs, err
} }
// 众包配送单追加小费
// https://developer.waimai.meituan.com/home/docDetail/158
func (a *API) OrderUpdateTip(orderID int64, tipAmount float64) (err error) {
params := map[string]interface{}{
KeyOrderID: orderID,
"tip_amount": tipAmount,
}
_, err = a.AccessAPI("order/zhongbao/update/tip", true, params)
return err
}

View File

@@ -154,16 +154,6 @@ func (a *API) RetailCatList(poiCode string) (retailCatList []*RetailCategoryInfo
return nil, err return nil, err
} }
// 商品名最长30个字符非字节
// 不能包含敏感词:特级
func (a *API) RetailInitData(poiCode, foodCode string, params map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/initdata", false, utils.MergeMaps(map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
}, params))
return err
}
// offset 从0开始limit最大不能超过200 // offset 从0开始limit最大不能超过200
// 返回的app_poi_code始终是空手动建的商品app_food_code也为空导致无法通过API删除 // 返回的app_poi_code始终是空手动建的商品app_food_code也为空导致无法通过API删除
func (a *API) RetailList(poiCode string, offset, limit int) (foodList []*AppFood, err error) { func (a *API) RetailList(poiCode string, offset, limit int) (foodList []*AppFood, err error) {
@@ -209,33 +199,42 @@ func handleRetailBatchResultByRegexp(result interface{}) (failedFoodList []*AppF
return failedFoodList, err return failedFoodList, err
} }
func (a *API) RetailBatchInitData(poiCode string, foodDataList []map[string]interface{}) (failedFoodList []*AppFoodResult, err error) { // 商品名最长30个字符非字节
func (a *API) RetailInitData(trackInfo, poiCode, foodCode string, params map[string]interface{}) (err error) {
_, err = a.AccessAPI2("retail/initdata", false, utils.MergeMaps(map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
}, params), resultKeyData, trackInfo)
return err
}
func (a *API) RetailBatchInitData(trackInfo, poiCode string, foodDataList []map[string]interface{}) (failedFoodList []*AppFoodResult, err error) {
result, err := a.AccessAPI2("retail/batchinitdata", false, map[string]interface{}{ result, err := a.AccessAPI2("retail/batchinitdata", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodDataList)), "food_data": string(utils.MustMarshal(foodDataList)),
}, resultKeyMsg) }, resultKeyMsg, trackInfo)
if err == nil { if err == nil {
failedFoodList, err = handleRetailBatchResult(result) failedFoodList, err = handleRetailBatchResult(result)
} }
return failedFoodList, err return failedFoodList, err
} }
func (a *API) RetailSkuPrice(poiCode string, foodData []*BareStoreFoodInfo) (failedFoodList []*AppFoodResult, err error) { func (a *API) RetailSkuPrice(trackInfo, poiCode string, foodData []*BareStoreFoodInfo) (failedFoodList []*AppFoodResult, err error) {
result, err := a.AccessAPI2("retail/sku/price", false, map[string]interface{}{ result, err := a.AccessAPI2("retail/sku/price", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)), "food_data": string(utils.MustMarshal(foodData)),
}, resultKeyMsg) }, resultKeyMsg, trackInfo)
if err == nil { if err == nil {
failedFoodList, err = handleRetailBatchResult(result) failedFoodList, err = handleRetailBatchResult(result)
} }
return failedFoodList, err return failedFoodList, err
} }
func (a *API) RetailSkuStock(poiCode string, foodData []*BareStoreFoodInfo) (failedFoodList []*AppFoodResult, err error) { func (a *API) RetailSkuStock(trackInfo, poiCode string, foodData []*BareStoreFoodInfo) (failedFoodList []*AppFoodResult, err error) {
result, err := a.AccessAPI2("retail/sku/stock", false, map[string]interface{}{ result, err := a.AccessAPI2("retail/sku/stock", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)), "food_data": string(utils.MustMarshal(foodData)),
}, resultKeyMsg) }, resultKeyMsg, trackInfo)
if err == nil { if err == nil {
failedFoodList, err = handleRetailBatchResult(result) failedFoodList, err = handleRetailBatchResult(result)
} }
@@ -243,12 +242,12 @@ func (a *API) RetailSkuStock(poiCode string, foodData []*BareStoreFoodInfo) (fai
} }
// retail/sku/sellStatus在部分失败时会返回错误其它相应的批处理函数则会返回成功 // retail/sku/sellStatus在部分失败时会返回错误其它相应的批处理函数则会返回成功
func (a *API) RetailSkuSellStatus(poiCode string, foodData []*BareStoreFoodInfo, sellStatus int) (failedFoodList []*AppFoodResult, err error) { func (a *API) RetailSkuSellStatus(trackInfo, poiCode string, foodData []*BareStoreFoodInfo, sellStatus int) (failedFoodList []*AppFoodResult, err error) {
_, err = a.AccessAPI2("retail/sku/sellStatus", false, map[string]interface{}{ _, err = a.AccessAPI2("retail/sku/sellStatus", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)), "food_data": string(utils.MustMarshal(foodData)),
"sell_status": sellStatus, "sell_status": sellStatus,
}, resultKeyMsg) }, resultKeyMsg, trackInfo)
if err != nil { if err != nil {
if errExt, ok := err.(*utils.ErrorWithCode); ok { if errExt, ok := err.(*utils.ErrorWithCode); ok {
failedFoodList, _ = handleRetailBatchResultByRegexp(errExt.ErrMsg()) failedFoodList, _ = handleRetailBatchResultByRegexp(errExt.ErrMsg())
@@ -281,20 +280,20 @@ func (a *API) RetailSkuSave(poiCode, foodCode string, standardSkus, unstandardSk
return err return err
} }
func (a *API) RetailDelete(poiCode, foodCode string) (err error) { func (a *API) RetailDelete(trackInfo, poiCode, foodCode string) (err error) {
_, err = a.AccessAPI("retail/delete", false, map[string]interface{}{ _, err = a.AccessAPI2("retail/delete", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode, KeyAppFoodCode: foodCode,
}) }, resultKeyData, trackInfo)
return err return err
} }
func (a *API) RetailSkuDelete(poiCode, foodCode, skuID string) (err error) { func (a *API) RetailSkuDelete(trackInfo, poiCode, foodCode, skuID string) (err error) {
_, err = a.AccessAPI("retail/sku/delete", false, map[string]interface{}{ _, err = a.AccessAPI2("retail/sku/delete", false, map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode, KeyAppFoodCode: foodCode,
"sku_id": skuID, "sku_id": skuID,
}) }, resultKeyData, trackInfo)
return err return err
} }
@@ -310,7 +309,7 @@ func (a *API) RetailGetSpTagIds() (tagIds []*RetailTag, err error) {
} }
// 此接口将申请授权后方可接入 // 此接口将申请授权后方可接入
func (a *API) RetailCatSkuBatchDelete(poiCode string, catNames []string, secondaryCatNames []string, foodCodes []string) (err error) { func (a *API) RetailCatSkuBatchDelete(trackInfo, poiCode string, catNames []string, secondaryCatNames []string, foodCodes []string) (err error) {
params := map[string]interface{}{ params := map[string]interface{}{
KeyAppPoiCode: poiCode, KeyAppPoiCode: poiCode,
} }
@@ -323,7 +322,7 @@ func (a *API) RetailCatSkuBatchDelete(poiCode string, catNames []string, seconda
if len(foodCodes) > 0 { if len(foodCodes) > 0 {
params["app_food_codes"] = strings.Join(foodCodes, ",") params["app_food_codes"] = strings.Join(foodCodes, ",")
} }
_, err = a.AccessAPI("retailCat/batchdelete/catandretail", false, params) _, err = a.AccessAPI2("retailCat/batchdelete/catandretail", false, params, resultKeyData, trackInfo)
return err return err
} }

View File

@@ -70,21 +70,21 @@ func TestRetailGetSpTagIds(t *testing.T) {
} }
func TestRetailCatSkuBatchDelete(t *testing.T) { func TestRetailCatSkuBatchDelete(t *testing.T) {
err := api.RetailCatSkuBatchDelete(testPoiCode, []string{"测试一级类别"}, nil, nil) err := api.RetailCatSkuBatchDelete(utils.GetUUID(), testPoiCode, []string{"测试一级类别"}, nil, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestRetailDelete(t *testing.T) { func TestRetailDelete(t *testing.T) {
err := api.RetailDelete(testPoiCode, "614") err := api.RetailDelete(utils.GetUUID(), testPoiCode, "614")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestRetailBatchInitData(t *testing.T) { func TestRetailBatchInitData(t *testing.T) {
failedFoods, err := api.RetailBatchInitData(testPoiCode, []map[string]interface{}{ failedFoods, err := api.RetailBatchInitData(utils.GetUUID(), testPoiCode, []map[string]interface{}{
map[string]interface{}{ map[string]interface{}{
"app_food_code": "23841", "app_food_code": "23841",
"box_num": 0, "box_num": 0,
@@ -139,7 +139,7 @@ func TestRetailBatchInitData(t *testing.T) {
} }
func TestRetailSkuPrice(t *testing.T) { func TestRetailSkuPrice(t *testing.T) {
result, err := api.RetailSkuPrice(testPoiCode, []*BareStoreFoodInfo{ result, err := api.RetailSkuPrice(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
&BareStoreFoodInfo{ &BareStoreFoodInfo{
AppFoodCode: "23841", AppFoodCode: "23841",
Skus: []*BareStoreSkuInfo{ Skus: []*BareStoreSkuInfo{
@@ -166,7 +166,7 @@ func TestRetailSkuPrice(t *testing.T) {
} }
func TestRetailSkuStock(t *testing.T) { func TestRetailSkuStock(t *testing.T) {
result, err := api.RetailSkuStock(testPoiCode, []*BareStoreFoodInfo{ result, err := api.RetailSkuStock(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
&BareStoreFoodInfo{ &BareStoreFoodInfo{
AppFoodCode: "23841", AppFoodCode: "23841",
Skus: []*BareStoreSkuInfo{ Skus: []*BareStoreSkuInfo{
@@ -194,7 +194,7 @@ func TestRetailSkuStock(t *testing.T) {
} }
func TestRetailSkuSellStatus(t *testing.T) { func TestRetailSkuSellStatus(t *testing.T) {
result, err := api.RetailSkuSellStatus(testPoiCode, []*BareStoreFoodInfo{ result, err := api.RetailSkuSellStatus(utils.GetUUID(), testPoiCode, []*BareStoreFoodInfo{
&BareStoreFoodInfo{ &BareStoreFoodInfo{
AppFoodCode: "23841", AppFoodCode: "23841",
Skus: []*BareStoreSkuInfo{ Skus: []*BareStoreSkuInfo{

View File

@@ -21,6 +21,8 @@ const (
DefSleepSecondWhenExceedLimit = 3 * time.Second DefSleepSecondWhenExceedLimit = 3 * time.Second
DefMaxRecoverableRetryCount = 1 DefMaxRecoverableRetryCount = 1
DefMaxExceedLimitRetryCount = 25 DefMaxExceedLimitRetryCount = 25
KeyTrackInfo = "TrackInfo"
) )
type APIRetryConfig struct { type APIRetryConfig struct {
@@ -98,24 +100,30 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
request.Body = ioutil.NopCloser(io.TeeReader(request.Body, savedBuf)) request.Body = ioutil.NopCloser(io.TeeReader(request.Body, savedBuf))
} }
beginTime := time.Now() beginTime := time.Now()
trackID := utils.GetUUID() trackInfo := request.Header.Get(KeyTrackInfo)
baseapi.SugarLogger.Debugf("begin AccessPlatformAPIWithRetry:%s do:%s url:%v", trackID, request.Method, request.URL) if trackInfo == "" {
trackInfo = utils.GetUUID()
// request.Header.Set(KeyTrackInfo, trackID)
} else {
request.Header.Del(KeyTrackInfo)
}
baseapi.SugarLogger.Debugf("begin AccessPlatformAPIWithRetry:%s do:%s url:%v", trackInfo, request.Method, request.URL)
response, err := client.Do(request) response, err := client.Do(request)
baseapi.SugarLogger.Debugf("end AccessPlatformAPIWithRetry:%s do url:%v, request:%s", trackID, request.URL, getClonedData(request.URL, savedBuf)) baseapi.SugarLogger.Debugf("end AccessPlatformAPIWithRetry:%s do url:%v, request:%s", trackInfo, request.URL, getClonedData(request.URL, savedBuf))
if err != nil { if err != nil {
baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry:%s client.Get return err:%v", trackID, err) baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry:%s client.Get return err:%v", trackInfo, err)
err, ok := err.(net.Error) err, ok := err.(net.Error)
recoverableErrorRetryCount++ recoverableErrorRetryCount++
if ok /*&& err.Timeout()*/ && recoverableErrorRetryCount <= config.MaxRecoverableRetryCount { // 只要是网络错误都重试 if ok /*&& err.Timeout()*/ && recoverableErrorRetryCount <= config.MaxRecoverableRetryCount { // 只要是网络错误都重试
continue continue
} else { } else {
baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s access api url:%v, request:%v, error:%v", trackID, request.URL, getClonedData(request.URL, savedBuf), err) baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s access api url:%v, request:%v, error:%v", trackInfo, request.URL, getClonedData(request.URL, savedBuf), err)
return ErrAPIAccessFailed return ErrAPIAccessFailed
} }
} }
usedMilliSecond := time.Now().Sub(beginTime) / time.Millisecond usedMilliSecond := time.Now().Sub(beginTime) / time.Millisecond
if usedMilliSecond > 5000 { if usedMilliSecond > 5000 {
baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s access api too slow, url:%v, request:%v, usedMilliSecond:%d", trackID, request.URL, getClonedData(request.URL, savedBuf), usedMilliSecond) baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s access api too slow, url:%v, request:%v, usedMilliSecond:%d", trackInfo, request.URL, getClonedData(request.URL, savedBuf), usedMilliSecond)
} }
defer response.Body.Close() defer response.Body.Close()
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
@@ -127,9 +135,9 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
} }
} }
if bodyData, err := ioutil.ReadAll(response.Body); err == nil { if bodyData, err := ioutil.ReadAll(response.Body); err == nil {
baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s HTTP code is:%d, url:%v, request:%v, response:%s", trackID, response.StatusCode, request.URL, getClonedData(request.URL, savedBuf), string(bodyData)) baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s HTTP code is:%d, url:%v, request:%v, response:%s", trackInfo, response.StatusCode, request.URL, getClonedData(request.URL, savedBuf), string(bodyData))
} else { } else {
baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s ioutil.ReadAll failed, HTTP code is:%d, url:%v, request:%v, error:%v", trackID, response.StatusCode, request.URL, getClonedData(request.URL, savedBuf), err) baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s ioutil.ReadAll failed, HTTP code is:%d, url:%v, request:%v, error:%v", trackInfo, response.StatusCode, request.URL, getClonedData(request.URL, savedBuf), err)
} }
return ErrHTTPCodeIsNot200 return ErrHTTPCodeIsNot200
} }
@@ -140,14 +148,14 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
) )
bodyData, err := ioutil.ReadAll(response.Body) bodyData, err := ioutil.ReadAll(response.Body)
if err != nil { if err != nil {
baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s ioutil.ReadAll failed, url:%v, request:%v, error:%v", trackID, request.URL, getClonedData(request.URL, savedBuf), err) baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s ioutil.ReadAll failed, url:%v, request:%v, error:%v", trackInfo, request.URL, getClonedData(request.URL, savedBuf), err)
errLevel = ErrLevelRecoverableErr // 读取数据错误,或数据格式错误认为是偶发情况,重试 errLevel = ErrLevelRecoverableErr // 读取数据错误,或数据格式错误认为是偶发情况,重试
} else { } else {
if err = utils.TryUnmarshalUseNumber(bodyData, &bodyMap); err != nil { if err = utils.TryUnmarshalUseNumber(bodyData, &bodyMap); err != nil {
parseJSONErr = err parseJSONErr = err
err = nil // 尝试忽略解析成json错 err = nil // 尝试忽略解析成json错
} else { } else {
baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry:%s url:%v, response:%s", trackID, request.URL, utils.Format4Output(bodyMap, true)) baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry:%s url:%v, response:%s", trackInfo, request.URL, utils.Format4Output(bodyMap, true))
} }
errLevel, err = handleResponse(response, string(bodyData), bodyMap) errLevel, err = handleResponse(response, string(bodyData), bodyMap)
if err != nil && parseJSONErr != nil { if err != nil && parseJSONErr != nil {
@@ -157,7 +165,7 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
if bodyDataLen > maxOutputLen { if bodyDataLen > maxOutputLen {
bodyData2 = bodyData2[:maxOutputLen] bodyData2 = bodyData2[:maxOutputLen]
} }
baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s TryUnmarshalUseNumber failed, url:%v, request:%v, error:%v, bodyData:%s", trackID, request.URL, getClonedData(request.URL, savedBuf), parseJSONErr, string(bodyData2)) baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s TryUnmarshalUseNumber failed, url:%v, request:%v, error:%v, bodyData:%s", trackInfo, request.URL, getClonedData(request.URL, savedBuf), parseJSONErr, string(bodyData2))
} }
} }
@@ -175,7 +183,7 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
continue continue
} }
} }
baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s failed, url:%v, response:%s, error:%v", trackID, request.URL, utils.Format4Output(bodyMap, true), err) baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s failed, url:%v, response:%s, error:%v", trackInfo, request.URL, utils.Format4Output(bodyMap, true), err)
return err return err
} }
} }