main_controller.dart 5.0 KB

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