upload project source code

This commit is contained in:
2026-04-30 18:49:43 +08:00
commit 9b394ba682
2277 changed files with 660945 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-

View File

@@ -0,0 +1,208 @@
# -*- coding: utf-8 -*-
from fastapi import APIRouter, Body, Depends, Path, Query
from fastapi.responses import JSONResponse, StreamingResponse
from app.common.response import StreamResponse, SuccessResponse
from app.core.base_params import PaginationQueryParam
from app.core.dependencies import AuthPermission, get_current_user, db_getter
from app.core.base_schema import BatchSetAvailable
from app.core.logger import log
from app.common.request import PaginationService
from app.core.router_class import OperationLogRoute
from app.utils.common_util import bytes2file_response
from sqlalchemy.ext.asyncio import AsyncSession
from ..auth.schema import AuthSchema
from .service import NoticeService
from .schema import (
NoticeCreateSchema,
NoticeUpdateSchema,
NoticeQueryParam,
NoticeOutSchema
)
from .crud import NoticeCRUD
NoticeRouter = APIRouter(route_class=OperationLogRoute, prefix="/notice", tags=["公告通知"])
@NoticeRouter.get("/mini/list", summary="小程序消息列表", description="小程序获取消息列表(无需后台权限)")
async def get_notice_mini_list_controller(
page_no: int = Query(1, ge=1, description="页码"),
page_size: int = Query(10, ge=1, le=50, description="每页数量"),
db: AsyncSession = Depends(db_getter),
) -> JSONResponse:
auth = AuthSchema(db=db, check_data_scope=False)
offset = (page_no - 1) * page_size
result = await NoticeCRUD(auth).page(
offset=offset,
limit=page_size,
order_by=[{"created_time": "desc"}],
search={"status": "0"},
out_schema=NoticeOutSchema,
)
log.info("小程序查询消息列表成功")
return SuccessResponse(data=result, msg="获取消息列表成功")
@NoticeRouter.get("/detail/{id}", summary="获取公告详情", description="获取公告详情")
async def get_obj_detail_controller(
id: int = Path(..., description="公告ID"),
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:query"]))
) -> JSONResponse:
"""
获取公告详情。
参数:
- id (int): 公告ID。
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含公告详情的响应模型。
"""
result_dict = await NoticeService.get_notice_detail_service(id=id, auth=auth)
log.info(f"获取公告详情成功 {id}")
return SuccessResponse(data=result_dict, msg="获取公告详情成功")
@NoticeRouter.get("/list", summary="查询公告", description="查询公告")
async def get_obj_list_controller(
page: PaginationQueryParam = Depends(),
search: NoticeQueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:query"]))
) -> JSONResponse:
"""
查询公告。
参数:
- page (PaginationQueryParam): 分页查询参数模型。
- search (NoticeQueryParam): 查询公告参数模型。
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含分页公告详情的响应模型。
"""
result_dict_list = await NoticeService.get_notice_list_service(auth=auth, search=search, order_by=page.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="查询公告列表成功")
@NoticeRouter.post("/create", summary="创建公告", description="创建公告")
async def create_obj_controller(
data: NoticeCreateSchema,
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:create"]))
) -> JSONResponse:
"""
创建公告。
参数:
- data (NoticeCreateSchema): 创建公告负载模型。
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含创建公告结果的响应模型。
"""
result_dict = await NoticeService.create_notice_service(auth=auth, data=data)
log.info(f"创建公告成功: {result_dict}")
return SuccessResponse(data=result_dict, msg="创建公告成功")
@NoticeRouter.put("/update/{id}", summary="修改公告", description="修改公告")
async def update_obj_controller(
data: NoticeUpdateSchema,
id: int = Path(..., description="公告ID"),
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:update"]))
) -> JSONResponse:
"""
修改公告。
参数:
- data (NoticeUpdateSchema): 修改公告负载模型。
- id (int): 公告ID。
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含修改公告结果的响应模型。
"""
result_dict = await NoticeService.update_notice_service(auth=auth, id=id, data=data)
log.info(f"修改公告成功: {result_dict}")
return SuccessResponse(data=result_dict, msg="修改公告成功")
@NoticeRouter.delete("/delete", summary="删除公告", description="删除公告")
async def delete_obj_controller(
ids: list[int] = Body(..., description="ID列表"),
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:delete"]))
) -> JSONResponse:
"""
删除公告。
参数:
- ids (list[int]): 公告ID列表。
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含删除公告结果的响应模型。
"""
await NoticeService.delete_notice_service(auth=auth, ids=ids)
log.info(f"删除公告成功: {ids}")
return SuccessResponse(msg="删除公告成功")
@NoticeRouter.patch("/available/setting", summary="批量修改公告状态", description="批量修改公告状态")
async def batch_set_available_obj_controller(
data: BatchSetAvailable,
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:patch"]))
) -> JSONResponse:
"""
批量修改公告状态。
参数:
- data (BatchSetAvailable): 批量修改公告状态负载模型。
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含批量修改公告状态结果的响应模型。
"""
await NoticeService.set_notice_available_service(auth=auth, data=data)
log.info(f"批量修改公告状态成功: {data.ids}")
return SuccessResponse(msg="批量修改公告状态成功")
@NoticeRouter.post('/export', summary="导出公告", description="导出公告")
async def export_obj_list_controller(
search: NoticeQueryParam = Depends(),
auth: AuthSchema = Depends(AuthPermission(["module_system:notice:export"]))
) -> StreamingResponse:
"""
导出公告。
参数:
- search (NoticeQueryParam): 查询公告参数模型。
- auth (AuthSchema): 认证信息模型。
返回:
- StreamingResponse: 包含导出公告的流式响应模型。
"""
result_dict_list = await NoticeService.get_notice_list_service(search=search, auth=auth)
export_result = await NoticeService.export_notice_service(notice_list=result_dict_list)
log.info('导出公告成功')
return StreamResponse(
data=bytes2file_response(export_result),
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
headers = {'Content-Disposition': 'attachment; filename=notice.xlsx'}
)
@NoticeRouter.get("/available", summary="获取全局启用公告", description="获取全局启用公告")
async def get_obj_list_available_controller(
auth: AuthSchema = Depends(get_current_user)
) -> JSONResponse:
"""
获取全局启用公告。
参数:
- auth (AuthSchema): 认证信息模型。
返回:
- JSONResponse: 包含分页已启用公告详情的响应模型。
"""
result_dict_list = await NoticeService.get_notice_list_available_service(auth=auth)
result_dict = await PaginationService.paginate(data_list= result_dict_list)
log.info(f"查询已启用公告列表成功")
return SuccessResponse(data=result_dict, msg="查询已启用公告列表成功")

