From 9527dcaee23fc22718222337147ed4775e593717 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Wed, 10 Apr 2024 17:38:23 +0800 Subject: [PATCH] feat: router permission --- lib/constants/constants.dart | 2 +- lib/router/auth_middleware.dart | 19 +++++++++ lib/{constants => router}/router.dart | 40 ++++++++++++++++--- lib/router/router.util.dart | 23 +++++++++++ lib/screens/hr_manage/hr_manage.dart | 2 +- lib/screens/workbench/workbench.dart | 7 ++-- .../workbench/workbench_controller.dart | 3 +- lib/store/resource.store.dart | 10 ++++- lib/util/router.util.dart | 12 ------ 9 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 lib/router/auth_middleware.dart rename lib/{constants => router}/router.dart (54%) create mode 100644 lib/router/router.util.dart delete mode 100644 lib/util/router.util.dart diff --git a/lib/constants/constants.dart b/lib/constants/constants.dart index 437b5ba..62adb00 100644 --- a/lib/constants/constants.dart +++ b/lib/constants/constants.dart @@ -4,6 +4,6 @@ export 'data.dart'; export 'enum.dart'; export 'global_url.dart'; export 'cache_key.dart'; -export 'router.dart'; +export '../router/router.dart'; export 'text_enum.dart'; export 'system_param_config.dart'; diff --git a/lib/router/auth_middleware.dart b/lib/router/auth_middleware.dart new file mode 100644 index 0000000..6f67d18 --- /dev/null +++ b/lib/router/auth_middleware.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; +import 'package:sk_base_mobile/store/resource.store.dart'; +import 'package:sk_base_mobile/util/util.dart'; + +class AuthMiddleware extends GetMiddleware { + @override + RouteSettings? redirect(String? route) { + bool isExsited = ResourceService.to.resources + .firstWhereOrNull((element) => element.path == route) != + null; + + if (!isExsited) { + SnackBarUtil().info('您没有权限'); + return null; + } + return null; + } +} diff --git a/lib/constants/router.dart b/lib/router/router.dart similarity index 54% rename from lib/constants/router.dart rename to lib/router/router.dart index 4ddbfde..9eb24f8 100644 --- a/lib/constants/router.dart +++ b/lib/router/router.dart @@ -1,5 +1,6 @@ import 'package:get/get.dart'; import 'package:sk_base_mobile/models/user_info.model.dart'; +import 'package:sk_base_mobile/router/auth_middleware.dart'; import 'package:sk_base_mobile/screens/hr_manage/components/employee_detail.dart'; import 'package:sk_base_mobile/screens/hr_manage/hr_manage.dart'; import 'package:sk_base_mobile/screens/inventory/inventory.dart'; @@ -13,18 +14,45 @@ class RouteConfig { static const String home = "/"; static const String login = '/login'; static const String userinfo = '/userinfo'; - static const String inventory = '/inventory'; + static const String inventory = '/workbench/inventory'; static const String saleQuotation = '/workbench/sale_quotation'; static const String hrManage = '/workbench/hr_manage'; static const String employeeDetail = '/employee_detail'; static final List getPages = [ - GetPage(name: login, page: () => LoginScreen()), + GetPage( + name: login, + page: () => LoginScreen(), + ), GetPage(name: home, page: () => LandingPage()), GetPage(name: userinfo, page: () => UserInfoPage()), - GetPage(name: inventory, page: () => const InventoryPage()), - GetPage(name: saleQuotation, page: () => SaleQuotationPage()), - GetPage(name: hrManage, page: () => HrManagePage()), - GetPage(name: employeeDetail, page: () => EmployeeDetail()) + GetPage( + name: inventory, + page: () => const InventoryPage(), + middlewares: [ + // AuthMiddleware(), // 使用拦截器 + ], + ), + GetPage( + name: saleQuotation, + page: () => SaleQuotationPage(), + middlewares: [ + // AuthMiddleware(), // 使用拦截器 + ], + ), + GetPage( + name: hrManage, + page: () => HrManagePage(), + middlewares: [ + // AuthMiddleware(), // 使用拦截器 + ], + ), + GetPage( + name: employeeDetail, + page: () => EmployeeDetail(), + middlewares: [ + // AuthMiddleware(), // 使用拦截器 + ], + ) ]; } diff --git a/lib/router/router.util.dart b/lib/router/router.util.dart new file mode 100644 index 0000000..4112bc1 --- /dev/null +++ b/lib/router/router.util.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; +import 'package:sk_base_mobile/store/resource.store.dart'; +import 'package:sk_base_mobile/util/snack_bar.util.dart'; + +class RouterUtil { + static Future toNamed(String routeName, {arguments}) async { + //关闭键盘 + if (Get.context != null) { + FocusScope.of(Get.context!).requestFocus(FocusNode()); + } + bool isExsited = ResourceService.to.resources + .firstWhereOrNull((element) => element.path == routeName) != + null; + + if (!isExsited) { + SnackBarUtil().info('您没有权限,请联系管理员分配权限,后期将隐藏无权限的菜单'); + return null; + } + + return await Get.toNamed(routeName, arguments: arguments); + } +} diff --git a/lib/screens/hr_manage/hr_manage.dart b/lib/screens/hr_manage/hr_manage.dart index 6a7baed..b1b1640 100644 --- a/lib/screens/hr_manage/hr_manage.dart +++ b/lib/screens/hr_manage/hr_manage.dart @@ -13,7 +13,7 @@ import 'package:sk_base_mobile/util/debouncer.dart'; import 'package:sk_base_mobile/util/device.util.dart'; import 'package:sk_base_mobile/util/media_util.dart'; import 'package:sk_base_mobile/util/modal.util.dart'; -import 'package:sk_base_mobile/util/router.util.dart'; +import 'package:sk_base_mobile/router/router.util.dart'; import 'package:sk_base_mobile/util/screen_adaper_util.dart'; import 'package:sk_base_mobile/util/snack_bar.util.dart'; import 'package:sk_base_mobile/widgets/core/sk_avatar.dart'; diff --git a/lib/screens/workbench/workbench.dart b/lib/screens/workbench/workbench.dart index 6196639..7c5095a 100644 --- a/lib/screens/workbench/workbench.dart +++ b/lib/screens/workbench/workbench.dart @@ -4,8 +4,9 @@ import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; import 'package:flutter_svg/svg.dart'; import 'package:get/get.dart'; import 'package:sk_base_mobile/app_theme.dart'; -import 'package:sk_base_mobile/constants/router.dart'; +import 'package:sk_base_mobile/router/router.dart'; import 'package:sk_base_mobile/screens/workbench/workbench_controller.dart'; +import 'package:sk_base_mobile/router/router.util.dart'; import 'package:sk_base_mobile/util/screen_adaper_util.dart'; import 'package:sk_base_mobile/util/snack_bar.util.dart'; import 'package:sk_base_mobile/widgets/core/sk_appbar.dart'; @@ -42,9 +43,9 @@ class WorkBenchPage extends StatelessWidget { .map((e) => e.name) .firstWhereOrNull((name) => name == controller.menus[index].route); if (route != null) { - Get.toNamed(controller.menus[index].route); + RouterUtil.toNamed(controller.menus[index].route); } else { - SnackBarUtil().info('功能待开发。'); + SnackBarUtil().info('您没有权限,请联系管理员。后期会隐藏没有权限的功能'); } }, child: Container( diff --git a/lib/screens/workbench/workbench_controller.dart b/lib/screens/workbench/workbench_controller.dart index e7ae3b0..74cc35b 100644 --- a/lib/screens/workbench/workbench_controller.dart +++ b/lib/screens/workbench/workbench_controller.dart @@ -4,7 +4,8 @@ import 'package:sk_base_mobile/models/workbench.model.dart'; class WorkBenchController extends GetxController { final List menus = [ - WorkBenchModel(title: '库存', route: '/inventory', icon: 'inventory.svg'), + WorkBenchModel( + title: '库存', route: RouteConfig.inventory, icon: 'inventory.svg'), WorkBenchModel(title: '产品', route: '/product', icon: 'product.svg'), WorkBenchModel(title: '合同', route: '/contract', icon: 'contract.svg'), WorkBenchModel(title: '人事', route: RouteConfig.hrManage, icon: 'hr.svg'), diff --git a/lib/store/resource.store.dart b/lib/store/resource.store.dart index abadbab..7db166a 100644 --- a/lib/store/resource.store.dart +++ b/lib/store/resource.store.dart @@ -17,9 +17,15 @@ class ResourceService extends GetxService { Future getResources() async { try { final response = await Api.getResources(); - resources.value = (response.data as List) + final appResources = (response.data as List) .map((item) => ResourceModel.fromJson(item)) - .toList(); + .toList() + .firstWhereOrNull( + (element) => element.name == 'App', + ); + if (appResources != null) { + resources.assignAll(appResources.children); + } } catch (e) { LoggerUtil().error('getResources error: $e'); } diff --git a/lib/util/router.util.dart b/lib/util/router.util.dart deleted file mode 100644 index 714ca27..0000000 --- a/lib/util/router.util.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:get/get.dart'; - -class RouterUtil { - static Future toNamed(String routeName, {arguments}) async { - //关闭键盘 - if (Get.context != null) { - FocusScope.of(Get.context!).requestFocus(FocusNode()); - } - return await Get.toNamed(routeName, arguments: arguments); - } -}