diff --git a/README.md b/README.md index dd1b8403..e49f5b46 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ it a pseudo three-tier architecture ## Test data -Execute ``backend/app/init_test_data.py`` file to automatically create test data +Initialize the test data using the `backend/sql/init_test_data.sql` file ## Development @@ -128,12 +128,7 @@ Execute tests via pytest cd backend/app/ ``` -3. Initialize the test data - - ```shell - python tests/init_test_data.py - ``` - +3. Initialize the test data using the `backend/sql/init_test_data.sql` file 4. Execute the test command ```shell diff --git a/README.zh-CN.md b/README.zh-CN.md index f521dd22..90e18b38 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -101,7 +101,9 @@ ## 测试数据 -执行 `backend/app/init_test_data.py` 文件,自动创建测试数据 +[//]: # (执行 `backend/app/init_test_data.py` 文件,自动创建测试数据) + +使用 `backend/sql/init_test_data.sql` 文件初始化测试数据 ## 开发 @@ -124,12 +126,7 @@ cd backend/app/ ``` -3. 初始化测试数据 - - ```shell - python tests/init_test_data.py - ``` - +3. 使用 `backend/sql/init_test_data.sql` 文件初始化测试数据 4. 执行测试命令 ```shell diff --git a/backend/app/common/exception/exception_handler.py b/backend/app/common/exception/exception_handler.py index f500d644..d3a0be94 100644 --- a/backend/app/common/exception/exception_handler.py +++ b/backend/app/common/exception/exception_handler.py @@ -79,9 +79,13 @@ def register_exception(app: FastAPI): sub_raw_exc = sub_raw_error.exc if isinstance(sub_raw_exc, (EnumMemberError, WrongConstantError)): if getattr(sub_raw_exc, 'code') == 'enum': - sub_raw_exc.__dict__['permitted'] = ', '.join(repr(v.value) for v in sub_raw_exc.enum_values) # type: ignore # noqa: E501 + sub_raw_exc.__dict__['permitted'] = ', '.join( + repr(v.value) for v in sub_raw_exc.enum_values # type: ignore + ) else: - sub_raw_exc.__dict__['permitted'] = ', '.join(repr(v) for v in sub_raw_exc.permitted) # type: ignore # noqa: E501 + sub_raw_exc.__dict__['permitted'] = ', '.join( + repr(v) for v in sub_raw_exc.permitted # type: ignore + ) # 处理异常信息 for error in raw_exc.errors()[:1]: field = str(error.get('loc')[-1]) diff --git a/backend/app/crud/crud_menu.py b/backend/app/crud/crud_menu.py index 092e11dc..09a96c1b 100644 --- a/backend/app/crud/crud_menu.py +++ b/backend/app/crud/crud_menu.py @@ -26,13 +26,15 @@ class CRUDMenu(CRUDBase[Menu, CreateMenu, UpdateMenu]): menu = await db.execute(se) return menu.scalars().all() - async def get_role_menus(self, db, menu_ids: list[int]) -> list[Menu]: - se = ( - select(self.model) - .where(self.model.id.in_(menu_ids)) - .where(self.model.status == 1) - .order_by(asc(self.model.sort)) - ) + async def get_role_menus(self, db, superuser: bool, menu_ids: list[int]) -> list[Menu]: + se = select(self.model).order_by(asc(self.model.sort)) + where_list = [ + self.model.menu_type.in_([0, 1]), + self.model.status == 1, + ] + if not superuser: + where_list.append(self.model.id.in_(menu_ids)) + se = se.where(and_(*where_list)) menu = await db.execute(se) return menu.scalars().all() diff --git a/backend/app/crud/crud_role.py b/backend/app/crud/crud_role.py index 7a725ff9..de10a750 100644 --- a/backend/app/crud/crud_role.py +++ b/backend/app/crud/crud_role.py @@ -43,9 +43,7 @@ class CRUDRole(CRUDBase[Role, CreateRole, UpdateRole]): async def update(self, db, role_id: int, obj_in: UpdateRole) -> int: role = await db.execute( - update(self.model) - .where(self.model.id == role_id) - .values(**obj_in.dict(exclude={'menus'})) + update(self.model).where(self.model.id == role_id).values(**obj_in.dict(exclude={'menus'})) ) current_role = await self.get_with_relation(db, role_id) # 更新菜单 diff --git a/backend/app/models/sys_menu.py b/backend/app/models/sys_menu.py index af24e188..38e68a27 100644 --- a/backend/app/models/sys_menu.py +++ b/backend/app/models/sys_menu.py @@ -26,6 +26,8 @@ class Menu(Base): component: Mapped[str | None] = mapped_column(String(255), default=None, comment='组件路径') perms: Mapped[str | None] = mapped_column(String(100), default=None, comment='权限标识') status: Mapped[int] = mapped_column(default=1, comment='菜单状态(0停用 1正常)') + show: Mapped[int] = mapped_column(default=1, comment='是否显示(0否 1是)') + cache: Mapped[int] = mapped_column(default=1, comment='是否缓存(0否 1是)') remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注') # 父级菜单一对多 parent_id: Mapped[int | None] = mapped_column( diff --git a/backend/app/schemas/menu.py b/backend/app/schemas/menu.py index 2cb17497..56915824 100644 --- a/backend/app/schemas/menu.py +++ b/backend/app/schemas/menu.py @@ -8,19 +8,6 @@ from backend.app.common.enums import MenuType, StatusType from backend.app.schemas.base import SchemaBase -class Meta(SchemaBase): - roles: list[str] | None = None - requiresAuth: bool = False - icon: str | None = None - locale: str | None = None - hideInMenu: bool | None = None - hideChildrenInMenu: bool | None = None - activeMenu: str | None = None - orderNo: int | None = None - noAffix: bool | None = None - ignoreCache: bool | None = None - - class MenuBase(SchemaBase): name: str parent_id: int | None = Field(default=None, description='菜单父级ID') @@ -28,10 +15,11 @@ class MenuBase(SchemaBase): icon: str | None = None path: str | None = None menu_type: MenuType = Field(default=MenuType.directory, ge=0, description='菜单类型(0目录 1菜单 2按钮)') - meta: Meta component: str | None = None perms: str | None = None status: StatusType = Field(default=StatusType.enable) + show: StatusType = Field(default=StatusType.enable) + cache: StatusType = Field(default=StatusType.enable) remark: str | None = None diff --git a/backend/app/services/auth_service.py b/backend/app/services/auth_service.py index 9d17a589..73141371 100644 --- a/backend/app/services/auth_service.py +++ b/backend/app/services/auth_service.py @@ -12,7 +12,6 @@ from backend.app.common.enums import LoginLogStatus from backend.app.common.exception import errors from backend.app.common.jwt import get_token from backend.app.common.redis import redis_client -from backend.app.common.response.response_code import CustomCode from backend.app.core.conf import settings from backend.app.crud.crud_user import UserDao from backend.app.database.db_mysql import async_db_session diff --git a/backend/app/services/menu_service.py b/backend/app/services/menu_service.py index 3afdfaaa..1d8bdd64 100644 --- a/backend/app/services/menu_service.py +++ b/backend/app/services/menu_service.py @@ -31,7 +31,7 @@ class MenuService: menu_ids = [] for role in roles: menu_ids.extend([menu.id for menu in role.menus]) - menu_select = await MenuDao.get_role_menus(db, menu_ids) + menu_select = await MenuDao.get_role_menus(db, request.user.is_superuser, menu_ids) menu_tree = await get_tree_data(menu_select) return menu_tree diff --git a/backend/sql/create_tables.sql b/backend/sql/create_tables.sql index 4be78758..15dda60f 100644 --- a/backend/sql/create_tables.sql +++ b/backend/sql/create_tables.sql @@ -107,6 +107,8 @@ CREATE TABLE sys_menu component VARCHAR(255) COMMENT '组件路径', perms VARCHAR(100) COMMENT '权限标识', status INTEGER NOT NULL COMMENT '菜单状态(0停用 1正常)', + `show` INTEGER NOT NULL comment '是否显示(0否 1是)', + cache INTEGER NOT NULL comment '是否缓存(0否 1是)', remark LONGTEXT COMMENT '备注', parent_id INTEGER COMMENT '父菜单ID', created_time DATETIME NOT NULL COMMENT '创建时间', diff --git a/backend/sql/init_test_data.sql b/backend/sql/init_test_data.sql index c3e93470..6f391b35 100644 --- a/backend/sql/init_test_data.sql +++ b/backend/sql/init_test_data.sql @@ -1,10 +1,14 @@ -INSERT INTO fba.sys_dept (id, name, level, sort, leader, phone, email, status, del_flag, parent_id, created_time, - updated_time) +INSERT INTO fba.sys_dept (id, name, level, sort, leader, phone, email, status, del_flag, parent_id, created_time, updated_time) VALUES (1, 'test', 0, 0, null, null, null, 1, 0, null, '2023-06-26 17:13:45', null); -INSERT INTO fba.sys_menu (id, name, level, sort, icon, path, menu_type, component, perms, status, remark, parent_id, - created_time, updated_time) -VALUES (1, 'test', 0, 0, null, null, 0, null, null, 1, null, null, '2023-06-26 17:13:45', null); +insert into fba.sys_menu (id, name, level, sort, icon, path, menu_type, component, perms, status, remark, parent_id, created_time, updated_time, show, cache) +values (1, 'test', 0, 0, null, null, 0, null, null, 1, null, null, '2023-06-26 17:13:45', null, 1, 1), + (2, 'dashboard', 0, 0, 'icon-dashboard', '/dashboard', 0, '/dashboard/workplace/index.vue', null, 1, null, null, '2023-06-30 10:10:34', null, 1, 1), + (3, 'Workplace', 0, 0, null, '/workplace', 0, null, null, 1, null, 2, '2023-06-30 10:11:40', null, 1, 1), + (4, 'arcoWebsite', 0, 0, 'icon-link', 'https://arco.design', 0, null, null, 1, null, null, '2023-06-30 10:13:04', null, 1, 1), + (5, 'log', 0, 0, 'icon-bug', '/log', 0, null, null, 1, null, null, '2023-06-30 10:13:54', null, 1, 1), + (6, 'Login', 0, 0, null, '/login', 0, '/log/login/index.vue', null, 1, null, 5, '2023-06-30 10:14:23', null, 1, 1), + (7, 'faq', 0, 0, 'icon-question-circle', 'https://arco.design/vue/docs/pro/faq', 0, null, null, 1, null, null, '2023-06-30 10:14:56', null, 1, 1); INSERT INTO fba.sys_role (id, name, data_scope, status, remark, created_time, updated_time) VALUES (1, 'test', 2, 1, null, '2023-06-26 17:13:45', null); @@ -12,11 +16,8 @@ VALUES (1, 'test', 2, 1, null, '2023-06-26 17:13:45', null); INSERT INTO fba.sys_role_menu (id, role_id, menu_id) VALUES (1, 1, 1); -INSERT INTO fba.sys_user (id, uuid, username, nickname, password, email, is_superuser, status, is_multi_login, avatar, - phone, join_time, last_login_time, dept_id, created_time, updated_time) -VALUES (1, 'af4c804f-3966-4949-ace2-3bb7416ea926', 'test', 'test', - '$2b$12$TpdL7kKriqhpJHSBMT.Fr.hJNBx5SdUybi.NT1DV5MojYpV9PpRre', 'test@gmail.com', 1, 1, 0, null, null, - '2023-06-26 17:13:45', null, 1, '2023-06-26 17:13:45', null); +INSERT INTO fba.sys_user (id, uuid, username, nickname, password, email, is_superuser, status, is_multi_login, avatar, phone, join_time, last_login_time, dept_id, created_time, updated_time) +VALUES (1, 'af4c804f-3966-4949-ace2-3bb7416ea926', 'test', 'test', '$2b$12$TpdL7kKriqhpJHSBMT.Fr.hJNBx5SdUybi.NT1DV5MojYpV9PpRre', 'test@gmail.com', 1, 1, 0, null, null, '2023-06-26 17:13:45', null, 1, '2023-06-26 17:13:45', null); INSERT INTO fba.sys_user_role (id, user_id, role_id) VALUES (1, 1, 1);