View File

@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
from typing import Sequence
from app.core.base_crud import CRUDBase
from ..auth.schema import AuthSchema
from .model import NoticeModel
from .schema import NoticeCreateSchema, NoticeUpdateSchema
class NoticeCRUD(CRUDBase[NoticeModel, NoticeCreateSchema, NoticeUpdateSchema]):
"""公告数据层"""
def __init__(self, auth: AuthSchema) -> None:
"""
初始化公告数据层。
参数:
- auth (AuthSchema): 认证信息模型。
"""
self.auth = auth
super().__init__(model=NoticeModel, auth=auth)
async def get_by_id_crud(self, id: int, preload: list | None = None) -> NoticeModel | None:
"""
根据ID获取公告详情。
参数:
- id (int): 公告ID。
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- NoticeModel | None: 公告模型实例。
"""
return await self.get(id=id, preload=preload)
async def get_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[NoticeModel]:
"""
获取公告列表。
参数:
- search (dict | None): 查询参数。
- order_by (list[dict] | None): 排序参数。
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- Sequence[NoticeModel]: 公告模型实例列表。
"""
return await self.list(search=search, order_by=order_by, preload=preload)
async def create_crud(self, data: NoticeCreateSchema) -> NoticeModel | None:
"""
创建公告。
参数:
- data (NoticeCreateSchema): 公告创建模型。
返回:
- NoticeModel | None: 公告模型实例。
"""
return await self.create(data=data)
async def update_crud(self, id: int, data: NoticeUpdateSchema) -> NoticeModel | None:
"""
更新公告。
参数:
- id (int): 公告ID。
- data (NoticeUpdateSchema): 公告更新模型。
返回:
- NoticeModel | None: 公告模型实例。
"""
return await self.update(id=id, data=data)
async def delete_crud(self, ids: list[int]) -> None:
"""
删除公告。
参数:
- ids (list[int]): 公告ID列表。
返回:
- None
"""
return await self.delete(ids=ids)
async def set_available_crud(self, ids: list[int], status: str) -> None:
"""
设置公告的可用状态。
参数:
- ids (list[int]): 公告ID列表。
- status (str): 可用状态。
返回:
- None
"""
return await self.set(ids=ids, status=status)

