Commit e0766888544dd4a07607e83bda49e70748c1950b
1 parent
caec5687
feat:解决微信支付异步回调里emitter(PaySuccessState())报错问题
Showing
3 changed files
with
30 additions
and
24 deletions
lib/common/request/dao/shop_dao.dart
@@ -8,20 +8,20 @@ class ShopDao { | @@ -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 | return await requestClient | 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 | ///获取alipay支付订单信息 | 16 | ///获取alipay支付订单信息 |
17 | - static Future getAliPayToken(String orderNo) async { | 17 | + static Future<Map<String, dynamic>?> getAliPayToken(String orderNo) async { |
18 | return await requestClient | 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 | ///获取weixin支付订单信息 | 22 | ///获取weixin支付订单信息 |
23 | - static Future getWxPayToken(String orderNo) async { | 23 | + static Future<Map<String, dynamic>?> getWxPayToken(String orderNo) async { |
24 | return await requestClient | 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<ShoppingEvent, ShoppingState> { | @@ -36,6 +36,9 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | ||
36 | on<InitEvent>(_init); | 36 | on<InitEvent>(_init); |
37 | on<ChangePaymentChannelEvent>(_changePaymentChannel); | 37 | on<ChangePaymentChannelEvent>(_changePaymentChannel); |
38 | on<DoPayEvent>(_startPay); | 38 | on<DoPayEvent>(_startPay); |
39 | + on<WxPaySuccessEvent>((event, emit) { | ||
40 | + emit(PaySuccessState()); | ||
41 | + }); | ||
39 | } | 42 | } |
40 | 43 | ||
41 | void _init(InitEvent event, Emitter<ShoppingState> emit) async { | 44 | void _init(InitEvent event, Emitter<ShoppingState> emit) async { |
@@ -55,7 +58,6 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | @@ -55,7 +58,6 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | ||
55 | 58 | ||
56 | void _startPay(DoPayEvent event, | 59 | void _startPay(DoPayEvent event, |
57 | Emitter<ShoppingState> emitter) async { | 60 | Emitter<ShoppingState> emitter) async { |
58 | - Log.d("开始支付 ${event.productEntity} ${event.paymentChannel}"); | ||
59 | //如果event.productEntity为空,中断流程并toast提示 | 61 | //如果event.productEntity为空,中断流程并toast提示 |
60 | if (event.productEntity == null) { | 62 | if (event.productEntity == null) { |
61 | showToast("商品信息为空"); | 63 | showToast("商品信息为空"); |
@@ -64,29 +66,31 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | @@ -64,29 +66,31 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | ||
64 | final productEntitySafely = event.productEntity!; | 66 | final productEntitySafely = event.productEntity!; |
65 | try { | 67 | try { |
66 | await loading(() async { | 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 | if (orderNo == null) { | 71 | if (orderNo == null) { |
71 | showToast("订单创建失败"); | 72 | showToast("订单创建失败"); |
72 | return; | 73 | return; |
73 | } | 74 | } |
74 | - Log.d("orderNo $orderNo"); | ||
75 | 75 | ||
76 | if (event.paymentChannel == PaymentChannel.wechatPay) { | 76 | if (event.paymentChannel == PaymentChannel.wechatPay) { |
77 | if (_isWxPayListenerInitialized == false) { | 77 | if (_isWxPayListenerInitialized == false) { |
78 | _isWxPayListenerInitialized = true; | 78 | _isWxPayListenerInitialized = true; |
79 | fluwx = Fluwx(); | 79 | fluwx = Fluwx(); |
80 | - fluwx?.registerApi(appId: "wx365e5a79956a450a", | 80 | + await fluwx?.registerApi(appId: "wx365e5a79956a450a", |
81 | universalLink: "https://app-api.wowenglish.com.cn/app/"); | 81 | universalLink: "https://app-api.wowenglish.com.cn/app/"); |
82 | wxPayResponseListener = (WeChatResponse response) { | 82 | wxPayResponseListener = (WeChatResponse response) { |
83 | - Log.d("wxPayResponseListener $response"); | 83 | + debugPrint("WqfPay wxPayResponseListener $response"); |
84 | if (response is WeChatPaymentResponse) { | 84 | if (response is WeChatPaymentResponse) { |
85 | if (response.errCode == 0) { | 85 | if (response.errCode == 0) { |
86 | - Log.d("wxPayResponseListener response=${response.errCode}"); | 86 | + debugPrint("WqfPay wxPayResponseListener response=${response.errCode}"); |
87 | showToast("支付成功"); | 87 | showToast("支付成功"); |
88 | // Log.d("emitter isDone=${emitter.isDone}"); | 88 | // Log.d("emitter isDone=${emitter.isDone}"); |
89 | + | ||
90 | + /// 报错!_isCompleted emit was called after an event handler completed normally | ||
89 | // emitter(PaySuccessState()); | 91 | // emitter(PaySuccessState()); |
92 | + | ||
93 | + add(WxPaySuccessEvent()); | ||
90 | } else { | 94 | } else { |
91 | showToast("支付失败"); | 95 | showToast("支付失败"); |
92 | } | 96 | } |
@@ -103,10 +107,8 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | @@ -103,10 +107,8 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | ||
103 | return; | 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 | showToast("微信订单创建失败"); | 112 | showToast("微信订单创建失败"); |
111 | return; | 113 | return; |
112 | } | 114 | } |
@@ -122,18 +124,18 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | @@ -122,18 +124,18 @@ class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> { | ||
122 | sign: wxPayOrderInfo['sign'].toString(), | 124 | sign: wxPayOrderInfo['sign'].toString(), |
123 | )); | 125 | )); |
124 | } else { | 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 | if (aliPayInfo == null) { | 130 | if (aliPayInfo == null) { |
129 | showToast("支付宝订单创建失败"); | 131 | showToast("支付宝订单创建失败"); |
130 | return; | 132 | return; |
131 | } | 133 | } |
132 | - Log.d("aliPayInfo=$aliPayInfo"); | 134 | + debugPrint("aliPayInfo=$aliPayInfo"); |
133 | ///打印aliPayOrderInfo的type | 135 | ///打印aliPayOrderInfo的type |
134 | Tobias tobias = Tobias(); | 136 | Tobias tobias = Tobias(); |
135 | final Map aliPayResult = await tobias.pay(aliPayInfo); | 137 | final Map aliPayResult = await tobias.pay(aliPayInfo); |
136 | - Log.d("aliPayResult=$aliPayResult"); | 138 | + debugPrint("aliPayResult=$aliPayResult"); |
137 | // 判断resultStatus 为9000则代表支付成功 | 139 | // 判断resultStatus 为9000则代表支付成功 |
138 | if (aliPayResult.getOrNull("resultStatus") == "9000") { | 140 | if (aliPayResult.getOrNull("resultStatus") == "9000") { |
139 | showToast("支付成功"); | 141 | showToast("支付成功"); |
lib/pages/shopping/event.dart
@@ -19,4 +19,8 @@ class DoPayEvent extends ShoppingEvent { | @@ -19,4 +19,8 @@ class DoPayEvent extends ShoppingEvent { | ||
19 | final PaymentChannel paymentChannel; | 19 | final PaymentChannel paymentChannel; |
20 | 20 | ||
21 | DoPayEvent(this.productEntity, this.paymentChannel); | 21 | DoPayEvent(this.productEntity, this.paymentChannel); |
22 | -} | ||
23 | \ No newline at end of file | 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 | \ No newline at end of file | 28 | \ No newline at end of file |