157 lines
4.7 KiB
Dart
157 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:loopin/IM/controller/im_user_info_controller.dart';
|
||
import 'package:loopin/IM/im_friend_listeners.dart';
|
||
import 'package:loopin/api/common_api.dart';
|
||
import 'package:loopin/pages/my/merchant/balance/model.dart';
|
||
import 'package:loopin/service/http.dart';
|
||
import 'package:loopin/utils/wxsdk.dart';
|
||
|
||
class BalanceController extends GetxController {
|
||
/// 钱包余额
|
||
final balance = 0.0.obs;
|
||
|
||
final data = <AccountBill>[].obs;
|
||
|
||
int currentPage = 1;
|
||
|
||
final isLoading = false.obs;
|
||
|
||
/// 是否还有更多
|
||
var hasMore = true.obs;
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
getData(reset: true);
|
||
}
|
||
|
||
/// 充值
|
||
Future<void> recharge({required String money}) async {
|
||
// 获取支付参数
|
||
final data = {"orderType": "RECHARGE", "clientType": "APP", "paymentMethod": "WECHAT", "paymentClient": "APP", "money": money};
|
||
final res = await Http.post(CommonApi.addBalance, data: data);
|
||
logger.w(res);
|
||
final payParams = res['data'];
|
||
logger.w(payParams);
|
||
// 拉起支付
|
||
await Wxsdk.payWithWx(
|
||
appId: payParams['appid'],
|
||
partnerId: payParams['partnerid'],
|
||
prepayId: payParams['prepayid'],
|
||
packageValue: payParams['package'],
|
||
nonceStr: payParams['noncestr'],
|
||
timestamp: int.parse(payParams['timestamp']),
|
||
sign: payParams['sign'],
|
||
);
|
||
// 在回调结果中获取新的数据
|
||
// getData(reset: true);
|
||
}
|
||
|
||
/// 提现
|
||
Future<void> withDraw({required String money}) async {
|
||
final infoCtl = Get.find<ImUserInfoController>();
|
||
final openId = infoCtl.customInfo['openId'];
|
||
if (openId == null || openId.isEmpty) {
|
||
showDialog(
|
||
context: Get.context!,
|
||
builder: (context) {
|
||
return AlertDialog(
|
||
title: Text('微信授权'),
|
||
content: const Text('余额提现至您的微信零钱内,是否前往微信授权?', style: TextStyle(fontSize: 16.0)),
|
||
backgroundColor: Colors.white,
|
||
surfaceTintColor: Colors.white,
|
||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
|
||
elevation: 2.0,
|
||
actionsPadding: const EdgeInsets.all(15.0),
|
||
actions: [
|
||
TextButton(onPressed: () => {Get.back()}, child: Text('取消', style: TextStyle(color: Colors.red))),
|
||
TextButton(
|
||
onPressed: () async {
|
||
//去授权
|
||
await Wxsdk.login();
|
||
Get.back();
|
||
},
|
||
child: Text('确认')),
|
||
],
|
||
);
|
||
},
|
||
);
|
||
return;
|
||
}
|
||
//
|
||
// final res = await Http.post(CommonApi.withdraw, data: {
|
||
// "money": money,
|
||
// "method": "1",
|
||
// });
|
||
// MyDialog.('提现成功,系统将在一个工作日内将余额提现至您绑定的微信内');
|
||
showDialog(
|
||
context: Get.context!,
|
||
builder: (context) {
|
||
return AlertDialog(
|
||
title: Text('提现成功'),
|
||
content: const Text('系统将在一个工作日内将余额提现至您绑定的微信内', style: TextStyle(fontSize: 16.0)),
|
||
backgroundColor: Colors.white,
|
||
surfaceTintColor: Colors.white,
|
||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
|
||
elevation: 2.0,
|
||
actionsPadding: const EdgeInsets.all(15.0),
|
||
actions: [
|
||
TextButton(onPressed: () => {Get.back()}, child: Text('确认')),
|
||
],
|
||
);
|
||
},
|
||
);
|
||
getData(reset: true);
|
||
}
|
||
|
||
/// 分页数据
|
||
Future<void> getData({bool reset = false}) async {
|
||
if (isLoading.value) {
|
||
logger.w('正在加载中,跳过');
|
||
return;
|
||
}
|
||
|
||
isLoading.value = true;
|
||
logger.w('开始加载数据,reset: $reset, currentPage: $currentPage');
|
||
|
||
await Future.delayed(const Duration(seconds: 2));
|
||
|
||
if (reset) {
|
||
logger.w('重置数据');
|
||
currentPage = 1;
|
||
data.clear();
|
||
hasMore.value = true;
|
||
}
|
||
|
||
List<AccountBill> newData = List.generate(
|
||
10,
|
||
(index) {
|
||
int id = currentPage * 10 + index + 1;
|
||
// logger.w('生成数据: id=$id');
|
||
return AccountBill(
|
||
id: id,
|
||
source: '来源 $currentPage-${index + 1}',
|
||
changeAmount: (index + 1) * 10.0,
|
||
changeType: index % 2 + 1,
|
||
createTime: DateTime.now().toString(),
|
||
);
|
||
},
|
||
);
|
||
|
||
data.addAll(newData);
|
||
logger.w('添加了 ${newData.length} 条数据,总数据量: ${data.length}');
|
||
|
||
currentPage++;
|
||
logger.w('页码增加到: $currentPage');
|
||
|
||
if (currentPage > 5) {
|
||
hasMore.value = false;
|
||
logger.w('没有更多数据了');
|
||
}
|
||
|
||
isLoading.value = false;
|
||
logger.w('加载完成');
|
||
}
|
||
}
|