From c7c24ef0fabf3c206709acc35e16361b0a34ea38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Mon, 23 Mar 2020 16:41:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=93=B6=E8=B1=B9api?= =?UTF-8?q?=E5=92=8Ctest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/yinbaoapi/yinbaoapi.go | 125 +++++++++++++++++++++++- platformapi/yinbaoapi/yinbaoapi_test.go | 51 ++++++++-- 2 files changed, 169 insertions(+), 7 deletions(-) diff --git a/platformapi/yinbaoapi/yinbaoapi.go b/platformapi/yinbaoapi/yinbaoapi.go index 1550b33d..5d5836f5 100644 --- a/platformapi/yinbaoapi/yinbaoapi.go +++ b/platformapi/yinbaoapi/yinbaoapi.go @@ -18,6 +18,14 @@ const ( sigKey = "sign" url = "https://area27-win.pospal.cn:443/pospal-api2/openapi/v1" statusSuccess = "success" + + skuExistErrCode = "5004" + + SkuStatusEnable = 1 + SkuStatusDisabled = 0 + SkuStatusDeleted = -1 + + PageMaxID = "LAST_RESULT_MAX_ID" ) type API struct { @@ -86,7 +94,7 @@ func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal errMsg += v.(string) } err = utils.NewErrorCode(errMsg, jsonResult1["status"].(string)) - baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) + baseapi.SugarLogger.Debugf("yinbao AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) } retVal = jsonResult1 } @@ -153,6 +161,10 @@ func (a *API) AddProductInfo(productInfoParam *ProductInfoParam) (addProductInfo return addProductInfoResult, err } +func IsErrSkuExist(err error) (isExist bool) { + return utils.IsErrMatch(err, skuExistErrCode, []string{"商品已存在"}) +} + //修改商品 //http://pospal.cn/openplatform/productapi.html#addProductInfo func (a *API) UpdateProductInfo(productInfoParam *ProductInfoParam) (err error) { @@ -182,3 +194,114 @@ func (a *API) UpdateProductInfo(productInfoParam *ProductInfoParam) (err error) }) return err } + +type QueryProductByBarcodeResult struct { + UID int64 `json:"uid"` + Name string `json:"name"` + Barcode string `json:"barcode"` + BuyPrice float64 `json:"buyPrice"` + SellPrice float64 `json:"sellPrice"` + SellPrice2 float64 `json:"sellPrice2"` + Stock float64 `json:"stock"` + MaxStock float64 `json:"maxStock"` + MinStock float64 `json:"minStock"` + Pinyin string `json:"pinyin"` + Enable int `json:"enable"` + CreatedDatetime string `json:"createdDatetime"` +} + +//按条形码查询商品(skuid) +//http://pospal.cn/openplatform/productapi.html#queryProductByBarcode +func (a *API) QueryProductByBarcode(barcode string) (queryProductByBarcodeResult *QueryProductByBarcodeResult, err error) { + result, err := a.AccessAPI("productOpenApi/queryProductByBarcode", map[string]interface{}{ + "barcode": barcode, + }) + if err == nil { + utils.Map2StructByJson(result["data"], &queryProductByBarcodeResult, false) + } + return queryProductByBarcodeResult, err +} + +//按条形码批量查询商品 +//http://pospal.cn/openplatform/productapi.html#queryProductByBarcodes +func (a *API) QueryProductByBarcodes(barcodes []string) (queryProductByBarcodeResult []*QueryProductByBarcodeResult, err error) { + result, err := a.AccessAPI("productOpenApi/queryProductByBarcodes", map[string]interface{}{ + "barcodes": barcodes, + }) + if err == nil { + utils.Map2StructByJson(result["data"], &queryProductByBarcodeResult, false) + } + return queryProductByBarcodeResult, err +} + +type QueryProductImagesByBarcodeResult struct { + ProductUID int64 `json:"productUid"` + ProductName string `json:"productName"` + ProductBarcode string `json:"productBarcode"` + ImageURL string `json:"imageUrl"` +} + +//按条形码查询商品图片 +//http://pospal.cn/openplatform/productapi.html#queryProductImagesByBarcode +func (a *API) QueryProductImagesByBarcode(barcode string) (queryProductImagesByBarcodeResult []*QueryProductImagesByBarcodeResult, err error) { + result, err := a.AccessAPI("productOpenApi/queryProductImagesByBarcode", map[string]interface{}{ + "barcode": barcode, + }) + if err == nil { + utils.Map2StructByJson(result["data"], &queryProductImagesByBarcodeResult, false) + } + return queryProductImagesByBarcodeResult, err +} + +type PostBackParameter struct { + ParameterType string `json:"parameterType"` + ParameterValue string `json:"parameterType"` +} + +type QueryProductPagesResult struct { + PostBackParameter struct { + ParameterType string `json:"parameterType"` + ParameterValue string `json:"parameterValue"` + } `json:"postBackParameter"` + Result []struct { + UID int64 `json:"uid"` + CategoryUID int `json:"categoryUid,omitempty"` + Name string `json:"name"` + Barcode string `json:"barcode"` + BuyPrice float64 `json:"buyPrice"` + SellPrice float64 `json:"sellPrice"` + SellPrice2 float64 `json:"sellPrice2"` + Stock float64 `json:"stock"` + MaxStock float64 `json:"maxStock"` + MinStock float64 `json:"minStock"` + Pinyin string `json:"pinyin"` + CustomerPrice float64 `json:"customerPrice,omitempty"` + IsCustomerDiscount int `json:"isCustomerDiscount,omitempty"` + SupplierUID int `json:"supplierUid,omitempty"` + Enable int `json:"enable"` + CreatedDatetime string `json:"createdDatetime"` + UpdatedDatetime string `json:"updatedDatetime,omitempty"` + Description string `json:"description,omitempty"` + ProductionDate string `json:"productionDate,omitempty"` + Attribute1 string `json:"attribute1,omitempty"` + Attribute2 string `json:"attribute2,omitempty"` + Attribute3 string `json:"attribute3,omitempty"` + Attribute4 string `json:"attribute4,omitempty"` + Attribute5 string `json:"attribute5,omitempty"` + Attribute6 string `json:"attribute6,omitempty"` + Attribute7 string `json:"attribute7,omitempty"` + } `json:"result"` + PageSize int `json:"pageSize"` +} + +//分页查询所有商品 +//http://pospal.cn/openplatform/productapi.html#queryProductPages +func (a *API) QueryProductPages(postBackParameter *PostBackParameter) (queryProductPagesResult *QueryProductPagesResult, err error) { + result, err := a.AccessAPI("productOpenApi/queryProductPages", map[string]interface{}{ + "postBackParameter": postBackParameter, + }) + if err == nil { + utils.Map2StructByJson(result["data"], &queryProductPagesResult, false) + } + return queryProductPagesResult, err +} diff --git a/platformapi/yinbaoapi/yinbaoapi_test.go b/platformapi/yinbaoapi/yinbaoapi_test.go index 706f5414..abd6dadf 100644 --- a/platformapi/yinbaoapi/yinbaoapi_test.go +++ b/platformapi/yinbaoapi/yinbaoapi_test.go @@ -17,7 +17,7 @@ func init() { logger, _ := zap.NewDevelopment() sugarLogger = logger.Sugar() baseapi.Init(sugarLogger) - api = New("682628966212343269", "18C0E0867E467DBC26EFF5E957B02EC4") + api = New("682628966212343269", "18C0E0867E467DBC26EFF5E957B02EC4") //总店 } func TestAddProductInfo(t *testing.T) { @@ -26,11 +26,12 @@ func TestAddProductInfo(t *testing.T) { ) result, err := api.AddProductInfo(&ProductInfoParam{ ProductInfo: &ProductInfo{ - Name: "测试商品", - Barcode: "12135", - BuyPrice: &stock, - SellPrice: &stock, - Stock: &stock, + Name: "测试商品", + Barcode: "12132", + BuyPrice: &stock, + SellPrice: &stock, + Stock: &stock, + SupplierUID: 123, }, }) if err != nil { @@ -53,3 +54,41 @@ func TestUpdateProductInfo(t *testing.T) { t.Fatal(err) } } + +func TestQueryProductByBarcode(t *testing.T) { + result, err := api.QueryProductByBarcode("22510") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestQueryProductByBarcodes(t *testing.T) { + result, err := api.QueryProductByBarcodes([]string{ + "22510", + "2003181534102", + }) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestQueryProductImagesByBarcode(t *testing.T) { + result, err := api.QueryProductImagesByBarcode("22510") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestQueryProductPages(t *testing.T) { + result, err := api.QueryProductPages(&PostBackParameter{ + ParameterType: "LAST_RESULT_MAX_ID", + ParameterValue: "7064573", + }) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +}