tracking51

package module
v0.0.0-...-48dfd08 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2022 License: BSD-3-Clause Imports: 23 Imported by: 0

README

51Tracking SDK

文档地址

https://www.51tracking.com/v3/api-index?language=Golang#api-version

数据更新频率

正常
  • 添加 30 天内未签收的属于正常更新频次也就是下限是 4 小时上限是 6 小时;
  • 30~45 天仍未签收就会降低更新频率至下限 6 小时上限 8 小时;
  • 45~60 天仍未签收更新频率变为下限 12 小时上限 14 小时;
  • 60~80 天仍未签收更新频率变为下限 24 小时上限 26 小时;
  • 超过 80 天仍未签收将停止更新。
其他
  • 快递更新间隔正常时间是 4~6 个小时;
  • 邮政更新间隔是 6~8 个小时;
  • sfb2c 更新间隔为 8 小时。

手动更新条件

  1. 查询不到超过 15 天,已经停止更新了的单号;
  2. 已经签收,并且已经停止更新了的单号;
  3. 添加时间超过 30 天,已经停止更新的单号;
  4. 其他条件暂时无法通过该接口更新。

安装

go get github.com/hiscaler/51tracking-go

使用

client := NewTracking51(config.Config{
    Debug:        true,
    Sandbox:      true,
    AppKey:       "xxx",
    IntervalTime: 1500,
})

配置说明

type Config struct {
	Debug        bool   // 是否为调试模式(调试模式下会输出 HTTP 请求和返回数据)
	Sandbox      bool   // 是否为沙箱测试环境
	Version      string // API 版本(当前固定为 V3)
	AppKey       string // App Key
	IntervalTime int64  // 当前请求与上次请求间隔的时间(单位为毫秒),默认为零,表示没有间隔,大于 0 表示实际间隔的毫秒数
}

服务

Account
  • 帐号情况
client.Services.Account.Profile()
Courier
  • 获取物流商列表
client.Services.Courier.List()
  • 修改包裹物流商
client.Services.Courier.Change("trackingNumber", "oldCourierCode", "newCourierCode")
Tracking
  • 添加物流单号
client.Services.Tracking.Create()
  • 修改单号信息
client.Services.Tracking.Update()
  • 获取查询结果
params := TracksQueryParams{}
client.Services.Tracking.Query(params)
  • 删除查询单号
client.Services.Tracking.Delete([]DeleteTrackRequest{})
  • 停止单号更新
client.Services.Tracking.StopUpdate(StopUpdateRequests{})
  • 手动更新
client.Services.Tracking.Refresh([]RefreshRequest{})
  • 统计包裹状态
client.Services.Tracking.StatusStatistic(StatusStatisticRequest{})
  • 时效
client.Services.Tracking.TransitTime(TransitTimeRequest{})
  • 检测偏远地区
client.Services.Tracking.RemoteDetection(RemoteDetectionRequest{})

Webhook

针对 51Tracking 的数据推送,提供了 WebhookRequest 结构体,您可以使用他来接受推送过来的数据,并判断 Code 是否为 200 且 Data.Valid() 是否有效来进行下一步的业务逻辑处理。

Data.Valid(you51TrackingAccountEmail) 用来判断推送的数据是否来自 51Tracking,根据需要,您可以跳过此步。

var wr WebRequest
if json.Unmarshal(resp.Body, &wr) != nil {
	if wr.Code == 200 && wr.Data.Valid(you51TrackingAccountEmail) {
	    // you code	
    }
}

Documentation

Index

Constants

View Source
const (
	Success                             = 200 // 无错误
	PaymentRequiredError                = 203 // API 服务只提供给付费账户,请付费购买单号以解锁 API 服务
	NoContent                           = 204 // 请求成功,但未获取到数据,可能是该单号、所查询目标数据不存在
	BadRequestError                     = 400 // 请求类型错误
	UnauthorizedError                   = 401 // 授权失败或没有权限,请检查并确保你 API Key 正确无误
	NotFoundError                       = 404 // 请求的资源不存在
	TimeOutError                        = 408 // 请求超时
	RequestParametersTooLongError       = 411 // 请求参数长度超过限制
	RequestParametersFormatError        = 412 // 请求参数格式不合要求
	RequestParametersExceededLimitError = 413 // 请求参数数量超过限制
	LostRequestParametersOrParseError   = 417 // 缺少请求参数或者请求参数无法解析
	ParametersInvalidError              = 421 // 部分必填参数为空
	CourierCodeInvalidError             = 422 // 物流商简码无法识别或者不支持该物流商
	TrackingNumberIsExistsError         = 423 // 跟踪单号已存在,无需再次创建
	TrackingNumberIsNotExistsError      = 424 // 跟踪单号不存在
	TooManyRequestsError                = 429 // API 请求频率次数限制,请稍后再试
	InternalError                       = 511 // 系统错误
)

