mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-24 05:26:58 +00:00
本次提交包含多项优化和修复: 1. 修复CRUD初始化参数传递、搜索参数处理逻辑 2. 更新环境配置中的大模型相关参数 3. 重构部分服务方法命名,统一代码风格 4. 新增多个枚举类型,补充模型关联关系和加载选项 5. 优化查询参数类实现,完善字段校验逻辑 6. 调整Pydantic模型字段注释和类型定义 7. 简化并移除冗余的CRUD方法实现 8. 新增超级管理员权限装饰器 9. 修复邮件日志模型的租户关联和字段定义
290 lines
9.5 KiB
Python
290 lines
9.5 KiB
Python
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Body, Depends, Path
|
|
|
|
from app.common.response import ResponseSchema, SuccessResponse
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.core.base_schema import AuthSchema, PageResultSchema
|
|
from app.core.dependencies import AuthPermission
|
|
from app.core.router_class import OperationLogRoute
|
|
|
|
from .schema import (
|
|
EmailConfigCreateSchema,
|
|
EmailConfigOutSchema,
|
|
EmailConfigQueryParam,
|
|
EmailConfigUpdateSchema,
|
|
EmailLogOutSchema,
|
|
EmailLogQueryParam,
|
|
EmailSendSchema,
|
|
EmailTemplateCreateSchema,
|
|
EmailTemplateOutSchema,
|
|
EmailTemplateQueryParam,
|
|
EmailTemplateUpdateSchema,
|
|
EmailTestSchema,
|
|
)
|
|
from .service import EmailConfigService, EmailLogService, EmailSendService, EmailTemplateService
|
|
|
|
EmailRouter = APIRouter(route_class=OperationLogRoute, prefix="/email", tags=["平台管理/邮件服务"])
|
|
|
|
|
|
@EmailRouter.get("/config/list", summary="SMTP 配置列表", response_model=ResponseSchema[PageResultSchema[EmailConfigOutSchema]])
|
|
async def email_config_list_controller(
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[EmailConfigQueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:query"]))],
|
|
):
|
|
"""
|
|
SMTP 配置列表
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数。
|
|
- search (EmailConfigQueryParam): 查询筛选参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含分页配置列表的 JSON 响应。
|
|
"""
|
|
result = await EmailConfigService.page_service(
|
|
auth=auth,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size,
|
|
search=search,
|
|
order_by=page.order_by,
|
|
)
|
|
return SuccessResponse(data=result, msg="查询成功")
|
|
|
|
|
|
@EmailRouter.get("/config/detail/{id}", summary="SMTP 配置详情", response_model=ResponseSchema[EmailConfigOutSchema])
|
|
async def email_config_detail_controller(
|
|
id: Annotated[int, Path()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:query"]))],
|
|
):
|
|
"""
|
|
SMTP 配置详情
|
|
|
|
参数:
|
|
- id (int): 配置 ID。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含配置详情的 JSON 响应。
|
|
"""
|
|
result = await EmailConfigService.detail_service(auth=auth, id=id)
|
|
return SuccessResponse(data=result, msg="查询成功")
|
|
|
|
|
|
@EmailRouter.post("/config/create", summary="创建 SMTP 配置", response_model=ResponseSchema[EmailConfigOutSchema])
|
|
async def email_config_create_controller(
|
|
data: EmailConfigCreateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
创建 SMTP 配置
|
|
|
|
参数:
|
|
- data (EmailConfigCreateSchema): 配置创建参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含创建后的配置详情的 JSON 响应。
|
|
"""
|
|
result = await EmailConfigService.create_service(auth=auth, data=data)
|
|
return SuccessResponse(data=result, msg="创建成功")
|
|
|
|
|
|
@EmailRouter.put("/config/update/{id}", summary="更新 SMTP 配置", response_model=ResponseSchema[EmailConfigOutSchema])
|
|
async def email_config_update_controller(
|
|
id: Annotated[int, Path()],
|
|
data: EmailConfigUpdateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
更新 SMTP 配置
|
|
|
|
参数:
|
|
- id (int): 配置 ID。
|
|
- data (EmailConfigUpdateSchema): 配置更新参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含更新后的配置详情的 JSON 响应。
|
|
"""
|
|
result = await EmailConfigService.update_service(auth=auth, id=id, data=data)
|
|
return SuccessResponse(data=result, msg="更新成功")
|
|
|
|
|
|
@EmailRouter.delete("/config/delete", summary="删除 SMTP 配置", response_model=ResponseSchema[None])
|
|
async def email_config_delete_controller(
|
|
ids: Annotated[list[int], Body()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
删除 SMTP 配置
|
|
|
|
参数:
|
|
- ids (list[int]): 配置 ID 列表。
|
|
|
|
返回:
|
|
- SuccessResponse: 删除结果。
|
|
"""
|
|
await EmailConfigService.delete_service(auth=auth, ids=ids)
|
|
return SuccessResponse(msg="删除成功")
|
|
|
|
|
|
@EmailRouter.post("/config/test", summary="测试 SMTP 连接", response_model=ResponseSchema)
|
|
async def email_config_test_controller(
|
|
data: EmailTestSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
测试 SMTP 连接
|
|
|
|
参数:
|
|
- data (EmailTestSchema): 测试参数(配置 ID 与目标邮箱)。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含测试结果的 JSON 响应。
|
|
"""
|
|
result = await EmailConfigService.test_service(auth=auth, data=data)
|
|
return SuccessResponse(data=result, msg="测试邮件已发送")
|
|
|
|
|
|
@EmailRouter.get("/template/list", summary="邮件模板列表", response_model=ResponseSchema[PageResultSchema[EmailTemplateOutSchema]])
|
|
async def email_template_list_controller(
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[EmailTemplateQueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:query"]))],
|
|
):
|
|
"""
|
|
邮件模板列表
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数。
|
|
- search (EmailTemplateQueryParam): 查询筛选参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含分页模板列表的 JSON 响应。
|
|
"""
|
|
result = await EmailTemplateService.page_service(
|
|
auth=auth,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size,
|
|
search=search,
|
|
order_by=page.order_by,
|
|
)
|
|
return SuccessResponse(data=result, msg="查询成功")
|
|
|
|
|
|
@EmailRouter.get("/template/detail/{id}", summary="邮件模板详情", response_model=ResponseSchema[EmailTemplateOutSchema])
|
|
async def email_template_detail_controller(
|
|
id: Annotated[int, Path()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:query"]))],
|
|
):
|
|
"""
|
|
邮件模板详情
|
|
|
|
参数:
|
|
- id (int): 模板 ID。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含模板详情的 JSON 响应。
|
|
"""
|
|
result = await EmailTemplateService.detail_service(auth=auth, id=id)
|
|
return SuccessResponse(data=result, msg="查询成功")
|
|
|
|
|
|
@EmailRouter.post("/template/create", summary="创建邮件模板", response_model=ResponseSchema[EmailTemplateOutSchema])
|
|
async def email_template_create_controller(
|
|
data: EmailTemplateCreateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
创建邮件模板
|
|
|
|
参数:
|
|
- data (EmailTemplateCreateSchema): 模板创建参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含创建后的模板详情的 JSON 响应。
|
|
"""
|
|
result = await EmailTemplateService.create_service(auth=auth, data=data)
|
|
return SuccessResponse(data=result, msg="创建成功")
|
|
|
|
|
|
@EmailRouter.put("/template/update/{id}", summary="更新邮件模板", response_model=ResponseSchema[EmailTemplateOutSchema])
|
|
async def email_template_update_controller(
|
|
id: Annotated[int, Path()],
|
|
data: EmailTemplateUpdateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
更新邮件模板
|
|
|
|
参数:
|
|
- id (int): 模板 ID。
|
|
- data (EmailTemplateUpdateSchema): 模板更新参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含更新后的模板详情的 JSON 响应。
|
|
"""
|
|
result = await EmailTemplateService.update_service(auth=auth, id=id, data=data)
|
|
return SuccessResponse(data=result, msg="更新成功")
|
|
|
|
|
|
@EmailRouter.delete("/template/delete", summary="删除邮件模板", response_model=ResponseSchema[None])
|
|
async def email_template_delete_controller(
|
|
ids: Annotated[list[int], Body()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
删除邮件模板
|
|
|
|
参数:
|
|
- ids (list[int]): 模板 ID 列表。
|
|
|
|
返回:
|
|
- SuccessResponse: 删除结果。
|
|
"""
|
|
await EmailTemplateService.delete_service(auth=auth, ids=ids)
|
|
return SuccessResponse(msg="删除成功")
|
|
|
|
|
|
@EmailRouter.post("/send", summary="手动发送邮件(超管测试/补发)", response_model=ResponseSchema)
|
|
async def email_send_controller(
|
|
data: EmailSendSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:update"]))],
|
|
):
|
|
"""
|
|
手动发送邮件
|
|
|
|
参数:
|
|
- data (EmailSendSchema): 发送参数(模板代码、目标邮箱、模板变量)。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含发送结果的 JSON 响应。
|
|
"""
|
|
result = await EmailSendService.manual_send_service(auth=auth, data=data)
|
|
return SuccessResponse(data=result, msg="发送成功")
|
|
|
|
|
|
@EmailRouter.get("/log/list", summary="邮件发送日志", response_model=ResponseSchema[PageResultSchema[EmailLogOutSchema]])
|
|
async def email_log_list_controller(
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[EmailLogQueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_platform:email:query"]))],
|
|
):
|
|
"""
|
|
邮件发送日志
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数。
|
|
- search (EmailLogQueryParam): 查询筛选参数。
|
|
|
|
返回:
|
|
- SuccessResponse: 包含分页日志列表的 JSON 响应。
|
|
"""
|
|
result = await EmailLogService.page_service(
|
|
auth=auth,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size,
|
|
search=search,
|
|
order_by=page.order_by,
|
|
)
|
|
return SuccessResponse(data=result, msg="查询成功")
|