feat init 初始化

TODO fiber v3 client proxy有bug,proxy不生效
This commit is contained in:
timerzz 2024-07-24 10:43:43 +08:00
commit 62b04c9599
13 changed files with 531 additions and 0 deletions

112
article-service.go Normal file
View File

@ -0,0 +1,112 @@
package dw_sdk
import (
"fmt"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v3/client"
"github.com/pkg/errors"
"time"
)
//商品服务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)).Debug()
if cfg.Proxy != nil {
err := cli.SetProxyURL(fmt.Sprintf("http://%s:%s@%s:%s", cfg.Proxy.User, cfg.Proxy.Pass, cfg.Proxy.Host, cfg.Proxy.Port))
if err != nil {
fmt.Println(err)
}
}
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 int `json:"brand_id"`
BrandName string `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
}

29
article-service_test.go Normal file
View File

@ -0,0 +1,29 @@
package dw_sdk
import "testing"
func TestNewArticleServiceClient(t *testing.T) {
//cli := NewArticleServiceClient(PublicConfig{
// Key: "340139807ccc413b8767f89ce2ffb121",
// Secret: "3c3ab5aa12fc4f7d8e11a32e11c08760",
// Env: "dev",
//})
cli := NewArticleServiceClient(Config{
Public: PublicConfig{
Key: "92a2afa02627410fae9e3331cc850224",
Secret: "73b5bf12bc5e4b4690f444b32b934dc0feb1e42977514333a76f263592ee37f4",
Env: "online",
},
Proxy: &ProxyConfig{
Host: "jd.timerzz.com",
Port: "8090",
User: "timerzz",
Pass: "zhhg1997",
},
})
resp, err := cli.BatchArticleNumber([]string{"cq067"})
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}

89
bid-consign.go Normal file
View File

@ -0,0 +1,89 @@
package dw_sdk
import (
"fmt"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v3/client"
"github.com/pkg/errors"
"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 {
LowestPrice(skuId int64) (*PublicResponse[LowestPriceResponse], error)
}
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 {
_ = cli.SetProxyURL(fmt.Sprintf("http://%s:%s@%s:%s", cfg.Proxy.User, cfg.Proxy.Pass, cfg.Proxy.Host, cfg.Proxy.Port))
}
return &ConsignBidClientImpl{
cli: cli,
cfg: cfg.Public,
baseUrl: baseUrl,
}
}
// 获取平台sku最低价【闪电直发】
const FastLowestPriceUrl = "/dop/api/v1/consign/bidding/lowest_price"
type ConsignLowestPriceReq struct {
PublicParams
SkuId int64 `json:"sku_id" url:"sku_id"`
}
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"`
}
func (a *ConsignBidClientImpl) LowestPrice(skuId int64) (*PublicResponse[LowestPriceResponse], error) {
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
}

21
bid-consign_test.go Normal file
View File

@ -0,0 +1,21 @@
package dw_sdk
import "testing"
var testConfig = Config{
Public: PublicConfig{
Key: "340139807ccc413b8767f89ce2ffb121",
Secret: "3c3ab5aa12fc4f7d8e11a32e11c08760",
Env: "dev",
},
Proxy: nil,
}
func TestNewBidConsignClient(t *testing.T) {
cli := NewConsignBidClient(testConfig)
resp, err := cli.LowestPrice(123)
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}

7
bid.go Normal file
View File

@ -0,0 +1,7 @@
package dw_sdk
type BiddingType int8
const (
BiddingType_Consign BiddingType = 5 //闪电直发
)

21
client.go Normal file
View File

@ -0,0 +1,21 @@
package dw_sdk
type Client struct {
articleServiceClient ArticleServiceClient
consignBidClient BidClient
}
func NewClient(cfg Config) *Client {
return &Client{
articleServiceClient: NewArticleServiceClient(cfg),
consignBidClient: NewConsignBidClient(cfg),
}
}
func (c *Client) ArticleService() ArticleServiceClient {
return c.articleServiceClient
}
func (c *Client) ConsignBidClient() BidClient {
return c.consignBidClient
}

19
config.go Normal file
View File

@ -0,0 +1,19 @@
package dw_sdk
type Config struct {
Public PublicConfig `json:"public" yaml:"public"`
Proxy *ProxyConfig `json:"proxy,omitempty" yaml:"proxy,omitempty"`
}
type PublicConfig struct {
Key string `json:"key" yaml:"key"`
Secret string `json:"secret" yaml:"secret"`
Env string `json:"env" yaml:"env"`
}
type ProxyConfig struct {
Host string `json:"host" yaml:"host"`
Port string `json:"port" yaml:"port"`
User string `json:"user" yaml:"user"`
Pass string `json:"pass" yaml:"pass"`
}

