diff --git a/inventory-backend/app/api/v1/transactions.py b/inventory-backend/app/api/v1/transactions.py index 20b3fcf..900ab22 100644 --- a/inventory-backend/app/api/v1/transactions.py +++ b/inventory-backend/app/api/v1/transactions.py @@ -41,6 +41,16 @@ def get_current_user_info(): 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'): """ 根据用户权限过滤 item 字典,无权限的字段值置为 None @@ -102,9 +112,10 @@ def scan_borrowed_item(): ) def submit_return(): data = request.get_json() - user = get_jwt_identity() # 库管 + # ★ 归还人存"姓名",而非 JWT 数字 ID + operator_name = _current_username() try: - TransService.process_return(data, operator_name=user) + TransService.process_return(data, operator_name=operator_name) return jsonify({'code': 200, 'msg': '还库成功'}) except Exception as e: return jsonify({'code': 400, 'msg': str(e)}), 400 @@ -131,9 +142,9 @@ def scrap_borrow(): if not record_ids: return jsonify({'code': 400, 'msg': '请选择要报废的借出记录'}), 400 - user = get_jwt_identity() or 'Unknown' + operator_name = _current_username() or 'Unknown' 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}) except ValueError as e: return jsonify({'code': 400, 'msg': str(e)}), 400 @@ -374,7 +385,7 @@ def dispatch_borrow(): borrow_no = TransService.execute_dispatch( approval_id=approval_id, items=data.get('items', []), - operator_name=get_jwt_identity(), + operator_name=_current_username(), borrower_name=data.get('borrower_name'), signature=data.get('signature_path'), remark=data.get('remark'),