third-platform-sdk/platform/meituan-union/api.go
2024-05-01 14:05:18 +08:00

108 lines
3.5 KiB
Go

package meituan_union
import (
"fmt"
"sort"
"strings"
"github.com/zeromicro/go-zero/core/logx"
"gitee.com/chengdu-lenntc/third-platform-sdk/client"
"gitee.com/chengdu-lenntc/third-platform-sdk/util"
)
// Api 调用第三方平台的api
type MeituanUnionApi interface {
GetLink(params GenerateLinkRequest) (*GenerateLinkResponse, error)
MiniCode(params MiniCodeRequest) (*MimiCodeResponse, error)
GetOrderBySinge(params GetOrderBySingeRequest) (*GetOrderBySingeResponse, error)
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,
}
}
func (a *meituanUnionApiImpl) GetLink(params GenerateLinkRequest) (*GenerateLinkResponse, error) {
params.Sign = a.sign(util.StructToMap(params))
queryArgs := util.StructToMap(params)
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
}
func (a *meituanUnionApiImpl) MiniCode(params MiniCodeRequest) (*MimiCodeResponse, error) {
params.Sign = a.sign(util.StructToMap(params))
queryArgs := util.StructToMap(params)
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
response := new(MimiCodeResponse)
if err := a.client.HttpGet(GetMiniCode, req, &client.HttpResponse{Result: response}); err != nil {
return nil, err
}
return response, nil
}
func (a *meituanUnionApiImpl) GetOrderBySinge(params GetOrderBySingeRequest) (*GetOrderBySingeResponse, error) {
params.Sign = a.sign(util.StructToMap(params))
queryArgs := util.StructToMap(params)
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
response := new(GetOrderBySingeResponse)
if err := a.client.HttpGet(GetOrderSinge, req, &client.HttpResponse{Result: response}); err != nil {
return nil, err
}
return response, nil
}
func (a *meituanUnionApiImpl) GetOrderByBatch(params GetOrderByBatchRequest) (*GetOrderByBatchResponse, error) {
params.Sign = a.sign(util.StructToMap(params))
queryArgs := util.StructToMap(params)
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
response := new(GetOrderByBatchResponse)
if err := a.client.HttpGet(GetOrderBatch, req, &client.HttpResponse{Result: response}); err != nil {
return nil, err
}
return response, nil
}
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)
}
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)
}
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:]
}