fix: 借库归还/报废/借库执行操作人由 JWT 数字 ID 改为用户名(归还人不再显示数字)

This commit is contained in:
yueli
2026-09-04 16:10:00 +08:00
parent 808e491fe8
commit 02f36b383c

View File

@ -41,6 +41,16 @@ def get_current_user_info():
return user.id if user else None, user.role if user else None return user.id if user else None, user.role if user else None
def _current_username():
"""获取当前登录用户的用户名(姓名/账号),用于操作人展示;避免把 JWT 数字 ID 存进记录"""
identity = get_jwt_identity()
if not identity:
return 'System'
from app.models.system import SysUser
user = SysUser.query.get(identity)
return user.username if user else str(identity)
def filter_item_by_permissions(item_dict, user_permissions, prefix='op_records'): def filter_item_by_permissions(item_dict, user_permissions, prefix='op_records'):
""" """
根据用户权限过滤 item 字典,无权限的字段值置为 None 根据用户权限过滤 item 字典,无权限的字段值置为 None
@ -102,9 +112,10 @@ def scan_borrowed_item():
) )
def submit_return(): def submit_return():
data = request.get_json() data = request.get_json()
user = get_jwt_identity() # 库管 # ★ 归还人存"姓名",而非 JWT 数字 ID
operator_name = _current_username()
try: try:
TransService.process_return(data, operator_name=user) TransService.process_return(data, operator_name=operator_name)
return jsonify({'code': 200, 'msg': '还库成功'}) return jsonify({'code': 200, 'msg': '还库成功'})
except Exception as e: except Exception as e:
return jsonify({'code': 400, 'msg': str(e)}), 400 return jsonify({'code': 400, 'msg': str(e)}), 400
@ -131,9 +142,9 @@ def scrap_borrow():
if not record_ids: if not record_ids:
return jsonify({'code': 400, 'msg': '请选择要报废的借出记录'}), 400 return jsonify({'code': 400, 'msg': '请选择要报废的借出记录'}), 400
user = get_jwt_identity() or 'Unknown' operator_name = _current_username() or 'Unknown'
try: try:
result = TransService.scrap_borrow(record_ids, operator_name=user, reason=reason) result = TransService.scrap_borrow(record_ids, operator_name=operator_name, reason=reason)
return jsonify({'code': 200, 'msg': f'已报废 {result["count"]} 条借出记录', 'data': result}) return jsonify({'code': 200, 'msg': f'已报废 {result["count"]} 条借出记录', 'data': result})
except ValueError as e: except ValueError as e:
return jsonify({'code': 400, 'msg': str(e)}), 400 return jsonify({'code': 400, 'msg': str(e)}), 400
@ -374,7 +385,7 @@ def dispatch_borrow():
borrow_no = TransService.execute_dispatch( borrow_no = TransService.execute_dispatch(
approval_id=approval_id, approval_id=approval_id,
items=data.get('items', []), items=data.get('items', []),
operator_name=get_jwt_identity(), operator_name=_current_username(),
borrower_name=data.get('borrower_name'), borrower_name=data.get('borrower_name'),
signature=data.get('signature_path'), signature=data.get('signature_path'),
remark=data.get('remark'), remark=data.get('remark'),