143 lines
4.6 KiB
Go
143 lines
4.6 KiB
Go
package jutuike
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/spf13/cast"
|
|
"reflect"
|
|
"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"
|
|
)
|
|
|
|
// JutuikeApi 美团-美天赚
|
|
// Api defines the interface of jutuike api
|
|
type JutuikeApi interface {
|
|
// GenerateLink 生成推广链接
|
|
GenerateLink(ctx context.Context, req GenerateLinkRequest) (*GenerateLinkData, error)
|
|
// QueryOrderList 查询订单列表
|
|
QueryOrderList(ctx context.Context, req QueryOrderListRequest) (*QueryOrderListData, error)
|
|
}
|
|
|
|
type jutuikeApiImpl struct {
|
|
log logx.Logger
|
|
client *Client
|
|
}
|
|
|
|
func newJutuikeApiImpl(log logx.Logger, client *Client) JutuikeApi {
|
|
return &jutuikeApiImpl{
|
|
log: log,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// GenerateLink 生成推广链接
|
|
func (a *jutuikeApiImpl) GenerateLink(ctx context.Context, req GenerateLinkRequest) (*GenerateLinkData, error) {
|
|
if req.ActId == 0 || len(req.Sid) == 0 {
|
|
return nil, errors.New("请求参数actId或sid不能为空")
|
|
}
|
|
if len(req.ApiKey) == 0 {
|
|
req.ApiKey = a.client.authConfig.ApiKey
|
|
}
|
|
args := util.StructToMap(req)
|
|
request := &client.HttpRequest{Headers: a.client.headers, QueryArgs: args}
|
|
response := new(GenerateLinkResponse)
|
|
if err := a.client.HttpGet(GetLinkUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
if response.Code != 1 {
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).Errorf("[jutuikeApiImpl][GenerateLink] response result error: %s", response.Msg)
|
|
return nil, errors.New(response.Msg)
|
|
}
|
|
result := new(GenerateLinkData)
|
|
data := cast.ToStringMap(response.Data)
|
|
if _, ok := data["act_name"]; ok {
|
|
result.ActName = data["act_name"].(string)
|
|
}
|
|
if _, ok := data["poster_qrcode_url"]; ok {
|
|
result.PosterQrcodeUrl = data["poster_qrcode_url"].(string)
|
|
}
|
|
if _, ok := data["h5"]; ok {
|
|
result.H5 = data["h5"].(string)
|
|
}
|
|
if _, ok := data["long_h5"]; ok {
|
|
result.LongH5 = data["long_h5"].(string)
|
|
}
|
|
if _, ok := data["h5_evoke"]; ok {
|
|
result.H5Evoke = data["h5_evoke"].(string)
|
|
}
|
|
if _, ok := data["deeplink"]; ok {
|
|
result.DeepLink = data["deeplink"].(string)
|
|
}
|
|
if _, ok := data["we_app_info"]; ok {
|
|
weApp := cast.ToStringMap(data["we_app_info"])
|
|
weAppInfoTemp := new(WeAppInfo)
|
|
if _, tok := weApp["app_id"]; tok {
|
|
weAppInfoTemp.AppId = weApp["app_id"].(string)
|
|
}
|
|
if _, tok := weApp["page_path"]; tok {
|
|
weAppInfoTemp.PagePath = weApp["page_path"].(string)
|
|
}
|
|
if _, tok := weApp["miniCode"]; tok {
|
|
weAppInfoTemp.MiniCode = weApp["miniCode"].(string)
|
|
}
|
|
result.WeAppInfo = weAppInfoTemp
|
|
}
|
|
if _, ok := data["alipay_app_info"]; ok {
|
|
alipayApp := cast.ToStringMap(data["alipay_app_info"])
|
|
alipayAppInfoTemp := new(AlipayAppInfo)
|
|
if _, tok := alipayApp["app_id"]; tok {
|
|
alipayAppInfoTemp.AppId = alipayApp["app_id"].(string)
|
|
}
|
|
if _, tok := alipayApp["page_path"]; tok {
|
|
alipayAppInfoTemp.PagePath = alipayApp["page_path"].(string)
|
|
}
|
|
result.AlipayAppInfo = alipayAppInfoTemp
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// QueryOrderList 查询订单列表
|
|
func (a *jutuikeApiImpl) QueryOrderList(ctx context.Context, req QueryOrderListRequest) (*QueryOrderListData, error) {
|
|
if len(req.StartTime) == 0 || len(req.EndTime) == 0 {
|
|
return nil, errors.New("请求参数start_time或end_time为必传")
|
|
}
|
|
if _, err := time.Parse(time.DateTime, req.StartTime); err != nil {
|
|
return nil, errors.New("开始时间start_time时间格式错误")
|
|
}
|
|
if _, err := time.Parse(time.DateTime, req.EndTime); err != nil {
|
|
return nil, errors.New("结束时间end_time时间格式错误")
|
|
}
|
|
if len(req.ApiKey) == 0 {
|
|
req.ApiKey = a.client.authConfig.ApiKey
|
|
}
|
|
args := util.StructToMap(req)
|
|
request := &client.HttpRequest{Headers: a.client.headers, BodyArgs: args}
|
|
response := new(QueryOrderListResponse)
|
|
if err := a.client.HttpPost(GetOrderListUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
responseData := new(QueryOrderListData)
|
|
if response.Code != 1 {
|
|
dataType := reflect.TypeOf(response.Data).String()
|
|
if dataType == "string" {
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).Errorf("[jutuikeApiImpl][QueryOrderList] response result error: %s", response.Msg)
|
|
return nil, errors.New(response.Msg)
|
|
} else {
|
|
responseData.PerPage = req.Page
|
|
responseData.CurrentPage = req.Page
|
|
responseData.LastPage = req.Page
|
|
responseData.Data = make([]*OrderItem, 0)
|
|
return responseData, nil
|
|
}
|
|
}
|
|
if err := xcopy.Copy(&responseData, response.Data); err != nil {
|
|
return nil, err
|
|
}
|
|
return responseData, nil
|
|
}
|