11package cn .binarywang .wx .miniapp .api .impl ;
22
3- import static cn .binarywang .wx .miniapp .constant .WxMaApiUrlConstants .Shop .Order .ORDER_ADD ;
4- import static cn .binarywang .wx .miniapp .constant .WxMaApiUrlConstants .Shop .Order .ORDER_CHECK_SCENE ;
5- import static cn .binarywang .wx .miniapp .constant .WxMaApiUrlConstants .Shop .Order .ORDER_GET ;
6- import static cn .binarywang .wx .miniapp .constant .WxMaApiUrlConstants .Shop .Order .ORDER_PAY ;
7-
83import cn .binarywang .wx .miniapp .api .WxMaService ;
94import cn .binarywang .wx .miniapp .api .WxMaShopOrderService ;
105import cn .binarywang .wx .miniapp .bean .shop .WxMaShopOrderInfo ;
116import cn .binarywang .wx .miniapp .bean .shop .request .WxMaShopOrderPayRequest ;
12- import cn .binarywang .wx .miniapp .bean .shop .response .WxMaShopAddOrderResponse ;
13- import cn .binarywang .wx .miniapp .bean .shop .response .WxMaShopBaseResponse ;
14- import cn .binarywang .wx .miniapp .bean .shop .response .WxMaShopGetOrderResponse ;
7+ import cn .binarywang .wx .miniapp .bean .shop .response .*;
158import cn .binarywang .wx .miniapp .json .WxMaGsonBuilder ;
169import com .google .gson .JsonObject ;
1710import lombok .RequiredArgsConstructor ;
2114import me .chanjar .weixin .common .error .WxErrorException ;
2215import me .chanjar .weixin .common .util .json .GsonHelper ;
2316import me .chanjar .weixin .common .util .json .GsonParser ;
17+ import org .apache .commons .lang3 .time .FastDateFormat ;
18+
19+ import java .text .Format ;
20+ import java .util .Date ;
21+
22+ import static cn .binarywang .wx .miniapp .constant .WxMaApiUrlConstants .Shop .Order .*;
2423
2524/**
2625 * @author boris
2726 */
2827@ RequiredArgsConstructor
2928@ Slf4j
3029public class WxMaShopOrderServiceImpl implements WxMaShopOrderService {
30+
31+ private final Format dateFormat = FastDateFormat .getInstance ("yyyy-MM-dd HH:mm:ss" );
32+
3133 private static final String ERR_CODE = "errcode" ;
3234 private static final String MATCH_KEY = "is_matched" ;
3335 private final WxMaService wxMaService ;
@@ -45,34 +47,49 @@ public Boolean checkScene(Integer scene) throws WxErrorException {
4547
4648 @ Override
4749 public WxMaShopAddOrderResponse addOrder (WxMaShopOrderInfo orderInfo ) throws WxErrorException {
48- String responseContent = this .wxMaService .post (ORDER_ADD , orderInfo );
49- JsonObject jsonObject = GsonParser .parse (responseContent );
50- if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
51- throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
52- }
53- return WxMaGsonBuilder .create ().fromJson (responseContent , WxMaShopAddOrderResponse .class );
50+ return this .post (ORDER_ADD ,orderInfo , WxMaShopAddOrderResponse .class );
5451 }
5552
5653 @ Override
5754 public WxMaShopBaseResponse orderPay (WxMaShopOrderPayRequest request ) throws WxErrorException {
58- String responseContent = this .wxMaService .post (ORDER_PAY , request );
59- JsonObject jsonObject = GsonParser .parse (responseContent );
60- if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
61- throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
55+ return this .post (ORDER_PAY ,request , WxMaShopBaseResponse .class );
56+ }
57+
58+ @ Override
59+ public WxMaShopGetOrderResponse getOrder (Integer orderId , String outOrderId , String openid ) throws WxErrorException {
60+ return this .post (ORDER_GET , GsonHelper .buildJsonObject ("order_id" , orderId , "out_order_id" , outOrderId ,
61+ "openid" , openid ), WxMaShopGetOrderResponse .class );
62+ }
63+
64+ @ Override
65+ public WxMaShopGetOrderListResponse getOrderList (Integer page , Integer pageSize , Boolean desc , Date startCreateTime , Date endCreateTime ) throws WxErrorException {
66+ JsonObject object = new JsonObject ();
67+ object .addProperty ("page" , page == null ? 1 : page );
68+ object .addProperty ("page_size" , pageSize == null ? 10 : pageSize );
69+ object .addProperty ("desc" , desc ? 1 : 2 );
70+ if (startCreateTime != null ) {
71+ object .addProperty ("start_create_time" , this .dateFormat .format (startCreateTime ));
72+ }
73+ if (endCreateTime != null ) {
74+ object .addProperty ("end_create_time" , this .dateFormat .format (endCreateTime ));
6275 }
63- return WxMaGsonBuilder . create (). fromJson ( responseContent , WxMaShopBaseResponse .class );
76+ return this . post ( ORDER_GET_LIST , object , WxMaShopGetOrderListResponse .class );
6477 }
6578
6679 @ Override
67- public WxMaShopGetOrderResponse getOrder (Integer orderId , String outOrderId , String openid )
68- throws WxErrorException {
69- String responseContent = this .wxMaService .post (ORDER_GET ,
80+ public WxMaShopGetPaymentParamsResponse getPaymentParams (String orderId , String outOrderId , String openid ) throws WxErrorException {
81+ return this .post (ORDER_GET_PAYMENT_PARAMS ,
7082 GsonHelper .buildJsonObject ("order_id" , orderId , "out_order_id" , outOrderId ,
71- "openid" , openid ));
83+ "openid" , openid ), WxMaShopGetPaymentParamsResponse .class );
84+ }
85+
86+
87+ private <T > T post (String url , Object params , Class <T > classOfT ) throws WxErrorException {
88+ String responseContent = this .wxMaService .post (url , params );
7289 JsonObject jsonObject = GsonParser .parse (responseContent );
7390 if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
7491 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
7592 }
76- return WxMaGsonBuilder .create ().fromJson (responseContent , WxMaShopGetOrderResponse . class );
93+ return WxMaGsonBuilder .create ().fromJson (responseContent , classOfT );
7794 }
7895}
0 commit comments