Skip to content

Commit

Permalink
🆕 #2510【小程序】增加微信小程序即时配送服务的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
luozeqiang8716 authored Jan 19, 2022
1 parent a6f55b3 commit 2c48303
Show file tree
Hide file tree
Showing 19 changed files with 1,936 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.delivery.AbnormalConfirmRequest;
import cn.binarywang.wx.miniapp.bean.delivery.AbnormalConfirmResponse;
import cn.binarywang.wx.miniapp.bean.delivery.AddOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.AddOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.BindAccountResponse;
import cn.binarywang.wx.miniapp.bean.delivery.CancelOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.CancelOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.GetOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.GetOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.MockUpdateOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.MockUpdateOrderResponse;
import me.chanjar.weixin.common.error.WxErrorException;

/**
* 微信小程序即时配送服务.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/immediate-delivery/overview.html
* </pre>
*
* @author Luo
* @version 1.0
* @date 2021-10-13 16:40
*/
public interface WxMaImmediateDeliveryService {

/**
* 拉取已绑定账号.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.getBindAccount.html
* </pre>
*
* @return 响应
* @throws WxErrorException 异常
*/
BindAccountResponse getBindAccount() throws WxErrorException;

/**
* 下配送单接口.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.addOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
AddOrderResponse addOrder(AddOrderRequest request) throws WxErrorException;

/**
* 拉取配送单信息.
* <pre>
* 商家可使用本接口查询某一配送单的配送状态,便于商家掌握配送情况。
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.getOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
GetOrderResponse getOrder(GetOrderRequest request) throws WxErrorException;

/**
* 取消配送单接口.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.cancelOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
CancelOrderResponse cancelOrder(CancelOrderRequest request) throws WxErrorException;

/**
* 异常件退回商家商家确认收货接口.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.abnormalConfirm.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
AbnormalConfirmResponse abnormalConfirm(AbnormalConfirmRequest request) throws WxErrorException;

/**
* 模拟配送公司更新配送单状态, 该接口只用于沙盒环境,即订单并没有真实流转到运力方.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.mockUpdateOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
MockUpdateOrderResponse mockUpdateOrder(MockUpdateOrderRequest request) throws WxErrorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,12 @@ public interface WxMaService extends WxService {
* @return WxMaDeviceSubscribeService plugin service
*/
WxMaMarketingService getMarketingService();

/**
* 返回微信小程序即时配送服务接口.
*
* @return WxMaImmediateDeliveryService
*/
WxMaImmediateDeliveryService getWxMaImmediateDeliveryService();

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
private final WxMaDeviceSubscribeService deviceSubscribeService = new WxMaDeviceSubscribeServiceImpl(this);
private final WxMaMarketingService marketingService = new WxMaMarketingServiceImpl(this);
private final WxMaImmediateDeliveryService immediateDeliveryService = new WxMaImmediateDeliveryServiceImpl(this);
private Map<String, WxMaConfig> configMap;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
Expand Down Expand Up @@ -581,4 +582,9 @@ public WxMaReimburseInvoiceService getReimburseInvoiceService() {

@Override
public WxMaMarketingService getMarketingService() {return this.marketingService; }

@Override
public WxMaImmediateDeliveryService getWxMaImmediateDeliveryService() {
return this.immediateDeliveryService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaImmediateDeliveryService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.delivery.AbnormalConfirmRequest;
import cn.binarywang.wx.miniapp.bean.delivery.AbnormalConfirmResponse;
import cn.binarywang.wx.miniapp.bean.delivery.AddOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.AddOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.BindAccountResponse;
import cn.binarywang.wx.miniapp.bean.delivery.CancelOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.CancelOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.GetOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.GetOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.MockUpdateOrderRequest;
import cn.binarywang.wx.miniapp.bean.delivery.MockUpdateOrderResponse;
import cn.binarywang.wx.miniapp.bean.delivery.base.WxMaDeliveryBaseResponse;
import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import javassist.bytecode.ConstPool;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
* 微信小程序即时配送服务.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/immediate-delivery/overview.html
* </pre>
*
* @author Luo
* @version 1.0
* @date 2021-10-13 16:40
*/
@RequiredArgsConstructor
public class WxMaImmediateDeliveryServiceImpl implements WxMaImmediateDeliveryService {

/**
* 微信响应码.
*/
public static final String ERR_CODE = "errcode";

/**
* 顺丰同城响应码.
*/
public static final String SF_ERR_CODE = "resultcode";

/**
* 顺丰同城响应说明.
*/
public static final String SF_ERR_MSG = "resultmsg";

/**
* 成功响应状态码.
*/
public static final int SUCCESS_CODE = 0;

private final WxMaService wxMaService;

/**
* 拉取已绑定账号.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.getBindAccount.html
* </pre>
*
* @return 响应
* @throws WxErrorException 异常
*/
@Override
public BindAccountResponse getBindAccount() throws WxErrorException {
return this.parse(this.wxMaService.post(WxMaApiUrlConstants.InstantDelivery.GET_BIND_ACCOUNT, "{}"),
BindAccountResponse.class);
}

/**
* 下配送单接口.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.addOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
@Override
public AddOrderResponse addOrder(final AddOrderRequest request) throws WxErrorException {
return this.parse(this.wxMaService.post(WxMaApiUrlConstants.InstantDelivery.PlaceAnOrder.ADD_ORDER, request),
AddOrderResponse.class);
}

/**
* 拉取配送单信息.
* <pre>
* 商家可使用本接口查询某一配送单的配送状态,便于商家掌握配送情况。
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.getOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
@Override
public GetOrderResponse getOrder(final GetOrderRequest request) throws WxErrorException {
return this.parse(this.wxMaService.post(WxMaApiUrlConstants.InstantDelivery.GET_ORDER, request),
GetOrderResponse.class);
}

/**
* 取消配送单接口.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.cancelOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
@Override
public CancelOrderResponse cancelOrder(final CancelOrderRequest request) throws WxErrorException {
return this.parse(this.wxMaService.post(WxMaApiUrlConstants.InstantDelivery.Cancel.CANCEL_ORDER, request),
CancelOrderResponse.class);
}

/**
* 异常件退回商家商家确认收货接口.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.abnormalConfirm.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
@Override
public AbnormalConfirmResponse abnormalConfirm(final AbnormalConfirmRequest request) throws WxErrorException {
return this.parse(this.wxMaService.post(WxMaApiUrlConstants.InstantDelivery.Cancel.ABNORMAL_CONFIRM, request),
AbnormalConfirmResponse.class);
}

/**
* 模拟配送公司更新配送单状态, 该接口只用于沙盒环境,即订单并没有真实流转到运力方.
* <pre>
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/immediate-delivery/by-business/immediateDelivery.mockUpdateOrder.html
* </pre>
*
* @param request request
* @return 响应
* @throws WxErrorException 异常
*/
@Override
public MockUpdateOrderResponse mockUpdateOrder(final MockUpdateOrderRequest request) throws WxErrorException {
return this.parse(this.wxMaService.post(WxMaApiUrlConstants.InstantDelivery.MOCK_UPDATE_ORDER, request),
MockUpdateOrderResponse.class);
}

/**
* 解析响应.
*
* @param responseContent 响应内容
* @param valueType 类型
* @param <T> 类型
* @return 结果
* @throws WxErrorException 异常
*/
private <T extends WxMaDeliveryBaseResponse> T parse(final String responseContent, final Class<T> valueType) throws WxErrorException {
if (StringUtils.isBlank(responseContent)) {
throw new RuntimeException("the responseContent cannot be empty");
}
// 解析成Json对象
JsonObject jsonObject = GsonParser.parse(responseContent);
// 是否为微信错误响应 当 errcode==0 或者 不存在 还需要看 运力方 resultcode 状态码
JsonElement element = jsonObject.get(ERR_CODE);
// 正常响应下不会有该字段返回
if (!ObjectUtils.isEmpty(element) && SUCCESS_CODE != element.getAsInt()) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
// 是否为运力方错误响应
JsonElement delivery = jsonObject.get(SF_ERR_CODE);
if (!ObjectUtils.isEmpty(delivery) && SUCCESS_CODE != delivery.getAsInt()) {
throw new WxErrorException(jsonObject.get(SF_ERR_MSG).getAsString());
}
// 解析成对应响应对象
return WxMaDeliveryBaseResponse.fromJson(responseContent, valueType);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cn.binarywang.wx.miniapp.bean.delivery;

import cn.binarywang.wx.miniapp.bean.delivery.base.WxMaDeliveryBaseRequest;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
* 微信小程序即时配送 异常件退回商家商家确认收货接口 请求参数.
*
* @author Luo
* @version 1.0
* @date 2021-10-14 10:49
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class AbnormalConfirmRequest extends WxMaDeliveryBaseRequest implements Serializable {

private static final long serialVersionUID = 3773007367000633663L;

/**
* 配送单id.
* <pre>
* 是否必填:是
* </pre>
*/
@SerializedName("waybill_id")
private String waybillId;

/**
* 备注.
* <pre>
* 是否必填:否
* </pre>
*/
@SerializedName("remark")
private String remark;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.binarywang.wx.miniapp.bean.delivery;

import cn.binarywang.wx.miniapp.bean.delivery.base.WxMaDeliveryBaseResponse;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
* 微信小程序即时配送 异常件退回商家商家确认收货接口 响应参数.
*
* @author Luo
* @version 1.0
* @date 2021-10-14 10:49
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class AbnormalConfirmResponse extends WxMaDeliveryBaseResponse implements Serializable {

private static final long serialVersionUID = 3773007367000633663L;


}
Loading

0 comments on commit 2c48303

Please sign in to comment.