|
@@ -13,6 +13,10 @@ from utils.ry_system_util import *
|
|
|
import json
|
|
|
from sqlalchemy.sql import func
|
|
|
from common.auth_user import *
|
|
|
+from common.enc import mpfun, sys_menu_data, sys_menu_layer_data
|
|
|
+import traceback
|
|
|
+from common.db import db_czrz
|
|
|
+from exceptions import HmacException
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
@@ -470,19 +474,21 @@ class SysMuneCreateForm(BaseModel):
|
|
|
queryParam:str=None
|
|
|
status:str
|
|
|
visible:str
|
|
|
- layer_template:str
|
|
|
+ layer_template:str=None
|
|
|
|
|
|
|
|
|
@router.post('/create')
|
|
|
async def create(
|
|
|
- form_data: SysMuneCreateForm,
|
|
|
- db: Session = Depends(get_db),
|
|
|
- body=Depends(remove_xss_json),
|
|
|
- user_id=Depends(valid_access_token)
|
|
|
+ request: Request,
|
|
|
+ form_data: SysMuneCreateForm,
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ body=Depends(remove_xss_json),
|
|
|
+ auth_user: AuthUser = Depends(find_auth_user),
|
|
|
+ user_id=Depends(valid_access_token)
|
|
|
):
|
|
|
try:
|
|
|
# 开始事务
|
|
|
- db.begin()
|
|
|
+ # db.begin()
|
|
|
|
|
|
# 创建新的菜单项
|
|
|
new_menu = SysMenu(
|
|
@@ -525,7 +531,8 @@ async def create(
|
|
|
db.add(new_menu_layer)
|
|
|
|
|
|
# 提交事务之前刷新new_menu以确保menu_id被数据库生成
|
|
|
- db.flush() # 使用flush而不是commit,这样可以保存数据但不结束事务
|
|
|
+ # db.flush() # 使用flush而不是commit,这样可以保存数据但不结束事务
|
|
|
+
|
|
|
db.refresh(new_menu) # 刷新new_menu对象以获取数据库生成的menu_id
|
|
|
new_menu_layer.menu_id = new_menu.menu_id # 现在可以设置new_menu_layer的menu_id了
|
|
|
|
|
@@ -535,12 +542,19 @@ async def create(
|
|
|
# 提交事务
|
|
|
db.commit()
|
|
|
|
|
|
+ sys_menu_data.sign_table()
|
|
|
+ sys_menu_layer_data.sign_table()
|
|
|
+
|
|
|
+ db_czrz.log(db, auth_user, "系统管理", f"后台管理新建地图菜单【{form_data.menuName}】成功", request.client.host)
|
|
|
+
|
|
|
return {
|
|
|
"code": 200,
|
|
|
"data": new_menu_layer.menu_id, # 返回新创建的SysMenuLayer的ID
|
|
|
"msg": "操作成功"
|
|
|
}
|
|
|
except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
# 如果发生异常,回滚事务
|
|
|
db.rollback()
|
|
|
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
|
|
@@ -565,6 +579,7 @@ class SysMuneUpdateForm(BaseModel):
|
|
|
@router.put('')
|
|
|
async def update(
|
|
|
request: Request,
|
|
|
+ auth_user: AuthUser = Depends(find_auth_user),
|
|
|
# form_data: SysMuneUpdateForm,
|
|
|
db: Session = Depends(get_db),
|
|
|
body = Depends(remove_xss_json),
|
|
@@ -572,44 +587,44 @@ async def update(
|
|
|
):
|
|
|
|
|
|
try:
|
|
|
- db.begin()
|
|
|
- query = db.query(SysMenu)
|
|
|
- query = query.filter(SysMenu.menu_id == body['menuId'])
|
|
|
- query = query.filter(SysMenu.del_flag != '2')
|
|
|
- menu = query.first()
|
|
|
- if not menu:
|
|
|
- detail = "菜单不存在"
|
|
|
- raise HTTPException(status_code=404, detail="菜单不存在")
|
|
|
-
|
|
|
- # 更新字段,排除主键和不可更新的字段
|
|
|
- if 'component' in body:
|
|
|
- menu.component=body['component']
|
|
|
- if 'icon' in body:
|
|
|
- menu.icon=body['icon']
|
|
|
- if 'isCache' in body:
|
|
|
- menu.is_cache=body['isCache']
|
|
|
- if 'isFrame' in body:
|
|
|
- menu.is_frame=body['isFrame']
|
|
|
- if 'menuName' in body:
|
|
|
- menu.menu_name=body['menuName']
|
|
|
- if 'menuType' in body:
|
|
|
- menu.menu_type=body['menuType']
|
|
|
- if 'orderNum' in body:
|
|
|
- menu.order_num=body['orderNum']
|
|
|
- if 'parentId' in body:
|
|
|
- menu.parent_id=body['parentId']
|
|
|
- if 'path' in body:
|
|
|
- menu.path=body['path']
|
|
|
- if 'perms' in body:
|
|
|
- menu.perms=body['perms']
|
|
|
- if 'queryParam' in body:
|
|
|
- menu.query_param=body['queryParam']
|
|
|
- if 'status' in body:
|
|
|
- menu.status=body['status']
|
|
|
- if 'visible' in body:
|
|
|
- menu.visible=body['visible']
|
|
|
- if user_id:
|
|
|
- menu.create_by = user_id
|
|
|
+ # db.begin()
|
|
|
+ # query = db.query(SysMenuLayer)
|
|
|
+ # query = query.filter(SysMenuLayer.menu_id == body['menuId'])
|
|
|
+ # query = query.filter(SysMenuLayer.del_flag != '2')
|
|
|
+ # menu = query.first()
|
|
|
+ # if not menu:
|
|
|
+ # detail = "菜单不存在"
|
|
|
+ # raise HTTPException(status_code=404, detail="菜单不存在")
|
|
|
+
|
|
|
+ # # 更新字段,排除主键和不可更新的字段
|
|
|
+ # if 'component' in body:
|
|
|
+ # menu.component=body['component']
|
|
|
+ # if 'icon' in body:
|
|
|
+ # menu.icon=body['icon']
|
|
|
+ # if 'isCache' in body:
|
|
|
+ # menu.is_cache=body['isCache']
|
|
|
+ # if 'isFrame' in body:
|
|
|
+ # menu.is_frame=body['isFrame']
|
|
|
+ # if 'menuName' in body:
|
|
|
+ # menu.menu_name=body['menuName']
|
|
|
+ # if 'menuType' in body:
|
|
|
+ # menu.menu_type=body['menuType']
|
|
|
+ # if 'orderNum' in body:
|
|
|
+ # menu.order_num=body['orderNum']
|
|
|
+ # if 'parentId' in body:
|
|
|
+ # menu.parent_id=body['parentId']
|
|
|
+ # if 'path' in body:
|
|
|
+ # menu.path=body['path']
|
|
|
+ # if 'perms' in body:
|
|
|
+ # menu.perms=body['perms']
|
|
|
+ # if 'queryParam' in body:
|
|
|
+ # menu.query_param=body['queryParam']
|
|
|
+ # if 'status' in body:
|
|
|
+ # menu.status=body['status']
|
|
|
+ # if 'visible' in body:
|
|
|
+ # menu.visible=body['visible']
|
|
|
+ # if user_id:
|
|
|
+ # menu.create_by = user_id
|
|
|
# for field, value in menu_data.items():
|
|
|
# if field != 'menu_id' and field in menu_to_update.__dict__:
|
|
|
# setattr(menu_to_update, field, value)
|
|
@@ -653,16 +668,25 @@ async def update(
|
|
|
menu.status = body['status']
|
|
|
if 'visible' in body:
|
|
|
menu.visible = body['visible']
|
|
|
- if user_id:
|
|
|
- menu.create_by = user_id
|
|
|
- if user_id:
|
|
|
- menu.layer_template =body['layer_template']
|
|
|
+ if 'layer_template' in body:
|
|
|
+ menu.layer_template = body['layer_template']
|
|
|
+
|
|
|
+ menu.update_by = user_id
|
|
|
+ menu.update_time = datetime.now()
|
|
|
+ menu.sign = sys_menu_layer_data.get_sign_hmac(menu)
|
|
|
+
|
|
|
db.commit()
|
|
|
+
|
|
|
+ db_czrz.log(db, auth_user, "系统管理", f"后台管理更新地图菜单【{body['menuName']}】成功", request.client.host)
|
|
|
+
|
|
|
+
|
|
|
return {
|
|
|
"code": 200,
|
|
|
"msg": "菜单更新成功"
|
|
|
}
|
|
|
except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
# db.rollback()
|
|
|
if str(e)=='':
|
|
|
e = detail
|
|
@@ -672,9 +696,11 @@ async def update(
|
|
|
|
|
|
@router.delete('/{menu_id}')
|
|
|
async def delete(
|
|
|
+ request: Request,
|
|
|
menu_id: int,
|
|
|
db: Session = Depends(get_db),
|
|
|
body = Depends(remove_xss_json),
|
|
|
+ auth_user: AuthUser = Depends(find_auth_user),
|
|
|
user_id = Depends(valid_access_token)
|
|
|
):
|
|
|
try:
|
|
@@ -701,15 +727,21 @@ async def delete(
|
|
|
if not menu_to_delete:
|
|
|
detail = "菜单不存在"
|
|
|
raise HTTPException(status_code=404, detail="菜单不存在")
|
|
|
- menu_to_delete.create_by = user_id
|
|
|
+ menu_to_delete.update_by = user_id
|
|
|
+ menu_to_delete.update_time = datetime.now()
|
|
|
menu_to_delete.del_flag = '2'
|
|
|
-
|
|
|
+ menu_to_delete.sign = sys_menu_layer_data.get_sign_hmac(menu_to_delete)
|
|
|
db.commit()
|
|
|
+
|
|
|
+ db_czrz.log(db, auth_user, "系统管理", f"后台管理删除菜单【{menu_to_delete.menu_name}】成功", request.client.host)
|
|
|
+
|
|
|
return {
|
|
|
"code": 200,
|
|
|
"msg": "菜单删除成功"
|
|
|
}
|
|
|
except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
db.rollback()
|
|
|
if str(e)=='':
|
|
|
e = detail
|