diff --git a/platformapi/ebaiapi/shop_sku.go b/platformapi/ebaiapi/shop_sku.go index a777d9ad..350c8117 100644 --- a/platformapi/ebaiapi/shop_sku.go +++ b/platformapi/ebaiapi/shop_sku.go @@ -184,6 +184,7 @@ func (a *API) SkuList(shopID string, params *SkuListParams) (skuInfo *PageDataIn return skuInfo, err } +// 饿百商品名可以相同,不会报错 func (a *API) SkuCreate(shopID string, customSkuID int, params map[string]interface{}) (skuID int64, err error) { defParams := map[string]interface{}{ KeyShopID: shopID, @@ -208,24 +209,6 @@ func (a *API) SkuCreate(shopID string, customSkuID int, params map[string]interf return 0, err } -func (a *API) GetEbaiSkuIDFromError(shopID string, customSkuID int, err error) int64 { - if err2, ok := err.(*utils.ErrorWithCode); ok && err2.IntCode() == 1 { - if strings.Index(err2.ErrMsg(), "shop sku exist") >= 0 { - searchResult := skuExistReg.FindStringSubmatch(err2.ErrMsg()) - if searchResult != nil && len(searchResult[1]) > 0 { - return utils.Str2Int64(searchResult[1]) - } - } else if strings.Index(err2.ErrMsg(), "商品已存在") >= 0 { - if skuInfo, err2 := a.SkuList(shopID, &SkuListParams{ - CustomSkuID: utils.Int2Str(customSkuID), - }); err2 == nil && skuInfo != nil && len(skuInfo.List) > 0 { - return skuInfo.List[0].SkuID - } - } - } - return 0 -} - func (a *API) SkuUpdate(shopID string, ebaiSkuID int64, params map[string]interface{}) (skuID int64, err error) { defParams := map[string]interface{}{ KeyShopID: shopID, @@ -384,3 +367,40 @@ func interface2Cat(data interface{}, level int) (cat *CategoryInfo) { } return cat } + +func IsErrCategoryExist(err error) (isExist bool) { + return utils.IsErrMatch(err, "1", []string{"商户已存在该自定义分类"}) +} + +func IsErrCategoryNotExist(err error) (isExist bool) { + return utils.IsErrMatch(err, "", []string{"不存在该自定义分类ID", "商户不存在该自定义分类"}) +} + +func IsErrSkuExist(err error) (isExist bool) { + return utils.IsErrMatch(err, "1", []string{"商品已存在", "shop sku exist"}) +} + +func IsErrSkuNotExist(err error) (isExist bool) { + return utils.IsErrMatch(err, "1", []string{"sku_id与shop_id不匹配", "SKU不存在或者已经被删除"}) +} + +func (a *API) GetEbaiSkuIDFromCustomID(shopID, customSkuID string) (ebaiSkuID int64) { + if skuInfo, err2 := a.SkuList(shopID, &SkuListParams{ + CustomSkuID: customSkuID, + }); err2 == nil && skuInfo != nil && len(skuInfo.List) > 0 { + ebaiSkuID = skuInfo.List[0].SkuID + } + return ebaiSkuID +} + +func (a *API) GetEbaiCatIDFromName(shopID, catName string) (ebaiCatID int64) { + if catList, err2 := a.ShopCategoryGet(shopID); err2 == nil { + for _, v := range catList { + if v.Name == catName { + ebaiCatID = v.CategoryID + break + } + } + } + return ebaiCatID +} diff --git a/platformapi/mtwmapi/mtwmapi.go b/platformapi/mtwmapi/mtwmapi.go index db6ddb72..15f50f18 100644 --- a/platformapi/mtwmapi/mtwmapi.go +++ b/platformapi/mtwmapi/mtwmapi.go @@ -43,7 +43,10 @@ const ( const ( ErrCodeSysErr = 700 // 系统错误,按美团外卖技术支持的说法,可当成需重试的错误 ErrCodeAccessLimited = 711 // 接口调用过于频繁,触发流控,请降低调用频率 - ErrCodeNoAppFood = 805 // 不存在此菜品 + + ErrCodeNoAppFood = 805 // 不存在此菜品 + ErrCodeSkuCategoryNotExist = 1021 // 菜品分类不存在 + ErrCodeSkuCategoryExist = 1037 // 菜品分类已存在 ) type API struct { diff --git a/platformapi/mtwmapi/mtwmapi_test.go b/platformapi/mtwmapi/mtwmapi_test.go index ab5af071..676e0f14 100644 --- a/platformapi/mtwmapi/mtwmapi_test.go +++ b/platformapi/mtwmapi/mtwmapi_test.go @@ -21,10 +21,10 @@ func init() { baseapi.Init(sugarLogger) // 菜市 - // api = New("589", "a81eb3df418d83d6a1a4b7c572156d2f", "") + api = New("589", "a81eb3df418d83d6a1a4b7c572156d2f", "") // 果园 - api = New("4123", "df2c88338b85f830cebce2a9eab56628", "") + // api = New("4123", "df2c88338b85f830cebce2a9eab56628", "") // api.SetUserCookie("_lx_utm", "utm_source%3D60066") // api.SetUserCookie("_lxsdk", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") diff --git a/platformapi/mtwmapi/retail.go b/platformapi/mtwmapi/retail.go index 5c685b66..634e6ea3 100644 --- a/platformapi/mtwmapi/retail.go +++ b/platformapi/mtwmapi/retail.go @@ -303,3 +303,15 @@ func interface2CatList(data interface{}, level int, interface2CatHandler func(da } return cats } + +func IsErrCategoryExist(err error) (isExist bool) { + return utils.IsErrMatch(err, utils.Int2Str(ErrCodeSkuCategoryExist), nil) +} + +func IsErrCategoryNotExist(err error) (isExist bool) { + return utils.IsErrMatch(err, utils.Int2Str(ErrCodeSkuCategoryNotExist), nil) +} + +func IsErrSkuNotExist(err error) (isExist bool) { + return utils.IsErrMatch(err, utils.Int2Str(ErrCodeNoAppFood), nil) +} diff --git a/utils/errorwithcode.go b/utils/errorwithcode.go index eb474d64..8b1b2d6e 100644 --- a/utils/errorwithcode.go +++ b/utils/errorwithcode.go @@ -3,6 +3,7 @@ package utils import ( "fmt" "strconv" + "strings" ) type ErrorWithCode struct { @@ -52,3 +53,19 @@ func (e *ErrorWithCode) IntCode() int { func (e *ErrorWithCode) ErrMsg() string { return e.errMsg } + +func IsErrMatch(err error, strCode string, strList []string) (isMatch bool) { + if err != nil { + if codeErr, ok := err.(*ErrorWithCode); ok { + if strCode == "" || codeErr.Code() == strCode { + for _, v := range strList { + if strings.Index(codeErr.ErrMsg(), v) > 0 { + isMatch = true + break + } + } + } + } + } + return isMatch +}