main_controller.dart 4.2 KB

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