third-platform-sdk/platform/meituan-csr/api.go

65 lines
2.1 KiB
Go
Raw Normal View History

2024-04-30 17:57:27 +08:00
package meituan_csr
import (
2024-04-30 18:30:05 +08:00
"context"
2024-04-30 17:57:27 +08:00
"fmt"
2024-04-30 18:30:05 +08:00
"time"
2024-04-30 17:57:27 +08:00
"github.com/zeromicro/go-zero/core/logx"
2024-04-30 18:30:05 +08:00
"github.com/zeromicro/go-zero/core/trace"
2024-04-30 17:57:27 +08:00
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 MeituanCsrApi interface {
2024-04-30 18:30:05 +08:00
GetLink(params PromotionLinkRequest) (*PromotionLinkResponse, error)
Url(ctx context.Context, url string) string
2024-04-30 17:57:27 +08:00
}
2024-04-30 18:30:05 +08:00
type meituanCsrApiImpl struct {
2024-04-30 17:57:27 +08:00
log logx.Logger
client *Client
}
2024-04-30 18:30:05 +08:00
func newMeituanCsrApiImpl(log logx.Logger, client *Client) MeituanCsrApi {
return &meituanCsrApiImpl{
2024-04-30 17:57:27 +08:00
log: log,
client: client,
}
2024-04-30 18:30:05 +08:00
}
func (a *meituanCsrApiImpl) GetLink(params PromotionLinkRequest) (*PromotionLinkResponse, error) {
2024-05-01 17:14:00 +08:00
url := a.Url(context.Background(), GetLinkUrl)
2024-04-30 18:30:05 +08:00
queryArgs := util.StructToMap(params)
req := &client.HttpRequest{Headers: a.client.Headers, QueryArgs: queryArgs}
response := new(PromotionLinkResponse)
2024-05-01 17:14:00 +08:00
if err := a.client.HttpGet(url, req, &client.HttpResponse{Result: response}); err != nil {
2024-04-30 18:30:05 +08:00
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小时
2024-05-01 17:14:00 +08:00
encodeStr, _ := a.client.aes.Encode(fmt.Sprintf("%s%d", a.client.authConfig.UtmSource, ts))
2024-04-30 18:30:05 +08:00
return encodeStr
}