152 lines
5.2 KiB
Go
152 lines
5.2 KiB
Go
package meituan_union
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zywaited/xcopy"
|
|
|
|
"repository.lenntc.com/lenntc/third-platform-sdk/client"
|
|
"repository.lenntc.com/lenntc/third-platform-sdk/util"
|
|
)
|
|
|
|
// MeituanUnionApi 美团千载 平台的api
|
|
type MeituanUnionApi interface {
|
|
// Sign 签名
|
|
Sign(params map[string]interface{}) string
|
|
// NotifySign 通知签名
|
|
NotifySign(params map[string]interface{}) string
|
|
// GetLink 获取推广链接
|
|
GetLink(params GenerateLinkRequest) (*GenerateLinkResponse, error)
|
|
// MiniCode 获取小程序码
|
|
MiniCode(params MiniCodeRequest) (*MimiCodeResponse, error)
|
|
// GetOrderBySinge 获取单个订单
|
|
GetOrderBySinge(params GetOrderBySingeRequest) (*GetOrderBySingeResponse, error)
|
|
// GetOrderByBatch 批量获取订单
|
|
GetOrderByBatch(params GetOrderByBatchRequest) (*GetOrderByBatchResponse, error)
|
|
}
|
|
|
|
type meituanUnionApiImpl struct {
|
|
log logx.Logger
|
|
client *Client
|
|
}
|
|
|
|
func newMeituanUnionApiImpl(log logx.Logger, client *Client) MeituanUnionApi {
|
|
return &meituanUnionApiImpl{
|
|
log: log,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// Sign 签名
|
|
func (a *meituanUnionApiImpl) Sign(params map[string]interface{}) string {
|
|
kvPairs := a.getSignStr(params)
|
|
sort.Strings(kvPairs)
|
|
paramStr := strings.Join(kvPairs, "")
|
|
return util.Md5String(a.client.authConfig.SignKey + paramStr + a.client.authConfig.SignKey)
|
|
}
|
|
|
|
// NotifySign 通知签名
|
|
func (a *meituanUnionApiImpl) NotifySign(params map[string]interface{}) string {
|
|
kvPairs := a.getSignStr(params)
|
|
sort.Strings(kvPairs)
|
|
paramStr := strings.Join(kvPairs, "")
|
|
return util.Md5String(a.client.authConfig.NotifyKey + paramStr + a.client.authConfig.NotifyKey)
|
|
}
|
|
|
|
// GetLink 获取推广链接
|
|
func (a *meituanUnionApiImpl) GetLink(params GenerateLinkRequest) (*GenerateLinkResponse, error) {
|
|
request := new(ThirdGenerateLinkRequest)
|
|
if err := xcopy.SetJSONTag(false).Copy(&request, params); err != nil {
|
|
a.log.WithFields().Errorf("[meituanUnionApiImpl][GetLink] copy request error: %v", err)
|
|
return nil, err
|
|
}
|
|
request.Sign = a.Sign(util.StructToMap(request))
|
|
queryArgs := util.StructToMap(request)
|
|
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
|
|
response := new(GenerateLinkResponse)
|
|
if err := a.client.HttpGet(GetLinkUrl, req, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// MiniCode 获取小程序码
|
|
func (a *meituanUnionApiImpl) MiniCode(params MiniCodeRequest) (*MimiCodeResponse, error) {
|
|
request := new(ThirdMiniCodeRequest)
|
|
if err := xcopy.SetJSONTag(false).Copy(&request, params); err != nil {
|
|
a.log.WithFields().Errorf("[meituanUnionApiImpl][GetLink] copy request error: %v", err)
|
|
return nil, err
|
|
}
|
|
request.Sign = a.Sign(util.StructToMap(request))
|
|
queryArgs := util.StructToMap(request)
|
|
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
|
|
response := new(MimiCodeResponse)
|
|
if err := a.client.HttpGet(GetMiniCodeUrl, req, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// GetOrderBySinge 获取单个订单
|
|
func (a *meituanUnionApiImpl) GetOrderBySinge(params GetOrderBySingeRequest) (*GetOrderBySingeResponse, error) {
|
|
request := new(ThirdGetOrderBySingeRequest)
|
|
if err := xcopy.SetJSONTag(false).Copy(&request, params); err != nil {
|
|
a.log.WithFields().Errorf("[meituanUnionApiImpl][GetLink] copy request error: %v", err)
|
|
return nil, err
|
|
}
|
|
request.Sign = a.Sign(util.StructToMap(request))
|
|
queryArgs := util.StructToMap(request)
|
|
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
|
|
// todo:: 返回完整的原始数据
|
|
response := new(GetOrderBySingeResponse)
|
|
if err := a.client.HttpGet(GetOrderSingeUrl, req, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// GetOrderByBatch 批量获取订单
|
|
func (a *meituanUnionApiImpl) GetOrderByBatch(params GetOrderByBatchRequest) (*GetOrderByBatchResponse, error) {
|
|
fmt.Printf("params: %+v\n", util.StructToMap(params))
|
|
request := new(ThirdGetOrderByBatchRequest)
|
|
if err := xcopy.SetJSONTag(false).Copy(&request, params); err != nil {
|
|
a.log.WithFields().Errorf("[meituanUnionApiImpl][GetLink] copy request error: %v", err)
|
|
return nil, err
|
|
}
|
|
request.Ts = int32(time.Now().Unix())
|
|
request.Sign = a.Sign(util.StructToMap(request))
|
|
queryArgs := util.StructToMap(request)
|
|
fmt.Printf("queryArgs: %+v\n", queryArgs)
|
|
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
|
|
// todo:: 返回完整的原始数据
|
|
response := new(GetOrderByBatchResponse)
|
|
if err := a.client.HttpGet(GetOrderBatchUrl, req, &client.HttpResponse{Result: response}); err != nil {
|
|
a.log.Errorf("GetOrderByBatch error: %v", err)
|
|
return nil, err
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (a *meituanUnionApiImpl) getSignStr(dataMap map[string]any) []string {
|
|
var kvPairs []string
|
|
for k, v := range dataMap {
|
|
key := a.lowerFirstLetter(k)
|
|
if key == "sign" || key == "Sign" {
|
|
continue
|
|
}
|
|
kvPairs = append(kvPairs, fmt.Sprintf("%s%v", key, v))
|
|
}
|
|
return kvPairs
|
|
}
|
|
|
|
func (a *meituanUnionApiImpl) lowerFirstLetter(s string) string {
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
return strings.ToLower(string(s[0])) + s[1:]
|
|
}
|