https://www.51tracking.com/v3/api-index?language=Golang#%E5%93%8D%E5%BA%94

View Source
const (
	StatusPending      = "pending"      // 查询中
	StatusNotFound     = "notfound"     // 查询不到
	StatusTransit      = "transit"      // 运输中
	StatusPickup       = "pickup"       // 到达待取
	StatusDelivered    = "delivered"    // 成功签收
	StatusExpired      = "notfound"     // 运输过久
	StatusUndelivered  = "undelivered"  // 投递失败
	StatusException    = "exception"    // 可能异常
	StatusInfoReceived = "inforeceived" // 待上网
)
View Source
const (
	ChineseLanguage = "cn"
	EnglishLanguage = "en"
)
View Source
const (
	Version = "0.0.1"
)

Variables

This section is empty.

Functions

func ErrorWrap

func ErrorWrap(code int, message string) error

ErrorWrap 错误包装

Types

type AccountProfile

type AccountProfile struct {
	Email       string `json:"email"`        // 登录邮箱
	RegTime     int    `json:"regtime"`      // 账户注册时间
	Phone       string `json:"phone"`        // 账户绑定的手机号码
	SMS         int    `json:"sms"`          // 短信剩余条数
	TrackNumber int    `json:"track_number"` // 账户剩余的单号额度
}

type Courier

type Courier struct {
	Name        string      `json:"courier_name"`
	Code        string      `json:"courier_code"`
	Phone       string      `json:"courier_phone"`
	CountryCode null.String `json:"country_code"`
	Type        string      `json:"courier_type"`
	URL         null.String `json:"courier_url"`
}

Courier https://www.51tracking.com/v3/api-index?language=Golang#%E7%89%A9%E6%B5%81%E5%95%86%E5%88%97%E8%A1%A8

type CreateResult

type CreateResult struct {
	TrackingNumber string `json:"tracking_number"` // 包裹物流单号
	CourierCode    string `json:"courier_code"`    // 物流商对应的唯一简码
	OrderNumber    string `json:"order_number"`    // 包裹的订单号,由商家/平台所产生的订单编号
}

type CreateTrackRequest

type CreateTrackRequest struct {
	TrackingNumber          string `json:"tracking_number"`                     // 包裹物流单号
	CourierCode             string `json:"courier_code"`                        // 物流商对应的唯一简码
	OrderNumber             string `json:"order_number,omitempty"`              // 包裹的订单号,由商家/平台所产生的订单编号
	Title                   string `json:"title,omitempty"`                     // 包裹名称
	DestinationCode         string `json:"destination_code,omitempty"`          // 目的国的二字简码
	LogisticsChannel        string `json:"logistics_channel,omitempty"`         // 自定义字段,用于填写物流渠道(比如某货代商)
	Note                    string `json:"note,omitempty"`                      // 备注
	CustomerName            string `json:"customer_name,omitempty"`             // 客户姓名
	CustomerEmail           string `json:"customer_email,omitempty"`            // 客户邮箱
	CustomerPhone           string `json:"customer_phone,omitempty"`            // 顾客接收短信的手机号码。手机号码的格式应该为:“+区号手机号码”(例子:+8612345678910)
	ShippingDate            string `json:"shipping_date,omitempty"`             // 包裹发货时间(例子:2020-09-17 16:51)
	TrackingShippingDate    string `json:"tracking_shipping_date,omitempty"`    // 包裹的发货时间,其格式为:YYYYMMDD,有部分的物流商(如 deutsch-post)需要这个参数(例子:20200102)
	TrackingPostalCode      string `json:"tracking_postal_code,omitempty"`      // 收件人所在地邮编,仅有部分的物流商(如 postnl-3s)需要这个参数
	TrackingDestinationCode string `json:"tracking_destination_code,omitempty"` // 目的国对应的二字简码,部分物流商(如postnl-3s)需要这个参数
	TrackingCourierAccount  string `json:"tracking_courier_account,omitempty"`  // 物流商的官方账号,仅有部分的物流商(如 dynamic-logistics)需要这个参数
}

func (CreateTrackRequest) Validate

