main_controller.dart 4.6 KB

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