upload project source code
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Path
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
|
||||
from app.common.request import PaginationService
|
||||
from app.common.response import SuccessResponse, StreamResponse
|
||||
from app.core.router_class import OperationLogRoute
|
||||
from app.utils.common_util import bytes2file_response
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.logger import log
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .schema import OperationLogQueryParam
|
||||
from .service import OperationLogService
|
||||
|
||||
|
||||
LogRouter = APIRouter(route_class=OperationLogRoute, prefix="/log", tags=["日志管理"])
|
||||
|
||||
|
||||
@LogRouter.get("/list", summary="查询日志", description="查询日志")
|
||||
async def get_obj_list_controller(
|
||||
page: PaginationQueryParam = Depends(),
|
||||
search: OperationLogQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:log:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询日志
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (OperationLogQueryParam): 日志查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含分页日志详情的 JSON 响应模型
|
||||
"""
|
||||
order_by = [{"created_time": "desc"}]
|
||||
if page.order_by:
|
||||
order_by = page.order_by
|
||||
result_dict_list = await OperationLogService.get_log_list_service(search=search, auth=auth, order_by=order_by)
|
||||
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= page.page_no, page_size = page.page_size)
|
||||
log.info(f"查询日志成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询日志成功")
|
||||
|
||||
|
||||
@LogRouter.get("/detail/{id}", summary="日志详情", description="日志详情")
|
||||
async def get_obj_detail_controller(
|
||||
id: int = Path(..., description="操作日志ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:log:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取日志详情
|
||||
|
||||
参数:
|
||||
- id (int): 操作日志ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含日志详情的 JSON 响应模型
|
||||
"""
|
||||
result_dict = await OperationLogService.get_log_detail_service(id=id, auth=auth)
|
||||
log.info(f"查询日志成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取日志详情成功")
|
||||
|
||||
|
||||
@LogRouter.delete("/delete", summary="删除日志", description="删除日志")
|
||||
async def delete_obj_log_controller(
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:log:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除日志
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 日志 ID 列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除结果的 JSON 响应模型
|
||||
"""
|
||||
await OperationLogService.delete_log_service(ids=ids, auth=auth)
|
||||
log.info(f"删除日志成功 {ids}")
|
||||
return SuccessResponse(msg="删除日志成功")
|
||||
|
||||
|
||||
@LogRouter.post("/export", summary="导出日志", description="导出日志")
|
||||
async def export_obj_list_controller(
|
||||
search: OperationLogQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["module_system:log:export"]))
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
导出日志
|
||||
|
||||
参数:
|
||||
- search (OperationLogQueryParam): 日志查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 包含导出日志的流式响应模型
|
||||
"""
|
||||
operation_log_list = await OperationLogService.get_log_list_service(search=search, auth=auth)
|
||||
operation_log_export_result = await OperationLogService.export_log_list_service(operation_log_list=operation_log_list)
|
||||
log.info('导出日志成功')
|
||||
|
||||
return StreamResponse(
|
||||
data=bytes2file_response(operation_log_export_result),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers = {
|
||||
'Content-Disposition': 'attachment; filename=log.xlsx'
|
||||
}
|
||||
)
|
||||
61
后端源码/yifan.action-ai.cn/app/api/v1/module_system/log/crud.py
Normal file
61
后端源码/yifan.action-ai.cn/app/api/v1/module_system/log/crud.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Sequence
|
||||
|
||||
from app.core.base_crud import CRUDBase
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .model import OperationLogModel
|
||||
from .schema import OperationLogCreateSchema
|
||||
|
||||
|
||||
class OperationLogCRUD(CRUDBase[OperationLogModel, OperationLogCreateSchema, OperationLogCreateSchema]):
|
||||
"""
|
||||
操作日志数据层。
|
||||
"""
|
||||
|
||||
def __init__(self, auth: AuthSchema) -> None:
|
||||
"""
|
||||
初始化操作日志CRUD。
|
||||
"""
|
||||
self.auth = auth
|
||||
super().__init__(model=OperationLogModel, auth=auth)
|
||||
|
||||
async def create_crud(self, data: OperationLogCreateSchema) -> OperationLogModel | None:
|
||||
"""
|
||||
创建操作日志记录。
|
||||
|
||||
参数:
|
||||
- data (OperationLogCreateSchema): 操作日志创建模型。
|
||||
|
||||
返回:
|
||||
- OperationLogModel | None: 创建后的日志记录。
|
||||
"""
|
||||
return await self.create(data=data)
|
||||
|
||||
async def get_by_id_crud(self, id: int, preload: list | None = None) -> OperationLogModel | None:
|
||||
"""
|
||||
根据ID获取操作日志详情。
|
||||
|
||||
参数:
|
||||
- id (int): 操作日志ID。
|
||||
- preload (list | None): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- OperationLogModel | None: 操作日志记录。
|
||||
"""
|
||||
return await self.get(id=id, preload=preload)
|
||||
|
||||
async def get_list_crud(self, search: dict | None = None, order_by: list | None = None, preload: list | None = None) -> Sequence[OperationLogModel]:
|
||||
"""
|
||||
获取操作日志列表。
|
||||
|
||||
参数:
|
||||
- search (Dict | None): 搜索条件字典。
|
||||
- order_by (List[Dict[str, str]] | None): 排序字段列表。
|
||||
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
|
||||
|
||||
返回:
|
||||
- Sequence[OperationLogModel]: 操作日志列表。
|
||||
"""
|
||||
return await self.list(search=search, order_by=order_by, preload=preload)
|
||||
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from sqlalchemy import String, Integer, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import ModelMixin, UserMixin
|
||||
|
||||
|
||||
class OperationLogModel(ModelMixin, UserMixin):
|
||||
"""
|
||||
系统日志模型
|
||||
日志类型:
|
||||
- 1: 登录日志
|
||||
- 2: 操作日志
|
||||
"""
|
||||
__tablename__: str = "sys_log"
|
||||
__table_args__: dict[str, str] = ({'comment': '系统日志表'})
|
||||
__loader_options__: list[str] = ["created_by", "updated_by"]
|
||||
|
||||
type: Mapped[int] = mapped_column(Integer, comment="日志类型(1登录日志 2操作日志)")
|
||||
request_path: Mapped[str] = mapped_column(String(255), comment="请求路径")
|
||||
request_method: Mapped[str] = mapped_column(String(10), comment="请求方式")
|
||||
request_payload: Mapped[str | None] = mapped_column(Text, comment="请求体")
|
||||
request_ip: Mapped[str | None] = mapped_column(String(50), comment="请求IP地址")
|
||||
login_location: Mapped[str | None] = mapped_column(String(255), comment="登录位置")
|
||||
request_os: Mapped[str | None] = mapped_column(String(64), nullable=True, comment="操作系统")
|
||||
request_browser: Mapped[str | None] = mapped_column(String(64), nullable=True, comment="浏览器")
|
||||
response_code: Mapped[int] = mapped_column(Integer, comment="响应状态码")
|
||||
response_json: Mapped[str | None] = mapped_column(Text, nullable=True, comment="响应体")
|
||||
process_time: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="处理时间")
|
||||
@@ -0,0 +1,97 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import re
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from fastapi import Query
|
||||
|
||||
from app.core.validator import DateTimeStr
|
||||
from app.core.base_schema import BaseSchema, UserBySchema
|
||||
|
||||
|
||||
class OperationLogCreateSchema(BaseModel):
|
||||
"""日志创建模型"""
|
||||
type: int | None = Field(default=None, description="日志类型(1登录日志 2操作日志)")
|
||||
request_path: str | None = Field(default=None, description="请求路径")
|
||||
request_method: str | None = Field(default=None, description="请求方法")
|
||||
request_payload: str | None = Field(default=None, description="请求负载")
|
||||
request_ip: str | None = Field(default=None, description="请求 IP 地址")
|
||||
login_location: str | None = Field(default=None, description="登录位置")
|
||||
request_os: str | None = Field(default=None, description="请求操作系统")
|
||||
request_browser: str | None = Field(default=None, description="请求浏览器")
|
||||
response_code: int | None = Field(default=None, description="响应状态码")
|
||||
response_json: str | None = Field(default=None, description="响应 JSON 数据")
|
||||
process_time: str | None = Field(default=None, description="处理时间")
|
||||
status: str = Field(default="0", description="是否成功")
|
||||
description: str | None = Field(default=None, max_length=255, description="描述")
|
||||
created_id: int | None = Field(default=None, description="创建人ID")
|
||||
updated_id: int | None = Field(default=None, description="更新人ID")
|
||||
|
||||
@field_validator("type")
|
||||
@classmethod
|
||||
def _validate_type(cls, value: int):
|
||||
if value is None:
|
||||
return value
|
||||
if value not in {1, 2}:
|
||||
raise ValueError("日志类型仅支持 1(登录) 或 2(操作)")
|
||||
return value
|
||||
|
||||
@field_validator("request_method")
|
||||
@classmethod
|
||||
def _validate_method(cls, value: str):
|
||||
if value is None:
|
||||
return value
|
||||
allowed = {"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"}
|
||||
if value.upper() not in allowed:
|
||||
raise ValueError(f"请求方法必须为 {', '.join(sorted(allowed))}")
|
||||
return value.upper()
|
||||
|
||||
@field_validator("request_ip")
|
||||
@classmethod
|
||||
def _validate_ip(cls, value: str | None):
|
||||
if value is None or value == "":
|
||||
return value
|
||||
ipv4 = r"^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}$"
|
||||
ipv6 = r"^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"
|
||||
if not re.match(ipv4, value) and not re.match(ipv6, value):
|
||||
raise ValueError("请求IP必须为有效的IPv4或IPv6地址")
|
||||
return value
|
||||
|
||||
|
||||
class OperationLogOutSchema(OperationLogCreateSchema, BaseSchema, UserBySchema):
|
||||
"""日志响应模型"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class OperationLogQueryParam:
|
||||
"""操作日志查询参数"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
type: int | None = Query(None, description="日志类型(1:登录日志, 2:操作日志)"),
|
||||
request_path: str | None = Query(None, description="请求路径"),
|
||||
request_method: str | None = Query(None, description="请求方法"),
|
||||
request_ip: str | None = Query(None, description="请求IP"),
|
||||
response_code: int | None = Query(None, description="响应状态码"),
|
||||
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
|
||||
created_id: int | None = Query(None, description="创建人"),
|
||||
updated_id: int | None = Query(None, description="更新人"),
|
||||
) -> None:
|
||||
|
||||
# 模糊查询字段
|
||||
self.request_path = ("like", f"%{request_path}%") if request_path else None
|
||||
|
||||
# 精确查询字段
|
||||
self.created_id = created_id
|
||||
self.updated_id = updated_id
|
||||
self.request_method = request_method
|
||||
self.request_ip = request_ip
|
||||
self.response_code = response_code
|
||||
self.type = type
|
||||
|
||||
# 时间范围查询 - 增加对单个时间参数的处理
|
||||
if created_time and len(created_time) == 2:
|
||||
self.created_time = ("between", (created_time[0], created_time[1]))
|
||||
if updated_time and len(updated_time) == 2:
|
||||
self.updated_time = ("between", (updated_time[0], updated_time[1]))
|
||||
|
||||
127
后端源码/yifan.action-ai.cn/app/api/v1/module_system/log/service.py
Normal file
127
后端源码/yifan.action-ai.cn/app/api/v1/module_system/log/service.py
Normal file
@@ -0,0 +1,127 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from app.core.exceptions import CustomException
|
||||
from app.utils.excel_util import ExcelUtil
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .crud import OperationLogCRUD
|
||||
from .schema import (
|
||||
OperationLogCreateSchema,
|
||||
OperationLogOutSchema,
|
||||
OperationLogQueryParam
|
||||
)
|
||||
|
||||
|
||||
class OperationLogService:
|
||||
"""
|
||||
日志模块服务层
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
async def get_log_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取日志详情
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 日志 ID
|
||||
|
||||
返回:
|
||||
- dict: 日志详情字典
|
||||
"""
|
||||
log = await OperationLogCRUD(auth).get_by_id_crud(id=id)
|
||||
log_dict = OperationLogOutSchema.model_validate(log).model_dump()
|
||||
return log_dict
|
||||
|
||||
@classmethod
|
||||
async def get_log_list_service(cls, auth: AuthSchema, search: OperationLogQueryParam | None = None, order_by: list | None = None) -> list[dict]:
|
||||
"""
|
||||
获取日志列表
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- search (OperationLogQueryParam | None): 日志查询参数模型
|
||||
- order_by (list | None): 排序字段列表
|
||||
|
||||
返回:
|
||||
- list[dict]: 日志详情字典列表
|
||||
"""
|
||||
|
||||
log_list = await OperationLogCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
|
||||
log_dict_list = [OperationLogOutSchema.model_validate(log).model_dump() for log in log_list]
|
||||
return log_dict_list
|
||||
|
||||
@classmethod
|
||||
async def create_log_service(cls, auth: AuthSchema, data: OperationLogCreateSchema) -> dict:
|
||||
"""
|
||||
创建日志
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- data (OperationLogCreateSchema): 日志创建模型
|
||||
|
||||
返回:
|
||||
- dict: 日志详情字典
|
||||
"""
|
||||
new_log = await OperationLogCRUD(auth).create(data=data)
|
||||
new_log_dict = OperationLogOutSchema.model_validate(new_log).model_dump()
|
||||
return new_log_dict
|
||||
|
||||
@classmethod
|
||||
async def delete_log_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除日志
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- ids (list[int]): 日志 ID 列表
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
await OperationLogCRUD(auth).delete(ids=ids)
|
||||
|
||||
@classmethod
|
||||
async def export_log_list_service(cls, operation_log_list: list[dict]) -> bytes:
|
||||
"""
|
||||
导出日志信息
|
||||
|
||||
参数:
|
||||
- operation_log_list (list[dict]): 操作日志信息列表
|
||||
|
||||
返回:
|
||||
- bytes: 操作日志信息excel的二进制数据
|
||||
"""
|
||||
# 操作日志字段映射
|
||||
mapping_dict = {
|
||||
'id': '编号',
|
||||
'type': '日志类型',
|
||||
'request_path': '请求URL',
|
||||
'request_method': '请求方式',
|
||||
'request_payload': '请求参数',
|
||||
'request_ip': '操作地址',
|
||||
'login_location': '登录位置',
|
||||
'request_os': '操作系统',
|
||||
'request_browser': '浏览器',
|
||||
'response_json': '返回参数',
|
||||
'response_code': '相应状态',
|
||||
'process_time': '处理时间',
|
||||
'description': '备注',
|
||||
'created_time': '创建时间',
|
||||
'updated_time': '更新时间',
|
||||
'created_id': '创建者ID',
|
||||
'updated_id': '更新者ID',
|
||||
}
|
||||
|
||||
# 处理数据
|
||||
data = operation_log_list.copy()
|
||||
for item in data:
|
||||
# 处理状态
|
||||
item['response_code'] = '成功' if item.get('response_code') == 200 else '失败'
|
||||
# 处理日志类型 - 修正与schema.py保持一致
|
||||
item['type'] = '登录日志' if item.get('type') == 1 else '操作日志'
|
||||
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
|
||||
|
||||
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
|
||||
Reference in New Issue
Block a user