185 lines
5.9 KiB
Go
185 lines
5.9 KiB
Go
package yinbaoapi
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
const (
|
|
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
|
|
}
|
|
|
|
func New(appKey, appID string, config ...*platformapi.APIConfig) *API {
|
|
curConfig := platformapi.DefAPIConfig
|
|
if len(config) > 0 {
|
|
curConfig = *config[0]
|
|
}
|
|
return &API{
|
|
appKey: appKey,
|
|
appID: appID,
|
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
|
config: &curConfig,
|
|
}
|
|
}
|
|
|
|
func (a *API) signParam(params map[string]interface{}) (sig string) {
|
|
var valueList []string
|
|
for k, v := range params {
|
|
if k != sigKey {
|
|
if str := fmt.Sprint(v); str != "" {
|
|
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
|
}
|
|
}
|
|
}
|
|
sort.Sort(sort.StringSlice(valueList))
|
|
valueList = append(valueList, fmt.Sprintf("key=%s", a.appKey))
|
|
sig = strings.Join(valueList, "&")
|
|
binSig := md5.Sum([]byte(sig))
|
|
sig = fmt.Sprintf("%X", binSig)
|
|
return sig
|
|
}
|
|
|
|
func (a *API) AccessAPI(action string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
|
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(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", fmt.Sprintf("%X", binSig))
|
|
request.Header.Set("time-stamp", utils.Int64ToStr(time.Now().Unix()))
|
|
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
return request
|
|
},
|
|
a.config,
|
|
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
|
if jsonResult1 == nil {
|
|
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
|
}
|
|
if err == nil {
|
|
if jsonResult1["status"] != statusSuccess {
|
|
errLevel = platformapi.ErrLevelGeneralFail
|
|
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
|
|
}
|
|
return errLevel, err
|
|
})
|
|
return retVal, err
|
|
}
|
|
|
|
type ProductInfo struct {
|
|
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 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(productInfoParam *ProductInfoParam) (addProductInfoResult *AddProductInfoResult, err error) {
|
|
mapP := map[string]interface{}{
|
|
"name": productInfoParam.ProductInfo.Name,
|
|
"barcode": productInfoParam.ProductInfo.Barcode,
|
|
"buyPrice": productInfoParam.ProductInfo.BuyPrice,
|
|
"sellPrice": productInfoParam.ProductInfo.SellPrice,
|
|
"stock": productInfoParam.ProductInfo.Stock,
|
|
}
|
|
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
|
|
}
|