银豹api新增修改商品
This commit is contained in:
@@ -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) {
|
func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||||
params := make(map[string]interface{})
|
bizParams["appId"] = a.appID
|
||||||
params["appId"] = a.appID
|
|
||||||
params = utils.MergeMaps(params, bizParams)
|
|
||||||
fullURL := utils.GenerateGetURL(url, action, nil)
|
fullURL := utils.GenerateGetURL(url, action, nil)
|
||||||
|
result, _ := json.MarshalIndent(bizParams, "", " ")
|
||||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||||
func() *http.Request {
|
func() *http.Request {
|
||||||
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(result)))
|
||||||
byteSignStr, _ := json.Marshal(params)
|
result2 := a.appKey + string(result)
|
||||||
signStr := string(byteSignStr) + a.appKey
|
binSig := md5.Sum([]byte(result2))
|
||||||
binSig := md5.Sum([]byte(signStr))
|
|
||||||
signStr = fmt.Sprintf("%X", binSig)
|
|
||||||
request.Header.Set("User-Agent", "openApi")
|
request.Header.Set("User-Agent", "openApi")
|
||||||
request.Header.Set("accept-encoding", "gzip,deflate")
|
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("time-stamp", utils.Int64ToStr(time.Now().Unix()))
|
||||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||||
fmt.Println(request.Header)
|
|
||||||
return request
|
return request
|
||||||
},
|
},
|
||||||
a.config,
|
a.config,
|
||||||
@@ -100,37 +96,89 @@ func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ProductInfo struct {
|
type ProductInfo struct {
|
||||||
CategoryUID int `json:"categoryUid"`
|
CategoryUID int `json:"categoryUid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Barcode string `json:"barcode"`
|
Barcode string `json:"barcode"`
|
||||||
BuyPrice int `json:"buyPrice"`
|
BuyPrice *float64 `json:"buyPrice"`
|
||||||
SellPrice int `json:"sellPrice"`
|
SellPrice *float64 `json:"sellPrice"`
|
||||||
Stock int `json:"stock"`
|
Stock *float64 `json:"stock"`
|
||||||
Pinyin string `json:"pinyin"`
|
Pinyin string `json:"pinyin"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
IsCustomerDiscount int `json:"isCustomerDiscount"`
|
IsCustomerDiscount *int `json:"isCustomerDiscount"`
|
||||||
SupplierUID int `json:"supplierUid"`
|
SupplierUID int `json:"supplierUid"`
|
||||||
Enable int `json:"enable"`
|
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
|
ProductInfo *ProductInfo
|
||||||
AppID string `json:"appId"`
|
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
|
//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{}{
|
mapP := map[string]interface{}{
|
||||||
"name": addProductInfoParam.ProductInfo.Name,
|
"name": productInfoParam.ProductInfo.Name,
|
||||||
"barcode": addProductInfoParam.ProductInfo.Barcode,
|
"barcode": productInfoParam.ProductInfo.Barcode,
|
||||||
"buyPrice": addProductInfoParam.ProductInfo.BuyPrice,
|
"buyPrice": productInfoParam.ProductInfo.BuyPrice,
|
||||||
"sellPrice": addProductInfoParam.ProductInfo.SellPrice,
|
"sellPrice": productInfoParam.ProductInfo.SellPrice,
|
||||||
"stock": addProductInfoParam.ProductInfo.Stock,
|
"stock": productInfoParam.ProductInfo.Stock,
|
||||||
}
|
}
|
||||||
str, _ := json.Marshal(mapP)
|
result, err := a.AccessAPI("productOpenApi/addProductInfo", map[string]interface{}{
|
||||||
a.AccessAPI("productOpenApi/addProductInfo", map[string]interface{}{
|
"productInfo": mapP,
|
||||||
"productInfo": string(str),
|
})
|
||||||
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi"
|
"git.rosy.net.cn/baseapi"
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,18 +21,35 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddProductInfo(t *testing.T) {
|
func TestAddProductInfo(t *testing.T) {
|
||||||
api.AddProductInfo(&AddProductInfoParam{
|
var (
|
||||||
|
stock = 5.1
|
||||||
|
)
|
||||||
|
result, err := api.AddProductInfo(&ProductInfoParam{
|
||||||
ProductInfo: &ProductInfo{
|
ProductInfo: &ProductInfo{
|
||||||
Name: "测试商品",
|
Name: "测试商品",
|
||||||
Barcode: "12138",
|
Barcode: "12135",
|
||||||
BuyPrice: 5,
|
BuyPrice: &stock,
|
||||||
SellPrice: 5,
|
SellPrice: &stock,
|
||||||
Stock: 99,
|
Stock: &stock,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
// result, err :=
|
if err != nil {
|
||||||
// if err != nil {
|
t.Fatal(err)
|
||||||
// t.Fatal(err)
|
}
|
||||||
// }
|
t.Log(utils.Format4Output(result, false))
|
||||||
// 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user