浏览代码

[fix]修复支付toast多次提示的问题

zk 1 年之前
父节点
当前提交
b32f5ba4cf

+ 3 - 1
lib/module/store/controller.dart

@@ -13,6 +13,7 @@ import 'package:electronic_assistant/sdk/pay/applepay/apple_pay_info.dart';
 import 'package:electronic_assistant/sdk/pay/code/agile_pay_code.dart';
 import 'package:electronic_assistant/utils/http_handler.dart';
 import 'package:electronic_assistant/utils/toast_util.dart';
+import 'package:flutter/widgets.dart';
 import 'package:get/get.dart';
 import '../../data/api/response/order_pay_response.dart';
 import '../../data/api/response/user_info_response.dart';
@@ -143,10 +144,12 @@ class StoreController extends BaseController implements PaymentStatusCallback {
           LoadingDialog.hide();
         });
       }, payError: (int error, String? errorMessage) {
+        debugPrint('zk---payError: $error, $errorMessage');
         errorPayToast(error);
         errorEventReport(payMethod);
         LoadingDialog.hide();
       }, error: (int errno, String? error) {
+        debugPrint('zk---error: $errno, $error');
         errorPayToast(errno);
         errorEventReport(payMethod);
         LoadingDialog.hide();
@@ -199,7 +202,6 @@ class StoreController extends BaseController implements PaymentStatusCallback {
   @override
   void onClose() {
     super.onClose();
-    AgilePay.dispose();
     paymentStatusManager.unregisterPaymentSuccessCallback(this);
   }
 

+ 0 - 3
lib/sdk/pay/agile_pay.dart

@@ -43,7 +43,4 @@ class AgilePay {
     }
   }
 
-  static void dispose() {
-    realPay?.dispose();
-  }
 }

+ 2 - 1
lib/sdk/pay/alipay/alipay.dart

@@ -34,6 +34,8 @@ class Alipay extends AgilePayStateInfo implements IAgilePay {
     } catch (e) {
       sendError(AgilePayCode.payCodeOtherError,
           AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+    } finally {
+      dispose();
     }
   }
 
@@ -50,7 +52,6 @@ class Alipay extends AgilePayStateInfo implements IAgilePay {
     }
   }
 
-  @override
   void dispose() {
     _paySubs.cancel();
   }

+ 1 - 4
lib/sdk/pay/assist/apple_or_google_pay.dart

@@ -23,6 +23,7 @@ abstract class AppleOrGooglePay extends AgilePayStateInfo {
         _iap.purchaseStream.listen((purchaseDetailsList) {
       listenToPurchaseUpdated(purchaseDetailsList);
     }, onDone: () {
+      debugPrint('agilePay-appleOrGooglePay---> onDone');
       _purchaseUpdatedSubscription.cancel();
     }, onError: (error) {});
   }
@@ -96,10 +97,6 @@ abstract class AppleOrGooglePay extends AgilePayStateInfo {
     return _iap.buyNonConsumable(purchaseParam: purchaseParam);
   }
 
-  void dispose() {
-    _purchaseUpdatedSubscription.cancel();
-  }
-
   void listenToPurchaseUpdated(
       List<PurchaseDetails> purchaseDetailsList) async {
     for (var purchaseDetails in purchaseDetailsList) {

+ 0 - 2
lib/sdk/pay/listener/i_agile_pay.dart

@@ -3,7 +3,5 @@ import 'agile_pay_state.dart';
 abstract class IAgilePay {
   void pay();
 
-  void dispose();
-
   void setPayListener(AgilePayState agilePayState);
 }

+ 17 - 11
lib/sdk/pay/wxpay/wechat_pay.dart

@@ -18,17 +18,24 @@ class WechatPay extends AgilePayStateInfo implements IAgilePay {
   }
 
   void _listenResp(WechatResp resp) {
-    if (resp is WechatPayResp) {
-      final String content = 'pay: ${resp.errorCode} ${resp.errorMsg}';
-      debugPrint('agilePay-wechat---> $content');
-      if (resp.errorCode == WechatResp.kErrorCodeSuccess) {
-        sendPaySuccess(resp.returnKey);
-      } else if (resp.errorCode == WechatResp.kErrorCodeUserCancel) {
-        sendError(AgilePayCode.payCodeCancelError,
-            AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
-      } else {
-        sendError(resp.errorCode, resp.errorMsg);
+    try {
+      if (resp is WechatPayResp) {
+        final String content = 'pay: ${resp.errorCode} ${resp.errorMsg}';
+        debugPrint('agilePay-wechat---> $content');
+        if (resp.errorCode == WechatResp.kErrorCodeSuccess) {
+          sendPaySuccess(resp.returnKey);
+        } else if (resp.errorCode == WechatResp.kErrorCodeUserCancel) {
+          sendError(AgilePayCode.payCodeCancelError,
+              AgilePayCode.getMessageByCode(AgilePayCode.payCodeCancelError));
+        } else {
+          sendError(resp.errorCode, resp.errorMsg);
+        }
       }
+    } catch (e) {
+      sendError(AgilePayCode.payCodeOtherError,
+          AgilePayCode.getMessageByCode(AgilePayCode.payCodeOtherError));
+    } finally {
+      dispose();
     }
   }
 
@@ -75,7 +82,6 @@ class WechatPay extends AgilePayStateInfo implements IAgilePay {
     return WechatKitPlatform.instance.isSupportApi();
   }
 
-  @override
   void dispose() {
     _respSubs.cancel();
   }

+ 4 - 4
lib/utils/toast_util.dart

@@ -9,14 +9,14 @@ class ToastUtil {
       SmartToastType? displayType = SmartToastType.normal,
       bool? addPostFrame}) {
     if (msg != null) {
-      if (addPostFrame == true) {
+      if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.idle) {
+        SmartDialog.showToast(msg,
+            displayType: displayType, displayTime: displayTime);
+      } else {
         SchedulerBinding.instance.addPostFrameCallback((_) {
           SmartDialog.showToast(msg,
               displayType: displayType, displayTime: displayTime);
         });
-      } else {
-        SmartDialog.showToast(msg,
-            displayType: displayType, displayTime: displayTime);
       }
     }
   }