feat: router permission

This commit is contained in:
louis 2024-04-10 17:38:23 +08:00
parent 11df20c98e
commit 9527dcaee2
9 changed files with 92 additions and 26 deletions

View File

@ -4,6 +4,6 @@ export 'data.dart';
export 'enum.dart'; export 'enum.dart';
export 'global_url.dart'; export 'global_url.dart';
export 'cache_key.dart'; export 'cache_key.dart';
export 'router.dart'; export '../router/router.dart';
export 'text_enum.dart'; export 'text_enum.dart';
export 'system_param_config.dart'; export 'system_param_config.dart';

View File

@ -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;
}
}

View File

@ -1,5 +1,6 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:sk_base_mobile/models/user_info.model.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/components/employee_detail.dart';
import 'package:sk_base_mobile/screens/hr_manage/hr_manage.dart'; import 'package:sk_base_mobile/screens/hr_manage/hr_manage.dart';
import 'package:sk_base_mobile/screens/inventory/inventory.dart'; import 'package:sk_base_mobile/screens/inventory/inventory.dart';
@ -13,18 +14,45 @@ class RouteConfig {
static const String home = "/"; static const String home = "/";
static const String login = '/login'; static const String login = '/login';
static const String userinfo = '/userinfo'; 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 saleQuotation = '/workbench/sale_quotation';
static const String hrManage = '/workbench/hr_manage'; static const String hrManage = '/workbench/hr_manage';
static const String employeeDetail = '/employee_detail'; static const String employeeDetail = '/employee_detail';
static final List<GetPage> getPages = [ static final List<GetPage> getPages = [
GetPage(name: login, page: () => LoginScreen()), GetPage(
name: login,
page: () => LoginScreen(),
),
GetPage(name: home, page: () => LandingPage()), GetPage(name: home, page: () => LandingPage()),
GetPage(name: userinfo, page: () => UserInfoPage()), GetPage(name: userinfo, page: () => UserInfoPage()),
GetPage(name: inventory, page: () => const InventoryPage()), GetPage(
GetPage(name: saleQuotation, page: () => SaleQuotationPage()), name: inventory,
GetPage(name: hrManage, page: () => HrManagePage()), page: () => const InventoryPage(),
GetPage(name: employeeDetail, page: () => EmployeeDetail()) middlewares: [
// AuthMiddleware(), // 使
],
),
GetPage(
name: saleQuotation,
page: () => SaleQuotationPage(),
middlewares: [
// AuthMiddleware(), // 使
],
),
GetPage(
name: hrManage,
page: () => HrManagePage(),
middlewares: [
// AuthMiddleware(), // 使
],
),
GetPage(
name: employeeDetail,
page: () => EmployeeDetail(),
middlewares: [
// AuthMiddleware(), // 使
],
)
]; ];
} }

View File

@ -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<T?> toNamed<T>(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<T?>(routeName, arguments: arguments);
}
}

View File

@ -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/device.util.dart';
import 'package:sk_base_mobile/util/media_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/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/screen_adaper_util.dart';
import 'package:sk_base_mobile/util/snack_bar.util.dart'; import 'package:sk_base_mobile/util/snack_bar.util.dart';
import 'package:sk_base_mobile/widgets/core/sk_avatar.dart'; import 'package:sk_base_mobile/widgets/core/sk_avatar.dart';

View File

@ -4,8 +4,9 @@ import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:sk_base_mobile/app_theme.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/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/screen_adaper_util.dart';
import 'package:sk_base_mobile/util/snack_bar.util.dart'; import 'package:sk_base_mobile/util/snack_bar.util.dart';
import 'package:sk_base_mobile/widgets/core/sk_appbar.dart'; import 'package:sk_base_mobile/widgets/core/sk_appbar.dart';
@ -42,9 +43,9 @@ class WorkBenchPage extends StatelessWidget {
.map((e) => e.name) .map((e) => e.name)
.firstWhereOrNull((name) => name == controller.menus[index].route); .firstWhereOrNull((name) => name == controller.menus[index].route);
if (route != null) { if (route != null) {
Get.toNamed(controller.menus[index].route); RouterUtil.toNamed(controller.menus[index].route);
} else { } else {
SnackBarUtil().info('功能待开发。'); SnackBarUtil().info('您没有权限,请联系管理员。后期会隐藏没有权限的功能');
} }
}, },
child: Container( child: Container(

View File

@ -4,7 +4,8 @@ import 'package:sk_base_mobile/models/workbench.model.dart';
class WorkBenchController extends GetxController { class WorkBenchController extends GetxController {
final List<WorkBenchModel> menus = [ final List<WorkBenchModel> 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: '/product', icon: 'product.svg'),
WorkBenchModel(title: '合同', route: '/contract', icon: 'contract.svg'), WorkBenchModel(title: '合同', route: '/contract', icon: 'contract.svg'),
WorkBenchModel(title: '人事', route: RouteConfig.hrManage, icon: 'hr.svg'), WorkBenchModel(title: '人事', route: RouteConfig.hrManage, icon: 'hr.svg'),

View File

@ -17,9 +17,15 @@ class ResourceService extends GetxService {
Future<void> getResources() async { Future<void> getResources() async {
try { try {
final response = await Api.getResources(); final response = await Api.getResources();
resources.value = (response.data as List) final appResources = (response.data as List)
.map((item) => ResourceModel.fromJson(item)) .map((item) => ResourceModel.fromJson(item))
.toList(); .toList()
.firstWhereOrNull(
(element) => element.name == 'App',
);
if (appResources != null) {
resources.assignAll(appResources.children);
}
} catch (e) { } catch (e) {
LoggerUtil().error('getResources error: $e'); LoggerUtil().error('getResources error: $e');
} }

View File

@ -1,12 +0,0 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class RouterUtil {
static Future<T?> toNamed<T>(String routeName, {arguments}) async {
//
if (Get.context != null) {
FocusScope.of(Get.context!).requestFocus(FocusNode());
}
return await Get.toNamed<T?>(routeName, arguments: arguments);
}
}