delivery.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package express
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. // 传运单接口,商户使用此接口向微信提供某交易单号对应的运单号。微信后台会跟踪运单的状态变化
  9. openMsgTraceWaybillURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=%s"
  10. // 查询运单接口,商户在调用完trace_waybill接口后,可以使用本接口查询到对应运单的详情信息
  11. openMsgQueryTraceURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/query_trace?access_token=%s"
  12. // 更新物流信息,更新物品信息
  13. openMsgUpdateWaybillGoodsURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/update_waybill_goods?access_token=%s"
  14. // 传运单接口,商户使用此接口向微信提供某交易单号对应的运单号。微信后台会跟踪运单的状态变化,在关键物流节点给下单用户推送消息通知
  15. openMsgFollowWaybillURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/follow_waybill?access_token=%s"
  16. // 查运单接口,商户在调用完follow_waybill接口后,可以使用本接口查询到对应运单的详情信息
  17. openMsgQueryFollowTraceURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/query_follow_trace?access_token=%s"
  18. // 更新物品信息接口
  19. openMsgUpdateFollowWaybillGoodsURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/update_follow_waybill_goods?access_token=%s"
  20. // 获取运力id列表
  21. openMsgGetDeliveryListURL = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=%s"
  22. )
  23. // TraceWaybill 传运单
  24. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#_2-%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8
  25. func (express *Express) TraceWaybill(ctx context.Context, in *TraceWaybillRequest) (res TraceWaybillResponse, err error) {
  26. accessToken, err := express.GetAccessToken()
  27. if err != nil {
  28. return
  29. }
  30. uri := fmt.Sprintf(openMsgTraceWaybillURL, accessToken)
  31. response, err := util.PostJSONContext(ctx, uri, in)
  32. if err != nil {
  33. return
  34. }
  35. // 使用通用方法返回错误
  36. err = util.DecodeWithError(response, &res, "TraceWaybill")
  37. return
  38. }
  39. // QueryTrace 查询运单详情信息
  40. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#_2-%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8
  41. func (express *Express) QueryTrace(ctx context.Context, in *QueryTraceRequest) (res QueryTraceResponse, err error) {
  42. accessToken, err := express.GetAccessToken()
  43. if err != nil {
  44. return
  45. }
  46. uri := fmt.Sprintf(openMsgQueryTraceURL, accessToken)
  47. response, err := util.PostJSONContext(ctx, uri, in)
  48. if err != nil {
  49. return
  50. }
  51. // 使用通用方法返回错误
  52. err = util.DecodeWithError(response, &res, "QueryTrace")
  53. return
  54. }
  55. // UpdateWaybillGoods 更新物品信息
  56. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#_2-%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8
  57. func (express *Express) UpdateWaybillGoods(ctx context.Context, in *UpdateWaybillGoodsRequest) (err error) {
  58. accessToken, err := express.GetAccessToken()
  59. if err != nil {
  60. return
  61. }
  62. uri := fmt.Sprintf(openMsgUpdateWaybillGoodsURL, accessToken)
  63. response, err := util.PostJSONContext(ctx, uri, in)
  64. if err != nil {
  65. return
  66. }
  67. // 使用通用方法返回错误
  68. err = util.DecodeWithCommonError(response, "UpdateWaybillGoods")
  69. return
  70. }
  71. // FollowWaybill 传运单
  72. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_open_msg.html#_4-1%E3%80%81%E4%BC%A0%E8%BF%90%E5%8D%95%E6%8E%A5%E5%8F%A3-follow-waybill
  73. func (express *Express) FollowWaybill(ctx context.Context, in *FollowWaybillRequest) (res FollowWaybillResponse, err error) {
  74. accessToken, err := express.GetAccessToken()
  75. if err != nil {
  76. return
  77. }
  78. uri := fmt.Sprintf(openMsgFollowWaybillURL, accessToken)
  79. response, err := util.PostJSONContext(ctx, uri, in)
  80. if err != nil {
  81. return
  82. }
  83. // 使用通用方法返回错误
  84. err = util.DecodeWithError(response, &res, "FollowWaybill")
  85. return
  86. }
  87. // QueryFollowTrace 查询运单详情信息
  88. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_open_msg.html#_4-2%E3%80%81%E6%9F%A5%E8%BF%90%E5%8D%95%E6%8E%A5%E5%8F%A3-query-follow-trace
  89. func (express *Express) QueryFollowTrace(ctx context.Context, in *QueryFollowTraceRequest) (res QueryFollowTraceResponse, err error) {
  90. accessToken, err := express.GetAccessToken()
  91. if err != nil {
  92. return
  93. }
  94. uri := fmt.Sprintf(openMsgQueryFollowTraceURL, accessToken)
  95. response, err := util.PostJSONContext(ctx, uri, in)
  96. if err != nil {
  97. return
  98. }
  99. // 使用通用方法返回错误
  100. err = util.DecodeWithError(response, &res, "QueryFollowTrace")
  101. return
  102. }
  103. // UpdateFollowWaybillGoods 更新物品信息
  104. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_open_msg.html#_4-3%E3%80%81%E6%9B%B4%E6%96%B0%E7%89%A9%E5%93%81%E4%BF%A1%E6%81%AF%E6%8E%A5%E5%8F%A3-update-follow-waybill-goods
  105. func (express *Express) UpdateFollowWaybillGoods(ctx context.Context, in *UpdateFollowWaybillGoodsRequest) (err error) {
  106. accessToken, err := express.GetAccessToken()
  107. if err != nil {
  108. return
  109. }
  110. uri := fmt.Sprintf(openMsgUpdateFollowWaybillGoodsURL, accessToken)
  111. response, err := util.PostJSONContext(ctx, uri, in)
  112. if err != nil {
  113. return
  114. }
  115. // 使用通用方法返回错误
  116. err = util.DecodeWithCommonError(response, "UpdateFollowWaybillGoods")
  117. return
  118. }
  119. // GetDeliveryList 获取运力id列表
  120. // https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_open_msg.html#_4-4%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list
  121. func (express *Express) GetDeliveryList(ctx context.Context) (res GetDeliveryListResponse, err error) {
  122. accessToken, err := express.GetAccessToken()
  123. if err != nil {
  124. return
  125. }
  126. uri := fmt.Sprintf(openMsgGetDeliveryListURL, accessToken)
  127. response, err := util.PostJSONContext(ctx, uri, map[string]interface{}{})
  128. if err != nil {
  129. return
  130. }
  131. // 使用通用方法返回错误
  132. err = util.DecodeWithError(response, &res, "GetDeliveryList")
  133. return
  134. }
  135. // TraceWaybillRequest 传运单接口请求参数
  136. type TraceWaybillRequest struct {
  137. GoodsInfo FollowWaybillGoodsInfo `json:"goods_info"` // 必选,商品信息
  138. Openid string `json:"openid"` // 必选,用户openid
  139. SenderPhone string `json:"sender_phone"` // 寄件人手机号
  140. ReceiverPhone string `json:"receiver_phone"` // 必选,收件人手机号,部分运力需要用户手机号作为查单依据
  141. DeliveryID string `json:"delivery_id"` // 运力id(运单号所属运力公司id)
  142. WaybillID string `json:"waybill_id"` // 必选,运单号
  143. TransID string `json:"trans_id"` // 必选,交易单号(微信支付生成的交易单号,一般以420开头)
  144. OrderDetailPath string `json:"order_detail_path"` // 订单详情页地址
  145. }
  146. // TraceWaybillResponse 传运单接口返回参数
  147. type TraceWaybillResponse struct {
  148. util.CommonError
  149. WaybillToken string `json:"waybill_token"` // 查询id
  150. }
  151. // QueryTraceRequest 查询运单详情接口请求参数
  152. type QueryTraceRequest struct {
  153. WaybillToken string `json:"waybill_token"` // 必选,查询id
  154. }
  155. // QueryTraceResponse 查询运单详情接口返回参数
  156. type QueryTraceResponse struct {
  157. util.CommonError
  158. WaybillInfo FlowWaybillInfo `json:"waybill_info"` // 运单信息
  159. ShopInfo FollowWaybillShopInfo `json:"shop_info"` // 商品信息
  160. DeliveryInfo FlowWaybillDeliveryInfo `json:"delivery_info"` // 运力信息
  161. }
  162. // UpdateWaybillGoodsRequest 更新物品信息接口请求参数
  163. type UpdateWaybillGoodsRequest struct {
  164. WaybillToken string `json:"waybill_token"` // 必选,查询id
  165. GoodsInfo FollowWaybillGoodsInfo `json:"goods_info"` // 必选,商品信息
  166. }
  167. // FollowWaybillRequest 传运单接口请求参数
  168. type FollowWaybillRequest struct {
  169. GoodsInfo FollowWaybillGoodsInfo `json:"goods_info"` // 必选,商品信息
  170. Openid string `json:"openid"` // 必选,用户openid
  171. SenderPhone string `json:"sender_phone"` // 寄件人手机号
  172. ReceiverPhone string `json:"receiver_phone"` // 必选,收件人手机号,部分运力需要用户手机号作为查单依据
  173. DeliveryID string `json:"delivery_id"` // 运力id(运单号所属运力公司id)
  174. WaybillID string `json:"waybill_id"` // 必选,运单号
  175. TransID string `json:"trans_id"` // 必选,交易单号(微信支付生成的交易单号,一般以420开头)
  176. OrderDetailPath string `json:"order_detail_path"` // 订单详情页地址
  177. }
  178. // FollowWaybillGoodsInfo 商品信息
  179. type FollowWaybillGoodsInfo struct {
  180. DetailList []FollowWaybillGoodsInfoItem `json:"detail_list"`
  181. }
  182. // FollowWaybillShopInfo 商品信息
  183. type FollowWaybillShopInfo struct {
  184. GoodsInfo FollowWaybillGoodsInfo `json:"goods_info"` // 商品信息
  185. }
  186. // FollowWaybillGoodsInfoItem 商品信息详情
  187. type FollowWaybillGoodsInfoItem struct {
  188. GoodsName string `json:"goods_name"` // 必选,商品名称(最大长度为utf-8编码下的60个字符)
  189. GoodsImgURL string `json:"goods_img_url"` // 必选,商品图片url
  190. GoodsDesc string `json:"goods_desc,omitempty"` // 商品详情描述,不传默认取“商品名称”值,最多40汉字
  191. }
  192. // FollowWaybillResponse 传运单接口返回参数
  193. type FollowWaybillResponse struct {
  194. util.CommonError
  195. WaybillToken string `json:"waybill_token"` // 查询id
  196. }
  197. // QueryFollowTraceRequest 查询运单详情信息请求参数
  198. type QueryFollowTraceRequest struct {
  199. WaybillToken string `json:"waybill_token"` // 必选,查询id
  200. }
  201. // QueryFollowTraceResponse 查询运单详情信息返回参数
  202. type QueryFollowTraceResponse struct {
  203. util.CommonError
  204. WaybillInfo FlowWaybillInfo `json:"waybill_info"` // 运单信息
  205. ShopInfo FollowWaybillShopInfo `json:"shop_info"` // 商品信息
  206. DeliveryInfo FlowWaybillDeliveryInfo `json:"delivery_info"` // 运力信息
  207. }
  208. // FlowWaybillInfo 运单信息
  209. type FlowWaybillInfo struct {
  210. WaybillID string `json:"waybill_id"` // 运单号
  211. Status WaybillStatus `json:"status"` // 运单状态
  212. }
  213. // UpdateFollowWaybillGoodsRequest 修改运单商品信息请求参数
  214. type UpdateFollowWaybillGoodsRequest struct {
  215. WaybillToken string `json:"waybill_token"` // 必选,查询id
  216. GoodsInfo FollowWaybillGoodsInfo `json:"goods_info"` // 必选,商品信息
  217. }
  218. // GetDeliveryListResponse 获取运力id列表返回参数
  219. type GetDeliveryListResponse struct {
  220. util.CommonError
  221. DeliveryList []FlowWaybillDeliveryInfo `json:"delivery_list"` // 运力公司列表
  222. Count int `json:"count"` // 运力公司个数
  223. }
  224. // FlowWaybillDeliveryInfo 运力公司信息
  225. type FlowWaybillDeliveryInfo struct {
  226. DeliveryID string `json:"delivery_id"` // 运力公司id
  227. DeliveryName string `json:"delivery_name"` // 运力公司名称
  228. }
  229. // WaybillStatus 运单状态
  230. type WaybillStatus int
  231. const (
  232. // WaybillStatusNotExist 运单不存在或者未揽收
  233. WaybillStatusNotExist WaybillStatus = iota
  234. // WaybillStatusPicked 已揽件
  235. WaybillStatusPicked
  236. // WaybillStatusTransporting 运输中
  237. WaybillStatusTransporting
  238. // WaybillStatusDispatching 派件中
  239. WaybillStatusDispatching
  240. // WaybillStatusSigned 已签收
  241. WaybillStatusSigned
  242. // WaybillStatusException 异常
  243. WaybillStatusException
  244. // WaybillStatusSignedByOthers 代签收
  245. WaybillStatusSignedByOthers
  246. )