2024-06-15 12:37:11 +08:00
|
|
|
package t3_union
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-06-15 15:09:19 +08:00
|
|
|
"errors"
|
2024-06-15 12:37:11 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
2024-07-29 23:52:23 +08:00
|
|
|
"repository.lenntc.com/lenntc/third-platform-sdk/client"
|
|
|
|
|
"repository.lenntc.com/lenntc/third-platform-sdk/util"
|
2024-06-15 12:37:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// T3UnionApi 调用第三方平台的api
|
|
|
|
|
// Api defines the interface of t3_union api
|
|
|
|
|
type T3UnionApi interface {
|
|
|
|
|
// Sign 签名
|
|
|
|
|
Sign(methodType string, url string, data map[string]interface{}) string
|
|
|
|
|
// GenerateLink 生成短链
|
2024-06-15 15:09:19 +08:00
|
|
|
GenerateLink(ctx context.Context, req GenerateLinkRequest) (*GenerateLinkData, error)
|
2024-06-15 12:37:11 +08:00
|
|
|
// GenerateCode 生成二维码
|
2024-06-15 15:09:19 +08:00
|
|
|
GenerateCode(ctx context.Context, req GenerateCodeRequest) (*GenerateCodeData, error)
|
2024-06-15 12:37:11 +08:00
|
|
|
// GeneratePoster 生成推广海报
|
2024-06-15 15:09:19 +08:00
|
|
|
GeneratePoster(ctx context.Context, req GeneratePosterRequest) (*GeneratePosterData, error)
|
2024-06-15 12:37:11 +08:00
|
|
|
// QueryOrderList 查询订单列表
|
2024-06-15 15:09:19 +08:00
|
|
|
QueryOrderList(ctx context.Context, req QueryOrderListRequest) (*QueryOrderData, error)
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type t3UnionApiImpl struct {
|
|
|
|
|
log logx.Logger
|
|
|
|
|
client *Client
|
2024-06-22 20:28:21 +08:00
|
|
|
sign *Sign
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newT3UnionApiImpl(log logx.Logger, client *Client) T3UnionApi {
|
2024-06-22 20:28:21 +08:00
|
|
|
sign := newSign(client.authConfig.AppKey, client.authConfig.AppSecret)
|
2024-06-15 12:37:11 +08:00
|
|
|
return &t3UnionApiImpl{
|
|
|
|
|
log: log,
|
|
|
|
|
client: client,
|
2024-06-22 20:28:21 +08:00
|
|
|
sign: sign,
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sign 签名
|
2024-06-15 15:09:19 +08:00
|
|
|
func (a *t3UnionApiImpl) Sign(methodType string, uri string, dataMap map[string]any) string {
|
2024-06-22 20:28:21 +08:00
|
|
|
headers := a.sign.BuildHeader(methodType, uri, dataMap)
|
2024-06-15 12:37:11 +08:00
|
|
|
if sign, ok := headers[XT3Signature]; ok {
|
|
|
|
|
return sign
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateLink 生成短链
|
2024-06-15 15:09:19 +08:00
|
|
|
func (a *t3UnionApiImpl) GenerateLink(ctx context.Context, req GenerateLinkRequest) (*GenerateLinkData, error) {
|
2024-06-15 12:37:11 +08:00
|
|
|
args := util.StructToMap(req)
|
2024-06-22 20:28:21 +08:00
|
|
|
headers := a.sign.BuildHeader(http.MethodPost, GetLinkUri, args)
|
2024-06-15 12:37:11 +08:00
|
|
|
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
|
|
|
|
|
}
|
2024-06-15 15:09:19 +08:00
|
|
|
if !response.Success || response.ErrCode != 0 {
|
|
|
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
|
|
|
|
Errorf("[t3UnionApiImpl][GenerateLink] response result error: %s", response.Msg)
|
|
|
|
|
return nil, errors.New(response.Msg)
|
|
|
|
|
}
|
|
|
|
|
return response.Data, nil
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateCode 生成二维码
|
2024-06-15 15:09:19 +08:00
|
|
|
func (a *t3UnionApiImpl) GenerateCode(ctx context.Context, req GenerateCodeRequest) (*GenerateCodeData, error) {
|
2024-06-15 12:37:11 +08:00
|
|
|
args := util.StructToMap(req)
|
2024-06-22 20:28:21 +08:00
|
|
|
headers := a.sign.BuildHeader(http.MethodPost, GetMiniCodeUri, args)
|
2024-06-15 12:37:11 +08:00
|
|
|
for k, v := range a.client.headers {
|
|
|
|
|
headers[k] = v
|
|
|
|
|
}
|
|
|
|
|
request := &client.HttpRequest{Headers: headers, BodyArgs: args}
|
|
|
|
|
response := new(GenerateCodeResponse)
|
|
|
|
|
if err := a.client.HttpPost(GetMiniCodeUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-06-15 15:09:19 +08:00
|
|
|
if !response.Success || response.ErrCode != 0 {
|
|
|
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
2024-06-15 15:11:38 +08:00
|
|
|
Errorf("[t3UnionApiImpl][GenerateCode] response result error: %s", response.Msg)
|
2024-06-15 15:09:19 +08:00
|
|
|
return nil, errors.New(response.Msg)
|
|
|
|
|
}
|
|
|
|
|
return response.Data, nil
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GeneratePoster 生成推广海报
|
2024-06-15 15:09:19 +08:00
|
|
|
func (a *t3UnionApiImpl) GeneratePoster(ctx context.Context, req GeneratePosterRequest) (*GeneratePosterData, error) {
|
2024-06-15 12:37:11 +08:00
|
|
|
args := util.StructToMap(req)
|
2024-06-22 20:28:21 +08:00
|
|
|
headers := a.sign.BuildHeader(http.MethodPost, GetPosterUri, args)
|
2024-06-15 12:37:11 +08:00
|
|
|
for k, v := range a.client.headers {
|
|
|
|
|
headers[k] = v
|
|
|
|
|
}
|
|
|
|
|
request := &client.HttpRequest{Headers: headers, BodyArgs: args}
|
|
|
|
|
response := new(GeneratePosterResponse)
|
|
|
|
|
if err := a.client.HttpPost(GetPosterUrl, request, &client.HttpResponse{Result: response}); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-06-15 15:09:19 +08:00
|
|
|
if !response.Success || response.ErrCode != 0 {
|
|
|
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
2024-06-15 15:11:38 +08:00
|
|
|
Errorf("[t3UnionApiImpl][GeneratePoster] response result error: %s", response.Msg)
|
2024-06-15 15:09:19 +08:00
|
|
|
return nil, errors.New(response.Msg)
|
|
|
|
|
}
|
|
|
|
|
return response.Data, nil
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryOrderList 查询订单列表
|
2024-06-15 15:09:19 +08:00
|
|
|
func (a *t3UnionApiImpl) QueryOrderList(ctx context.Context, req QueryOrderListRequest) (*QueryOrderData, error) {
|
2024-06-15 21:01:51 +08:00
|
|
|
if len(req.SupplierUuid) == 0 {
|
|
|
|
|
req.SupplierUuid = a.client.authConfig.SupplierID
|
|
|
|
|
}
|
2024-06-15 12:37:11 +08:00
|
|
|
args := util.StructToMap(req)
|
2024-06-22 20:28:21 +08:00
|
|
|
headers := a.sign.BuildHeader(http.MethodPost, GetOrderListUri, args)
|
2024-06-15 12:37:11 +08:00
|
|
|
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
|
|
|
|
|
}
|
2024-06-15 15:09:19 +08:00
|
|
|
if !response.Success || response.ErrCode != 0 {
|
|
|
|
|
a.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
2024-06-15 15:11:38 +08:00
|
|
|
Errorf("[t3UnionApiImpl][QueryOrderList] response result error: %s", response.Msg)
|
2024-06-15 15:09:19 +08:00
|
|
|
return nil, errors.New(response.Msg)
|
|
|
|
|
}
|
|
|
|
|
return response.Data, nil
|
2024-06-15 12:37:11 +08:00
|
|
|
}
|