public.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """公开 API:附件上传、反馈/建言提交、热线/二维码读取(无需登录)。"""
  2. from typing import Annotated, Optional
  3. from fastapi import APIRouter, Depends, File, Form, UploadFile
  4. from sqlalchemy.orm import Session
  5. from app.core import storage
  6. from app.core.database import get_db
  7. from app.core.response import AppHTTPError, success_response
  8. from app.core.storage import StorageError
  9. from app.crud import attachment as attachment_crud
  10. from app.schemas.business import FeedbackCreateBody, SuggestionCreateBody
  11. from app.services import config_service, feedback_service, suggestion_service
  12. router = APIRouter(prefix="/public", tags=["public"])
  13. @router.post("/attachment/upload")
  14. async def upload_attachment(
  15. db: Annotated[Session, Depends(get_db)],
  16. file: UploadFile = File(...),
  17. folder: Optional[str] = Form(""),
  18. ):
  19. if not file or not file.filename:
  20. raise AppHTTPError(400, "请选择文件")
  21. content = await file.read()
  22. try:
  23. stored = storage.upload_file(content, file.filename, folder or "")
  24. except StorageError as exc:
  25. raise AppHTTPError(400, exc.message) from exc
  26. row = attachment_crud.create_attachment(
  27. db,
  28. file_name=file.filename,
  29. object_key=stored["objectKey"],
  30. url=stored["url"],
  31. size=len(content),
  32. )
  33. return success_response(
  34. {
  35. "id": row.id,
  36. "url": row.url,
  37. "fileName": row.file_name,
  38. "size": row.size,
  39. }
  40. )
  41. @router.get("/hotline")
  42. def get_hotline(db: Annotated[Session, Depends(get_db)]):
  43. return success_response(config_service.get_hotline_payload(db))
  44. @router.get("/qrcode")
  45. def get_qrcode(db: Annotated[Session, Depends(get_db)]):
  46. return success_response(config_service.get_qrcode_payload(db))
  47. @router.post("/feedback")
  48. def submit_feedback(
  49. body: FeedbackCreateBody,
  50. db: Annotated[Session, Depends(get_db)],
  51. ):
  52. data = feedback_service.submit_feedback(
  53. db,
  54. fb_type=body.fb_type,
  55. content=body.content,
  56. target_name=body.target_name,
  57. target_dept=body.target_dept,
  58. target_post=body.target_post,
  59. reporter_name=body.reporter_name,
  60. reporter_phone=body.reporter_phone,
  61. attachment_urls=body.attachment_urls,
  62. )
  63. return success_response(data)
  64. @router.post("/suggestion")
  65. def submit_suggestion(
  66. body: SuggestionCreateBody,
  67. db: Annotated[Session, Depends(get_db)],
  68. ):
  69. data = suggestion_service.submit_suggestion(
  70. db,
  71. content=body.content,
  72. contact_name=body.contact_name,
  73. contact_unit=body.contact_unit,
  74. contact_post=body.contact_post,
  75. contact_phone=body.contact_phone,
  76. attachment_urls=body.attachment_urls,
  77. )
  78. return success_response(data)