银豹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) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user