main_controller.dart 5.0 KB

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