dw-sdk/article-service.go
2024-09-04 11:19:27 +08:00

112 lines
3.6 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 dw_sdk
import (
"fmt"
"time"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v3/client"
"github.com/pkg/errors"
"github.com/valyala/fasthttp/fasthttpproxy"
)
//商品服务API
//https://open.dewu.com/#/api/body?apiId=156&id=2&title=%E5%95%86%E5%93%81%E6%9C%8D%E5%8A%A1API
type ArticleServiceClient interface {
BatchArticleNumber(ArticleNumbers []string) (*PublicResponse[[]Spu], error)
}
type ArticleServiceClientImpl struct {
cli *client.Client
cfg PublicConfig
}
func NewArticleServiceClient(cfg Config) ArticleServiceClient {
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)
}).SetBaseURL(GetUrl(cfg.Public.Env))
if cfg.Proxy != nil {
cli.SetDial(fasthttpproxy.FasthttpHTTPDialer(fmt.Sprintf("%s:%s@%s:%s", cfg.Proxy.User, cfg.Proxy.Pass, cfg.Proxy.Host, cfg.Proxy.Port)))
}
return &ArticleServiceClientImpl{
cli: cli,
cfg: cfg.Public,
}
}
//商品spu信息
//https://open.dewu.com/#/api/body?apiId=156&id=2&title=%E5%95%86%E5%93%81%E6%9C%8D%E5%8A%A1API
type BatchArticleNumber struct {
PublicParams
ArticleNumbers []string `json:"article_numbers" url:"article_numbers"`
}
type ArticleStatus int
// 商品状态
const (
ArticleStatus_Offline ArticleStatus = iota
ArticleStatus_Online
)
type Spu struct {
SpuId int `json:"spu_id"`
CategoryId int `json:"category_id"`
CategoryName string `json:"category_name"`
BrandId BrandId `json:"brand_id"`
BrandName BrandName `json:"brand_name"`
ArticleNumber string `json:"article_number"` //商品货号
OtherNumbers string `json:"other_numbers"` // 辅助货号,逗号分隔,可传多个
Title string `json:"title"`
SpuProperties string `json:"spu_properties,omitempty"`
AuthPrice int `json:"auth_price"` //官方售价(单位:分)
Status ArticleStatus `json:"status"` //商品上下架状态(1:上架0:下架)
SpuLogo string `json:"spu_logo"`
Skus []Sku `json:"skus"`
}
type Sku struct {
SkuId int `json:"sku_id"`
Status ArticleStatus `json:"status"`
Properties string `json:"properties"` //{"尺码":"M"} 商品规格属性
Barcode string `json:"barcode"` //商品条码
MerchantSkuCode string `json:"merchant_sku_code,omitempty"` // 商家商品编码
OtherMerchantSkuCodes []string `json:"other_merchant_sku_codes,omitempty"` //辅助商家商品编码
ExtraCode string `json:"extra_code,omitempty"` //sku得物码附加码
}
const BatchArticleNumberURL = "/dop/api/v1/spu/batch_article_number"
func (a *ArticleServiceClientImpl) BatchArticleNumber(ArticleNumbers []string) (*PublicResponse[[]Spu], error) {
if len(ArticleNumbers) == 0 {
return nil, Err_Params_None
}
data := BatchArticleNumber{
PublicParams: PublicParams{
AppKey: a.cfg.Key,
Timestamp: time.Now().UnixMilli(),
},
ArticleNumbers: ArticleNumbers,
}
Sign(&data, a.cfg.Secret)
r := a.cli.R()
defer client.ReleaseRequest(r)
response, err := r.SetJSON(data).Post(BatchArticleNumberURL)
if err != nil {
return nil, errors.Wrapf(Err_Request_Err, "BatchArticleNumber err:%v", err)
}
defer client.ReleaseResponse(response)
var resp PublicResponse[[]Spu]
if err = response.JSON(&resp); err != nil {
return nil, errors.Wrapf(Err_JSON_UNMarshal, "BatchArticleNumber err:%v", err)
}
return &resp, nil
}