From 6ce9dfb03cfd6c54a8c347d0d58c25b06c323c7e Mon Sep 17 00:00:00 2001 From: liuhao Date: Mon, 2 Dec 2024 20:11:15 +0800 Subject: [PATCH] add orders API --- tiktok/api/order/config.go | 171 +++++++++++++++++++++++++++++++++++++ tiktok/api/order/order.go | 68 +++++++++++++++ tiktok/tiktok.go | 7 ++ 3 files changed, 246 insertions(+) create mode 100644 tiktok/api/order/config.go create mode 100644 tiktok/api/order/order.go diff --git a/tiktok/api/order/config.go b/tiktok/api/order/config.go new file mode 100644 index 0000000..5ea2af9 --- /dev/null +++ b/tiktok/api/order/config.go @@ -0,0 +1,171 @@ +package order + +type HandlingDuration struct { + Days string `json:"days"` + Type string `json:"type"` +} + +type CombinedListingSkus struct { + ProductID string `json:"product_id"` + SellerSku string `json:"seller_sku"` + SkuCount int `json:"sku_count"` + SkuID string `json:"sku_id"` +} + +type ItemTax struct { + TaxAmount string `json:"tax_amount"` + TaxRate string `json:"tax_rate"` + TaxType string `json:"tax_type"` +} + +type LineItems struct { + BuyerServiceFee string `json:"buyer_service_fee"` + CancelReason string `json:"cancel_reason"` + CancelUser string `json:"cancel_user"` + CombinedListingSkus []CombinedListingSkus `json:"combined_listing_skus"` + Currency string `json:"currency"` + DisplayStatus string `json:"display_status"` + HandlingDurationDays string `json:"handling_duration_days"` + ID string `json:"id"` + IsGift bool `json:"is_gift"` + ItemTax []ItemTax `json:"item_tax"` + OriginalPrice string `json:"original_price"` + PackageID string `json:"package_id"` + PackageStatus string `json:"package_status"` + PlatformDiscount string `json:"platform_discount"` + ProductID string `json:"product_id"` + ProductName string `json:"product_name"` + RetailDeliveryFee string `json:"retail_delivery_fee"` + RtsTime int `json:"rts_time"` + SalePrice string `json:"sale_price"` + SellerDiscount string `json:"seller_discount"` + SellerSku string `json:"seller_sku"` + ShippingProviderID string `json:"shipping_provider_id"` + ShippingProviderName string `json:"shipping_provider_name"` + SkuID string `json:"sku_id"` + SkuImage string `json:"sku_image"` + SkuName string `json:"sku_name"` + SkuType string `json:"sku_type"` + SmallOrderFee string `json:"small_order_fee"` + TrackingNumber string `json:"tracking_number"` +} + +type Packages struct { + Id string `json:"id"` +} + +type Payment struct { + BuyerServiceFee string `json:"buyer_service_fee"` + Currency string `json:"currency"` + HandlingFee string `json:"handling_fee"` + ItemInsuranceFee string `json:"item_insurance_fee"` + OriginalShippingFee string `json:"original_shipping_fee"` + OriginalTotalProductPrice string `json:"original_total_product_price"` + PlatformDiscount string `json:"platform_discount"` + ProductTax string `json:"product_tax"` + RetailDeliveryFee string `json:"retail_delivery_fee"` + SellerDiscount string `json:"seller_discount"` + ShippingFee string `json:"shipping_fee"` + ShippingFeePlatformDiscount string `json:"shipping_fee_platform_discount"` + ShippingFeeSellerDiscount string `json:"shipping_fee_seller_discount"` + ShippingFeeTax string `json:"shipping_fee_tax"` + ShippingInsuranceFee string `json:"shipping_insurance_fee"` + SmallOrderFee string `json:"small_order_fee"` + SubTotal string `json:"sub_total"` + Tax string `json:"tax"` + TotalAmount string `json:"total_amount"` +} + +type DeliveryPreferences struct { + DropOffLocation string `json:"drop_off_location"` +} + +type DistrictInfo struct { + AddressLevel string `json:"address_level"` + AddressLevelName string `json:"address_level_name"` + AddressName string `json:"address_name"` +} + +type RecipientAddress struct { + AddressDetail string `json:"address_detail"` + AddressLine1 string `json:"address_line1"` + AddressLine2 string `json:"address_line2"` + AddressLine3 string `json:"address_line3"` + AddressLine4 string `json:"address_line4"` + DistrictInfo []DistrictInfo `json:"district_info"` + FirstName string `json:"first_name"` + FullAddress string `json:"full_address"` + LastName string `json:"last_name"` + Name string `json:"name"` + PhoneNumber string `json:"phone_number"` + PostalCode string `json:"postal_code"` + RegionCode string `json:"region_code"` +} + +type Order struct { + BuyerEmail string `json:"buyer_email"` + BuyerMessage string `json:"buyer_message"` + CancelOrderSLATime int `json:"cancel_order_sla_time"` + CancelReason string `json:"cancel_reason"` + CancelTime int `json:"cancel_time"` + CancellationInitiator string `json:"cancellation_initiator"` + CollectionDueTime int `json:"collection_due_time"` + CollectionTime int `json:"collection_time"` + CommercePlatform string `json:"commerce_platform"` + Cpf string `json:"cpf"` + CreateTime int `json:"create_time"` + DeliveryDueTime int `json:"delivery_due_time"` + DeliveryOptionID string `json:"delivery_option_id"` + DeliveryOptionName string `json:"delivery_option_name"` + DeliveryOptionRequiredDeliveryTime int `json:"delivery_option_required_delivery_time"` + DeliverySLATime int `json:"delivery_sla_time"` + DeliveryTime int `json:"delivery_time"` + DeliveryType string `json:"delivery_type"` + FastDispatchSLATime int `json:"fast_dispatch_sla_time"` + FulfillmentType string `json:"fulfillment_type"` + HandlingDuration HandlingDuration `json:"handling_duration"` + HasUpdatedRecipientAddress bool `json:"has_updated_recipient_address"` + ID string `json:"id"` + IsBuyerRequestCancel bool `json:"is_buyer_request_cancel"` + IsCod bool `json:"is_cod"` + IsOnHoldOrder bool `json:"is_on_hold_order"` + IsReplacementOrder bool `json:"is_replacement_order"` + IsSampleOrder bool `json:"is_sample_order"` + LineItems []LineItems `json:"line_items"` + NeedUploadInvoice string `json:"need_upload_invoice"` + OrderType string `json:"order_type"` + Packages []Packages `json:"packages"` + PaidTime int `json:"paid_time"` + Payment Payment `json:"payment"` + PaymentMethodName string `json:"payment_method_name"` + PickUpCutOffTime int `json:"pick_up_cut_off_time"` + RecipientAddress RecipientAddress `json:"recipient_address"` + ReleaseDate int `json:"release_date"` + ReplacedOrderID string `json:"replaced_order_id"` + RequestCancelTime int `json:"request_cancel_time"` + RtsSLATime int `json:"rts_sla_time"` + RtsTime int `json:"rts_time"` + SellerNote string `json:"seller_note"` + ShippingDueTime int `json:"shipping_due_time"` + ShippingProvider string `json:"shipping_provider"` + ShippingProviderID string `json:"shipping_provider_id"` + ShippingType string `json:"shipping_type"` + SplitOrCombineTag string `json:"split_or_combine_tag"` + Status string `json:"status"` + TrackingNumber string `json:"tracking_number"` + TtsSLATime int `json:"tts_sla_time"` + UpdateTime int `json:"update_time"` + UserID string `json:"user_id"` + WarehouseID string `json:"warehouse_id"` +} + +type OrdersRsp struct { + Orders []Order `json:"orders"` +} + +type OrderResultRsp struct { + Code int `json:"code"` //逻辑状态码 + Message string `json:"message"` //错误信息 + Data OrdersRsp `json:"data"` //数据 + HttpCode int `json:"httpCode"` //请求tiktok的HTTP状态码 +} diff --git a/tiktok/api/order/order.go b/tiktok/api/order/order.go new file mode 100644 index 0000000..0dfc24c --- /dev/null +++ b/tiktok/api/order/order.go @@ -0,0 +1,68 @@ +package order + +import ( + "context" + "encoding/json" + "fmt" + "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/common/common" + "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/common/config" +) + +type TiktokOrder struct { + config *config.Config + *common.TiktokShopCommon +} + +var newServer *TiktokOrder + +type OrderApiClientInterface interface { + GetOrders(ctx context.Context, token string, query map[string]string) OrderResultRsp + GetOrdersConfig(token string) common.GetApiConfig +} + +// GetOrderService 获取实例 +func GetOrderService(config *config.Config) OrderApiClientInterface { + if newServer == nil { + newServer = &TiktokOrder{ + config: config, + TiktokShopCommon: common.GetNewService(config), + } + } + return newServer +} + +// GetOrders 获取订单详情(可批量) +func (to *TiktokOrder) GetOrders(ctx context.Context, token string, query map[string]string) OrderResultRsp { + //请求接口 + r := to.SendTiktokApi(ctx, to.GetOrdersConfig(token), query, nil, nil) + result := OrderResultRsp{ + Code: r.Code, + Message: r.Message, + HttpCode: r.HttpCode, + } + if !to.IsSuccess(r) { + return result + } + + //解析数据 + err := json.Unmarshal(r.Data, &result) + if err != nil { + r.Code = common.ErrCode + r.Message = "GetOrders response error " + err.Error() + return result + } + + return result +} + +func (to *TiktokOrder) GetOrdersConfig(token string) common.GetApiConfig { //请求方式 + api := fmt.Sprintf("/order/%s/orders", to.config.Version) //请求API PATH + + return common.GetApiConfig{ + ContentType: "application/json", //请求头content-type 类型 + Method: "get", //请求方法类型 + Api: api, //请求API PATH地址不带域名 + FullApi: to.config.TkApiDomain + api, //请求的API 完整地址,带域名 + Token: token, + } +} diff --git a/tiktok/tiktok.go b/tiktok/tiktok.go index d707a3e..02f2868 100644 --- a/tiktok/tiktok.go +++ b/tiktok/tiktok.go @@ -4,6 +4,7 @@ package tiktokShop import ( apiAuth "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/api/auth" "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/api/logistics" + "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/api/order" "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/api/product" "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/api/widget" "github.com/huangchunlong818/go-tiktok-shop-api/tiktok/auth" @@ -15,6 +16,7 @@ type AuthApiClient apiAuth.AuthApiClientInterface //授权API服务 type WidgetApiClient widget.WidgetApiClientInterface //小部件API服务接口 type ProductApiClient product.ProductApiClientInterface //小部件API服务接口 type LogisticsApiClient logistics.LogisticsApiClientInterface //物流信息API服务接口 +type OrderApiClient order.OrderApiClientInterface //订单API服务接口 type TiktokShop struct { client *config.TiktokShopClient @@ -60,3 +62,8 @@ func (tk *TiktokShop) GetProductApiClient() ProductApiClient { func (tk *TiktokShop) GetLogisticsApiClient() LogisticsApiClient { return logistics.GetNewService(tk.client.GetConfig()) } + +// 获取订单资源相关API实例 有sign签名请求 +func (tk *TiktokShop) GetOrderApiClient() OrderApiClient { + return order.GetOrderService(tk.client.GetConfig()) +}