login page
This commit is contained in:
0
backend/dbhelper/__init__.py
Normal file
0
backend/dbhelper/__init__.py
Normal file
15
backend/dbhelper/role.py
Normal file
15
backend/dbhelper/role.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from tortoise import connections
|
||||
|
||||
|
||||
async def get_role_menus(rid: int):
|
||||
"""
|
||||
根据角色id 获取菜单
|
||||
"""
|
||||
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
|
||||
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""",
|
||||
[rid],
|
||||
)
|
72
backend/dbhelper/user.py
Normal file
72
backend/dbhelper/user.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from tortoise.transactions import atomic
|
||||
|
||||
from core.enums import Status
|
||||
from models import RoleModel, UserModel, UserRoleModel
|
||||
from schemas.user import UserRole
|
||||
|
||||
|
||||
async def get_user(kwargs):
|
||||
"""
|
||||
根据条件查询到第一条符合结果的数据
|
||||
Args:
|
||||
kwargs:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return await UserModel.filter(**kwargs).first()
|
||||
|
||||
|
||||
async def get_user_info(pk: int):
|
||||
"""
|
||||
根据id查用户角色列表,当前激活角色
|
||||
"""
|
||||
user = await UserModel.get(pk=pk).values(
|
||||
"id", "username", "nickname", "identity", "created", "modified"
|
||||
)
|
||||
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") == Status.SELECTED:
|
||||
active_rid = obj.get("rid")
|
||||
rids.append(obj.get("rid"))
|
||||
return {**user, "active_rid": active_rid, "rids": rids}
|
||||
|
||||
|
||||
async def get_users(skip: int, limit: int, kwargs: dict):
|
||||
"""
|
||||
分页获取用户并且支持字段模糊查询
|
||||
Args:
|
||||
skip: 偏移量
|
||||
limit: 数量
|
||||
kwargs: 查询字典
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
kwargs = {f"{k}__contains": v for k, v in kwargs.items()}
|
||||
result = (
|
||||
UserModel.filter(status__not_in=[9, 5], **kwargs).all().order_by("-created")
|
||||
)
|
||||
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())
|
||||
|
||||
user_role = UserRole(rid=rid, uid=obj.id)
|
||||
if index == 0:
|
||||
user_role.status = Status.SELECTED
|
||||
# 第一个角色默认, 添加到关系表
|
||||
await UserRoleModel.create(**user_role.dict())
|
||||
return user
|
Reference in New Issue
Block a user