import 'package:cpt_auth/modules/setting/setting_page.dart'; import 'package:domain/entity/home_module.dart'; import 'package:domain/entity/response/hotel_info_entity.dart'; import 'package:domain/repository/auth_repository.dart'; import 'package:get/get.dart'; import 'package:plugin_basic/constants/app_constant.dart'; import 'package:plugin_basic/service/app_config_service.dart'; import 'package:plugin_basic/service/user_service.dart'; import 'package:plugin_platform/engine/sp/sp_util.dart'; import 'package:plugin_platform/engine/toast/toast_engine.dart'; import 'package:router/componentRouter/component_router_service.dart'; import 'package:widgets/load_state_layout.dart'; import 'package:widgets/widget_export.dart'; import 'main_state.dart'; class MainController extends GetxController { final AuthRepository _authRepository = Get.find(); final MainState state = MainState(); var _needShowPlaceholder = true; //页面PlaceHolder的展示 LoadState loadingState = LoadState.State_Success; String? errorMessage; //刷新页面状态 void changeLoadingState(LoadState state) { loadingState = state; update(); } // Refresh 控制器 final EasyRefreshController refreshController = EasyRefreshController( controlFinishRefresh: true, controlFinishLoad: false, ); // Refresh 刷新事件 Future onRefresh() async { fetchHomeData(); } // 重试请求 Future retryRequest() async { _needShowPlaceholder = true; fetchHomeData(); } /// 获取首页的数据 void fetchHomeData() async { if (_needShowPlaceholder) { changeLoadingState(LoadState.State_Loading); } //获取到数据 var result = await _authRepository.fetchHotelInfo(); //处理数据 if (result.isSuccess) { final hotelInfo = result.data; if (hotelInfo != null) { UserService.to.hotelInfo.value = hotelInfo; _handleList(hotelInfo.menus); } else { ToastEngine.show(result.errorMsg ?? "Network Load Error".tr); } } else { ToastEngine.show(result.errorMsg ?? "Network Load Error".tr); } //停止刷新 refreshController.finishRefresh(); //最后赋值 _needShowPlaceholder = false; } // 处理数据与展示的逻辑 void _handleList(List? list) { if (list != null && list.isNotEmpty) { //有数据,判断是刷新还是加载更多的数据 state.datas.clear(); int? isAdmin = SPUtil.getInt(AppConstant.storageIsAdmin); if (isAdmin == 1) { //如果是管理员登录,根据Key筛选需要展示的模块 for (var hotelInfo in list) { if (hotelInfo.key != null) { state.datas.addAll(_filterModulesByKey(hotelInfo.key!)); } } } else { //如果只是签到签出模式,手动的添加模块 state.datas.addAll(_filterModulesByKey("sign")); } //更新状态 changeLoadingState(LoadState.State_Success); } else { //展示无数据的布局 state.datas.clear(); changeLoadingState(LoadState.State_Empty); } } List _filterModulesByKey(String key) { return state.modules.where((module) => module.key == key).toList(); } @override void onReady() async { super.onReady(); fetchHomeData(); } /// 跳转到指定的模块中去 void gotoModulePage(HomeModule module) { switch (module.key) { case 'labReq': if (ConfigService.to.selectCountry.value == 1) { //新加坡的用工请求 ComponentRouterServices.labourSGService.startLabourRequestPage(); } else { //越南的用工请求 ComponentRouterServices.labourService.startLabourRequestPage(); } break; case 'jobList': if (ConfigService.to.selectCountry.value == 1) { ComponentRouterServices.labourSGService.startJobListPage(); } else { //越南的工作列表 ComponentRouterServices.jobService.startJobListPage(); } break; case 'sign': ComponentRouterServices.jobService.startSignInSignOutPage(); break; case 'reviseList': ComponentRouterServices.jobSGService.startReviseListPage(); break; case 'device': ComponentRouterServices.reportService.startDeviceListPage(); break; case 'reqReview': ComponentRouterServices.labourService.startLabourReviewPage(); break; case 'attReview': ComponentRouterServices.jobService.startAttendanceReviewPage(); break; case 'template': //越南的用工请求 ComponentRouterServices.labourService.startLabourTemplatePage(); break; case 'jobTitle': //新加坡的用工请求 ComponentRouterServices.labourSGService.startJobTitlePage(); break; case 'report': ComponentRouterServices.reportService.startReportMainPage(); break; } } /// 跳转到设置页面 void gotoSettingPage() { SettingPage.startInstance(); } }