fix: 处理order_status异步
This commit is contained in:
@@ -104,8 +104,9 @@ class KRDeleteAccountController extends GetxController {
|
||||
KRCommonUtil.kr_showToast(error.msg);
|
||||
},
|
||||
(success) {
|
||||
KRCommonUtil.kr_showToast('account.deleteSuccess'.tr);
|
||||
KRAppRunData.getInstance().kr_loginOut();
|
||||
// KRCommonUtil.kr_showToast('account.deleteSuccess'.tr);
|
||||
KRCommonUtil.kr_showLoading();
|
||||
KRAppRunData.getInstance().kr_loginOut_loading();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -120,21 +120,6 @@ class KRDeleteAccountView extends GetView<KRDeleteAccountController> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Obx(() {
|
||||
final _ = KRAppRunData.getInstance().kr_isLogin.value;
|
||||
final email = KRAppRunData.getInstance().kr_account.value ?? '';
|
||||
return Text(
|
||||
email.isNotEmpty ? email : '',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 20.sp,
|
||||
fontWeight: FontWeight.bold,
|
||||
height: 0.9,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
);
|
||||
}),
|
||||
Obx(() {
|
||||
final account = KRAppRunData.getInstance().kr_account.value;
|
||||
final isDeviceLogin =
|
||||
@@ -145,10 +130,12 @@ class KRDeleteAccountView extends GetView<KRDeleteAccountController> {
|
||||
return Text(
|
||||
accountText,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withOpacity(0.85),
|
||||
fontSize: 14.sp,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
);
|
||||
}),
|
||||
KRSubscriptionExpiryText(
|
||||
@@ -156,7 +143,7 @@ class KRDeleteAccountView extends GetView<KRDeleteAccountController> {
|
||||
.kr_subscribeService.kr_currentSubscribe.value?.expireTime,
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12.sp,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -681,6 +681,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
} else {
|
||||
// kr_updateBottomPanelHeight();
|
||||
}
|
||||
_checkQuickConnectAutoStart();
|
||||
_kr_testLatencyWithoutVpn();
|
||||
break;
|
||||
case KRSubscribeServiceStatus.kr_none:
|
||||
@@ -2477,6 +2478,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
|
||||
// 强制更新UI
|
||||
update();
|
||||
print('isQuickConnect$isQuickConnect');
|
||||
if (isQuickConnect == true) {
|
||||
_checkQuickConnectAutoStart();
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ class KROrderStatusController extends GetxController {
|
||||
/// 是否正在加载
|
||||
final RxBool kr_isLoading = true.obs;
|
||||
|
||||
final RxBool kr_isRefreshingData = false.obs;
|
||||
|
||||
/// 支付URL
|
||||
final String kr_paymentUrl = Get.arguments['url'] as String? ?? '';
|
||||
|
||||
@@ -205,8 +207,9 @@ class KROrderStatusController extends GetxController {
|
||||
// 使用公开接口查询订单状态
|
||||
final result = await kr_subscribeApi.kr_queryOrderStatus(kr_order);
|
||||
|
||||
result.fold(
|
||||
(error) {
|
||||
// 注意:这里改为 async 回调以支持内部的 await
|
||||
await result.fold(
|
||||
(error) async {
|
||||
if (kDebugMode) {
|
||||
print('❌ 查询失败: ${error.msg}');
|
||||
}
|
||||
@@ -215,46 +218,29 @@ class KROrderStatusController extends GetxController {
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.checkFailedTitle;
|
||||
kr_statusDescription.value = AppTranslations.kr_orderStatus.checkFailedDescription;
|
||||
},
|
||||
(kr_orderStatus) {
|
||||
// 保存订单创建时间(用于倒计时)
|
||||
(kr_orderStatus) async {
|
||||
// 1. 保存订单创建时间(用于倒计时)
|
||||
if (kr_orderCreatedAt == null && kr_orderStatus.kr_createdAt > 0) {
|
||||
// 判断是秒级还是毫秒级时间戳
|
||||
// 如果时间戳大于10位数字,说明是毫秒级
|
||||
final timestamp = kr_orderStatus.kr_createdAt;
|
||||
final isMilliseconds = timestamp > 10000000000; // 大于10位数说明是毫秒级
|
||||
|
||||
final isMilliseconds = timestamp > 10000000000;
|
||||
kr_orderCreatedAt = DateTime.fromMillisecondsSinceEpoch(
|
||||
isMilliseconds ? timestamp : timestamp * 1000
|
||||
isMilliseconds ? timestamp : timestamp * 1000
|
||||
);
|
||||
|
||||
if (kDebugMode) {
|
||||
print('📅 订单创建时间: ${kr_orderCreatedAt}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📅 原始时间戳: $timestamp');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📅 时间戳类型: ${isMilliseconds ? "毫秒级" : "秒级"}');
|
||||
}
|
||||
if (kDebugMode) {
|
||||
print('📅 转换后时间戳(ms): ${kr_orderCreatedAt!.millisecondsSinceEpoch}');
|
||||
}
|
||||
}
|
||||
|
||||
if (kDebugMode) {
|
||||
print('📊 订单状态: ${kr_orderStatus.kr_status} (${_getStatusName(kr_orderStatus.kr_status)})');
|
||||
}
|
||||
|
||||
// 2. 根据状态处理业务逻辑
|
||||
switch (kr_orderStatus.kr_status) {
|
||||
case kr_statusPending:
|
||||
// 待支付状态,继续轮询
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.pendingTitle;
|
||||
kr_statusDescription.value = '${AppTranslations.kr_orderStatus.pendingDescription}\n${AppTranslations.kr_orderStatus.remainingTime}: ${kr_formattedCountdown.value}';
|
||||
kr_statusIcon.value = 'payment_success';
|
||||
break;
|
||||
|
||||
case kr_statusPaid:
|
||||
// 已支付状态,继续轮询直到完成
|
||||
if (kDebugMode) {
|
||||
print('✅ 订单已支付,等待确认...');
|
||||
}
|
||||
@@ -264,64 +250,56 @@ class KROrderStatusController extends GetxController {
|
||||
break;
|
||||
|
||||
case kr_statusFinished:
|
||||
// 订单完成
|
||||
// 订单完成流程
|
||||
if (kDebugMode) {
|
||||
print('🎉 订单完成!停止轮询');
|
||||
}
|
||||
kr_isPaymentSuccess.value = true;
|
||||
kr_isLoading.value = false;
|
||||
|
||||
// 停止所有定时器
|
||||
kr_timer?.cancel();
|
||||
kr_countdownTimer?.cancel();
|
||||
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.successTitle;
|
||||
kr_statusDescription.value = AppTranslations.kr_orderStatus.successDescription;
|
||||
kr_statusIcon.value = 'payment_success';
|
||||
|
||||
// kr_subscribeService.kr_fetchAvailableSubscribes();
|
||||
// 🔧 修复:移除自动发送事件,改为在用户点击返回按钮时发送
|
||||
// 这样可以确保导航完成后才发送事件,避免旧控制器被销毁时收到事件
|
||||
// KREventBus().kr_sendMessage(KRMessageType.kr_payment);
|
||||
// 刷新订阅信息
|
||||
kr_isRefreshingData.value = true;
|
||||
try {
|
||||
await kr_subscribeService.kr_fetchAvailableSubscribes();
|
||||
} catch (e) {
|
||||
KRLogUtil.kr_e('刷新订阅信息异常: $e', tag: 'OrderStatusController');
|
||||
} finally {
|
||||
kr_isRefreshingData.value = false;
|
||||
}
|
||||
|
||||
if (kDebugMode) {
|
||||
print('✅ [OrderStatus] 订单完成,等待用户返回首页时触发刷新');
|
||||
print('✅ [OrderStatus] 订单完成,数据已同步');
|
||||
}
|
||||
break;
|
||||
|
||||
case kr_statusClose:
|
||||
// 订单已关闭
|
||||
if (kDebugMode) {
|
||||
print('❌ 订单已关闭');
|
||||
}
|
||||
kr_isLoading.value = false;
|
||||
kr_timer?.cancel();
|
||||
kr_countdownTimer?.cancel();
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.closedTitle;
|
||||
kr_statusDescription.value = AppTranslations.kr_orderStatus.closedDescription;
|
||||
kr_statusIcon.value = 'payment_success';
|
||||
break;
|
||||
|
||||
case kr_statusFailed:
|
||||
// 支付失败
|
||||
if (kDebugMode) {
|
||||
print('❌ 支付失败');
|
||||
}
|
||||
kr_isLoading.value = false;
|
||||
kr_timer?.cancel();
|
||||
kr_countdownTimer?.cancel();
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.failedTitle;
|
||||
kr_statusDescription.value = AppTranslations.kr_orderStatus.failedDescription;
|
||||
kr_statusTitle.value = kr_orderStatus.kr_status == kr_statusClose
|
||||
? AppTranslations.kr_orderStatus.closedTitle
|
||||
: AppTranslations.kr_orderStatus.failedTitle;
|
||||
kr_statusDescription.value = kr_orderStatus.kr_status == kr_statusClose
|
||||
? AppTranslations.kr_orderStatus.closedDescription
|
||||
: AppTranslations.kr_orderStatus.failedDescription;
|
||||
kr_statusIcon.value = 'payment_success';
|
||||
break;
|
||||
|
||||
default:
|
||||
// 未知状态
|
||||
if (kDebugMode) {
|
||||
print('⚠️ 未知状态: ${kr_orderStatus.kr_status}');
|
||||
}
|
||||
kr_isLoading.value = false;
|
||||
kr_timer?.cancel();
|
||||
kr_countdownTimer?.cancel();
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.unknownTitle;
|
||||
kr_statusDescription.value = AppTranslations.kr_orderStatus.unknownDescription;
|
||||
kr_statusIcon.value = 'payment_success';
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -332,7 +310,7 @@ class KROrderStatusController extends GetxController {
|
||||
if (kDebugMode) {
|
||||
print('❌ 异常: $error');
|
||||
}
|
||||
KRLogUtil.kr_e('检查支付状态失败: $error', tag: 'OrderStatusController');
|
||||
KRLogUtil.kr_e('检查支付状态程序异常: $error', tag: 'OrderStatusController');
|
||||
kr_isLoading.value = false;
|
||||
kr_statusTitle.value = AppTranslations.kr_orderStatus.checkFailedTitle;
|
||||
kr_statusDescription.value = AppTranslations.kr_orderStatus.checkFailedDescription;
|
||||
|
||||
@@ -101,71 +101,70 @@ class KROrderStatusView extends GetView<KROrderStatusController> {
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 48.h,
|
||||
child: ElevatedButton(
|
||||
onPressed: () async {
|
||||
// 1. 先触发业务层的数据刷新请求(确保内存数据是最新的)
|
||||
// 假设 kr_subscribeService.kr_fetchAvailableSubscribes() 返回 Future
|
||||
await controller.kr_subscribeService.kr_fetchAvailableSubscribes();
|
||||
child: Obx(() {
|
||||
// 获取当前的刷新状态
|
||||
final bool isRefreshing = controller.kr_isRefreshingData.value;
|
||||
|
||||
// 2. 发送事件总线通知
|
||||
KREventBus().kr_sendMessage(KRMessageType.kr_payment);
|
||||
return ElevatedButton(
|
||||
// 如果正在刷新数据,则禁用点击(设为 null)
|
||||
onPressed: isRefreshing
|
||||
? null
|
||||
: () async {
|
||||
// 1. 先触发业务层的数据刷新请求
|
||||
controller.kr_subscribeService.kr_fetchAvailableSubscribes();
|
||||
|
||||
// 3. 关键:稍微延迟一下再执行 offAllNamed
|
||||
// 这样可以确保 EventBus 的消息被其他观察者消费完毕,并让网络请求有时间在内存中沉淀
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// 4. 返回到首页(清空导航栈)
|
||||
Get.offAllNamed(Routes.KR_HOME);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.white,
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.r),
|
||||
// 2. 发送事件总线通知
|
||||
KREventBus().kr_sendMessage(KRMessageType.kr_payment);
|
||||
// 4. 返回到首页
|
||||
Get.offAllNamed(Routes.KR_HOME);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: isRefreshing ? Colors.grey[300] : Colors.white,
|
||||
foregroundColor: Colors.black,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.r),
|
||||
),
|
||||
elevation: 0,
|
||||
// 禁用时的样式
|
||||
disabledBackgroundColor: Colors.grey[200],
|
||||
),
|
||||
elevation: 0,
|
||||
),
|
||||
child: Text(
|
||||
AppTranslations.kr_orderStatus.backToHome,
|
||||
style: KrAppTextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.black,
|
||||
child: isRefreshing
|
||||
? Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Loading 动画
|
||||
SizedBox(
|
||||
height: 20.w,
|
||||
width: 20.w,
|
||||
child: CircularProgressIndicator(
|
||||
color: Colors.black54,
|
||||
strokeWidth: 2.w,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 12.w),
|
||||
// 正在刷新的提示文字
|
||||
Text(
|
||||
"订阅更新中...", // 或者从 AppTranslations 获取
|
||||
style: KrAppTextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.black54,
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Text(
|
||||
AppTranslations.kr_orderStatus.backToHome,
|
||||
style: KrAppTextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.black,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
SizedBox(height: 12.h),
|
||||
// 查看订阅按钮
|
||||
// SizedBox(
|
||||
// width: double.infinity,
|
||||
// height: 48.h,
|
||||
// child: OutlinedButton(
|
||||
// onPressed: () {
|
||||
// // 返回到首页并切换到订阅页面
|
||||
// Get.offAllNamed('/');
|
||||
// // 可以通过事件总线发送消息切换tab
|
||||
// },
|
||||
// style: OutlinedButton.styleFrom(
|
||||
// foregroundColor: Theme.of(Get.context!).primaryColor,
|
||||
// side: BorderSide(
|
||||
// color: Theme.of(Get.context!).primaryColor,
|
||||
// width: 1.5,
|
||||
// ),
|
||||
// shape: RoundedRectangleBorder(
|
||||
// borderRadius: BorderRadius.circular(12.r),
|
||||
// ),
|
||||
// ),
|
||||
// child: Text(
|
||||
// AppTranslations.kr_orderStatus.viewSubscription,
|
||||
// style: KrAppTextStyle(
|
||||
// fontSize: 16,
|
||||
// fontWeight: FontWeight.w600,
|
||||
// color: Colors.black,
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user