third-platform-sdk/platform/meituan-csr/api.go
2024-07-29 23:52:23 +08:00

65 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package meituan_csr
import (
"context"
"fmt"
"time"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/trace"
"repository.lenntc.com/lenntc/third-platform-sdk/client"
"repository.lenntc.com/lenntc/third-platform-sdk/util"
)
// Api 调用第三方平台的api
type MeituanCsrApi interface {
// GetLink 获取推广链接
GetLink(params PromotionLinkRequest) (*PromotionLinkResponse, error)
}
type meituanCsrApiImpl struct {
log logx.Logger
client *Client
}
func newMeituanCsrApiImpl(log logx.Logger, client *Client) MeituanCsrApi {
return &meituanCsrApiImpl{
log: log,
client: client,
}
}
func (a *meituanCsrApiImpl) GetLink(params PromotionLinkRequest) (*PromotionLinkResponse, error) {
url := a.url(context.Background(), GetLinkUrl)
queryArgs := util.StructToMap(params)
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
response := new(PromotionLinkResponse)
if err := a.client.HttpGet(url, req, &client.HttpResponse{Result: response}); err != nil {
return nil, err
}
return response, nil
}
func (a *meituanCsrApiImpl) url(ctx context.Context, url string) string {
requestId := trace.TraceIDFromContext(ctx)
timestamp := time.Now().Unix()
accessToken := a.generateAccessToken(timestamp)
return fmt.Sprintf("%s?requestId=%s&version=%s&accessToken=%s&utmSource=%s&timestamp=%d", url, requestId, "2.0", accessToken, a.client.authConfig.UtmSource, timestamp)
}
func (a *meituanCsrApiImpl) sign(params map[string]any) string {
//kvPairs := util.MapToSliceByKeyValue(params)
//sort.Strings(kvPairs)
//paramStr := strings.Join(kvPairs, "")
//return util.Md5String(a.SignKey + paramStr + a.SignKey)
return ""
}
func (a *meituanCsrApiImpl) generateAccessToken(ts int64) string {
//Access_token生成规则
//将媒体utmSource与时间戳拼接起来然后使用AES加密生成。例如utmSource为1000时间戳1555510603则access_token为AesEncode(10001555510603)每个token有效期为1小时
encodeStr, _ := a.client.aes.Encode(fmt.Sprintf("%s%d", a.client.authConfig.UtmSource, ts))
return encodeStr
}