137 lines
4.1 KiB
Go
137 lines
4.1 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) {
|
|
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()))
|
|
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,
|
|
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 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) {
|
|
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
|
|
}
|