169 lines
5.1 KiB
Go
169 lines
5.1 KiB
Go
package jd_union
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"github.com/spf13/cast"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
"repository.lenntc.com/lenntc/third-platform-sdk/client"
|
||
"repository.lenntc.com/lenntc/third-platform-sdk/util"
|
||
"sort"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
// JdUnionApi 京东联盟平台
|
||
type JdUnionApi interface {
|
||
// Sign 签名
|
||
Sign(data map[string]interface{}) string
|
||
// QueryOrderList 查询订单列表
|
||
QueryOrderList(req QueryOrderListRequest) (*QueryOrderListResponse, error)
|
||
// PromotionLink 转链
|
||
PromotionLink(PromotionLinkRequest) (*PromotionLinkResponse, error)
|
||
}
|
||
|
||
type jdUnionApiImpl struct {
|
||
log logx.Logger
|
||
client *Client
|
||
params map[string]any
|
||
}
|
||
|
||
func newJdUnionApiImpl(log logx.Logger, client *Client) JdUnionApi {
|
||
return &jdUnionApiImpl{
|
||
log: log,
|
||
client: client,
|
||
params: map[string]any{
|
||
"method": "",
|
||
"access_token": "",
|
||
"app_key": client.authConfig.AppKey,
|
||
"sign_method": SignMethod,
|
||
"timestamp": time.Now().Format(time.DateTime),
|
||
"360buy_param_json": "",
|
||
"v": Version,
|
||
},
|
||
}
|
||
}
|
||
|
||
func (t *jdUnionApiImpl) Sign(data map[string]any) string {
|
||
// key排序
|
||
arr := sort.StringSlice{}
|
||
for k := range data {
|
||
if k == "sign" {
|
||
continue
|
||
}
|
||
arr = append(arr, k)
|
||
}
|
||
arr.Sort()
|
||
// 参数拼接
|
||
var build strings.Builder
|
||
build.WriteString(t.client.authConfig.AppSecret)
|
||
for _, k := range arr {
|
||
build.WriteString(fmt.Sprintf("%s%v", k, data[k]))
|
||
}
|
||
build.WriteString(t.client.authConfig.AppSecret)
|
||
return strings.ToUpper(util.Md5String(build.String()))
|
||
}
|
||
|
||
func (t *jdUnionApiImpl) QueryOrderList(req QueryOrderListRequest) (*QueryOrderListResponse, error) {
|
||
params := make(map[string]any)
|
||
if req.OrderId != "" {
|
||
params["orderId"] = req.OrderId
|
||
} else {
|
||
params["pageIndex"] = req.PageIndex
|
||
params["pageSize"] = req.PageSize
|
||
params["type"] = req.Type
|
||
params["startTime"] = req.StartTime
|
||
params["endTime"] = req.EndTime
|
||
}
|
||
requestPrams := map[string]any{
|
||
"orderReq": params,
|
||
}
|
||
paramsJson, _ := json.Marshal(requestPrams)
|
||
|
||
SystemParams := t.params
|
||
SystemParams["method"] = QueryOrderListMethod
|
||
SystemParams["360buy_param_json"] = string(paramsJson)
|
||
SystemParams["sign"] = t.Sign(SystemParams)
|
||
|
||
//values := url.Values{}
|
||
//for key, value := range SystemParams {
|
||
// values.Set(key, cast.ToString(value))
|
||
//}
|
||
//fmt.Println(fmt.Sprintf("%s?%s", BaseUrl, values.Encode()))
|
||
|
||
requestUrl := BaseUrl
|
||
request := &client.HttpRequest{Headers: t.client.headers, QueryArgs: SystemParams}
|
||
response := new(JdUnionOpenOrderRowQueryResponse)
|
||
if err := t.client.HttpPost(requestUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
||
return nil, err
|
||
}
|
||
if response == nil {
|
||
return nil, nil
|
||
}
|
||
if response.ErrorResponse != nil {
|
||
return nil, fmt.Errorf("京东联盟请求订单接口响应错误,Code[%s],请求ID[%s],错误信息:%s", response.ErrorResponse.Code, response.ErrorResponse.RequestId, response.ErrorResponse.ZhDesc)
|
||
}
|
||
if response.JdUnionOpenOrderRowQueryResponse == nil {
|
||
return nil, nil
|
||
}
|
||
if cast.ToInt64(response.JdUnionOpenOrderRowQueryResponse.Code) != 0 {
|
||
return nil, nil
|
||
}
|
||
resp := new(QueryOrderListResponse)
|
||
err := json.Unmarshal([]byte(response.JdUnionOpenOrderRowQueryResponse.QueryResult), resp)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("京东联盟请求订单接口解析QueryResult失败,错误信息:%s", err.Error())
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// 转链
|
||
func (t *jdUnionApiImpl) PromotionLink(req PromotionLinkRequest) (*PromotionLinkResponse, error) {
|
||
params := map[string]any{
|
||
"materialId": req.MaterialId,
|
||
"positionId": req.PositionId,
|
||
"siteId": req.SiteId,
|
||
}
|
||
requestPrams := map[string]any{
|
||
"promotionCodeReq": params,
|
||
}
|
||
paramsJson, _ := json.Marshal(requestPrams)
|
||
|
||
SystemParams := t.params
|
||
SystemParams["method"] = PromotionLinkMethod
|
||
SystemParams["360buy_param_json"] = string(paramsJson)
|
||
SystemParams["sign"] = t.Sign(SystemParams)
|
||
|
||
//values := url.Values{}
|
||
//for key, value := range SystemParams {
|
||
// values.Set(key, cast.ToString(value))
|
||
//}
|
||
//fmt.Println(fmt.Sprintf("%s?%s", BaseUrl, values.Encode()))
|
||
|
||
requestUrl := BaseUrl
|
||
request := &client.HttpRequest{Headers: t.client.headers, QueryArgs: SystemParams}
|
||
response := new(JdUnionOpenPromotionCommonGetResponse)
|
||
if err := t.client.HttpPost(requestUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
||
return nil, err
|
||
}
|
||
if response == nil {
|
||
return nil, nil
|
||
}
|
||
if response.ErrorResponse != nil {
|
||
return nil, fmt.Errorf("京东联盟请求转链接口响应错误,Code[%s],请求ID[%s],错误信息:%s", response.ErrorResponse.Code, response.ErrorResponse.RequestId, response.ErrorResponse.ZhDesc)
|
||
}
|
||
if response.JdUnionOpenPromotionCommonGetResponse == nil {
|
||
return nil, nil
|
||
}
|
||
if cast.ToInt64(response.JdUnionOpenPromotionCommonGetResponse.Code) != 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
resp := new(PromotionLinkResponse)
|
||
err := json.Unmarshal([]byte(response.JdUnionOpenPromotionCommonGetResponse.GetResult), resp)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("京东联盟请求转链接口解析GetResult失败,错误信息:%s", err.Error())
|
||
}
|
||
return resp, nil
|
||
}
|