银豹api,eclp库存api修改

This commit is contained in:
苏尹岚
2020-03-18 14:06:47 +08:00
parent 6cbf7ab59a
commit bd834157b1
4 changed files with 109 additions and 34 deletions

View File

@@ -139,6 +139,23 @@ type SearchShopStockResult struct {
PageNumber string `json:"pageNumber"`
}
type QueryStockResult struct {
RecordCount int `json:"recordCount"`
IsvSku string `json:"isvSku"`
DeptName string `json:"deptName"`
SellerGoodsSign string `json:"sellerGoodsSign"`
GoodsNo string `json:"goodsNo"`
UsableNum int `json:"usableNum"`
DeptNo string `json:"deptNo"`
WarehouseName string `json:"warehouseName"`
TotalNum int `json:"totalNum"`
StockType string `json:"stockType"`
StockStatus string `json:"stockStatus"`
GoodsLevel string `json:"goodsLevel"`
WarehouseNo string `json:"warehouseNo"`
GoodsName string `json:"goodsName"`
}
func New(accessToken, appKey, appSecret string, config ...*platformapi.APIConfig) *API {
curConfig := platformapi.DefAPIConfig
if len(config) > 0 {
@@ -296,7 +313,7 @@ func (a *API) SearchShopStock(goodsNo string) (searchShopStockResult *SearchShop
//查询仓库商品库存
//https://open.jd.com/home/home#/doc/api?apiCateId=138&apiId=3396&apiName=jingdong.eclp.stock.searchShopStock
func (a *API) QueryStock(goodsNo string) (searchShopStockResult *SearchShopStockResult, err error) {
func (a *API) QueryStock(goodsNo string) (queryStockResult []*QueryStockResult, err error) {
params := map[string]interface{}{
"deptNo": DepartmentNo,
"warehouseNo": WarehouseNo,
@@ -307,7 +324,7 @@ func (a *API) QueryStock(goodsNo string) (searchShopStockResult *SearchShopStock
}
result, err := a.AccessAPI("jingdong.eclp.stock.queryStock", prodURL, params)
if err == nil {
utils.Map2StructByJson(result["jingdong_eclp_stock_queryStock_responce"].(map[string]interface{})["querystock_result"], &searchShopStockResult, false)
utils.Map2StructByJson(result["jingdong_eclp_stock_queryStock_responce"].(map[string]interface{})["querystock_result"], &queryStockResult, false)
}
return searchShopStockResult, err
return queryStockResult, err
}

View File

@@ -84,7 +84,7 @@ func TestSearchShopStock(t *testing.T) {
}
func TestQueryStock(t *testing.T) {
result, err := api.QueryStock("JX10001")
result, err := api.QueryStock("")
if err != nil {
t.Fatal(err)
}

View File

@@ -2,6 +2,7 @@ package yinbaoapi
import (
"crypto/md5"
"encoding/json"
"fmt"
"net/http"
"sort"
@@ -14,15 +15,16 @@ import (
)
const (
sigKey = "sign"
url = "https://area27-win.pospal.cn:443/pospal-api2/openapi/v1"
sigKey = "sign"
url = "https://area27-win.pospal.cn:443/pospal-api2/openapi/v1"
statusSuccess = "success"
)
type API struct {
appKey string
appID string
client *http.Client
config *platformapi.APIConfig
appKey string
appID string
client *http.Client
config *platformapi.APIConfig
}
func New(appKey, appID string, config ...*platformapi.APIConfig) *API {
@@ -31,10 +33,10 @@ func New(appKey, appID string, config ...*platformapi.APIConfig) *API {
curConfig = *config[0]
}
return &API{
appKey: appKey,
appID: appID,
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
appKey: appKey,
appID: appID,
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
}
}
@@ -58,16 +60,21 @@ 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)
fullURL := utils.GenerateGetURL(url, action, nil)
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
signStr := a.signParam(params)
byteSignStr, _ := json.Marshal(params)
signStr := string(byteSignStr) + a.appKey
binSig := md5.Sum([]byte(signStr))
signStr = fmt.Sprintf("%X", binSig)
request.Header.Set("User-Agent", "openApi")
request.Header.Set("accept-encoding", "gzip,deflate")
request.Header.Set("data-signature", signStr)
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,
@@ -76,9 +83,13 @@ func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
}
if err == nil {
if jsonResult1["error_response"] != nil {
if jsonResult1["status"] != statusSuccess {
errLevel = platformapi.ErrLevelGeneralFail
err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string))
errMsg := ""
for _, v := range jsonResult1["messages"].([]interface{}) {
errMsg += v.(string)
}
err = utils.NewErrorCode(errMsg, jsonResult1["status"].(string))
baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
}
retVal = jsonResult1
@@ -88,27 +99,38 @@ func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal
return retVal, err
}
type AddProductInfoParam struct{
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"`
} `json:"productInfo"`
AppID string `json:"appId"`
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"`
}
type AddProductInfoParam struct {
ProductInfo *ProductInfo
AppID string `json:"appId"`
}
//新增商品
//http://pospal.cn/openplatform/productapi.html#addProductInfo
func (a *API) AddProductInfo(addProductInfoParam *AddProductInfoParam) (err error) {
result, err := a.AccessAPI("productOpenApi/addProductInfo", utils.Struct2FlatMap(addProductInfoParam))
mapP := map[string]interface{}{
"name": addProductInfoParam.ProductInfo.Name,
"barcode": addProductInfoParam.ProductInfo.Barcode,
"buyPrice": addProductInfoParam.ProductInfo.BuyPrice,
"sellPrice": addProductInfoParam.ProductInfo.SellPrice,
"stock": addProductInfoParam.ProductInfo.Stock,
}
str, _ := json.Marshal(mapP)
a.AccessAPI("productOpenApi/addProductInfo", map[string]interface{}{
"productInfo": string(str),
})
return err
}

View File

@@ -1 +1,37 @@
package yinbaoapi
import (
"testing"
"git.rosy.net.cn/baseapi"
"go.uber.org/zap"
)
var (
api *API
sugarLogger *zap.SugaredLogger
)
func init() {
logger, _ := zap.NewDevelopment()
sugarLogger = logger.Sugar()
baseapi.Init(sugarLogger)
api = New("682628966212343269", "18C0E0867E467DBC26EFF5E957B02EC4")
}
func TestAddProductInfo(t *testing.T) {
api.AddProductInfo(&AddProductInfoParam{
ProductInfo: &ProductInfo{
Name: "测试商品",
Barcode: "12138",
BuyPrice: 5,
SellPrice: 5,
Stock: 99,
},
})
// result, err :=
// if err != nil {
// t.Fatal(err)
// }
// t.Log(utils.Format4Output(result, false))
}