Commit e0766888544dd4a07607e83bda49e70748c1950b

Authored by 吴启风
1 parent caec5687

feat:解决微信支付异步回调里emitter(PaySuccessState())报错问题

lib/common/request/dao/shop_dao.dart
... ... @@ -8,20 +8,20 @@ class ShopDao {
8 8 }
9 9  
10 10 ///创建订单
11   - static Future createOrder(ProductEntity productEntity) async {
  11 + static Future<Map<String, dynamic>?> createOrder(ProductEntity productEntity) async {
12 12 return await requestClient
13   - .post<Map<String, dynamic>>(Apis.createOrder, data: {'courseComboId': productEntity.id});
  13 + .post<Map<String, dynamic>?>(Apis.createOrder, data: {'courseComboId': productEntity.id});
14 14 }
15 15  
16 16 ///获取alipay支付订单信息
17   - static Future getAliPayToken(String orderNo) async {
  17 + static Future<Map<String, dynamic>?> getAliPayToken(String orderNo) async {
18 18 return await requestClient
19   - .post<Map<String, dynamic>>(Apis.getAliPayToken, data: {'orderNo': orderNo});
  19 + .post<Map<String, dynamic>?>(Apis.getAliPayToken, data: {'orderNo': orderNo});
20 20 }
21 21  
22 22 ///获取weixin支付订单信息
23   - static Future getWxPayToken(String orderNo) async {
  23 + static Future<Map<String, dynamic>?> getWxPayToken(String orderNo) async {
24 24 return await requestClient
25   - .post<Map<String, dynamic>>(Apis.getWxPayToken, data: {'orderNo': orderNo});
  25 + .post<Map<String, dynamic>?>(Apis.getWxPayToken, data: {'orderNo': orderNo});
26 26 }
27 27 }
... ...
lib/pages/shopping/bloc.dart
... ... @@ -36,6 +36,9 @@ class ShoppingBloc extends Bloc&lt;ShoppingEvent, ShoppingState&gt; {
36 36 on<InitEvent>(_init);
37 37 on<ChangePaymentChannelEvent>(_changePaymentChannel);
38 38 on<DoPayEvent>(_startPay);
  39 + on<WxPaySuccessEvent>((event, emit) {
  40 + emit(PaySuccessState());
  41 + });
39 42 }
40 43  
41 44 void _init(InitEvent event, Emitter<ShoppingState> emit) async {
... ... @@ -55,7 +58,6 @@ class ShoppingBloc extends Bloc&lt;ShoppingEvent, ShoppingState&gt; {
55 58  
56 59 void _startPay(DoPayEvent event,
57 60 Emitter<ShoppingState> emitter) async {
58   - Log.d("开始支付 ${event.productEntity} ${event.paymentChannel}");
59 61 //如果event.productEntity为空,中断流程并toast提示
60 62 if (event.productEntity == null) {
61 63 showToast("商品信息为空");
... ... @@ -64,29 +66,31 @@ class ShoppingBloc extends Bloc&lt;ShoppingEvent, ShoppingState&gt; {
64 66 final productEntitySafely = event.productEntity!;
65 67 try {
66 68 await loading(() async {
67   - final Map<String, dynamic> orderInfo = await ShopDao.createOrder(productEntitySafely);
68   - Log.d("orderInfo $orderInfo");
69   - final String? orderNo = orderInfo.getOrNull("orderNo");
  69 + final Map<String, dynamic>? orderInfo = await ShopDao.createOrder(productEntitySafely);
  70 + final String? orderNo = orderInfo?.getOrNull("orderNo");
70 71 if (orderNo == null) {
71 72 showToast("订单创建失败");
72 73 return;
73 74 }
74   - Log.d("orderNo $orderNo");
75 75  
76 76 if (event.paymentChannel == PaymentChannel.wechatPay) {
77 77 if (_isWxPayListenerInitialized == false) {
78 78 _isWxPayListenerInitialized = true;
79 79 fluwx = Fluwx();
80   - fluwx?.registerApi(appId: "wx365e5a79956a450a",
  80 + await fluwx?.registerApi(appId: "wx365e5a79956a450a",
81 81 universalLink: "https://app-api.wowenglish.com.cn/app/");
82 82 wxPayResponseListener = (WeChatResponse response) {
83   - Log.d("wxPayResponseListener $response");
  83 + debugPrint("WqfPay wxPayResponseListener $response");
84 84 if (response is WeChatPaymentResponse) {
85 85 if (response.errCode == 0) {
86   - Log.d("wxPayResponseListener response=${response.errCode}");
  86 + debugPrint("WqfPay wxPayResponseListener response=${response.errCode}");
87 87 showToast("支付成功");
88 88 // Log.d("emitter isDone=${emitter.isDone}");
  89 +
  90 + /// 报错!_isCompleted emit was called after an event handler completed normally
89 91 // emitter(PaySuccessState());
  92 +
  93 + add(WxPaySuccessEvent());
90 94 } else {
91 95 showToast("支付失败");
92 96 }
... ... @@ -103,10 +107,8 @@ class ShoppingBloc extends Bloc&lt;ShoppingEvent, ShoppingState&gt; {
103 107 return;
104 108 }
105 109  
106   - final Map<String, dynamic> wxPayOrderInfo = await ShopDao.getWxPayToken(orderNo);
107   - Log.d("wxPayOrderInfo=$wxPayOrderInfo type=${wxPayOrderInfo.runtimeType}");
108   - final String? wxPayInfo = wxPayOrderInfo.getOrNull("appId");
109   - if (wxPayInfo == null) {
  110 + final Map<String, dynamic>? wxPayOrderInfo = await ShopDao.getWxPayToken(orderNo);
  111 + if (wxPayOrderInfo == null) {
110 112 showToast("微信订单创建失败");
111 113 return;
112 114 }
... ... @@ -122,18 +124,18 @@ class ShoppingBloc extends Bloc&lt;ShoppingEvent, ShoppingState&gt; {
122 124 sign: wxPayOrderInfo['sign'].toString(),
123 125 ));
124 126 } else {
125   - final Map<String, dynamic> aliPayOrderInfo = await ShopDao.getAliPayToken(orderNo);
126   - Log.d("aliPayOrderInfo=$aliPayOrderInfo type=${aliPayOrderInfo.runtimeType}");
127   - final String? aliPayInfo = aliPayOrderInfo.getOrNull("token");
  127 + final Map<String, dynamic>? aliPayOrderInfo = await ShopDao.getAliPayToken(orderNo);
  128 + debugPrint("aliPayOrderInfo=$aliPayOrderInfo type=${aliPayOrderInfo?.runtimeType}");
  129 + final String? aliPayInfo = aliPayOrderInfo?.getOrNull("token");
128 130 if (aliPayInfo == null) {
129 131 showToast("支付宝订单创建失败");
130 132 return;
131 133 }
132   - Log.d("aliPayInfo=$aliPayInfo");
  134 + debugPrint("aliPayInfo=$aliPayInfo");
133 135 ///打印aliPayOrderInfo的type
134 136 Tobias tobias = Tobias();
135 137 final Map aliPayResult = await tobias.pay(aliPayInfo);
136   - Log.d("aliPayResult=$aliPayResult");
  138 + debugPrint("aliPayResult=$aliPayResult");
137 139 // 判断resultStatus 为9000则代表支付成功
138 140 if (aliPayResult.getOrNull("resultStatus") == "9000") {
139 141 showToast("支付成功");
... ...
lib/pages/shopping/event.dart
... ... @@ -19,4 +19,8 @@ class DoPayEvent extends ShoppingEvent {
19 19 final PaymentChannel paymentChannel;
20 20  
21 21 DoPayEvent(this.productEntity, this.paymentChannel);
22   -}
23 22 \ No newline at end of file
  23 +}
  24 +
  25 +// 微信由于是异步回调方式通知支付状态,在异步回调里emitter(PaySuccessState())时报错
  26 +// !_isCompleted emit was called after an event handler completed normally
  27 +class WxPaySuccessEvent extends ShoppingEvent {}
24 28 \ No newline at end of file
... ...