You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
5.5 KiB
206 lines
5.5 KiB
import 'package:dating_touchme_app/model/mine/rose_data.dart';
|
|
import 'package:dating_touchme_app/network/user_api.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
import 'package:fluwx/fluwx.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class RoseController extends GetxController with WidgetsBindingObserver {
|
|
|
|
// UserApi实例
|
|
late UserApi _userApi;
|
|
final roseList = <RoseData>[].obs;
|
|
|
|
final roseNum = 0.obs;
|
|
|
|
final payChecked = false.obs;
|
|
|
|
final activePay = 0.obs;
|
|
|
|
final Fluwx fluwx = Fluwx();
|
|
|
|
final appState = AppLifecycleState.resumed.obs;
|
|
|
|
final orderId = "".obs;
|
|
|
|
final launchWX = false.obs;
|
|
|
|
final count = 0.obs;
|
|
|
|
changePayActive(int index){
|
|
activePay.value = index;
|
|
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
_userApi = Get.find<UserApi>();
|
|
|
|
getRoseList();
|
|
getRoseNum();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
super.onClose();
|
|
WidgetsBinding.instance.removeObserver(this); // 记得取消
|
|
}
|
|
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
appState.value = state;
|
|
|
|
switch (state) {
|
|
case AppLifecycleState.resumed:
|
|
// ✅ 回到前台
|
|
print('App 回到前台');
|
|
if(launchWX.value){
|
|
SmartDialog.showLoading(msg: '正在验证支付结果...');
|
|
getPaymentDetail();
|
|
}
|
|
break;
|
|
case AppLifecycleState.paused:
|
|
// ✅ 进入后台(Android 常见)
|
|
print('App 进入后台');
|
|
// 比如:暂停播放、保存状态等
|
|
break;
|
|
case AppLifecycleState.inactive:
|
|
// iOS/切换页面时会短暂进入
|
|
print('App inactive');
|
|
break;
|
|
case AppLifecycleState.detached:
|
|
print('App detached');
|
|
break;
|
|
case AppLifecycleState.hidden:
|
|
// 新版本多了这个状态,有的话也顺便处理一下
|
|
print('App hidden');
|
|
break;
|
|
}
|
|
}
|
|
|
|
getPaymentDetail() async {
|
|
try {
|
|
final response = await _userApi.getPaymentOrderDetail(
|
|
id: orderId.value
|
|
);
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
final data = response.data.data;
|
|
if(data?.status == 3){
|
|
SmartDialog.dismiss();
|
|
SmartDialog.showToast('支付成功');
|
|
launchWX.value = false;
|
|
count.value = 0;
|
|
getRoseNum();
|
|
} else {
|
|
count.value += 1;
|
|
if(count.value < 3){
|
|
await Future.delayed(Duration(seconds: 1));
|
|
getPaymentDetail();
|
|
} else {
|
|
SmartDialog.dismiss();
|
|
SmartDialog.showToast('支付失败');
|
|
launchWX.value = false;
|
|
count.value = 0;
|
|
}
|
|
}
|
|
} else {
|
|
count.value += 1;
|
|
if(count.value < 3){
|
|
await Future.delayed(Duration(seconds: 1));
|
|
getPaymentDetail();
|
|
} else {
|
|
SmartDialog.dismiss();
|
|
SmartDialog.showToast('支付失败');
|
|
launchWX.value = false;
|
|
count.value = 0;
|
|
}
|
|
}
|
|
} catch (e){
|
|
if(count.value < 3){
|
|
await Future.delayed(Duration(seconds: 1));
|
|
getPaymentDetail();
|
|
} else {
|
|
SmartDialog.dismiss();
|
|
print('支付失败: $e');
|
|
SmartDialog.showToast('支付失败');
|
|
launchWX.value = false;
|
|
count.value = 0;
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
getRoseList() async {
|
|
try{
|
|
final response = await _userApi.listVirtualCurrencyProduct({});
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
final data = response.data.data;
|
|
|
|
roseList.addAll(data?.toList() ?? []);
|
|
} else {
|
|
// 响应失败,抛出异常
|
|
final errorMessage = response.data.message.isNotEmpty
|
|
? response.data.message
|
|
: '获取数据失败';
|
|
throw Exception(errorMessage);
|
|
}
|
|
} catch (e) {
|
|
print('玫瑰列表获取失败: $e');
|
|
SmartDialog.showToast('玫瑰列表获取失败');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
getRoseNum() async {
|
|
try {
|
|
final response = await _userApi.getVirtualAccount({});
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
final data = response.data.data;
|
|
roseNum.value = data?.balance ?? 0;
|
|
} else {
|
|
roseNum.value = 0;
|
|
}
|
|
} catch (e){
|
|
print('玫瑰数量取失败: $e');
|
|
SmartDialog.showToast('玫瑰数量取失败');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
submitOrder() async {
|
|
try {
|
|
print(roseList[activePay.value].productDesc);
|
|
final response = await _userApi.submitOrder({
|
|
"productSpecId": roseList[activePay.value].productSpecId
|
|
});
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
final data = response.data.data!;
|
|
orderId.value = data.paymentOrderId ?? "";
|
|
fluwx.open(target: MiniProgram(
|
|
username: 'gh_9ea8d46add6f',
|
|
miniProgramType: WXMiniProgramType.preview,
|
|
path:"pages/index/index?amount=${roseList[activePay.value].unitSellingPrice}&paymentOrderId=${data.paymentOrderId}&url=touchme-fee"
|
|
));
|
|
launchWX.value = true;
|
|
|
|
SmartDialog.showToast('下单成功');
|
|
} else {
|
|
// 响应失败,抛出异常
|
|
final errorMessage = response.data.message.isNotEmpty
|
|
? response.data.message
|
|
: '获取数据失败';
|
|
throw Exception(errorMessage);
|
|
}
|
|
} catch (e) {
|
|
print('玫瑰列表获取失败: $e');
|
|
SmartDialog.showToast('下单失败');
|
|
rethrow;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|