rewards_confirm_vm.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import 'package:cpt_rewards/modules/rewards_confirm/dialog/account_deactivation_dialog.dart';
  2. import 'package:cpt_rewards/modules/rewards_successful/rewards_successful_page.dart';
  3. import 'package:domain/entity/rewards_buy_entity.dart';
  4. import 'package:domain/entity/rewards_detail_entity.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  7. import 'package:plugin_platform/http/http_result.dart';
  8. import 'package:riverpod_annotation/riverpod_annotation.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  11. import 'package:widgets/load_state_layout.dart';
  12. import 'package:widgets/picker/option_pick_util.dart';
  13. import 'package:widgets/widget_export.dart';
  14. import './rewards_confirm_state.dart';
  15. import './rewards_confirm_repository.dart';
  16. part 'rewards_confirm_vm.g.dart';
  17. @riverpod
  18. class RewardsConfirmVm extends _$RewardsConfirmVm {
  19. late RewardsConfirmRepository rewardsConfirmRepository;
  20. bool _needShowPlaceholder = false; //是否展示LoadingView
  21. late int detailId;
  22. // Refresh 控制器
  23. final EasyRefreshController refreshController = EasyRefreshController(
  24. controlFinishRefresh: true, //允许刷新
  25. controlFinishLoad: true, //允许加载
  26. );
  27. RewardsConfirmState initState() {
  28. return RewardsConfirmState();
  29. }
  30. @override
  31. RewardsConfirmState build() {
  32. // 引入数据仓库
  33. rewardsConfirmRepository = ref.read(rewardsConfirmRepositoryProvider);
  34. // 初始化状态
  35. RewardsConfirmState state = initState();
  36. // 初始化列表数据
  37. return state;
  38. }
  39. // 初始化页面数据
  40. initPageData({int? id}) {
  41. Log.d("----property_news_vm-----initPageData");
  42. detailId = id!;
  43. getListData();
  44. }
  45. // 上拉加载
  46. Future onLoadData() async {
  47. Log.d("----property_news_vm-----initListData");
  48. getListData();
  49. }
  50. // 去新闻详情页
  51. void goNewsDetail(String item) {
  52. Log.d(item);
  53. // PropertyPage.startInstance(context: context, item: item);
  54. }
  55. // 重试请求
  56. Future retryRequest({int? id}) async {
  57. _needShowPlaceholder = true;
  58. getListData();
  59. }
  60. //刷新页面状态
  61. void changeLoadingState(LoadState loadState, String? errorMsg) {
  62. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  63. }
  64. // 获取list 列表数据
  65. void getListData<T>() async {
  66. Log.d("加载listData数据---------------start-----");
  67. try {
  68. //请求网络
  69. Map<String, dynamic> params = {"id": detailId};
  70. Log.d("请求参数------$params");
  71. final result =
  72. await rewardsConfirmRepository.fetchPropertyNewsList(params);
  73. Log.d("请求完成结果------${result.data}");
  74. //校验成功失败
  75. if (result.isSuccess) {
  76. state = state.copyWith(
  77. detailInfo: result.data as RewardsDetailEntity,
  78. );
  79. state = state.copyWith(
  80. amount: state.detailInfo?.point,
  81. );
  82. Log.d("123------${state.detailInfo}");
  83. changeLoadingState(LoadState.State_Success, null);
  84. } else {
  85. String errorMessage = result.errorMsg!;
  86. changeLoadingState(LoadState.State_Error, errorMessage);
  87. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  88. }
  89. } catch (e) {
  90. ToastEngine.show("Error: $e");
  91. }
  92. }
  93. void numberAdd() {
  94. int numNew = state.number! + 1;
  95. int amountNew = numNew * state.detailInfo!.point;
  96. state = state.copyWith(amount: amountNew, number: numNew);
  97. }
  98. void numberDec() {
  99. if (state.number! > 1) {
  100. int numNew = state.number! - 1;
  101. int amountNew = numNew * state.detailInfo!.point;
  102. state = state.copyWith(amount: amountNew, number: numNew);
  103. }
  104. }
  105. void rewardBuy<T>() async {
  106. Log.d("加载listData数据---------------start-----");
  107. try {
  108. //请求网络
  109. Map<String, dynamic> params = {"id": detailId, "number": state.number};
  110. Log.d("请求参数------$params");
  111. final result = await rewardsConfirmRepository.rewardBuy(params);
  112. Log.d("请求完成结果------${result.data}");
  113. //校验成功失败
  114. if (result.isSuccess) {
  115. Log.d("123------${result.list}");
  116. final data = result.list as List<RewardsBuyEntity>;
  117. Log.d("123------$data");
  118. // RewardsSuccessfulPage.startInstance(data: data);
  119. String title = data[0].reward.title;
  120. String resources = data[0].reward.resources[0];
  121. String redeemedStart = data[0].reward.redeemedStart;
  122. String redeemedEnd = data[0].reward.redeemedEnd;
  123. String redeemedDate = '$redeemedStart until $redeemedEnd';
  124. String createdAt = data[0].createdAt;
  125. RewardsSuccessfulPage.startInstance(
  126. amount: state.amount,
  127. title: title,
  128. resources: resources,
  129. redeemedDate: redeemedDate,
  130. createdAt: createdAt);
  131. } else {
  132. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  133. }
  134. } catch (e) {
  135. ToastEngine.show("Error: $e");
  136. }
  137. }
  138. void doDeleteAccount() {
  139. DialogEngine.show(widget: AccountDeactivationDialog(
  140. confirmAction: () {
  141. ToastEngine.show("点击了确定");
  142. },
  143. ));
  144. }
  145. }