From 8be594e91d0e5a1c9fa74cd7fac0fc750119c0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Wed, 13 May 2020 09:41:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=AC=E4=B8=9C=E5=95=86=E5=9F=8E=E4=BE=9D?= =?UTF-8?q?=E7=A8=80=E4=B8=8B=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/jdshopapi/jdshopapi.go | 2 + platformapi/jdshopapi/sku.go | 85 ++++++++++++++++++++++-------- platformapi/jdshopapi/sku_test.go | 48 +++++++++-------- 3 files changed, 92 insertions(+), 43 deletions(-) diff --git a/platformapi/jdshopapi/jdshopapi.go b/platformapi/jdshopapi/jdshopapi.go index ab67503e..35577b15 100644 --- a/platformapi/jdshopapi/jdshopapi.go +++ b/platformapi/jdshopapi/jdshopapi.go @@ -125,10 +125,12 @@ func (a *API) AccessAPI2(action string, url string, bizParams map[string]interfa signStr := a.signParam(params) params["sign"] = signStr fullURL := utils.GenerateGetURL(url, "", nil) + // rParam, _ := json.Marshal(bizParams) // delete(params, "ware") // delete(params, "skus") // ware := bizParams["ware"].(string) // skus := bizParams["skus"].(string) + // str := strings.ReplaceAll(string(rParam), "\\", "") err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.URLQueryEscape(utils.Map2URLValues(params).Encode()))) diff --git a/platformapi/jdshopapi/sku.go b/platformapi/jdshopapi/sku.go index ea90211b..c42334cc 100644 --- a/platformapi/jdshopapi/sku.go +++ b/platformapi/jdshopapi/sku.go @@ -120,16 +120,22 @@ type CreateSkuParamAttrs struct { AttrValues []string `json:"attrValues"` //是 无 下沉到sku属性值 单选或者多选 通过接口jingdong.category.read.findValuesByAttrIdUnlimit获取 输入类型请手动输入 } +type CreateSkuParam struct { + Ware *CreateSkuParamWare `json:"ware"` + Skus []*CreateSkuParamSkus `json:"skus"` +} + type CreateSkuResult struct { - WareID int `json:"wareId"` - skus []struct { - SkuID int `json:"skuId"` - saleAttrs []struct { - AttrValueAlias []string `json:"attrValueAlias"` - AttrID string `json:"attrId"` - AttrValues []string `json:"attrValues"` - } - } + WareID int64 `json:"wareId"` + Skus []struct { + WareID int64 `json:"wareId"` + SkuID int64 `json:"skuId"` + SaleAttrs []struct { + // AttrValueAlias []string `json:"attrValueAlias"` + AttrID string `json:"attrId"` + AttrValues []string `json:"attrValues"` + } `json:"saleAttrs"` + } `json:"skus"` } type UpdateWareParam struct { @@ -176,11 +182,13 @@ func (a *API) FindVendorCategories() (findVendorCategoriesResult []*FindVendorCa //京东商城发布商品(创建商品) //https://open.jd.com/home/home#/doc/api?apiCateId=48&apiId=1379&apiName=jingdong.ware.write.add func (a *API) CreateWare(createSkuParamWare *CreateSkuParamWare, createSkuParamSkus []*CreateSkuParamSkus) (createSkuResult *CreateSkuResult, err error) { - wares, _ := json.Marshal(createSkuParamWare) - skus, _ := json.Marshal(createSkuParamSkus) + c := &CreateSkuParam{ + Ware: createSkuParamWare, + Skus: createSkuParamSkus, + } + cc, _ := json.Marshal(c) result, err := a.AccessAPI2("jingdong.ware.write.add", prodURL, map[string]interface{}{ - "ware": string(wares), - "skus": string(skus), + "360buy_param_json": string(cc), }) if err == nil { utils.Map2StructByJson(result["jingdong_ware_write_add_responce"].(map[string]interface{})["ware"], &createSkuResult, false) @@ -249,34 +257,69 @@ func (a *API) DeleteWare(wareId int) (err error) { return err } +type FindAttrsResult struct { + ID int `json:"id"` + OrderSort int `json:"orderSort"` + InputType int `json:"inputType"` + Name string `json:"name"` + Features []struct { + Fvalue string `json:"fvalue"` + Key string `json:"key"` + } `json:"features"` + CatID int `json:"catId"` + AttributeType int `json:"attributeType"` + IsRequired bool `json:"isRequired"` +} + //获取类目属性ID //https://open.jd.com/home/home#/doc/api?apiCateId=62&apiId=2809&apiName=jingdong.category.read.findAttrsByCategoryIdUnlimitCate -func (a *API) FindAttrs(cid int) (err error) { - _, err = a.AccessAPI("jingdong.category.read.findAttrsByCategoryIdUnlimitCate", prodURL, map[string]interface{}{ +func (a *API) FindAttrs(cid int) (findAttrsResult []*FindAttrsResult, err error) { + result, err := a.AccessAPI("jingdong.category.read.findAttrsByCategoryIdUnlimitCate", prodURL, map[string]interface{}{ "cid": cid, + // "attributeType": 4, }) - return err + if err == nil { + utils.Map2StructByJson(result["jingdong_category_read_findAttrsByCategoryIdUnlimitCate_responce"].(map[string]interface{})["findattrsbycategoryidunlimitcate_result"], &findAttrsResult, false) + } + return findAttrsResult, err +} + +type FindValuesByAttrIdResult struct { + ID int64 `json:"id"` + OrderSort int `json:"orderSort"` + Name string `json:"name"` + CatID int `json:"catId"` + AttID int `json:"attId"` } //获取类目属性值 //https://open.jd.com/home/home#/doc/api?apiCateId=62&apiId=2821&apiName=jingdong.category.read.findValuesByAttrIdUnlimit -func (a *API) FindValuesByAttrId(categoryAttrId int) (err error) { - _, err = a.AccessAPI("jingdong.category.read.findValuesByAttrIdUnlimit", prodURL, map[string]interface{}{ +func (a *API) FindValuesByAttrId(categoryAttrId int) (findValuesByAttrIdResult []*FindValuesByAttrIdResult, maxOrder int, err error) { + result, err := a.AccessAPI("jingdong.category.read.findValuesByAttrIdUnlimit", prodURL, map[string]interface{}{ "categoryAttrId": categoryAttrId, }) - return err + if err == nil { + utils.Map2StructByJson(result["jingdong_category_read_findValuesByAttrIdUnlimit_responce"].(map[string]interface{})["findvaluesbyattridunlimit_result"], &findValuesByAttrIdResult, false) + } + return findValuesByAttrIdResult, findValuesByAttrIdResult[len(findValuesByAttrIdResult)-1].OrderSort, err } //新增类目属性值 //https://open.jd.com/home/home#/doc/api?apiCateId=62&apiId=1262&apiName=jingdong.category.write.saveVenderAttrValue -func (a *API) SaveVenderAttrValue(attValue string, attributeId, categoryId, indexId int) (err error) { +func (a *API) SaveVenderAttrValue(attValue string, attributeId, categoryId, indexId int) (categoryAttrId int64, err error) { _, err = a.AccessAPI("jingdong.category.write.saveVenderAttrValue", prodURL, map[string]interface{}{ "attValue": attValue, "attributeId": attributeId, "categoryId": categoryId, "indexId": indexId, }) - return err + result, _, err := a.FindValuesByAttrId(attributeId) + for _, v := range result { + if v.OrderSort == indexId { + categoryAttrId = v.ID + } + } + return categoryAttrId, err } //设置sku库存 diff --git a/platformapi/jdshopapi/sku_test.go b/platformapi/jdshopapi/sku_test.go index 7a1c41b7..e91ba06e 100644 --- a/platformapi/jdshopapi/sku_test.go +++ b/platformapi/jdshopapi/sku_test.go @@ -134,23 +134,23 @@ func TestCreateSku(t *testing.T) { var attrs2 []*CreateSkuParamAttrs var attrs3 []*CreateSkuParamAttrs ware := &CreateSkuParamWare{ - Title: "测试商品1", - ShopCategorys: []int{11}, - CategoryID: 13577, - TransportID: TransportID, - MobileDesc: "测试", - Introduction: "测试", - WareStatus: 8, - OuterID: "1", - Weight: 2300, - Height: 100, - Length: 100, - Width: 100, - JdPrice: 20, - MarketPrice: 10, - VenderID: 0, - BrandID: JxBrandId, - Is7ToReturn: 0, + Title: "测试商品1", + // ShopCategorys: []int{11}, + CategoryID: 13577, + TransportID: TransportID, + MobileDesc: "测试", + Introduction: "测试", + WareStatus: 8, + OuterID: "1", + Weight: 2300, + Height: 100, + Length: 100, + Width: 100, + JdPrice: 20, + MarketPrice: 10, + VenderID: 0, + BrandID: JxBrandId, + Is7ToReturn: 0, } image := &CreateSkuParamImages{ ColorID: "0000000000", @@ -164,12 +164,14 @@ func TestCreateSku(t *testing.T) { StockNum: 1, Type: "com.jd.pop.ware.ic.api.domain.sku", Type2: "com.jd.pop.ware.ic.api.domain.Sku", + OuterID: "1", } sku2 := &CreateSkuParamSkus{ JdPrice: 10, StockNum: 1, Type: "com.jd.pop.ware.ic.api.domain.sku", Type2: "com.jd.pop.ware.ic.api.domain.Sku", + OuterID: "2", } attr1 := &CreateSkuParamAttrs{ AttrID: "109692", @@ -206,6 +208,7 @@ func TestCreateSku(t *testing.T) { sku.SaleAttrs = attrs2 skus = append(skus, sku) skus = append(skus, sku2) + // ware.Skus = skus result, err := api.CreateWare(ware, skus) if err != nil { t.Fatal(err) @@ -214,23 +217,24 @@ func TestCreateSku(t *testing.T) { } func TestFindAttrs(t *testing.T) { - err := api.FindAttrs(13577) + result, err := api.FindAttrs(13577) if err != nil { t.Fatal(err) } - // t.Log(utils.Format4Output(result, false)) + t.Log(utils.Format4Output(result, false)) } func TestFindValuesByAttrId(t *testing.T) { - err := api.FindValuesByAttrId(109692) + result, no, err := api.FindValuesByAttrId(109692) if err != nil { t.Fatal(err) } - // t.Log(utils.Format4Output(result, false)) + t.Log(utils.Format4Output(result, false)) + fmt.Println(no) } func TestSaveVenderAttrValue(t *testing.T) { - err := api.SaveVenderAttrValue("0.5", 160806, 13571, 1) + err := api.SaveVenderAttrValue("100g", 1001027606, 13577, 2) if err != nil { t.Fatal(err) }