main_controller.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import 'package:cpt_auth/modules/setting/setting_page.dart';
  2. import 'package:domain/entity/home_module.dart';
  3. import 'package:domain/entity/response/hotel_info_entity.dart';
  4. import 'package:domain/repository/auth_repository.dart';
  5. import 'package:get/get.dart';
  6. import 'package:plugin_basic/service/user_service.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'package:router/componentRouter/component_router_service.dart';
  9. import 'package:widgets/load_state_layout.dart';
  10. import 'package:widgets/widget_export.dart';
  11. import 'main_state.dart';
  12. class MainController extends GetxController {
  13. final AuthRepository _authRepository = Get.find();
  14. final MainState state = MainState();
  15. var _needShowPlaceholder = true;
  16. //页面PlaceHolder的展示
  17. LoadState loadingState = LoadState.State_Success;
  18. String? errorMessage;
  19. //刷新页面状态
  20. void changeLoadingState(LoadState state) {
  21. loadingState = state;
  22. update();
  23. }
  24. // Refresh 控制器
  25. final EasyRefreshController refreshController = EasyRefreshController(
  26. controlFinishRefresh: true,
  27. controlFinishLoad: false,
  28. );
  29. // Refresh 刷新事件
  30. Future onRefresh() async {
  31. fetchHomeData();
  32. }
  33. // 重试请求
  34. Future retryRequest() async {
  35. _needShowPlaceholder = true;
  36. fetchHomeData();
  37. }
  38. /// 获取首页的数据
  39. void fetchHomeData() async {
  40. if (_needShowPlaceholder) {
  41. changeLoadingState(LoadState.State_Loading);
  42. }
  43. //获取到数据
  44. var result = await _authRepository.fetchHotelInfo();
  45. //处理数据
  46. if (result.isSuccess) {
  47. final hotelInfo = result.data;
  48. if (hotelInfo != null) {
  49. UserService.to.hotelInfo.value = hotelInfo;
  50. _handleList(hotelInfo.menus);
  51. } else {
  52. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  53. }
  54. } else {
  55. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  56. }
  57. //停止刷新
  58. refreshController.finishRefresh();
  59. //最后赋值
  60. _needShowPlaceholder = false;
  61. }
  62. // 处理数据与展示的逻辑
  63. void _handleList(List<HotelInfoMenus>? list) {
  64. if (list != null && list.isNotEmpty) {
  65. //有数据,判断是刷新还是加载更多的数据
  66. state.datas.clear();
  67. //根据Key筛选需要展示的模块
  68. for (var hotelInfo in list) {
  69. if (hotelInfo.key != null) {
  70. state.datas.addAll(_filterModulesByKey(hotelInfo.key!));
  71. }
  72. }
  73. //更新状态
  74. changeLoadingState(LoadState.State_Success);
  75. } else {
  76. //展示无数据的布局
  77. state.datas.clear();
  78. changeLoadingState(LoadState.State_Empty);
  79. }
  80. }
  81. List<HomeModule> _filterModulesByKey(String key) {
  82. return state.modules.where((module) => module.key == key).toList();
  83. }
  84. @override
  85. void onReady() async {
  86. super.onReady();
  87. fetchHomeData();
  88. }
  89. /// 跳转到指定的模块中去
  90. void gotoModulePage(HomeModule module) {
  91. switch (module.key) {
  92. case 'labReq':
  93. ComponentRouterServices.labourService.startLabourRequestPage();
  94. break;
  95. case 'jobList':
  96. ComponentRouterServices.jobService.startJobListPage();
  97. break;
  98. case 'sign':
  99. ComponentRouterServices.jobService.startSignInSignOutPage();
  100. break;
  101. case 'devices':
  102. ToastEngine.show("进入 Devices 模块");
  103. break;
  104. case 'reqReview':
  105. ToastEngine.show("进入 Labour Request Review 模块");
  106. break;
  107. case 'attReview':
  108. ToastEngine.show("进入 Attendance Review 模块");
  109. break;
  110. case 'template':
  111. ToastEngine.show("进入 Default Job Title 模块");
  112. break;
  113. case 'report':
  114. ToastEngine.show("进入 Report 模块");
  115. break;
  116. }
  117. }
  118. /// 跳转到设置页面
  119. void gotoSettingPage() {
  120. SettingPage.startInstance();
  121. }
  122. }