View File

@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from sqlalchemy import String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.core.base_model import ModelMixin, UserMixin
class NoticeModel(ModelMixin, UserMixin):
"""
通知公告表
"""
__tablename__: str = "sys_notice"
__table_args__: dict[str, str] = ({'comment': '通知公告表'})
__loader_options__: list[str] = ["created_by", "updated_by"]
notice_title: Mapped[str] = mapped_column(String(50), nullable=False, comment='公告标题')
notice_type: Mapped[str] = mapped_column(String(50), nullable=False, comment='公告类型(1通知 2公告)')
notice_content: Mapped[str | None] = mapped_column(Text, nullable=True, comment='公告内容')

View File

@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from fastapi import Query
from app.core.validator import DateTimeStr
from app.core.base_schema import BaseSchema, UserBySchema
class NoticeCreateSchema(BaseModel):
"""公告通知创建模型"""
notice_title: str = Field(..., max_length=50, description='公告标题')
notice_type: str = Field(..., description='公告类型1通知 2公告')
notice_content: str = Field(..., description='公告内容')
status: str = Field(default="0", description="是否启用(0:启用 1:禁用)")
description: str | None = Field(default=None, max_length=255, description="描述")
@field_validator("notice_type")
@classmethod
def _validate_notice_type(cls, value: str):
if value not in {"1", "2"}:
raise ValueError("公告类型仅支持 '1'(通知) 或 '2'(公告)")
return value
@model_validator(mode='after')
def _validate_after(self):
if not self.notice_title.strip():
raise ValueError("公告标题不能为空")
if not self.notice_content.strip():
raise ValueError("公告内容不能为空")
return self
class NoticeUpdateSchema(NoticeCreateSchema):
"""公告通知更新模型"""
...
class NoticeOutSchema(NoticeCreateSchema, BaseSchema, UserBySchema):
"""公告通知响应模型"""
model_config = ConfigDict(from_attributes=True)
class NoticeQueryParam:
"""公告通知查询参数"""
def __init__(
self,
notice_title: str | None = Query(None, description="公告标题"),
notice_type: str | None = Query(None, description="公告类型"),
status: str | 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.notice_title = ("like", notice_title)
# 精确查询字段
self.created_id = created_id
self.updated_id = updated_id
self.status = status
self.notice_type = notice_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]))

View File

