2024-04-30 17:57:27 +08:00
|
|
|
package meituan_union
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
2024-05-01 14:05:18 +08:00
|
|
|
"gitee.com/chengdu-lenntc/third-platform-sdk/client"
|
|
|
|
|
"gitee.com/chengdu-lenntc/third-platform-sdk/util"
|
2024-04-30 17:57:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
type meituanUnionApiImpl struct {
|
2024-04-30 17:57:27 +08:00
|
|
|
log logx.Logger
|
|
|
|
|
client *Client
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func newMeituanUnionApiImpl(log logx.Logger, client *Client) MeituanUnionApi {
|
|
|
|
|
return &meituanUnionApiImpl{
|
2024-04-30 17:57:27 +08:00
|
|
|
log: log,
|
|
|
|
|
client: client,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) GetLink(params GenerateLinkRequest) (*GenerateLinkResponse, error) {
|
2024-04-30 17:57:27 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) MiniCode(params MiniCodeRequest) (*MimiCodeResponse, error) {
|
2024-04-30 17:57:27 +08:00
|
|
|
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
|
|
|
|
|
}
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) GetOrderBySinge(params GetOrderBySingeRequest) (*GetOrderBySingeResponse, error) {
|
2024-04-30 17:57:27 +08:00
|
|
|
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
|
|
|
|
|
}
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) GetOrderByBatch(params GetOrderByBatchRequest) (*GetOrderByBatchResponse, error) {
|
2024-04-30 17:57:27 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) sign(params map[string]interface{}) string {
|
2024-04-30 17:57:27 +08:00
|
|
|
kvPairs := a.getSignStr(params)
|
|
|
|
|
sort.Strings(kvPairs)
|
|
|
|
|
paramStr := strings.Join(kvPairs, "")
|
|
|
|
|
return util.Md5String(a.client.authConfig.SignKey + paramStr + a.client.authConfig.SignKey)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) notifySign(params map[string]interface{}) string {
|
2024-04-30 17:57:27 +08:00
|
|
|
kvPairs := a.getSignStr(params)
|
|
|
|
|
sort.Strings(kvPairs)
|
|
|
|
|
paramStr := strings.Join(kvPairs, "")
|
|
|
|
|
return util.Md5String(a.client.authConfig.NotifyKey + paramStr + a.client.authConfig.NotifyKey)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) getSignStr(dataMap map[string]any) []string {
|
2024-04-30 17:57:27 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 18:30:05 +08:00
|
|
|
func (a *meituanUnionApiImpl) lowerFirstLetter(s string) string {
|
2024-04-30 17:57:27 +08:00
|
|
|
if len(s) == 0 {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
return strings.ToLower(string(s[0])) + s[1:]
|
|
|
|
|
}
|