Refactor backend MVC (#2)
* docs(requirements.txt):升级fastapi、uvicorn版本 * refactor(user):重构用户router、service * ref: role list api * doc: 1 * refactor(backend): mvc ref
This commit is contained in:
0
backend/service/__init__.py
Normal file
0
backend/service/__init__.py
Normal file
26
backend/service/auth.py
Normal file
26
backend/service/auth.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import asyncio
|
||||
|
||||
from websockets.exceptions import WebSocketException
|
||||
|
||||
from core.dbhelper import has_user
|
||||
from core.security import generate_token, verify_password
|
||||
from core.utils import get_system_info
|
||||
|
||||
|
||||
async def user_login(data):
|
||||
"""用户登录"""
|
||||
user_obj = await has_user(data.username)
|
||||
if user_obj:
|
||||
if verify_password(data.password, user_obj.password):
|
||||
return dict(data=dict(id=user_obj.id, token=generate_token(data.username)))
|
||||
return dict(code=400, msg="账号或密码错误")
|
||||
|
||||
|
||||
async def system_info(ws):
|
||||
await ws.accept()
|
||||
try:
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
await ws.send_json(get_system_info())
|
||||
except WebSocketException:
|
||||
await ws.close()
|
26
backend/service/menu.py
Normal file
26
backend/service/menu.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from core.dbhelper import MenuDao
|
||||
from core.service import Service
|
||||
from core.utils import list_to_tree
|
||||
|
||||
|
||||
class MenuService(Service):
|
||||
def __init__(self):
|
||||
super(MenuService, self).__init__(MenuDao)
|
||||
|
||||
async def get_items(self):
|
||||
sql = "select * from sys_menu where status != 9 ;"
|
||||
menus = await self.dao.raw_sql(sql)
|
||||
try:
|
||||
return dict(data=list_to_tree(menus))
|
||||
except KeyError:
|
||||
return dict(code=400, msg="菜单根节点丢失")
|
||||
|
||||
async def delete_item(self, pk):
|
||||
if await MenuDao.select({"pid": pk, "status__not": 9}) is not None:
|
||||
return dict(code=400, msg="请先删除子节点")
|
||||
if await MenuDao.delete(pk) == 0:
|
||||
return dict(code=400, msg="菜单不存在")
|
||||
return dict()
|
||||
|
||||
|
||||
service = MenuService()
|
61
backend/service/role.py
Normal file
61
backend/service/role.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from core.dbhelper import MenuDao, RoleDao, RoleMenuDao, has_permissions
|
||||
from core.service import Service
|
||||
from core.utils import list_to_tree
|
||||
|
||||
|
||||
class RoleService(Service):
|
||||
def __init__(self):
|
||||
super(RoleService, self).__init__(RoleDao)
|
||||
|
||||
async def create_item(self, role):
|
||||
"""
|
||||
创建角色
|
||||
:param role: pydantic model
|
||||
:return:
|
||||
"""
|
||||
if not all(
|
||||
[await MenuDao.select({"id": mid, "status__not": 9}) for mid in role.menus]
|
||||
):
|
||||
return dict(code=400, msg="菜单不存在")
|
||||
|
||||
obj = await RoleDao.insert(dict(name=role.name, remark=role.remark))
|
||||
# 写入菜单
|
||||
await RoleMenuDao.inserts([dict(rid=obj.id, mid=mid) for mid in role.menus])
|
||||
return dict(data=obj)
|
||||
|
||||
async def update_item(self, pk, data):
|
||||
"""
|
||||
更新角色
|
||||
:param pk:
|
||||
:param data:
|
||||
:return:
|
||||
"""
|
||||
if await RoleDao.select({"id": pk}) is None:
|
||||
return dict(code=400, msg="角色不存在")
|
||||
# 如果不为ture -> 有菜单id不存在
|
||||
if not all([await MenuDao.select({"id": mid}) for mid in data.menus]):
|
||||
return dict(code=400, msg="菜单不存在")
|
||||
|
||||
await RoleDao.update(dict(id=pk), dict(name=data.name, remark=data.remark))
|
||||
await RoleMenuDao.update(dict(rid=pk), dict(status=9))
|
||||
|
||||
await RoleMenuDao.inserts([dict(rid=pk, mid=mid) for mid in data.menus])
|
||||
|
||||
return dict()
|
||||
|
||||
@staticmethod
|
||||
async def has_tree_menus(pk):
|
||||
"""
|
||||
查询角色拥有菜单
|
||||
:param pk:
|
||||
:return:
|
||||
"""
|
||||
menus = await has_permissions(pk, is_menu=True)
|
||||
|
||||
try:
|
||||
return dict(data=list_to_tree(menus))
|
||||
except KeyError:
|
||||
return dict(code=400, msg="菜单缺少根节点.")
|
||||
|
||||
|
||||
service = RoleService()
|
90
backend/service/user.py
Normal file
90
backend/service/user.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
|
||||
from core.dbhelper import RoleDao, UserDao, UserRoleDao, has_roles
|
||||
from core.security import get_password_hash
|
||||
from core.service import Service
|
||||
|
||||
|
||||
class UserService(Service):
|
||||
def __init__(self):
|
||||
super(UserService, self).__init__(UserDao)
|
||||
|
||||
async def create_item(self, data):
|
||||
"""创建用户"""
|
||||
# 检查用户是否存在
|
||||
if await self.dao.select({"username": data.username}) is not None:
|
||||
return dict(code=400, msg="用户名已存在")
|
||||
rids = data.roles
|
||||
del data.roles
|
||||
data.password = get_password_hash(data.password)
|
||||
# 检查选中的角色是否存在
|
||||
for role in rids:
|
||||
if await RoleDao.select(dict(id=role.rid, status__not=9)) is None:
|
||||
return dict(code=400, msg=f"角色{role.rid}不存在")
|
||||
|
||||
# 创建用户- 用户表写入数据
|
||||
user_obj = await UserDao.insert(data.dict())
|
||||
# 关联表写入数据
|
||||
await UserRoleDao.inserts(
|
||||
[dict(rid=role.rid, uid=user_obj.id, status=role.status) for role in rids]
|
||||
)
|
||||
return dict(data=user_obj)
|
||||
|
||||
async def get_item(self, pk):
|
||||
"""获取用户信息"""
|
||||
user_obj = await self.dao.select({"id": pk})
|
||||
if user_obj is None:
|
||||
return dict(code=400, msg="用户不存在")
|
||||
roles = await has_roles(user_obj.id)
|
||||
return dict(data=dict(**jsonable_encoder(user_obj), roles=roles))
|
||||
|
||||
async def update_item(self, pk, data):
|
||||
"""用户编辑修改"""
|
||||
if await self.dao.select({"id": pk}) is None:
|
||||
return dict(code=400, msg="用户不存在")
|
||||
|
||||
rids = data.roles
|
||||
del data.roles
|
||||
for role in rids:
|
||||
if await RoleDao.select({"id": role.rid, "status__not": 9}) is None:
|
||||
return role.rid
|
||||
# 更新用户
|
||||
if data.password != "加密之后的密码":
|
||||
data.password = get_password_hash(data.password)
|
||||
else:
|
||||
del data.password
|
||||
await UserDao.update(dict(id=pk), data.dict())
|
||||
|
||||
# todo 1. 先前有的角色,这次更新成没有 2. 先前没有的角色 这次更新成有, 3. 只更新了状态
|
||||
|
||||
roles = await has_roles(pk)
|
||||
|
||||
# 2. 将先有的数据标记 删除
|
||||
[
|
||||
await UserRoleDao.update(dict(rid=role["id"], uid=pk), dict(status=9))
|
||||
for role in roles
|
||||
]
|
||||
|
||||
# 2. 新增次此更新的数据
|
||||
await UserRoleDao.inserts(
|
||||
[dict(role.dict(), uid=pk, status=role.status) for role in rids]
|
||||
)
|
||||
return dict()
|
||||
|
||||
@staticmethod
|
||||
async def change_current_role(uid, rid):
|
||||
"""用户切换角色"""
|
||||
# 1.将用户id 未删除角色状态置为正常 1 ( 除切换角色id )
|
||||
await UserRoleDao.update(
|
||||
dict(uid=uid, rid__not=rid, status__not=9), dict(status=1)
|
||||
)
|
||||
# 2.将用户id 角色id 和当前角色匹配的数据置为选中
|
||||
res = await UserRoleDao.update(
|
||||
dict(uid=uid, rid=rid, status__not=9), dict(status=5)
|
||||
)
|
||||
if res == 0:
|
||||
return dict(code=400, msg=f"角色不存在{res}")
|
||||
return dict()
|
||||
|
||||
|
||||
service = UserService()
|
Reference in New Issue
Block a user