11
errors.go Normal file
View File

@ -0,0 +1,11 @@
package dw_sdk
import "github.com/pkg/errors"
var (
Err_Params_None = errors.New("请求参数为空")
Err_Params_Invalid = errors.New("请求参数错误")
Err_Request_Err = errors.New("请求错误")
Err_JSON_UNMarshal = errors.New("JSON解析错误")
)

31
go.mod Normal file
View File

@ -0,0 +1,31 @@
module gitea.timerzz.com/kedaya_haitao/dw-sdk
go 1.22.5
require (
github.com/bytedance/sonic v1.11.9
github.com/gofiber/fiber/v3 v3.0.0-beta.3
github.com/google/go-querystring v1.1.0
github.com/pkg/errors v0.9.1
)
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gofiber/utils/v2 v2.0.0-beta.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
)

71
go.sum Normal file
View File

@ -0,0 +1,71 @@
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v3 v3.0.0-beta.3 h1:7Q2I+HsIqnIEEDB+9oe7Gadpakh6ZLhXpTYz/L20vrg=
github.com/gofiber/fiber/v3 v3.0.0-beta.3/go.mod h1:kcMur0Dxqk91R7p4vxEpJfDWZ9u5IfvrtQc8Bvv/JmY=
github.com/gofiber/utils/v2 v2.0.0-beta.4 h1:1gjbVFFwVwUb9arPcqiB6iEjHBwo7cHsyS41NeIW3co=
github.com/gofiber/utils/v2 v2.0.0-beta.4/go.mod h1:sdRsPU1FXX6YiDGGxd+q2aPJRMzpsxdzCXo9dz+xtOY=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

53
params.go Normal file
View File

@ -0,0 +1,53 @@
package dw_sdk
import (
"crypto/md5"
"fmt"
"github.com/bytedance/sonic"
"github.com/google/go-querystring/query"
"net/url"
"strings"
)
type PublicParams struct {
AppKey string `json:"app_key" url:"app_key"`
Sign string `json:"sign" url:"-"`
AccessToken string `json:"access_token,omitempty" url:"access_token,omitempty"`
Timestamp int64 `json:"timestamp" url:"timestamp"`
}
func (p *PublicParams) SetSign(sign string) {
p.Sign = sign
}
type SetSigner interface {
SetSign(string)
}
func Sign(signer SetSigner, secret string) {
v, _ := query.Values(signer)
stringSignTemp := v.Encode() + secret
data := []byte(stringSignTemp)
has := md5.Sum(data)
signer.SetSign(fmt.Sprintf("%X", has))
}
// 对sign中的数组处理
type ListParams[T any] []T
func (s ListParams[T]) EncodeValues(key string, v *url.Values) error {
var list = make([]string, 0, len(s))
for _, item := range s {
js, _ := sonic.MarshalString(&item)
list = append(list, js)
}
v.Set(key, strings.Join(list, ","))
return nil
}
type PublicResponse[T any] struct {
Msg string `json:"msg"`
Code int `json:"code"`
Data T `json:"data"`
}

54
params_test.go Normal file
View File

@ -0,0 +1,54 @@
package dw_sdk
import (
"testing"
)
// 用于测试的请求
type TestRequest struct {
AppointNo string `json:"appoint_no" url:"appoint_no"`
SkuList ListParams[TestSku] `json:"sku_list" url:"sku_list"`
PublicParams
}
type TestSku struct {
SpuId int `json:"spu_id"`
SkuId int `json:"sku_id"`
BarCode string `json:"bar_code"`
ArticleNumber string `json:"article_number"`
AppointNum int `json:"appoint_num"`
BrandId int `json:"brand_id"`
CategoryId int `json:"category_id"`
}
func TestParams(t *testing.T) {
var p = TestRequest{
PublicParams: PublicParams{
AppKey: "4d1715e032c44b709ef4954ef13e0950",
Timestamp: 1603353500369,
},
AppointNo: "A14343543654",
SkuList: ListParams[TestSku]{
{
SpuId: 81293,
SkuId: 487752589,
BarCode: "487752589",
ArticleNumber: "wucaishi",
AppointNum: 10,
BrandId: 10444,
CategoryId: 46,
},
{
SpuId: 81293,
SkuId: 487752589,
BarCode: "487752589",
ArticleNumber: "wucaishi",
AppointNum: 10,
BrandId: 10444,
CategoryId: 46,
},
},
}
Sign(&p, "")
}

13
url.go Normal file
View File

@ -0,0 +1,13 @@
package dw_sdk
const (
DEV_URL = "https://openapi-sandbox.dewu.com"
Online_URL = "https://openapi.dewu.com"
)
func GetUrl(env string) string {
if env == "online" {
return Online_URL
}
return DEV_URL
}