|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.oa00.com/go/jcook-sdk/util/http"
|
|
|
|
"git.oa00.com/go/jcook-sdk/util/security"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Requester interface {
|
|
|
|
GetApiName() string
|
|
|
|
GetRespObj() interface{}
|
|
|
|
GetRespName() string
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnKnow = "UnKnow"
|
|
|
|
|
|
|
|
type SkuParam struct {
|
|
|
|
SkuID string `json:"skuId"`
|
|
|
|
SkuPrice float64 `json:"skuPrice"`
|
|
|
|
Quantity uint `json:"quantity"`
|
|
|
|
SkuName string `json:"skuName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Address struct {
|
|
|
|
CityID uint `json:"cityId"`
|
|
|
|
CountyID uint `json:"countyId"`
|
|
|
|
ProvinceID uint `json:"provinceId"`
|
|
|
|
TownID uint `json:"townId"`
|
|
|
|
FullAddress string `json:"fullAddress"`
|
|
|
|
AddressDetail string `json:"addressDetail"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CtpProtocol struct {
|
|
|
|
AppKey string `json:"appKey"`
|
|
|
|
ChannelID uint `json:"channelId"`
|
|
|
|
CustomerID uint `json:"customerId"`
|
|
|
|
TraceID string `json:"traceId"`
|
|
|
|
OpName string `json:"opName"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Result struct {
|
|
|
|
ErrCode uint `json:"errCode"`
|
|
|
|
ErrMsg string `json:"errMsg"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
PAppKey = "app_key"
|
|
|
|
PApi = "method"
|
|
|
|
PAccessToken = "access_token"
|
|
|
|
PVersion = "v"
|
|
|
|
PFormat = "format"
|
|
|
|
PTimestamp = "timestamp"
|
|
|
|
PSign = "sign"
|
|
|
|
PJsonParamKey = "360buy_param_json"
|
|
|
|
PCode = "code"
|
|
|
|
PSubCode = "sub_code"
|
|
|
|
PMsg = "msg"
|
|
|
|
PSubMsg = "sub_msg"
|
|
|
|
Rest = "https://api.jd.com/routerjson"
|
|
|
|
Version = "2.0"
|
|
|
|
Format = "json"
|
|
|
|
ResponseSuffix = "_responce"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
AppKey string
|
|
|
|
AppSecret string
|
|
|
|
AccessKey string
|
|
|
|
OpName string
|
|
|
|
Debug bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(appKey, AppSecret, AccessKey, OpName string, debug bool) *Client {
|
|
|
|
return &Client{
|
|
|
|
AppKey: appKey,
|
|
|
|
AppSecret: AppSecret,
|
|
|
|
AccessKey: AccessKey,
|
|
|
|
OpName: OpName,
|
|
|
|
Debug: debug,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Exec(r Requester) (interface{}, error) {
|
|
|
|
j, _ := json.Marshal(&r)
|
|
|
|
jsonParams := string(j)
|
|
|
|
t := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
data := map[string]string{
|
|
|
|
PAppKey: c.AppKey,
|
|
|
|
PApi: r.GetApiName(),
|
|
|
|
PAccessToken: c.AccessKey,
|
|
|
|
PVersion: Version,
|
|
|
|
PFormat: Format,
|
|
|
|
PTimestamp: t,
|
|
|
|
PJsonParamKey: jsonParams,
|
|
|
|
}
|
|
|
|
|
|
|
|
sign := security.Md5OrderlyWithSecret(c.AppSecret, data)
|
|
|
|
data[PSign] = sign
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
|
|
|
|
for key, value := range data {
|
|
|
|
params.Add(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.JGet(Rest, params, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(resp), r.GetRespName()) {
|
|
|
|
return nil, fmt.Errorf("system err: %s", string(resp))
|
|
|
|
}
|
|
|
|
//fmt.Println(string(resp))
|
|
|
|
|
|
|
|
var temp map[string]interface{}
|
|
|
|
err = json.Unmarshal(resp, &temp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if c.Debug {
|
|
|
|
fmt.Println(temp)
|
|
|
|
}
|
|
|
|
result := temp[r.GetRespName()]
|
|
|
|
response := r.GetRespObj()
|
|
|
|
if err = mapstructure.Decode(result, &response); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, err
|
|
|
|
}
|