import 'package:cpt_uk/modules/attendance/device_list/device_list_page.dart'; import 'package:cpt_uk/modules/attendance/e_attendance_list/e_attendance_list_page.dart'; import 'package:cpt_uk/modules/attendance/security_registration/security_registration_page.dart'; import 'package:cpt_uk/modules/job/job_category/job_category_page.dart'; import 'package:cpt_uk/modules/report/report_list/report_list_page.dart'; import 'package:domain/entity/home_module.dart'; import 'package:domain/repository/auth_repository.dart'; import 'package:get/get.dart'; import 'package:plugin_basic/service/user_service.dart'; import 'package:plugin_platform/engine/toast/toast_engine.dart'; import 'package:widgets/load_state_layout.dart'; import 'package:widgets/widget_export.dart'; import 'package:router/componentRouter/component_router_service.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; //展示UK全部的模块 state.datas.clear(); state.datas.addAll(state.modules); changeLoadingState(LoadState.State_Success); } 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 'job': JobCategoryPage.startInstance(); break; case 'device': UKDeviceListPage.startInstance(); break; case 'security': UKSecurityRegistrationPage.startInstance(); break; case 'attendance': UKEAttendanceListPage.startInstance(); break; case 'report': UKReportListPage.startInstance(); break; } } /// 跳转到设置页面 void gotoSettingPage() { ComponentRouterServices.authService.startSettingPage(); } }