|
@@ -109,37 +109,79 @@ async def roleupdate(
|
|
|
traceback.print_exc()
|
|
|
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
|
|
|
+@router.put('/dataScope')
|
|
|
+async def roleupdate(
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ user_id: int = Depends(valid_access_token),
|
|
|
+ body = Depends(remove_xss_json)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ roleId = body['roleId']
|
|
|
+
|
|
|
+ query = db.query(SysRole)
|
|
|
+ query = query.filter(SysRole.del_flag != '2')
|
|
|
+ query = query.filter(SysRole.role_id == roleId)
|
|
|
+ role = query.first()
|
|
|
+ if not role :
|
|
|
+ return JSONResponse(status_code=410, content={
|
|
|
+ 'errcode': 410,
|
|
|
+ 'errmsg': f'角色{roleId}不存在'
|
|
|
+ })
|
|
|
+
|
|
|
+ role.data_scope = body['dataScope']
|
|
|
+
|
|
|
+
|
|
|
+ deptIds = body['deptIds']
|
|
|
+
|
|
|
+
|
|
|
+ # 清除当前用户的所有角色关联
|
|
|
+ db.query(SysRoleMenu).filter(SysRoleMenu.role_id == roleId).delete()
|
|
|
+
|
|
|
+ # 创建新的用户角色关联
|
|
|
+ new_role_depts = [SysRoleDept(role_id=roleId, dept_id=dept_id) for dept_id in deptIds]
|
|
|
+ db.add_all(new_role_depts)
|
|
|
+
|
|
|
+
|
|
|
+ db.commit()
|
|
|
+ return {"code": 200, "msg": "更新成功", "data": None}
|
|
|
+
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
|
|
|
#deptTree
|
|
|
@router.get('/deptTree/{roleId}')
|
|
|
async def getmunutreeselect(request: Request,roleId:int,db: Session = Depends(get_db), user_id: int = Depends(valid_access_token)):
|
|
|
- def build_dept_tree(menus, parent_dept):
|
|
|
- menu_tree = []
|
|
|
- for menu_info in menus:
|
|
|
- menu = {
|
|
|
- "id": menu_info.menu_id,
|
|
|
- "label": menu_info.menu_name,
|
|
|
- "parentId": menu_info.parent_id,
|
|
|
- "weight": menu_info.order_num
|
|
|
+ def build_dept_tree(depts, parent_dept):
|
|
|
+ dept_tree = []
|
|
|
+ for dept_info in depts:
|
|
|
+ dept = {
|
|
|
+ "id": dept_info.dept_id,
|
|
|
+ "label": dept_info.dept_name,
|
|
|
+ "parentId": dept_info.parent_id,
|
|
|
+ "weight": dept_info.order_num
|
|
|
}
|
|
|
# print(dept_info.dept_id)
|
|
|
- children = parent_id_get_menu_info(db, menu_info.menu_id)
|
|
|
+ children = parent_id_get_dept_info(db, dept_info.dept_id)
|
|
|
if len(children) > 0:
|
|
|
- children_depts = build_dept_tree(children, menu)
|
|
|
- menu["children"] = children_depts
|
|
|
- menu_tree.append(menu)
|
|
|
- return menu_tree
|
|
|
+ children_depts = build_dept_tree(children, dept)
|
|
|
+ dept["children"] = children_depts
|
|
|
+ dept_tree.append(dept)
|
|
|
+ return dept_tree
|
|
|
|
|
|
- checkedKeys = role_id_get_role_menus(db,roleId)
|
|
|
- menus = build_dept_tree(parent_id_get_menu_info(db, 0), None)
|
|
|
+ checkedKeys = role_id_get_role_depts(db, roleId)
|
|
|
+ result = build_dept_tree(parent_id_get_dept_info(db, 0), None)
|
|
|
return {
|
|
|
"code": 200,
|
|
|
"msg": "操作成功",
|
|
|
- "data": {"menus":menus,"checkedKeys":checkedKeys}
|
|
|
+ "data": {"depts":result,"checkedKeys":checkedKeys}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
@router.get('/list')
|
|
|
async def rolelist( roleName: int = Query(None ,description='角色名称'),
|
|
|
roleKey: str = Query(None, description='权限字符'),
|