2024-07-24 10:43:43 +08:00
|
|
|
package dw_sdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-25 13:30:34 +08:00
|
|
|
|
2024-07-24 10:43:43 +08:00
|
|
|
"github.com/bytedance/sonic"
|
|
|
|
"github.com/gofiber/fiber/v3/client"
|
|
|
|
"github.com/pkg/errors"
|
2024-07-25 13:30:34 +08:00
|
|
|
"github.com/valyala/fasthttp/fasthttpproxy"
|
|
|
|
|
2024-07-24 10:43:43 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//https://open.dewu.com/#/api/body?apiId=42&id=3&title=%E5%87%BA%E4%BB%B7%E6%9C%8D%E5%8A%A1API
|
|
|
|
|
|
|
|
type BidClient interface {
|
2024-07-29 15:45:13 +08:00
|
|
|
LowestPrice(skuId int) (*PublicResponse[LowestPriceResponse], error)
|
2024-07-24 10:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConsignBidClientImpl struct {
|
|
|
|
cli *client.Client
|
|
|
|
cfg PublicConfig
|
|
|
|
baseUrl string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsignBidClient(cfg Config) BidClient {
|
|
|
|
baseUrl := GetUrl(cfg.Public.Env)
|
|
|
|
cli := client.New().SetJSONMarshal(func(v any) ([]byte, error) {
|
|
|
|
return sonic.Marshal(v)
|
|
|
|
}).SetJSONUnmarshal(func(data []byte, v any) error {
|
|
|
|
return sonic.Unmarshal(data, v)
|
|
|
|
}).AddRequestHook(func(c *client.Client, request *client.Request) error {
|
|
|
|
request.SetURL(baseUrl + request.URL())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if cfg.Proxy != nil {
|
2024-07-25 13:30:34 +08:00
|
|
|
cli.SetDial(fasthttpproxy.FasthttpHTTPDialer(fmt.Sprintf("%s:%s@%s:%s", cfg.Proxy.User, cfg.Proxy.Pass, cfg.Proxy.Host, cfg.Proxy.Port)))
|
2024-07-24 10:43:43 +08:00
|
|
|
}
|
|
|
|
return &ConsignBidClientImpl{
|
|
|
|
cli: cli,
|
|
|
|
cfg: cfg.Public,
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取平台sku最低价【闪电直发】
|
|
|
|
const FastLowestPriceUrl = "/dop/api/v1/consign/bidding/lowest_price"
|
|
|
|
|
|
|
|
type ConsignLowestPriceReq struct {
|
|
|
|
PublicParams
|
2024-07-29 15:45:13 +08:00
|
|
|
SkuId int `json:"sku_id" url:"sku_id"`
|
2024-07-24 10:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type LowestPriceResponse struct {
|
|
|
|
SkuId int `json:"sku_id"`
|
|
|
|
Items []LowestPriceItem `json:"items"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LowestPriceItem struct {
|
|
|
|
LowestPrice int `json:"lowest_price"`
|
|
|
|
BiddingType BiddingType `json:"bidding_type"`
|
|
|
|
}
|
|
|
|
|
2024-07-29 15:45:13 +08:00
|
|
|
func (a *ConsignBidClientImpl) LowestPrice(skuId int) (*PublicResponse[LowestPriceResponse], error) {
|
2024-07-24 10:43:43 +08:00
|
|
|
if skuId == 0 {
|
|
|
|
return nil, Err_Params_None
|
|
|
|
}
|
|
|
|
data := ConsignLowestPriceReq{
|
|
|
|
PublicParams: PublicParams{
|
|
|
|
AppKey: a.cfg.Key,
|
|
|
|
Timestamp: time.Now().UnixMilli(),
|
|
|
|
},
|
|
|
|
SkuId: skuId,
|
|
|
|
}
|
|
|
|
|
|
|
|
Sign(&data, a.cfg.Secret)
|
|
|
|
|
|
|
|
r := a.cli.R()
|
|
|
|
defer client.ReleaseRequest(r)
|
|
|
|
|
|
|
|
response, err := r.SetJSON(data).Post(FastLowestPriceUrl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(Err_Request_Err, "consign/bidding/lowest_price err:%v", err)
|
|
|
|
}
|
|
|
|
defer client.ReleaseResponse(response)
|
|
|
|
var resp PublicResponse[LowestPriceResponse]
|
|
|
|
if err = response.JSON(&resp); err != nil {
|
|
|
|
return nil, errors.Wrapf(Err_JSON_UNMarshal, "consign/bidding/lowest_price err:%v", err)
|
|
|
|
}
|
|
|
|
return &resp, nil
|
|
|
|
}
|