main_controller.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'package:cpt_vn/modules/job/attendance_review_list/attendance_review_page.dart';
  2. import 'package:cpt_vn/modules/job/job_list/job_list_page.dart';
  3. import 'package:cpt_vn/modules/job/sign_in_sign_out/sign_in_sign_out_page.dart';
  4. import 'package:cpt_vn/modules/labour/labour_request_list/labour_request_list_page.dart';
  5. import 'package:cpt_vn/modules/labour/labour_review_list/labour_review_page.dart';
  6. import 'package:cpt_vn/modules/labour/labour_template_list/labour_template_list_page.dart';
  7. import 'package:cpt_vn/modules/report/device_list/device_list_page.dart';
  8. import 'package:cpt_vn/modules/report/report_list/report_list_page.dart';
  9. import 'package:domain/entity/home_module.dart';
  10. import 'package:domain/entity/response/hotel_info_entity.dart';
  11. import 'package:domain/repository/auth_repository.dart';
  12. import 'package:get/get.dart';
  13. import 'package:plugin_basic/constants/app_constant.dart';
  14. import 'package:plugin_basic/service/app_config_service.dart';
  15. import 'package:plugin_basic/service/user_service.dart';
  16. import 'package:plugin_platform/engine/sp/sp_util.dart';
  17. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  18. import 'package:router/componentRouter/component_router_service.dart';
  19. import 'package:widgets/load_state_layout.dart';
  20. import 'package:widgets/widget_export.dart';
  21. import 'main_state.dart';
  22. class MainController extends GetxController {
  23. final AuthRepository _authRepository = Get.find();
  24. final MainState state = MainState();
  25. var _needShowPlaceholder = true;
  26. //页面PlaceHolder的展示
  27. LoadState loadingState = LoadState.State_Success;
  28. String? errorMessage;
  29. //刷新页面状态
  30. void changeLoadingState(LoadState state) {
  31. loadingState = state;
  32. update();
  33. }
  34. // Refresh 控制器
  35. final EasyRefreshController refreshController = EasyRefreshController(
  36. controlFinishRefresh: true,
  37. controlFinishLoad: false,
  38. );
  39. // Refresh 刷新事件
  40. Future onRefresh() async {
  41. fetchHomeData();
  42. }
  43. // 重试请求
  44. Future retryRequest() async {
  45. _needShowPlaceholder = true;
  46. fetchHomeData();
  47. }
  48. /// 获取首页的数据
  49. void fetchHomeData() async {
  50. if (_needShowPlaceholder) {
  51. changeLoadingState(LoadState.State_Loading);
  52. }
  53. //获取到数据
  54. var result = await _authRepository.fetchHotelInfo();
  55. //处理数据
  56. if (result.isSuccess) {
  57. final hotelInfo = result.data;
  58. if (hotelInfo != null) {
  59. UserService.to.hotelInfo.value = hotelInfo;
  60. _handleList(hotelInfo.menus);
  61. } else {
  62. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  63. }
  64. } else {
  65. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  66. }
  67. //停止刷新
  68. refreshController.finishRefresh();
  69. //最后赋值
  70. _needShowPlaceholder = false;
  71. }
  72. // 处理数据与展示的逻辑
  73. void _handleList(List<HotelInfoMenus>? list) {
  74. if (list != null && list.isNotEmpty) {
  75. //有数据,判断是刷新还是加载更多的数据
  76. state.datas.clear();
  77. int? isAdmin = SPUtil.getInt(AppConstant.storageIsAdmin);
  78. if (isAdmin == 1) {
  79. //如果是管理员登录,根据Key筛选需要展示的模块
  80. for (var hotelInfo in list) {
  81. if (hotelInfo.key != null) {
  82. state.datas.addAll(_filterModulesByKey(hotelInfo.key!));
  83. }
  84. }
  85. } else {
  86. //如果只是签到签出模式,手动的添加模块
  87. state.datas.addAll(_filterModulesByKey("sign"));
  88. }
  89. //更新状态
  90. changeLoadingState(LoadState.State_Success);
  91. } else {
  92. //展示无数据的布局
  93. state.datas.clear();
  94. changeLoadingState(LoadState.State_Empty);
  95. }
  96. }
  97. List<HomeModule> _filterModulesByKey(String key) {
  98. return state.modules.where((module) => module.key == key).toList();
  99. }
  100. @override
  101. void onReady() async {
  102. super.onReady();
  103. fetchHomeData();
  104. }
  105. /// 跳转到指定的模块中去
  106. void gotoModulePage(HomeModule module) {
  107. switch (module.key) {
  108. case 'labReq':
  109. LabourRequestListPage.startInstance();
  110. break;
  111. case 'jobList':
  112. //越南的工作列表
  113. JobListPage.startInstance();
  114. break;
  115. case 'sign':
  116. SignInSignOutPage.startInstance();
  117. break;
  118. case 'device':
  119. DeviceListPage.startInstance();
  120. break;
  121. case 'reqReview':
  122. LabourReviewPage.startInstance();
  123. break;
  124. case 'attReview':
  125. AttendanceReviewPage.startInstance();
  126. break;
  127. case 'template':
  128. //越南的用工请求
  129. LabourTemplateListPage.startInstance();
  130. break;
  131. case 'report':
  132. ReportListPage.startInstance();
  133. break;
  134. }
  135. }
  136. /// 跳转到设置页面
  137. void gotoSettingPage() {
  138. ComponentRouterServices.authService.startSettingPage();
  139. }
  140. }