- jd spu related funcs

This commit is contained in:
gazebo
2018-12-11 12:35:18 +08:00
parent 454e1df040
commit 45d19a2a78
3 changed files with 185 additions and 0 deletions

View File

@@ -1,6 +1,9 @@
package jdapi
import (
"fmt"
"math"
"git.rosy.net.cn/baseapi/utils"
)
@@ -24,6 +27,9 @@ const (
KeyLength = "length"
KeyLiquidStatue = "liquidStatue"
KeyOutSkuId = "outSkuId"
KeyOutSpuId = "outSuperId"
KeySpuName = "superName"
KeySkuMainInfo = "skuMainInfo"
KeyPID = "pid"
KeyProductDesc = "productDesc"
KeyPageNo = "pageNo"
@@ -55,6 +61,12 @@ const (
MaxBatchSize4BatchUpdateOutSkuId = 50
)
const (
SkuFixedStatusOnline = 1
SkuFixedStatusOffline = 2
SkuFixedStatusDeleted = 4
)
type SkuIDPair struct {
SkuId int64 `json:"skuId"`
OutSkuId string `json:"outSkuId"`
@@ -379,6 +391,128 @@ func (a *API) GetProductStatus(storeId, skuId string) (retVal *ProductStatus, er
}
// 新增SPU信息接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=180&apiid=e30936da75294e55bcfdabb92c3815bc
func (a *API) AddSpu(outSpuId string, cagtegoryId int, shopCategories []int64, brandId int, spuName string, images []string, fixedStatus int, addParams map[string]interface{}, skus []map[string]interface{}) (spuId int64, skuPairs []*SkuIDPair, err error) {
attrList := []string{}
for k, v := range skus {
attr := getSkuAttr(v[KeyWeight].(float32))
attrList = append(attrList, attr)
skus[k] = utils.MergeMaps(map[string]interface{}{
"saleAttrValues": genAttrMapList(attr),
}, v)
}
fixedParams := map[string]interface{}{
KeyOutSpuId: outSpuId,
KeyCategoryId: cagtegoryId,
KeyShopCategories: shopCategories,
KeyBrandId: brandId,
KeySpuName: spuName,
KeyImages: images,
KeyFixedStatus: fixedStatus,
KeySkuMainInfo: skus,
KeyProductDesc: spuName, // todo
"saleAttrRelationInfoList": []map[string]interface{}{
map[string]interface{}{
"saleAttrName": "规格",
"saleAttrValueNameList": attrList,
},
map[string]interface{}{
"saleAttrName": "口味",
"saleAttrValueNameList": "好吃",
},
},
}
result, err := a.AccessAPINoPage("pms/addSpu", utils.MergeMaps(fixedParams, addParams), nil, nil, nil)
if err == nil {
resultMap := result.(map[string]interface{})
skuMainParterResponseList := resultMap["skuMainParterResponseList"].([]interface{})
skuPairs = make([]*SkuIDPair, len(skuMainParterResponseList))
for k, v := range skuMainParterResponseList {
v2 := v.(map[string]interface{})
skuPairs[k] = &SkuIDPair{
OutSkuId: v2["outSkuId"].(string),
SkuId: utils.Str2Int64(v2["skuId"].(string)),
}
}
return utils.MustInterface2Int64(resultMap["superId"]), skuPairs, nil
}
return 0, nil, err
}
// 追加新的SKU到指定的SPU接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=180&apiid=cffb839b06f042b28d6397b6bd4f2676
func (a *API) AppendSku(outSpuId string, outSkuId string, cagtegoryId int, shopCategories []int64, brandId int, skuName string, skuPrice int, weight float32, images []string, fixedStatus int, isSale bool, addParams map[string]interface{}) (skuID int64, err error) {
attr := getSkuAttr(weight)
if err = a.AppendSpuSaleAttr(outSpuId, "1001", attr); err != nil {
return 0, err
}
fixedParams := map[string]interface{}{
KeyOutSpuId: outSpuId,
KeyOutSkuId: outSkuId,
KeyCategoryId: cagtegoryId,
KeyShopCategories: shopCategories,
KeyBrandId: brandId,
KeySkuName: skuName,
KeySkuPrice: skuPrice,
KeyWeight: weight,
KeyImages: images,
KeyFixedStatus: fixedStatus,
KeyIsSale: isSale,
"saleAttrValues": genAttrMapList(attr),
}
result, err := a.AccessAPINoPage("pms/appendSku", utils.MergeMaps(fixedParams, addParams), nil, nil, nil)
if err == nil {
resultMap := result.(map[string]interface{})
skuMainParterResponseList := resultMap["skuMainParterResponseList"].([]interface{})
return utils.Str2Int64(skuMainParterResponseList[0].(map[string]interface{})["skuId"].(string)), nil
}
return 0, err
}
// 更新SPU基础信息,不可以更新销售属性
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=180&apiid=3816c5eadc5d4f268f760cac14483ec0
func (a *API) UpdateSpu(outSpuId string, params map[string]interface{}) (err error) {
_, err = a.AccessAPINoPage("pms/updateSpu", utils.MergeMaps(params, utils.Params2Map(KeyOutSpuId, outSpuId)), nil, nil, nil)
return err
}
// 更新SPU下指定SKU的基础信息
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=180&apiid=8be2b5d153044179a13089e7f19e24e6
func (a *API) UpdateSkuBaseInfo(outSpuId, outSkuId string, params map[string]interface{}) (err error) {
_, err = a.AccessAPINoPage("pms/updateSkuBaseInfo", utils.MergeMaps(params, utils.Params2Map(KeyOutSpuId, outSpuId, KeyOutSkuId, outSkuId)), nil, nil, nil)
return err
}
func (a *API) GetSkuSaleAttrName() (saleAttrNameList []string, err error) {
result, err := a.AccessAPINoPage("pms/getSkuSaleAttrName", nil, nil, nil, nil)
if err == nil {
return utils.Slice2StringSlice(result.([]interface{})), nil
}
return nil, err
}
func (a *API) AppendSpuSaleAttr(outSpuId, attrName, attrNewValue string) (err error) {
_, err = a.AccessAPINoPage("pms/appendSpuSaleAttr", map[string]interface{}{
KeyOutSpuId: outSpuId,
"saleAttrId": attrName,
"saleAttrValueName": attrNewValue,
}, nil, nil, nil)
return err
}
func (a *API) GetSpuSaleAttr(outSpuId string) (attrList []map[string]interface{}, err error) {
result, err := a.AccessAPINoPage("pms/getSpuSaleAttr", map[string]interface{}{
KeyOutSpuId: outSpuId,
}, nil, nil, nil)
if err == nil {
return utils.Slice2MapSlice(result.([]interface{})), nil
}
return nil, err
}
///////////////////////////
// 私有辅助函数
func interface2Cat(data interface{}, level int) (cat *CategoryInfo) {
@@ -408,3 +542,21 @@ func interface2CatList(data interface{}, level int, interface2CatHandler func(da
}
return cats
}
func getSkuAttr(weight float32) (attr string) {
attr = fmt.Sprintf("约%dg", int(math.Round(float64(weight*1000))))
return attr
}
func genAttrMapList(kgAttr string) (attrList []map[string]interface{}) {
return []map[string]interface{}{
map[string]interface{}{
"saleAttrName": "规格",
"saleAttrValueName": kgAttr,
},
map[string]interface{}{
"saleAttrName": "口味",
"saleAttrValueName": "好吃",
},
}
}

View File

@@ -4,6 +4,7 @@ import (
"testing"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
)
func TestQueryPageBrandInfo(t *testing.T) {
@@ -131,3 +132,19 @@ func TestDelShopCategory(t *testing.T) {
t.Fatal(err)
}
}
func TestGetSkuSaleAttrName(t *testing.T) {
result, err := jdapi.GetSkuSaleAttrName()
if err != nil {
t.Fatal(err)
}
t.Log(utils.Format4Output(result, false))
}
func TestGetSpuSaleAttr(t *testing.T) {
result, err := jdapi.GetSpuSaleAttr("1224")
if err != nil {
t.Fatal(err)
}
t.Log(utils.Format4Output(result, false))
}

View File

@@ -166,6 +166,22 @@ func Slice2MapSlice(data []interface{}) []map[string]interface{} {
return retVal
}
func Slice2int64Slice(data []interface{}) []int64 {
retVal := make([]int64, len(data))
for k, v := range data {
retVal[k] = MustInterface2Int64(v)
}
return retVal
}
func Slice2StringSlice(data []interface{}) []string {
retVal := make([]string, len(data))
for k, v := range data {
retVal[k] = Interface2String(v)
}
return retVal
}
//
func Bool2String(value bool) string {
if value {