1
This commit is contained in:
83
platformapi/ali_logistics_query/api.go
Normal file
83
platformapi/ali_logistics_query/api.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package ali_logistics_query
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
BastUrl = "http://express3.market.alicloudapi.com" // 基础访问路由
|
||||
GetOrderDetailApi = "express3" // 获取物流订单详情
|
||||
)
|
||||
|
||||
type API struct {
|
||||
appKey string
|
||||
appSecret string
|
||||
appCode string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(appKey, appSecret, appCode string, config ...*platformapi.APIConfig) (a *API) {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
|
||||
a = &API{
|
||||
appKey: appKey,
|
||||
appSecret: appSecret,
|
||||
appCode: appCode,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *API) GetAppCode() string {
|
||||
return a.appCode
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
// 序列化
|
||||
data, err := json.Marshal(bizParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
sendUrl := func() *http.Request {
|
||||
var request *http.Request
|
||||
if http.MethodPost == method {
|
||||
request, _ = http.NewRequest(http.MethodPost, utils.GenerateGetURL(baseUrl, actionApi, nil), strings.NewReader(string(data)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Authorization", "APPCODE "+a.appCode)
|
||||
return request
|
||||
}
|
||||
|
||||
// 数据解析
|
||||
dataMarshal := func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if utils.MustInterface2Int64(jsonResult1["code"]) != 200 {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"])))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
return errLevel, err
|
||||
}
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
||||
return retVal, err
|
||||
}
|
||||
79
platformapi/ali_logistics_query/logistics_model.go
Normal file
79
platformapi/ali_logistics_query/logistics_model.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package ali_logistics_query
|
||||
|
||||
// LogisticsStatusReceive 物流状态标签说明
|
||||
// https://market.aliyun.com/products/57126001/cmapi00054243.html?spm=5176.2020520132.101.2.3c557218Al5EBA#sku=yuncode4824300001
|
||||
const (
|
||||
// 揽件
|
||||
LogisticsStatusRECEVIE = "RECEVIE" // 接单中
|
||||
LogisticsStatusWAIT_ACCEPT = "WAIT_ACCEPT" // 待揽件
|
||||
LogisticsStatusACCEPT = "ACCEPT" // 已揽收
|
||||
|
||||
// 运输中
|
||||
LogisticsStatusTRANSPORT = "TRANSPORT" // 运输中
|
||||
LogisticsStatusSEND_ON = "SEND_ON" // 转单或修改地址
|
||||
LogisticsStatusARRIVE_CITY = "ARRIVE_CITY" // 到达目的城市
|
||||
|
||||
// 派件
|
||||
LogisticsStatusDELIVERING = "DELIVERING" // 派件中
|
||||
LogisticsStatusSTA_INBOUND = "STA_INBOUND" // 已放入快递柜或者驿站
|
||||
|
||||
// 签收
|
||||
LogisticsStatusAGENT_SIGN = "AGENT_SIGN" // 已代签收
|
||||
LogisticsStatusSIGN = "SIGN" // 已签收
|
||||
LogisticsStatusSTA_SIGN = "STA_SIGN" // 从快递柜或驿站取出
|
||||
LogisticsStatusRETURN_SIGN = "RETURN_SIGN" // 退回签收
|
||||
|
||||
// 包裹异常
|
||||
LogisticsStatusFAILED = "FAILED" // 包裹异常
|
||||
LogisticsStatusREFUSE_SIGN = "REFUSE_SIGN" // 拒收
|
||||
LogisticsStatusDELIVER_ABNORMAL = "DELIVER_ABNORMAL" // 派件异常
|
||||
LogisticsStatusRETENTION = "RETENTION" // 滞留件
|
||||
LogisticsStatusISSUE = "ISSUE" // 问题件
|
||||
LogisticsStatusRETURN = "RETURN" // 退回件
|
||||
LogisticsStatusDAMAGE = "DAMAGE" // 破损
|
||||
LogisticsStatusCANCEL_ORDER = "CANCEL_ORDER" // 揽件取消
|
||||
)
|
||||
|
||||
// BaseModel 基础物流返回值
|
||||
type BaseModel struct {
|
||||
Msg string `json:"msg"` // 请求返回通知
|
||||
Success bool `json:"success"` // 是否成功
|
||||
Code int `json:"code"` // 返回code
|
||||
}
|
||||
|
||||
// OrderDetail 物流订单详情
|
||||
type OrderDetail struct {
|
||||
OrderNo string `json:"orderNo"` // 订单id
|
||||
Info []struct { // 物流详情
|
||||
TheLastTime string `json:"theLastTime"` // 运单号物流流转当前最新变更时间
|
||||
CpCode string `json:"cpCode"` // 快递公司代号
|
||||
MailNo string `json:"mailNo"` // 快递单号
|
||||
CpMobile string `json:"cpMobile"` // 快递公司电话
|
||||
TheLastMessage string `json:"theLastMessage"` // 运单号物流流转当前最新描述
|
||||
LogisticsCompanyName string `json:"logisticsCompanyName"` // 快递公司名称
|
||||
CpUrl string `json:"cpUrl"` // 快递公司官网
|
||||
Courier string `json:"courier"` // 配送人名称
|
||||
LogisticsStatusDesc string `json:"logisticsStatusDesc"` // 运单号当前物流状态文字描述
|
||||
LogisticsTraceDetailList []struct {
|
||||
AreaCode string `json:"areaCode,omitempty"` // 当前节点所在地址行政编码
|
||||
AreaName string `json:"areaName,omitempty"` // 当前节点所在地区,省,市,区或省,市
|
||||
SubLogisticsStatus string `json:"subLogisticsStatus"` // 物流流转子状态,详见:物流状态编码对照表
|
||||
Time int64 `json:"time"` // 时间,单位毫秒
|
||||
LogisticsStatus string `json:"logisticsStatus"` // 物流流转状态,详见:物流状态编码对照表
|
||||
Desc string `json:"desc"` // 物流流转描述
|
||||
Courier string `json:"courier,omitempty"` // 配送人名称
|
||||
CourierPhone string `json:"courierPhone,omitempty"` // 配送人电话
|
||||
} `json:"logisticsTraceDetailList"`
|
||||
CourierPhone string `json:"courierPhone"` // 配送人电话
|
||||
LogisticsStatus string `json:"logisticsStatus"` // 物流流转状态
|
||||
} `json:"info"`
|
||||
}
|
||||
|
||||
/************************************************************************************/
|
||||
|
||||
type GetOrderDetail struct {
|
||||
Msg string `json:"msg"` // 请求返回通知
|
||||
Success bool `json:"success"` // 是否成功
|
||||
Code int `json:"code"` // 返回code
|
||||
Data *OrderDetail `json:"data"`
|
||||
}
|
||||
32
platformapi/ali_logistics_query/logistics_query.go
Normal file
32
platformapi/ali_logistics_query/logistics_query.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package ali_logistics_query
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (a *API) GetLogisticsInfo(logisticsNumber string) (*OrderDetail, error) {
|
||||
if len(logisticsNumber) == 0 {
|
||||
return nil, errors.New("参数不能为空")
|
||||
}
|
||||
param := make(map[string]interface{}, 1)
|
||||
param["number"] = logisticsNumber
|
||||
|
||||
result, err := a.AccessAPI(BastUrl, GetOrderDetailApi, http.MethodGet, param)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var detail *GetOrderDetail
|
||||
if err := utils.Map2StructByJson(result, &detail, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if detail.Code != 200 || !detail.Success {
|
||||
return nil, errors.New(detail.Msg)
|
||||
}
|
||||
|
||||
return detail.Data, nil
|
||||
|
||||
}
|
||||
20
platformapi/ali_logistics_query/logistics_test.go
Normal file
20
platformapi/ali_logistics_query/logistics_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package ali_logistics_query
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
appKey = "23537670"
|
||||
appSecret = "4a77a431c0bf01f6dfa63d1a1ec9ecd2"
|
||||
appCode = "00a6eefba0204d3fa310ac0ee7a6fc54"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
api := New(appKey, appSecret, appCode)
|
||||
result, err := api.GetLogisticsInfo("JD0088184529553")
|
||||
globals.SugarLogger.Debugf("result======== %s", utils.Format4Output(result, false))
|
||||
globals.SugarLogger.Debugf("err======== %s", utils.Format4Output(err, false))
|
||||
}
|
||||
Reference in New Issue
Block a user