func (m CreateTrackRequest) Validate() error

type DeleteTrackRequest

type DeleteTrackRequest trackingNumberCourierCode

type DeleteTrackRequests

type DeleteTrackRequests []DeleteTrackRequest

func (DeleteTrackRequests) Validate

func (m DeleteTrackRequests) Validate() error

type DeleteTrackResult

type DeleteTrackResult trackingNumberCourierCode

type Information

type Information struct {
	CourierCode            string             `json:"courier_code"`             // 物流商对应的唯一简码
	CourierPhone           string             `json:"courier_phone"`            // 物流商官网上的电话
	Weblink                string             `json:"weblink"`                  // 物流商的官网的链接
	ReferenceNumber        string             `json:"reference_number"`         // 包裹对应的另一个单号,作用与当前单号相同(仅有少部分物流商提供)
	ReceivedDate           string             `json:"received_date"`            // 物流商接收包裹的时间(也称为上	网时间)
	DispatchedDate         string             `json:"dispatched_date"`          // 包裹封发时间,封发指将多个小包裹打包成一个货物(方便运输)
	DepartedAirportDate    string             `json:"departed_airport_date"`    // 包裹离开此出发机场的时间
	ArrivedAbroadDate      string             `json:"arrived_abroad_date"`      // 包裹达到目的国的时间
	CustomsReceivedDate    string             `json:"customs_received_date"`    // 包裹移交给海关的时间
	ArrivedDestinationDate string             `json:"arrived_destination_date"` // 包裹达到目的国、目的城市的时间
	TrackInfo              []TrackInformation `json:"trackinfo"`                // 详细物流信息
}

type NormalResponse

type NormalResponse struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type RefreshResultError

type RefreshResultError struct {
	ErrorCode    int    `json:"errorCode"`
	ErrorMessage string `json:"errorMessage"`
	// contains filtered or unexported fields
}

type RefreshResultSuccess

type RefreshResultSuccess struct {
	// contains filtered or unexported fields
}

type RefreshTrackRequest

type RefreshTrackRequest trackingNumberCourierCode

func (RefreshTrackRequest) Validate

func (m RefreshTrackRequest) Validate() error

type RefreshTrackRequests

type RefreshTrackRequests []RefreshTrackRequest

func (RefreshTrackRequests) Validate

func (m RefreshTrackRequests) Validate() error

type RemoteDetectionRequest

type RemoteDetectionRequest struct {
	PostalCode  string `json:"postal_code"`            // 该偏远地区所对应的邮编
	Country     string `json:"country,omitempty"`      // 城市名称或者国家二字简码
	CourierCode string `json:"courier_code,omitempty"` // 物流商简码
}

func (RemoteDetectionRequest) Validate

func (m RemoteDetectionRequest) Validate() error

type RemoteDetectionResult

type RemoteDetectionResult struct {
	PostalCode        string   `json:"postal_code"`         // 该偏远地区所对应的邮编
	CountryCode       string   `json:"country"`             // 该偏远地区所对应的国家名称
	RemoteCourierCode []string `json:"remote_courier_code"` // 支持该偏远地区的物流商。51tracking 目前仅支持DHL, UPS, Fedex, TNT四家物流商的偏远地区查询
}

type StatusStatistic

type StatusStatistic struct {
	Pending      int `json:"pending"`      // 查询中:新增包裹正在查询中,请等待
	NotFound     int `json:"notfound"`     // 查询不到:包裹信息目前查询不到
	Transit      int `json:"transit"`      // 运输途中:物流商已揽件,包裹正被发往目的地
	Pickup       int `json:"pickup"`       // 到达待取:包裹正在派送中,或到达当地收发点
	Delivered    int `json:"delivered"`    // 成功签收:包裹已被成功投递
	Expired      int `json:"expired"`      // 运输过久:包裹在很长时间内都未投递成功。快递包裹超过30天、邮政包裹超过60天未投递成功,该查询会被识别为此状态
	Undelivered  int `json:"undelivered"`  // 投递失败:快递员投递失败(通常会留有通知并再次尝试投递)
	Exception    int `json:"exception"`    // 可能异常:包裹退回、包裹丢失、清关失败等异常情况
	InfoReceived int `json:"infoReceived"` // 待上网:包裹正在等待被揽件
}

type StatusStatisticRequest