@@ -0,0 +1,175 @@
# -*- coding: utf-8 -*-
from app.core.base_schema import BatchSetAvailable
from app.core.exceptions import CustomException
from app.utils.excel_util import ExcelUtil
from ..auth.schema import AuthSchema
from .schema import NoticeCreateSchema, NoticeUpdateSchema, NoticeOutSchema, NoticeQueryParam
from .crud import NoticeCRUD
class NoticeService:
"""
公告管理模块服务层
"""
@classmethod
async def get_notice_detail_service(cls, auth: AuthSchema, id: int) -> dict:
"""
获取公告详情。
参数:
- auth (AuthSchema): 认证信息模型。
- id (int): 公告ID。
返回:
- Dict: 公告详情字典。
"""
notice_obj = await NoticeCRUD(auth).get_by_id_crud(id=id)
return NoticeOutSchema.model_validate(notice_obj).model_dump()
@classmethod
async def get_notice_list_available_service(cls, auth: AuthSchema) -> list[dict]:
"""
获取可用的公告列表。
参数:
- auth (AuthSchema): 认证信息模型。
返回:
- list[dict]: 可用公告详情字典列表。
"""
notice_obj_list = await NoticeCRUD(auth).get_list_crud(search={'status': '0',})
return [NoticeOutSchema.model_validate(notice_obj).model_dump() for notice_obj in notice_obj_list]
@classmethod
async def get_notice_list_service(cls, auth: AuthSchema, search: NoticeQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
"""
获取公告列表。
参数:
- auth (AuthSchema): 认证信息模型。
- search (NoticeQueryParam | None): 查询参数模型。
- order_by (list[dict] | None): 排序参数列表。
返回:
- list[dict]: 公告详情字典列表。
"""
notice_obj_list = await NoticeCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
return [NoticeOutSchema.model_validate(notice_obj).model_dump() for notice_obj in notice_obj_list]
@classmethod
async def create_notice_service(cls, auth: AuthSchema, data: NoticeCreateSchema) -> dict:
"""
创建公告。
参数:
- auth (AuthSchema): 认证信息模型。
- data (NoticeCreateSchema): 创建公告负载模型。
返回:
- dict: 创建的公告详情字典。
异常:
- CustomException: 创建失败,该公告通知已存在。
"""
notice = await NoticeCRUD(auth).get(notice_title=data.notice_title)
if notice:
raise CustomException(msg='创建失败,该公告通知已存在')
notice_obj = await NoticeCRUD(auth).create_crud(data=data)
return NoticeOutSchema.model_validate(notice_obj).model_dump()
@classmethod
async def update_notice_service(cls, auth: AuthSchema, id: int, data: NoticeUpdateSchema) -> dict:
"""
更新公告。
参数:
- auth (AuthSchema): 认证信息模型。
- id (int): 公告ID。
- data (NoticeUpdateSchema): 更新公告负载模型。
返回:
- dict: 更新的公告详情字典。
异常:
- CustomException: 更新失败,该公告通知不存在或公告通知标题重复。
"""
notice = await NoticeCRUD(auth).get_by_id_crud(id=id)
if not notice:
raise CustomException(msg='更新失败,该公告通知不存在')
exist_notice = await NoticeCRUD(auth).get(notice_title=data.notice_title)
if exist_notice and exist_notice.id != id:
raise CustomException(msg='更新失败,公告通知标题重复')
notice_obj = await NoticeCRUD(auth).update_crud(id=id, data=data)
return NoticeOutSchema.model_validate(notice_obj).model_dump()
@classmethod
async def delete_notice_service(cls, auth: AuthSchema, ids: list[int]) -> None:
"""
删除公告。
参数:
- auth (AuthSchema): 认证信息模型。
- ids (list[int]): 删除的ID列表。
异常:
- CustomException: 删除失败,删除对象不能为空或该公告通知不存在。
"""
if len(ids) < 1:
raise CustomException(msg='删除失败,删除对象不能为空')
for id in ids:
notice = await NoticeCRUD(auth).get_by_id_crud(id=id)
if not notice:
raise CustomException(msg='删除失败,该公告通知不存在')
await NoticeCRUD(auth).delete_crud(ids=ids)
@classmethod
async def set_notice_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
"""
批量设置公告状态。
参数:
- auth (AuthSchema): 认证信息模型。
- data (BatchSetAvailable): 批量设置可用负载模型。
异常:
- CustomException: 批量设置失败,该公告通知不存在。
"""
await NoticeCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
@classmethod
async def export_notice_service(cls, notice_list: list[dict]) -> bytes:
"""
导出公告列表。
参数:
- notice_list (list[dict]): 公告详情字典列表。
返回:
- bytes: Excel 文件的字节流。
"""
mapping_dict = {
'id': '编号',
'notice_title': '公告标题',
'notice_type': '公告类型1通知 2公告',
'notice_content': '公告内容',
'status': '状态',
'description': '备注',
'created_time': '创建时间',
'updated_time': '更新时间',
'created_id': '创建者ID',
'updated_id': '更新者ID',
}
# 复制数据并转换状态
data = notice_list.copy()
for item in data:
# 处理状态
item['status'] = '启用' if item.get('status') == '0' else '停用'
# 处理公告类型
item['notice_type'] = '通知' if item.get('notice_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)