feat: role & menu api
This commit is contained in:
@@ -5,3 +5,39 @@ from schemas.menu import MenuIn
|
||||
async def insert_menu(menu: MenuIn):
|
||||
"""新增菜单"""
|
||||
return await MenuModel.create(**menu.dict())
|
||||
|
||||
|
||||
async def get_menus(skip: int, limit: int, kwargs: dict = None):
|
||||
"""
|
||||
分页获取用户并且支持字段模糊查询
|
||||
Args:
|
||||
skip: 偏移量
|
||||
limit: 数量
|
||||
kwargs: 查询字典
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if kwargs is not None:
|
||||
kwargs = {f"{k}__contains": v for k, v in kwargs.items()}
|
||||
else:
|
||||
kwargs = {}
|
||||
result = MenuModel.filter(status__not=9, **kwargs).all().order_by("-created")
|
||||
return await result.offset(skip).limit(limit), await result.count()
|
||||
|
||||
|
||||
async def get_menu(kwargs):
|
||||
"""
|
||||
根据条件查询到第一条符合结果的数据
|
||||
Args:
|
||||
kwargs:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return await MenuModel.filter(**kwargs).first()
|
||||
|
||||
|
||||
async def del_menu(mid: int):
|
||||
"""删除用户"""
|
||||
return await MenuModel.filter(id=mid).update(status=9)
|
||||
|
31
backend/dbhelper/relation.py
Normal file
31
backend/dbhelper/relation.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from tortoise import connections
|
||||
|
||||
from dbhelper.menu import get_menu
|
||||
from models import RoleMenuModel, UserRoleModel
|
||||
from schemas import UserRole
|
||||
|
||||
|
||||
async def user_assigned_role(data: UserRole):
|
||||
"""给用户分配角色"""
|
||||
return await UserRoleModel.create(**data.dict())
|
||||
|
||||
|
||||
async def role_assigned_menu(data):
|
||||
"""给角色分配菜单"""
|
||||
for mid in data.menus:
|
||||
if await get_menu({"id": mid}) is None:
|
||||
return mid
|
||||
|
||||
# todo 性能优化
|
||||
db = connections.get("default")
|
||||
# 1. 先把所有数据做删除
|
||||
await db.execute_query_dict(
|
||||
"""
|
||||
update sys_role_menu set status = 9 where rid = (?)
|
||||
""",
|
||||
[data.rid],
|
||||
)
|
||||
# 2. 新增数据
|
||||
await RoleMenuModel.bulk_create(
|
||||
[RoleMenuModel(rid=data.rid, mid=mid) for mid in data.menus]
|
||||
)
|
@@ -11,9 +11,9 @@ async def get_role_menus(rid: int):
|
||||
db = connections.get("default")
|
||||
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_regx,m.api, m.method, m.sort
|
||||
select m.id, m.name, m.meta, m.path, m.type, m.component, m.pid, m.identifier, m.regx,m.api, m.method
|
||||
FROM sys_menu as m, sys_role_menu WHERE m.id = sys_role_menu.mid
|
||||
AND sys_role_menu.rid = (%s) AND m.`status` = 1 ORDER BY m.sort""",
|
||||
AND sys_role_menu.rid = (?) AND m.`status` = 1""",
|
||||
[rid],
|
||||
)
|
||||
|
||||
@@ -21,3 +21,44 @@ async def get_role_menus(rid: int):
|
||||
async def new_role(role: RoleIn):
|
||||
"""新增角色"""
|
||||
return await RoleModel.create(**role.dict())
|
||||
|
||||
|
||||
async def get_roles(skip: int, limit: int, kwargs: dict = None):
|
||||
"""
|
||||
分页获取用户并且支持字段模糊查询
|
||||
Args:
|
||||
skip: 偏移量
|
||||
limit: 数量
|
||||
kwargs: 查询字典
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if kwargs is not None:
|
||||
kwargs = {f"{k}__contains": v for k, v in kwargs.items()}
|
||||
else:
|
||||
kwargs = {}
|
||||
result = RoleModel.filter(status__not=9, **kwargs).all().order_by("-created")
|
||||
return await result.offset(skip).limit(limit), await result.count()
|
||||
|
||||
|
||||
async def get_role(kwargs):
|
||||
"""
|
||||
根据条件查询到第一条符合结果的数据
|
||||
Args:
|
||||
kwargs:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return await RoleModel.filter(**kwargs).first()
|
||||
|
||||
|
||||
async def del_role(rid: int):
|
||||
"""删除用户"""
|
||||
return await RoleModel.filter(id=rid).update(status=9)
|
||||
|
||||
|
||||
async def put_role(pk: int, data):
|
||||
"""更新角色"""
|
||||
return await RoleModel.filter(id=pk).update(**data.dict())
|
||||
|
@@ -1,7 +1,9 @@
|
||||
from tortoise.transactions import atomic
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from tortoise import connections
|
||||
|
||||
from models import RoleModel, UserModel, UserRoleModel
|
||||
from schemas.user import UserIn, UserRole
|
||||
from dbhelper.role import get_role
|
||||
from models import UserModel, UserRoleModel
|
||||
from schemas import UserPut
|
||||
|
||||
|
||||
async def get_user(kwargs):
|
||||
@@ -16,25 +18,21 @@ async def get_user(kwargs):
|
||||
return await UserModel.filter(**kwargs).first()
|
||||
|
||||
|
||||
async def get_user_info(pk: int):
|
||||
async def get_user_info(user: UserModel):
|
||||
"""
|
||||
根据id查用户角色列表,当前激活角色
|
||||
根据id查用户角色列表 按激活角色倒序显示
|
||||
"""
|
||||
user = await UserModel.get(pk=pk).values(
|
||||
"id", "username", "nickname", "identity", "created", "modified"
|
||||
db = connections.get("default")
|
||||
sql_result = await db.execute_query_dict(
|
||||
"""
|
||||
select r.id, r.name, ur.status from sys_role as r
|
||||
left join sys_user_role as ur on r.id = ur.rid where
|
||||
ur.uid = (?) and ur.status != 9 and r.status != 9 order by ur.status desc
|
||||
""",
|
||||
[user.id],
|
||||
)
|
||||
role = (
|
||||
await UserRoleModel.filter(uid=pk, status__not_in=[9, 5])
|
||||
.all()
|
||||
.values("rid", "status")
|
||||
)
|
||||
active_rid = role[0].get("rid")
|
||||
rids = []
|
||||
for obj in role:
|
||||
if obj.get("status") == 5:
|
||||
active_rid = obj.get("rid")
|
||||
rids.append(obj.get("rid"))
|
||||
return {**user, "active_rid": active_rid, "rids": rids}
|
||||
|
||||
return {**jsonable_encoder(user), "roles": sql_result}
|
||||
|
||||
|
||||
async def get_users(skip: int, limit: int, kwargs: dict = None):
|
||||
@@ -58,22 +56,51 @@ async def get_users(skip: int, limit: int, kwargs: dict = None):
|
||||
return await result.offset(skip).limit(limit), await result.count()
|
||||
|
||||
|
||||
@atomic()
|
||||
async def insert_user(user, roles):
|
||||
for index, rid in enumerate(roles):
|
||||
# 1. 查角色表是否有该角色
|
||||
await RoleModel.get(pk=rid)
|
||||
# 创建用户
|
||||
obj = await UserModel.create(**user.dict())
|
||||
"""新增用户,选择角色"""
|
||||
for role in roles:
|
||||
if await get_role({"id": role.rid, "status__not": 9}) is None:
|
||||
return role.rid
|
||||
|
||||
user_role = UserRole(rid=rid, uid=obj.id)
|
||||
if index == 0:
|
||||
user_role.status = 5
|
||||
# 第一个角色默认, 添加到关系表
|
||||
await UserRoleModel.create(**user_role.dict())
|
||||
return user
|
||||
# 创建用户
|
||||
obj = await UserModel.create(**user.dict())
|
||||
|
||||
await UserRoleModel.bulk_create(
|
||||
[UserRoleModel(rid=role.rid, uid=obj.id, status=role.status) for role in roles]
|
||||
)
|
||||
return obj
|
||||
|
||||
|
||||
async def new_user(user: UserIn):
|
||||
"""新增用户"""
|
||||
return await UserModel.create(**user.dict())
|
||||
async def del_user(uid: int):
|
||||
"""删除用户"""
|
||||
return await UserModel.filter(id=uid).update(status=9)
|
||||
|
||||
|
||||
async def put_user(uid: int, data: UserPut):
|
||||
"""更新用户"""
|
||||
from core.security import get_password_hash
|
||||
|
||||
roles = data.rids
|
||||
del data.rids
|
||||
for role in roles:
|
||||
if await get_role({"id": role.rid, "status__not": 9}) is None:
|
||||
return role.rid
|
||||
|
||||
# 更新用户
|
||||
data.password = get_password_hash(data.password)
|
||||
await UserModel.filter(id=uid).update(**data.dict())
|
||||
|
||||
# todo 1. 先前有的角色,这次更新成没有 2. 先前没有的角色 这次更新成有, 3. 只更新了状态
|
||||
|
||||
db = connections.get("default")
|
||||
# 1. 先把所有数据做删除
|
||||
await db.execute_query_dict(
|
||||
"""
|
||||
update sys_user_role set status = 9 where uid = (?)
|
||||
""",
|
||||
[uid],
|
||||
)
|
||||
# 2. 新增次此更新的数据
|
||||
await UserRoleModel.bulk_create(
|
||||
[UserRoleModel(uid=uid, **role.dict()) for role in roles]
|
||||
)
|
||||
|
Reference in New Issue
Block a user