type StatusStatisticRequest struct {
	CourierCode     string `url:"courier_code,omitempty"`      // 物流商对应的唯一简码
	CreatedDateMin  int64  `url:"created_date_min,omitempty"`  // 创建查询的起始时间(时间戳格式)
	CreatedDateMax  int64  `url:"created_date_max,omitempty"`  // 创建查询的结束时间(时间戳格式)
	ShippingDateMin int64  `url:"shipping_date_min,omitempty"` // 发货的起始时间(时间戳格式)
	ShippingDateMax int64  `url:"shipping_date_max,omitempty"` // 发货的结束时间(时间戳格式)
}

func (StatusStatisticRequest) Validate

func (m StatusStatisticRequest) Validate() error

type StopUpdateResultError

type StopUpdateResultError trackingNumberCourierCode

type StopUpdateResultSuccess

type StopUpdateResultSuccess trackingNumberCourierCode

type StopUpdateTrackRequest

type StopUpdateTrackRequest trackingNumberCourierCode

type StopUpdateTrackRequests

type StopUpdateTrackRequests []StopUpdateTrackRequest

func (StopUpdateTrackRequests) Validate

func (m StopUpdateTrackRequests) Validate() error

type Track

type Track struct {
	TrackingNumber         string      `json:"tracking_number"`          // 包裹物流单号
	CourierCode            string      `json:"courier_code"`             // 物流商对应的唯一简码
	LogisticsChannel       string      `json:"logistics_channel"`        // 自定义字段,用于填写物流渠道(比如某货代商)
	Destination            string      `json:"destination"`              // 目的国的二字简码
	TrackUpdate            bool        `json:"track_update"`             // 自动更新查询功能的状态,“true”代表系统会自动更新查询结果,“false”则反之
	Consignee              string      `json:"consignee"`                // 签收人
	Updating               bool        `json:"updating"`                 // “true”表示该单号会被继续更新,“false”表示该单号已停止更新
	CreatedAt              string      `json:"created_at"`               // 创建查询的时间
	UpdateDate             string      `json:"update_date"`              // 系统最后更新查询的时间
	OrderCreateTime        string      `json:"order_create_time"`        // 包裹发货时间
	CustomerEmail          string      `json:"customer_email"`           // 客户邮箱
	CustomerPhone          string      `json:"customer_phone"`           // 顾客接收短信的手机号码
	Title                  string      `json:"title"`                    // 包裹名称
	OrderNumber            string      `json:"order_number"`             // 包裹的订单号,由商家/平台所产生的订单编号
	Note                   string      `json:"note"`                     // 备注,可自定义
	CustomerName           string      `json:"customer_name"`            // 客户姓名
	Archived               bool        `json:"archived"`                 // “true”表示该单号已被归档,“false”表示该单号处于未归档状态
	Original               string      `json:"original"`                 // 发件国的名称
	DestinationCountry     string      `json:"destination_country"`      // 目的国的名称
	TransitTime            int         `json:"transit_time"`             // 包裹的从被揽收至被送达的时长(天)
	StayTime               int         `json:"stay_time"`                // 物流信息未更新的时长(单位:天),由当前时间减去物流信息最近更新时间得到
	DestinationTrackNumber string      `json:"destination_track_number"` // 该包裹在目的国的物流单号
	ExchangeNumber         string      `json:"exchangeNumber"`           // 该包裹在中转站的物流商单号
	ServiceCode            string      `json:"service_code"`             // 快递服务类型,比如次日达(部分物流商返回)
	StatusInfo             string      `json:"status_info"`              // 最新的一条物流信息
	Weight                 string      `json:"weight"`                   // 该货物的重量(多个包裹会被打包成一个“货物”)
	DestinationInfo        Information `json:"destination_info"`         // 目的国的物流信息
	LatestEvent            string      `json:"latest_event"`             // 最新物流信息的梗概,包括以下信息:状态、地址、时间
	LatestCheckpointTime   string      `json:"lastest_checkpoint_time"`  // 最新物流信息的更新时间
	OriginInfo             Information `json:"origin_info"`              // 发件国的物流信息
}

type TrackInformation

type TrackInformation struct {
	CheckpointDate              string `json:"checkpoint_date"`               // 本条物流信息的更新时间,由物流商提供(包裹被扫描时,物流信息会被更新)
	TrackingDetail              string `json:"tracking_detail"`               // 具体的物流情况
	Location                    string `json:"location"`                      // 物流信息更新的地址(该包裹被扫描时,所在的地址)
	CheckpointDeliveryStatus    string `json:"checkpoint_delivery_status"`    // 根据具体物流情况所识别出来的物流状态
	CheckpointDeliverySubStatus string `json:"checkpoint_delivery_substatus"` // 物流状态的子状态(物流状态)
}

