fix: 处理order_status异步
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user