diff --git a/backend/controller/menu.py b/backend/controller/menu.py index 09080c7..bb9fc4a 100644 --- a/backend/controller/menu.py +++ b/backend/controller/menu.py @@ -1,5 +1,5 @@ from core.utils import list_to_tree -from dbhelper.menu import del_menu, get_tree_menu, insert_menu, put_menu +from dbhelper.menu import del_menu, get_menu, get_tree_menu, insert_menu, put_menu from schemas import MenuIn, MenuRead, Response @@ -9,10 +9,16 @@ async def menu_add(data: MenuIn) -> Response[MenuRead]: async def menu_arr() -> Response: menus = await get_tree_menu() - return Response(data=list_to_tree(menus)) + try: + data = list_to_tree(menus) + except KeyError: + return Response(code=400, msg="菜单根节点丢失") + return Response(data=data) async def menu_del(pk: int) -> Response: + if await get_menu({"pid": pk}) is not None: + return Response(code=400, msg="请先删除子节点") if await del_menu(pk) == 0: return Response(code=400, msg="菜单不存在") return Response() diff --git a/backend/controller/role.py b/backend/controller/role.py index 0dea596..dc68144 100644 --- a/backend/controller/role.py +++ b/backend/controller/role.py @@ -26,8 +26,7 @@ async def role_has_menu(rid: int): rid: 角色ID """ menus = await get_role_menus(rid) - for obj in menus: - obj["meta"] = json.loads(obj["meta"]) if obj["meta"] is not None else None + try: result = list_to_tree(menus) except KeyError: diff --git a/backend/core/security.py b/backend/core/security.py index a8cd394..0237c06 100644 --- a/backend/core/security.py +++ b/backend/core/security.py @@ -70,7 +70,7 @@ async def check_permissions(request: Request, user: UserModel = Depends(check_to result = await get_user_info(user) active_rid = result["roles"][0]["id"] - # 白名单 + # 白名单 登录用户信息, 登录用户菜单信息 whitelist = [f"/user/{user.id}", f"/role/{active_rid}/menu"] flag = request.url.path in whitelist and request.method == "GET" if flag: diff --git a/backend/dbhelper/menu.py b/backend/dbhelper/menu.py index 952d8cd..aae779d 100644 --- a/backend/dbhelper/menu.py +++ b/backend/dbhelper/menu.py @@ -45,7 +45,7 @@ async def get_menu(kwargs): async def del_menu(mid: int): - """删除用户""" + """删除菜单""" return await MenuModel.filter(id=mid).update(status=9) diff --git a/backend/dbhelper/role.py b/backend/dbhelper/role.py index 3ab6e98..1124346 100644 --- a/backend/dbhelper/role.py +++ b/backend/dbhelper/role.py @@ -9,11 +9,12 @@ async def get_role_menus(rid: int): 根据角色id 获取菜单 """ db = connections.get("default") + # asc 降序 return await db.execute_query_dict( """ - select m.id, m.name, m.meta, m.path, m.type, m.component, m.pid, m.identifier, m.api, m.method + select m.id, m.name, m.icon, m.path, m.type, m.component, m.pid, m.identifier, m.api, m.method FROM sys_menu as m, sys_role_menu WHERE m.id = sys_role_menu.mid - AND sys_role_menu.rid = (?) AND sys_role_menu.`status` = 1""", + AND sys_role_menu.rid = (?) AND sys_role_menu.`status` = 1 order by m.id asc""", [rid], ) diff --git a/backend/dbhelper/user.py b/backend/dbhelper/user.py index cc193f1..741c8cf 100644 --- a/backend/dbhelper/user.py +++ b/backend/dbhelper/user.py @@ -31,7 +31,6 @@ async def get_user_info(user: UserModel): """, [user.id], ) - return { **jsonable_encoder(user), "roles": sql_result, @@ -81,9 +80,9 @@ async def put_user(uid: int, data: UserPut): """更新用户""" from core.security import get_password_hash - roles = data.roles + rids = data.roles del data.roles - for role in roles: + for role in rids: if await get_role({"id": role.rid, "status__not": 9}) is None: return role.rid # 更新用户 @@ -104,13 +103,12 @@ async def put_user(uid: int, data: UserPut): """, [uid], ) - print(has_roles) # 2. 将先有的数据标记 删除 [ await db.execute_query_dict( """ - update sys_user_role set status = 9 where rid = (?) + update sys_user_role set status = 9 where rid = (?) """, [role["id"]], ) @@ -119,11 +117,13 @@ async def put_user(uid: int, data: UserPut): # 2. 新增次此更新的数据 await UserRoleModel.bulk_create( - [UserRoleModel(uid=uid, **role.dict()) for role in roles] + [UserRoleModel(uid=uid, **role.dict()) for role in rids] ) async def select_role(uid: int, rid: int): """用户切换角色""" - await UserRoleModel.filter(uid=uid, rid__not=rid).update(status=1) - return await UserRoleModel.filter(uid=uid, rid=rid).update(status=5) + # 1.将用户id 未删除角色状态置为正常 1 ( 除切换角色id ) + await UserRoleModel.filter(uid=uid, rid__not=rid, status__not=9).update(status=1) + # 2.将用户id 角色id 和当前角色匹配的数据置为选中 + return await UserRoleModel.filter(uid=uid, rid=rid, status__not=9).update(status=5) diff --git a/backend/mini.db b/backend/mini.db new file mode 100644 index 0000000..e2a2455 Binary files /dev/null and b/backend/mini.db differ diff --git a/backend/mini.db-shm b/backend/mini.db-shm new file mode 100644 index 0000000..975cdd3 Binary files /dev/null and b/backend/mini.db-shm differ diff --git a/backend/mini.db-wal b/backend/mini.db-wal new file mode 100644 index 0000000..7f21fe6 Binary files /dev/null and b/backend/mini.db-wal differ diff --git a/backend/models/menu.py b/backend/models/menu.py index e36676f..67e5176 100644 --- a/backend/models/menu.py +++ b/backend/models/menu.py @@ -7,9 +7,9 @@ class MenuModel(Table): """ name = fields.CharField(max_length=20, description="名称", null=True) - meta = fields.JSONField(description="元数据信息", null=True) + icon = fields.CharField(max_length=100, description="菜单图标", null=True) path = fields.CharField(max_length=128, description="菜单url", null=True) - type = fields.SmallIntField(description="菜单类型 0目录 1组件 2按钮") + type = fields.SmallIntField(description="菜单类型 0目录 1组件 2按钮 3数据") component = fields.CharField(max_length=128, description="组件地址", null=True) pid = fields.IntField(description="父id", null=True) identifier = fields.CharField(max_length=30, description="权限标识 user:add", null=True) diff --git a/backend/router/url.py b/backend/router/url.py index 9f6b037..77d92a1 100644 --- a/backend/router/url.py +++ b/backend/router/url.py @@ -121,7 +121,6 @@ class Route(routing.APIRoute): has_perm = {"dependencies": [Depends(check_permissions)]} -has_perm = {} routes = [ Route.post("/login", endpoint=login, tags=["公共"], summary="登录"), diff --git a/backend/schemas/menu.py b/backend/schemas/menu.py index 0f43829..aa80d75 100644 --- a/backend/schemas/menu.py +++ b/backend/schemas/menu.py @@ -6,8 +6,8 @@ from schemas.common import ReadBase class MenuBasic(BaseModel): - name: str - meta: dict = Field(default=None, description="元信息") + name: str = Field(..., description="菜单名称") + icon: str = Field(default=None, description="菜单图标") path: Optional[str] = Field(default=None, description="前端路由地址") type: int = Field(description="0 目录 1 组件 2 按钮 3数据") component: Optional[str] = Field(default=None, description="前端组件地址") diff --git a/backend/tests/test_case.py b/backend/tests/test_case.py index 359fb31..05e97a2 100644 --- a/backend/tests/test_case.py +++ b/backend/tests/test_case.py @@ -13,29 +13,21 @@ dirs = [ ( "/menu", MenuIn( # id 1 - name="系统管理", - meta={"icon": "AppstoreOutlined"}, - path="/system", + name="系统面板", + icon="DashboardOutlined", + path="/dashboard", type=0, - component=None, pid=0, - identifier=None, - api=None, - method=None, ).dict(), ), ( "/menu", MenuIn( # id 2 - name="系统设置", - meta={"icon": "SettingOutlined"}, + name="系统管理", + icon="AppstoreOutlined", path="/system", type=0, - component=None, pid=0, - identifier=None, - api=None, - method=None, ).dict(), ), ] @@ -54,44 +46,44 @@ menus = [ "/menu", MenuIn( # id 3 name="用户管理", - meta={"icon": "TeamOutlined", "title": "用户管理"}, + icon="TeamOutlined", path="/system/user", type=1, component="/system/user/user.vue", - pid=1, + pid=2, ).dict(), ), ( "/menu", MenuIn( # id 4 name="角色管理", - meta={"icon": "UserOutlined", "title": "角色管理"}, + icon="UserOutlined", path="/system/role", type=1, component="/system/role/role.vue", - pid=1, + pid=2, ).dict(), ), ( "/menu", MenuIn( # id 5 name="菜单管理", - meta={"icon": "MenuOutlined", "title": "菜单管理"}, + icon="MenuOutlined", path="/system/menu", type=1, component="/system/menu/menu.vue", - pid=1, + pid=2, ).dict(), ), ( "/menu", MenuIn( # id 6 - name="关于", - meta={"icon": "DashboardOutlined", "title": "关于"}, - path="/setting/about", + name="数据面板", + icon="AreaChartOutlined", + path="/dashboard/index", type=1, - component="/setting/about/about.vue", - pid=2, + component="/dashboard/index/index.vue", + pid=1, ).dict(), ), ] @@ -174,7 +166,6 @@ role_manager_pre = [ ), MenuIn( name="角色查询", - meta={"icon": "Search"}, type=2, identifier="role:query", api="/role/query", @@ -260,13 +251,14 @@ menus_len = ( + len(dirs) + len(role_manager_pre) + len(menu_manager_pre) + + 1 ) datas = [ ( "/role", RoleIn( - name="superStar", + name="超管", remark="全部权限", menus=[num for num in range(1, menus_len)], ), @@ -276,7 +268,7 @@ datas = [ "/user", UserAdd( username="admin", - nickname="666管理员", + nickname="乐师高渐离", password="123456", roles=[RoleActive(rid=1, status=5)], ), diff --git a/frontend/src/components/layout/layout-header.vue b/frontend/src/components/layout/header.vue similarity index 93% rename from frontend/src/components/layout/layout-header.vue rename to frontend/src/components/layout/header.vue index d6d8c14..3d19fac 100644 --- a/frontend/src/components/layout/layout-header.vue +++ b/frontend/src/components/layout/header.vue @@ -1,6 +1,6 @@