|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaOrderShippingService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 5 | +import cn.binarywang.wx.miniapp.bean.shop.request.WxMaOrderShippingIsTradeManagedRequest; |
| 6 | +import cn.binarywang.wx.miniapp.bean.shop.request.shipping.*; |
| 7 | +import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingInfoBaseResponse; |
| 8 | +import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingInfoGetListResponse; |
| 9 | +import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingInfoGetResponse; |
| 10 | +import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingIsTradeManagedResponse; |
| 11 | +import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder; |
| 12 | +import com.google.gson.JsonObject; |
| 13 | +import lombok.RequiredArgsConstructor; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | +import me.chanjar.weixin.common.api.WxConsts; |
| 16 | +import me.chanjar.weixin.common.enums.WxType; |
| 17 | +import me.chanjar.weixin.common.error.WxError; |
| 18 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 19 | +import me.chanjar.weixin.common.util.json.GsonHelper; |
| 20 | +import me.chanjar.weixin.common.util.json.GsonParser; |
| 21 | + |
| 22 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.OrderShipping.*; |
| 23 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Product.OTHER.GET_CATEGORY; |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * @author xzh |
| 28 | + * created on 2023/5/17 17:44 |
| 29 | + */ |
| 30 | +@Slf4j |
| 31 | +@RequiredArgsConstructor |
| 32 | +public class WxMaOrderShippingServiceImpl implements WxMaOrderShippingService { |
| 33 | + |
| 34 | + private final WxMaService wxMaService; |
| 35 | + |
| 36 | + /** |
| 37 | + * 查询小程序是否已开通发货信息管理服务 |
| 38 | + * |
| 39 | + * @param appId 待查询小程序的 appid,非服务商调用时仅能查询本账号 |
| 40 | + * @return WxMaOrderShippingInfoBaseResponse |
| 41 | + * @throws WxErrorException e |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public WxMaOrderShippingIsTradeManagedResponse isTradeManaged(String appId) throws WxErrorException { |
| 45 | + WxMaOrderShippingIsTradeManagedRequest request = WxMaOrderShippingIsTradeManagedRequest.builder().appId(appId).build(); |
| 46 | + return request(IS_TRADE_MANAGED, request, WxMaOrderShippingIsTradeManagedResponse.class); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 发货信息录入接口 |
| 51 | + * |
| 52 | + * @param request 请求 |
| 53 | + * @return WxMaOrderShippingInfoBaseResponse |
| 54 | + * @throws WxErrorException e |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public WxMaOrderShippingInfoBaseResponse upload(WxMaOrderShippingInfoUploadRequest request) throws WxErrorException { |
| 58 | + return request(UPLOAD_SHIPPING_INFO, request, WxMaOrderShippingInfoBaseResponse.class); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 发货信息合单录入接口 |
| 63 | + * |
| 64 | + * @param request 请求 |
| 65 | + * @return WxMaOrderShippingInfoBaseResponse |
| 66 | + * @throws WxErrorException e |
| 67 | + */ |
| 68 | + @Override |
| 69 | + public WxMaOrderShippingInfoBaseResponse upload(WxMaOrderCombinedShippingInfoUploadRequest request) throws WxErrorException { |
| 70 | + return request(UPLOAD_COMBINED_SHIPPING_INFO, request, WxMaOrderShippingInfoBaseResponse.class); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 查询订单发货状态 |
| 75 | + * 你可以通过交易单号或商户号+商户单号来查询该支付单的发货状态。 |
| 76 | + * |
| 77 | + * @param request 请求 |
| 78 | + * @return WxMaOrderShippingInfoGetResponse |
| 79 | + * @throws WxErrorException e |
| 80 | + */ |
| 81 | + @Override |
| 82 | + public WxMaOrderShippingInfoGetResponse get(WxMaOrderShippingInfoGetRequest request) throws WxErrorException { |
| 83 | + return request(GET_SHIPPING_INFO, request, WxMaOrderShippingInfoGetResponse.class); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * 查询订单列表 |
| 88 | + * 你可以通过支付时间、支付者openid或订单状态来查询订单列表。 |
| 89 | + * |
| 90 | + * @param request 请求 |
| 91 | + * @return WxMaOrderShippingInfoGetListResponse |
| 92 | + * @throws WxErrorException e |
| 93 | + */ |
| 94 | + @Override |
| 95 | + public WxMaOrderShippingInfoGetListResponse getList(WxMaOrderShippingInfoGetListRequest request) throws WxErrorException { |
| 96 | + return request(GET_SHIPPING_INFO_LIST, request, WxMaOrderShippingInfoGetListResponse.class); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 确认收货提醒接口 |
| 101 | + * 如你已经从你的快递物流服务方获知到用户已经签收相关商品,可以通过该接口提醒用户及时确认收货,以提高资金结算效率,每个订单仅可调用一次。 |
| 102 | + * |
| 103 | + * @param request 请求 |
| 104 | + * @return WxMaOrderShippingInfoBaseResponse |
| 105 | + * @throws WxErrorException e |
| 106 | + */ |
| 107 | + @Override |
| 108 | + public WxMaOrderShippingInfoBaseResponse notifyConfirmReceive(WxMaOrderShippingInfoNotifyConfirmRequest request) throws WxErrorException { |
| 109 | + return request(NOTIFY_CONFIRM_RECEIVE, request, WxMaOrderShippingInfoBaseResponse.class); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * 消息跳转路径设置接口 |
| 114 | + * 如你已经在小程序内接入平台提供的确认收货组件,可以通过该接口设置发货消息及确认收货消息的跳转动作,用户点击发货消息时会直接进入你的小程序订单列表页面或详情页面进行确认收货,进一步优化用户体验。 |
| 115 | + * |
| 116 | + * @param path 商户自定义跳转路径 |
| 117 | + * @return WxMaOrderShippingInfoBaseResponse |
| 118 | + * @throws WxErrorException e |
| 119 | + */ |
| 120 | + @Override |
| 121 | + public WxMaOrderShippingInfoBaseResponse setMsgJumpPath(String path) throws WxErrorException { |
| 122 | + JsonObject jsonObject = GsonHelper.buildJsonObject("path", path); |
| 123 | + return request(SET_MSG_JUMP_PATH, jsonObject, WxMaOrderShippingInfoBaseResponse.class); |
| 124 | + } |
| 125 | + |
| 126 | + private <T> T request(String url, Object request, Class<T> resultT) throws WxErrorException { |
| 127 | + String responseContent = this.wxMaService.post(url, request); |
| 128 | + JsonObject jsonObject = GsonParser.parse(responseContent); |
| 129 | + if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) { |
| 130 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); |
| 131 | + } |
| 132 | + return WxMaGsonBuilder.create().fromJson(responseContent, resultT); |
| 133 | + } |
| 134 | +} |
0 commit comments