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" ) // MeituanUnionApi 美团联盟平台的api type MeituanUnionApi interface { // Sign 签名 Sign(params map[string]interface{}) string // NotifySign 通知签名 NotifySign(params map[string]interface{}) string // GetLink 获取推广链接 GetLink(params GenerateLinkRequest) (*GenerateLinkResponse, error) // MiniCode 获取小程序码 MiniCode(params MiniCodeRequest) (*MimiCodeResponse, error) // GetOrderBySinge 获取单个订单 GetOrderBySinge(params GetOrderBySingeRequest) (*GetOrderBySingeResponse, error) // GetOrderByBatch 批量获取订单 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, } } // Sign 签名 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) } // NotifySign 通知签名 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) } // GetLink 获取推广链接 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 } // MiniCode 获取小程序码 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 } // GetOrderBySinge 获取单个订单 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 } // GetOrderByBatch 批量获取订单 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) 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:] }