From 4e5ca49985396b5a2bba2298972f2e2336add6d2 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, 18 Mar 2020 18:02:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=B6=E8=B1=B9api=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/yinbaoapi/yinbaoapi.go | 110 +++++++++++++++++------- platformapi/yinbaoapi/yinbaoapi_test.go | 38 +++++--- 2 files changed, 107 insertions(+), 41 deletions(-) diff --git a/platformapi/yinbaoapi/yinbaoapi.go b/platformapi/yinbaoapi/yinbaoapi.go index b9e5de63..1550b33d 100644 --- a/platformapi/yinbaoapi/yinbaoapi.go +++ b/platformapi/yinbaoapi/yinbaoapi.go @@ -58,23 +58,19 @@ func (a *API) signParam(params map[string]interface{}) (sig string) { } func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) { - params := make(map[string]interface{}) - params["appId"] = a.appID - params = utils.MergeMaps(params, bizParams) + bizParams["appId"] = a.appID fullURL := utils.GenerateGetURL(url, action, nil) + result, _ := json.MarshalIndent(bizParams, "", " ") err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { - request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode())) - byteSignStr, _ := json.Marshal(params) - signStr := string(byteSignStr) + a.appKey - binSig := md5.Sum([]byte(signStr)) - signStr = fmt.Sprintf("%X", binSig) + request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(result))) + result2 := a.appKey + string(result) + binSig := md5.Sum([]byte(result2)) request.Header.Set("User-Agent", "openApi") request.Header.Set("accept-encoding", "gzip,deflate") - request.Header.Set("data-signature", signStr) + request.Header.Set("data-signature", fmt.Sprintf("%X", binSig)) request.Header.Set("time-stamp", utils.Int64ToStr(time.Now().Unix())) request.Header.Set("Content-Type", "application/json; charset=utf-8") - fmt.Println(request.Header) return request }, a.config, @@ -100,37 +96,89 @@ func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal } type ProductInfo struct { - CategoryUID int `json:"categoryUid"` - Name string `json:"name"` - Barcode string `json:"barcode"` - BuyPrice int `json:"buyPrice"` - SellPrice int `json:"sellPrice"` - Stock int `json:"stock"` - Pinyin string `json:"pinyin"` - Description string `json:"description"` - IsCustomerDiscount int `json:"isCustomerDiscount"` - SupplierUID int `json:"supplierUid"` - Enable int `json:"enable"` + CategoryUID int `json:"categoryUid"` + Name string `json:"name"` + Barcode string `json:"barcode"` + BuyPrice *float64 `json:"buyPrice"` + SellPrice *float64 `json:"sellPrice"` + Stock *float64 `json:"stock"` + Pinyin string `json:"pinyin"` + Description string `json:"description"` + IsCustomerDiscount *int `json:"isCustomerDiscount"` + SupplierUID int `json:"supplierUid"` + Enable *int `json:"enable"` + CustomerPrice *float64 `json:"customerPrice"` + Attribute1 string `json:"attribute1"` + Attribute2 string `json:"attribute2"` + Attribute3 string `json:"attribute3"` + Attribute4 string `json:"attribute4"` + UID int64 `json:"uid"` } -type AddProductInfoParam struct { +type ProductInfoParam struct { ProductInfo *ProductInfo AppID string `json:"appId"` } +type AddProductInfoResult struct { + ID int `json:"id"` + UID int64 `json:"uid"` + UserID int `json:"userId"` + Name string `json:"name"` + Barcode string `json:"barcode"` + BuyPrice int `json:"buyPrice"` + SellPrice int `json:"sellPrice"` + Stock int `json:"stock"` + Pinyin string `json:"pinyin"` + Enable int `json:"enable"` + CreatedDatetime string `json:"createdDatetime"` +} + //新增商品 //http://pospal.cn/openplatform/productapi.html#addProductInfo -func (a *API) AddProductInfo(addProductInfoParam *AddProductInfoParam) (err error) { +func (a *API) AddProductInfo(productInfoParam *ProductInfoParam) (addProductInfoResult *AddProductInfoResult, err error) { mapP := map[string]interface{}{ - "name": addProductInfoParam.ProductInfo.Name, - "barcode": addProductInfoParam.ProductInfo.Barcode, - "buyPrice": addProductInfoParam.ProductInfo.BuyPrice, - "sellPrice": addProductInfoParam.ProductInfo.SellPrice, - "stock": addProductInfoParam.ProductInfo.Stock, + "name": productInfoParam.ProductInfo.Name, + "barcode": productInfoParam.ProductInfo.Barcode, + "buyPrice": productInfoParam.ProductInfo.BuyPrice, + "sellPrice": productInfoParam.ProductInfo.SellPrice, + "stock": productInfoParam.ProductInfo.Stock, } - str, _ := json.Marshal(mapP) - a.AccessAPI("productOpenApi/addProductInfo", map[string]interface{}{ - "productInfo": string(str), + result, err := a.AccessAPI("productOpenApi/addProductInfo", map[string]interface{}{ + "productInfo": mapP, + }) + if err == nil { + utils.Map2StructByJson(result["data"], &addProductInfoResult, false) + } + return addProductInfoResult, err +} + +//修改商品 +//http://pospal.cn/openplatform/productapi.html#addProductInfo +func (a *API) UpdateProductInfo(productInfoParam *ProductInfoParam) (err error) { + mapP := map[string]interface{}{ + "uid": productInfoParam.ProductInfo.UID, + } + if productInfoParam.ProductInfo.BuyPrice != nil { + mapP["buyPrice"] = productInfoParam.ProductInfo.BuyPrice + } + if productInfoParam.ProductInfo.SellPrice != nil { + mapP["sellPrice"] = productInfoParam.ProductInfo.SellPrice + } + if productInfoParam.ProductInfo.Stock != nil { + mapP["stock"] = productInfoParam.ProductInfo.Stock + } + if productInfoParam.ProductInfo.Enable != nil { + mapP["enable"] = productInfoParam.ProductInfo.Enable + } + if productInfoParam.ProductInfo.IsCustomerDiscount != nil { + mapP["isCustomerDiscount"] = productInfoParam.ProductInfo.IsCustomerDiscount + } + if productInfoParam.ProductInfo.CustomerPrice != nil { + mapP["customerPrice"] = productInfoParam.ProductInfo.CustomerPrice + } + a.AccessAPI("productOpenApi/updateProductInfo", map[string]interface{}{ + "productInfo": mapP, }) return err } diff --git a/platformapi/yinbaoapi/yinbaoapi_test.go b/platformapi/yinbaoapi/yinbaoapi_test.go index a72974d4..706f5414 100644 --- a/platformapi/yinbaoapi/yinbaoapi_test.go +++ b/platformapi/yinbaoapi/yinbaoapi_test.go @@ -4,6 +4,7 @@ import ( "testing" "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" "go.uber.org/zap" ) @@ -20,18 +21,35 @@ func init() { } func TestAddProductInfo(t *testing.T) { - api.AddProductInfo(&AddProductInfoParam{ + var ( + stock = 5.1 + ) + result, err := api.AddProductInfo(&ProductInfoParam{ ProductInfo: &ProductInfo{ Name: "测试商品", - Barcode: "12138", - BuyPrice: 5, - SellPrice: 5, - Stock: 99, + Barcode: "12135", + BuyPrice: &stock, + SellPrice: &stock, + Stock: &stock, }, }) - // result, err := - // if err != nil { - // t.Fatal(err) - // } - // t.Log(utils.Format4Output(result, false)) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestUpdateProductInfo(t *testing.T) { + var ( + sellPrice = float64(50) + ) + err := api.UpdateProductInfo(&ProductInfoParam{ + ProductInfo: &ProductInfo{ + UID: 285305464077105187, + Stock: &sellPrice, + }, + }) + if err != nil { + t.Fatal(err) + } }