91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package tong_cheng
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"gitee.com/chengdu-lenntc/third-platform-sdk/client"
|
|
"gitee.com/chengdu-lenntc/third-platform-sdk/util"
|
|
)
|
|
|
|
// TongChengApi 同程酒店的api
|
|
// Api defines the interface of tong-cheng api
|
|
type TongChengApi interface {
|
|
// GetExternalChannelConfig 获取渠道配置信息
|
|
GetExternalChannelConfig(ctx context.Context, req GetExternalChannelConfigRequest, env string) (*ExternalChannelConfigResponse, error)
|
|
// QueryOrderList 查询订单列表
|
|
QueryOrderList(ctx context.Context, req QueryOrderListRequest, env string) (*ExternalOrderData, error)
|
|
}
|
|
|
|
type tongChengApiImpl struct {
|
|
log logx.Logger
|
|
client *Client
|
|
}
|
|
|
|
func newTongChengApiImpl(log logx.Logger, client *Client) TongChengApi {
|
|
return &tongChengApiImpl{
|
|
log: log,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// GenerateLink 生成短链
|
|
func (t *tongChengApiImpl) GetExternalChannelConfig(ctx context.Context, req GetExternalChannelConfigRequest, env string) (*ExternalChannelConfigResponse, error) {
|
|
var url string
|
|
token := t.client.authConfig.Token
|
|
tn := time.Now()
|
|
actionTime := tn.Unix()
|
|
h := md5.New()
|
|
h.Write([]byte(fmt.Sprintf("%s%d", token, actionTime)))
|
|
queryArgs := &GetExternalChannelConfigReq{
|
|
ActionTime: tn.Unix(),
|
|
Token: token,
|
|
Code: string(h.Sum(nil)),
|
|
Trackid: req.Trackid,
|
|
}
|
|
args := util.StructToMap(queryArgs)
|
|
request := &client.HttpRequest{Headers: t.client.headers, QueryArgs: args}
|
|
response := new(GetExternalChannelConfigResponse)
|
|
if env == EnvHuidu {
|
|
url = HDApiDomain + GetLinkUri
|
|
} else {
|
|
url = ApiDomain + GetLinkUri
|
|
}
|
|
if err := t.client.HttpGet(url, request, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
if response.Code != 200 {
|
|
t.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
|
Errorf("[tongChengApiImpl][GetExternalChannelConfig] response result error: %s", response.Msg)
|
|
return nil, errors.New(response.Msg)
|
|
}
|
|
return response.Data, nil
|
|
}
|
|
|
|
// QueryOrderList 查询订单列表
|
|
func (t *tongChengApiImpl) QueryOrderList(ctx context.Context, req QueryOrderListRequest, env string) (*ExternalOrderData, error) {
|
|
var url string
|
|
args := util.StructToMap(req)
|
|
request := &client.HttpRequest{Headers: t.client.headers, QueryArgs: args}
|
|
response := new(GetExternalOrderResp)
|
|
if env == EnvHuidu {
|
|
url = HDApiDomain + GetOrderListUri
|
|
} else {
|
|
url = ApiDomain + GetOrderListUri
|
|
}
|
|
if err := t.client.HttpGet(url, request, &client.HttpResponse{Result: response}); err != nil {
|
|
return nil, err
|
|
}
|
|
if response.Code != 200 {
|
|
t.log.WithFields(logx.LogField{Key: "data", Value: map[string]any{"req": req, "resp": response}}).
|
|
Errorf("[tongChengApiImpl][QueryOrderList] response result error: %s", response.Msg)
|
|
return nil, errors.New(response.Msg)
|
|
}
|
|
return response.Data, nil
|
|
}
|