TrackInformation 详细物流信息

type Tracking51

type Tracking51 struct {
	Services services // API Services
	// contains filtered or unexported fields
}

func NewTracking51

func NewTracking51(config config.Config) *Tracking51

func (*Tracking51) SetDebug

func (t *Tracking51) SetDebug(v bool) *Tracking51

SetDebug 设置是否开启调试模式

type TracksQueryParams

type TracksQueryParams struct {
	TrackingNumbers string `url:"tracking_numbers,omitempty"`  // 查询单号,每次不得超过40个,单号间以逗号分隔
	OrderNumbers    string `url:"order_numbers,omitempty"`     // 订单号,每次查询不得超过40个,订单号间以逗号分隔
	DeliveryStatus  string `url:"delivery_status,omitempty"`   // 发货状态
	ArchivedStatus  string `url:"archived_status,omitempty"`   // 指定该单号是否被归档。如果参数为字符串“true”,该单号将处于“归档”状态;如果参数为“false”,该单号处于“未归档”状态
	ItemsAmount     int    `url:"items_amount,omitempty"`      // 每页展示的单号个数
	PagesAmount     int    `url:"pages_amount,omitempty"`      // 返回结果的页数
	CreatedDateMin  int64  `url:"created_date_min,omitempty"`  // 创建查询的起始时间(时间戳格式)
	CreatedDateMax  int64  `url:"created_date_max,omitempty"`  // 创建查询的结束时间(时间戳格式)
	ShippingDateMin int64  `url:"shipping_date_min,omitempty"` // 发货的起始时间(时间戳格式)
	ShippingDateMax int64  `url:"shipping_date_max,omitempty"` // 发货的结束时间(时间戳格式)
	UpdatedDateMin  int64  `url:"updated_date_min,omitempty"`  // 查询更新的起始时间(时间戳格式)
	UpdatedDateMax  int64  `url:"updated_date_max,omitempty"`  // 查询更新的结束时间(时间戳格式)
	Lang            string `url:"lang,omitempty"`              // 查询结果的语言(例子:cn, en),若未指定该参数,结果会以英文或中文呈现。 注意:只有物流商支持多语言查询结果时,该指定才会生效
}

func (TracksQueryParams) Validate

func (m TracksQueryParams) Validate() error

type TransitTime

type TransitTime struct {
	CourierCode         string  `json:"courier_code"`          // 物流商对应的唯一简码
	OriginalCode        string  `json:"original_code"`         // 发件国二字简码
	DestinationCode     string  `json:"destination_code"`      // 目的国二字简码
	Total               int     `json:"total"`                 // 未签收的总单号数量
	Delivered           int     `json:"delivered"`             // 已签收的总单号数量
	Range1To7           float64 `json:"range_1_7"`             // 送达时间为0~7天的单号的占比
	Range8To15          float64 `json:"range_8_15"`            // 送达时间为7~15天的单号的占比
	Range16To30         float64 `json:"range_16_30"`           // 送达时间为16~30天的单号的占比
	Range31To60         float64 `json:"range_31_60"`           // 送达时间为31~60天的单号的占比
	Range60Up           float64 `json:"range_60_up"`           // 送达时间大于60天的单号的占比
	AverageDeliveryTime float64 `json:"average_delivery_time"` // 平均送达时间(单位:天)
}

type TransitTimeRequest

type TransitTimeRequest struct {
	CourierCode     string `json:"courier_code"`      // 物流商对应的唯一简码
	OriginalCode    string `json:"original_code"`     // 发件国二字简码
	DestinationCode string `json:"destination_code "` // 目的国的二字简码
}

type TransitTimeRequests

type TransitTimeRequests []TransitTimeRequest

func (TransitTimeRequests) Validate

func (m TransitTimeRequests) Validate() error

type UpdateResult

type UpdateResult CreateResult

type UpdateTrackRequest

type UpdateTrackRequest = CreateTrackRequest

type Webhook

type Webhook struct {
	Track
	Verify webhookVerify `json:"verify"`
}

func (Webhook) Valid

func (wr Webhook) Valid(email string) bool

Valid 传递 51tracking 用户邮箱验证是否为有效的 51Tracking 推送

type WebhookRequest

type WebhookRequest struct {
	Code    int     `json:"code"`
	Message string  `json:"message"`
	Data    Webhook `json:"data"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL