2024-06-22 20:28:21 +08:00
|
|
|
package meituan_media
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
|
|
|
|
"gitee.com/chengdu-lenntc/third-platform-sdk/client"
|
|
|
|
|
"gitee.com/chengdu-lenntc/third-platform-sdk/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MeituanMediaApi 美团-美天赚
|
2024-06-23 14:35:54 +08:00
|
|
|
// Api defines the interface of meituan_media api
|
2024-06-22 20:28:21 +08:00
|
|
|
type MeituanMediaApi interface {
|
|
|
|
|
// Sign 签名
|
|
|
|
|
Sign(methodType string, url string, data map[string]interface{}) string
|
|
|
|
|
// GenerateLink 生成推广链接
|
|
|
|
|
GenerateLink(ctx context.Context, req GenerateLinkRequest) (*GenerateLinkResponse, error)
|
|
|
|
|
// QueryOrderList 查询订单列表
|
|
|
|
|
QueryOrderList(ctx context.Context, req QueryOrderListRequest) (*QueryOrderData, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type meituanMediaApiImpl struct {
|
|
|
|
|
log logx.Logger
|
|
|
|
|
client *Client
|
|
|
|
|
sign *Sign
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newMeituanMediaApiImpl(log logx.Logger, client *Client, sign *Sign) MeituanMediaApi {
|
|
|
|
|
return &meituanMediaApiImpl{
|
|
|
|
|
log: log,
|
|
|
|
|
client: client,
|
|
|
|
|
sign: sign,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sign todo:: 签名
|
|
|
|
|
func (a *meituanMediaApiImpl) Sign(methodType string, uri string, data map[string]interface{}) string {
|
|
|
|
|
headers := a.sign.BuildHeader(methodType, uri, data)
|
|
|
|
|
if sign, ok := headers[SCaSignature]; ok {
|
|
|
|
|
return sign
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateLink 生成推广链接
|
|
|
|
|
func (a *meituanMediaApiImpl) GenerateLink(ctx context.Context, req GenerateLinkRequest) (*GenerateLinkResponse, error) {
|
|
|
|
|
args := util.StructToMap(req)
|
|
|
|
|
headers := a.sign.BuildHeader(http.MethodPost, GetLinkUri, args)
|
|
|
|
|
for k, v := range a.client.headers {
|
|
|
|
|
headers[k] = v
|
|
|
|
|
}
|
|
|
|
|
request := &client.HttpRequest{Headers: headers, BodyArgs: args}
|
|
|
|
|
response := new(GenerateLinkResponse)
|
|
|
|
|
if err := a.client.HttpPost(GetLinkUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if response.Code != 0 {
|
|
|
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "args": args, "resp": response}}).
|
|
|
|
|
Errorf("[meituanMediaApiImpl][GenerateLink] response result error: %s", response.Message)
|
|
|
|
|
return nil, errors.New(response.Message)
|
|
|
|
|
}
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryOrderList 查询订单列表
|
|
|
|
|
func (a *meituanMediaApiImpl) QueryOrderList(ctx context.Context, req QueryOrderListRequest) (*QueryOrderData, error) {
|
|
|
|
|
args := util.StructToMap(req)
|
|
|
|
|
headers := a.sign.BuildHeader(http.MethodPost, GetOrderListUri, args)
|
|
|
|
|
for k, v := range a.client.headers {
|
|
|
|
|
headers[k] = v
|
|
|
|
|
}
|
|
|
|
|
request := &client.HttpRequest{Headers: headers, BodyArgs: args}
|
|
|
|
|
response := new(QueryOrderListResponse)
|
|
|
|
|
if err := a.client.HttpPost(GetOrderListUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if response.Code != 0 {
|
|
|
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
2024-06-22 22:30:52 +08:00
|
|
|
Errorf("[meituanMediaApiImpl][QueryOrderList] response result error: %s", response.Message)
|
2024-06-22 20:28:21 +08:00
|
|
|
return nil, errors.New(response.Message)
|
|
|
|
|
}
|
|
|
|
|
return response.Data, nil
|
|
|
|
|
}
|