chore: initialize GSREA website project
This commit is contained in:
commit
d3c5461de5
1842 files changed
+246140
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,44 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.about import public_about_page, public_expert, public_experts
|
||||
from app.models.home import DEFAULT_COLUMN_PAGE_SIZE, public_column_articles
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/about/pages/{page_key}")
|
||||
def read_public_about_page(page_key: str, db: Session = Depends(get_db)) -> dict:
|
||||
body = public_about_page(db, page_key)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="单篇页键不正确")
|
||||
return body
|
||||
|
||||
|
||||
@router.get("/about/articles/{category}")
|
||||
def read_public_column_articles(
|
||||
category: str,
|
||||
current: int = Query(1, ge=1),
|
||||
size: int = Query(DEFAULT_COLUMN_PAGE_SIZE, ge=1, le=100),
|
||||
kind: str | None = Query(None),
|
||||
q: str | None = Query(None),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
body = public_column_articles(db, category, current=current, size=size, kind=kind, q=q)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="文章类别不正确")
|
||||
return body
|
||||
|
||||
|
||||
@router.get("/about/experts")
|
||||
def read_public_experts(db: Session = Depends(get_db)) -> dict:
|
||||
return public_experts(db)
|
||||
|
||||
|
||||
@router.get("/about/experts/{expert_id}")
|
||||
def read_public_expert(expert_id: int, db: Session = Depends(get_db)) -> dict:
|
||||
body = public_expert(db, expert_id)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="专家委员会不存在")
|
||||
return body
|
||||
@@ -0,0 +1,124 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import create_access_token
|
||||
from app.db import get_db
|
||||
from app.models.member import create_member, get_member_by_email, set_member_password
|
||||
from app.services.email_code import (
|
||||
consume_email_code,
|
||||
is_valid_email,
|
||||
issue_email_code,
|
||||
normalize_email,
|
||||
)
|
||||
from app.services.password import hash_password, validate_password, verify_password
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class EmailCodeRequest(BaseModel):
|
||||
email: str
|
||||
purpose: str
|
||||
|
||||
|
||||
class RegisterRequest(BaseModel):
|
||||
email: str
|
||||
code: str
|
||||
password: str
|
||||
privacyAccepted: bool = False
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
email: str
|
||||
password: str
|
||||
code: str | None = None
|
||||
|
||||
|
||||
class ResetPasswordRequest(BaseModel):
|
||||
email: str
|
||||
code: str
|
||||
password: str
|
||||
|
||||
|
||||
def _bad(detail: str) -> None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
|
||||
|
||||
|
||||
@router.post("/auth/email-code")
|
||||
def send_email_code(body: EmailCodeRequest, db: Session = Depends(get_db)) -> dict:
|
||||
purpose = (body.purpose or "").strip()
|
||||
email = normalize_email(body.email)
|
||||
if purpose not in {"register", "login", "reset"}:
|
||||
_bad("验证码用途不正确")
|
||||
if not is_valid_email(email):
|
||||
_bad("邮箱格式不正确")
|
||||
existing = get_member_by_email(db, email)
|
||||
if purpose == "register" and existing is not None:
|
||||
_bad("该邮箱已注册")
|
||||
if purpose in {"login", "reset"} and existing is None:
|
||||
_bad("该邮箱尚未注册")
|
||||
if purpose in {"login", "reset"} and existing is not None and existing.status != "active":
|
||||
_bad("账号不可用")
|
||||
try:
|
||||
issue_email_code(email=email, purpose=purpose)
|
||||
except ValueError as exc:
|
||||
_bad(str(exc))
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.post("/auth/register")
|
||||
def register(body: RegisterRequest, db: Session = Depends(get_db)) -> dict:
|
||||
email = normalize_email(body.email)
|
||||
if not is_valid_email(email):
|
||||
_bad("邮箱格式不正确")
|
||||
if not body.privacyAccepted:
|
||||
_bad("请先阅读并同意隐私政策")
|
||||
if get_member_by_email(db, email) is not None:
|
||||
_bad("该邮箱已注册")
|
||||
# 先校验密码,避免弱密码拒收时已消费验证码,用户改密后无法再用同一码。
|
||||
validate_password(body.password)
|
||||
if not consume_email_code(email=email, purpose="register", code=body.code):
|
||||
_bad("验证码不正确或已过期")
|
||||
member = create_member(
|
||||
db,
|
||||
email=email,
|
||||
password_hash=hash_password(body.password),
|
||||
privacy_accepted_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
||||
)
|
||||
token = create_access_token(str(member.id), "member")
|
||||
return {"accessToken": token, "tokenType": "member", "email": member.email}
|
||||
|
||||
|
||||
@router.post("/auth/login")
|
||||
def login(body: LoginRequest, db: Session = Depends(get_db)) -> dict:
|
||||
email = normalize_email(body.email)
|
||||
if not is_valid_email(email):
|
||||
_bad("邮箱格式不正确")
|
||||
member = get_member_by_email(db, email)
|
||||
if member is None or member.status != "active":
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="邮箱或密码不正确")
|
||||
if body.code is not None and str(body.code).strip():
|
||||
if not consume_email_code(email=email, purpose="login", code=body.code):
|
||||
_bad("验证码不正确或已过期")
|
||||
if not verify_password(body.password, member.password_hash):
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="邮箱或密码不正确")
|
||||
token = create_access_token(str(member.id), "member")
|
||||
return {"accessToken": token, "tokenType": "member", "email": member.email}
|
||||
|
||||
|
||||
@router.post("/auth/reset-password")
|
||||
def reset_password(body: ResetPasswordRequest, db: Session = Depends(get_db)) -> dict:
|
||||
email = normalize_email(body.email)
|
||||
if not is_valid_email(email):
|
||||
_bad("邮箱格式不正确")
|
||||
member = get_member_by_email(db, email)
|
||||
if member is None or member.status != "active":
|
||||
_bad("该邮箱尚未注册")
|
||||
validate_password(body.password)
|
||||
if not consume_email_code(email=email, purpose="reset", code=body.code):
|
||||
_bad("验证码不正确或已过期")
|
||||
set_member_password(db, member, hash_password(body.password))
|
||||
token = create_access_token(str(member.id), "member")
|
||||
return {"accessToken": token, "tokenType": "member", "email": member.email}
|
||||
@@ -0,0 +1,72 @@
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.content import read_upload_limit
|
||||
from app.models.discipline import create_complaint, public_complaint_status, public_notice
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _content_type(value: str | None) -> str:
|
||||
raw = (value or "").split(";", 1)[0].strip().lower()
|
||||
return raw.replace("\r", "").replace("\n", "")[:128]
|
||||
|
||||
|
||||
def _read_limited(upload: UploadFile, limit: int) -> bytes:
|
||||
chunks = bytearray()
|
||||
while True:
|
||||
piece = upload.file.read(65536)
|
||||
if not piece:
|
||||
break
|
||||
if len(chunks) + len(piece) > limit:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="文件超过大小上限")
|
||||
chunks.extend(piece)
|
||||
return bytes(chunks)
|
||||
|
||||
|
||||
@router.get("/discipline/notice")
|
||||
def read_notice(db: Session = Depends(get_db)) -> dict:
|
||||
return public_notice(db)
|
||||
|
||||
|
||||
@router.post("/discipline/complaints")
|
||||
def submit_complaint(
|
||||
title: str = Form(""),
|
||||
subject: str = Form(""),
|
||||
body: str = Form(""),
|
||||
contactName: str = Form(""),
|
||||
mobile: str = Form(""),
|
||||
email: str = Form(""),
|
||||
evidence: list[UploadFile] | None = File(default=None),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
limit = read_upload_limit(db)
|
||||
if limit is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="未配置上传大小上限")
|
||||
files: list[tuple[str, str, bytes]] = []
|
||||
for upload in evidence or []:
|
||||
if upload is None or not getattr(upload, "filename", None):
|
||||
continue
|
||||
files.append((upload.filename, _content_type(upload.content_type), _read_limited(upload, limit)))
|
||||
try:
|
||||
return create_complaint(
|
||||
db,
|
||||
title=title,
|
||||
subject=subject,
|
||||
body=body,
|
||||
contact_name=contactName,
|
||||
mobile=mobile,
|
||||
email=email,
|
||||
files=files,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/discipline/complaints/{code}")
|
||||
def read_complaint_status(code: str, db: Session = Depends(get_db)) -> dict:
|
||||
body = public_complaint_status(db, code)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="未找到该查询码")
|
||||
return body
|
||||
@@ -0,0 +1,12 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.home import public_edu_platforms
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/edu/platforms")
|
||||
def read_edu_platforms(db: Session = Depends(get_db)) -> dict:
|
||||
return public_edu_platforms(db)
|
||||
@@ -0,0 +1,28 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.home import public_article, public_home, public_slide
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/home")
|
||||
def read_public_home(db: Session = Depends(get_db)) -> dict:
|
||||
return public_home(db)
|
||||
|
||||
|
||||
@router.get("/home/articles/{article_id}")
|
||||
def read_public_article(article_id: int, db: Session = Depends(get_db)) -> dict:
|
||||
body = public_article(db, article_id)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="文章不存在")
|
||||
return body
|
||||
|
||||
|
||||
@router.get("/home/slides/{slide_id}")
|
||||
def read_public_slide(slide_id: int, db: Session = Depends(get_db)) -> dict:
|
||||
body = public_slide(db, slide_id)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="轮播不存在")
|
||||
return body
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.legal import public_legal_page
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/legal/{page_key}")
|
||||
def read_public_legal_page(page_key: str, db: Session = Depends(get_db)) -> dict:
|
||||
body = public_legal_page(db, page_key)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合规页键不正确")
|
||||
return body
|
||||
@@ -0,0 +1,171 @@
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.deps import require_member
|
||||
from app.db import get_db
|
||||
from app.models.content import AdminFile, read_upload_limit
|
||||
from app.models.expert import create_question, member_question, member_questions
|
||||
from app.models.member import get_member_by_id
|
||||
from app.models.uploaded_case import create_uploaded_case
|
||||
from app.services.storage import (
|
||||
make_object_key,
|
||||
private_object_url,
|
||||
remove_stored_object,
|
||||
safe_filename,
|
||||
upload_object,
|
||||
)
|
||||
|
||||
router = APIRouter(dependencies=[Depends(require_member)])
|
||||
|
||||
|
||||
class QuestionWrite(BaseModel):
|
||||
title: str = ""
|
||||
body: str = ""
|
||||
attachmentFileIds: list[int] = Field(default_factory=list)
|
||||
|
||||
|
||||
def _member_id(principal: dict) -> int:
|
||||
try:
|
||||
return int(principal["sub"])
|
||||
except (TypeError, ValueError, KeyError) as exc:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="登录已失效") from exc
|
||||
|
||||
|
||||
def _active_member(db: Session, principal: dict):
|
||||
member = get_member_by_id(db, _member_id(principal))
|
||||
if member is None or member.status != "active":
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="登录已失效")
|
||||
return member
|
||||
|
||||
|
||||
def _content_type(value: str | None) -> str:
|
||||
raw = (value or "").split(";", 1)[0].strip().lower()
|
||||
return raw.replace("\r", "").replace("\n", "")[:128]
|
||||
|
||||
|
||||
def _read_limited(upload: UploadFile, limit: int) -> bytes:
|
||||
chunks = bytearray()
|
||||
while True:
|
||||
piece = upload.file.read(65536)
|
||||
if not piece:
|
||||
break
|
||||
if len(chunks) + len(piece) > limit:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="文件超过大小上限")
|
||||
chunks.extend(piece)
|
||||
return bytes(chunks)
|
||||
|
||||
|
||||
@router.get("/me")
|
||||
def member_me(
|
||||
principal: dict = Depends(require_member),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
member = _active_member(db, principal)
|
||||
return {
|
||||
"id": member.id,
|
||||
"email": member.email,
|
||||
"tokenType": principal["typ"],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/files")
|
||||
def upload_private_file(
|
||||
file: UploadFile = File(...),
|
||||
principal: dict = Depends(require_member),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
member = _active_member(db, principal)
|
||||
limit = read_upload_limit(db)
|
||||
if limit is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="未配置上传大小上限")
|
||||
data = _read_limited(file, limit)
|
||||
filename = safe_filename(file.filename)
|
||||
content_type = _content_type(file.content_type)
|
||||
object_key = make_object_key(filename)
|
||||
upload_object("private", object_key, data, content_type)
|
||||
row = AdminFile(
|
||||
bucket="private",
|
||||
object_key=object_key,
|
||||
filename=filename,
|
||||
content_type=content_type,
|
||||
size=len(data),
|
||||
created_by=member.id,
|
||||
)
|
||||
db.add(row)
|
||||
try:
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
except Exception:
|
||||
db.rollback()
|
||||
try:
|
||||
remove_stored_object("private", object_key)
|
||||
except HTTPException:
|
||||
pass
|
||||
raise
|
||||
url, expires_at = private_object_url(row.object_key)
|
||||
return {
|
||||
"id": row.id,
|
||||
"bucket": row.bucket,
|
||||
"filename": row.filename,
|
||||
"contentType": row.content_type,
|
||||
"size": row.size,
|
||||
"url": url,
|
||||
"expiresAt": expires_at.isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/expert-questions")
|
||||
def list_my_questions(
|
||||
principal: dict = Depends(require_member),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
member = _active_member(db, principal)
|
||||
return member_questions(db, member.id)
|
||||
|
||||
|
||||
@router.get("/expert-questions/{question_id}")
|
||||
def read_my_question(
|
||||
question_id: int,
|
||||
principal: dict = Depends(require_member),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
member = _active_member(db, principal)
|
||||
body = member_question(db, member.id, question_id)
|
||||
if body is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="提问不存在")
|
||||
return body
|
||||
|
||||
|
||||
@router.post("/expert-questions")
|
||||
def ask_question(
|
||||
body: QuestionWrite,
|
||||
principal: dict = Depends(require_member),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
member = _active_member(db, principal)
|
||||
try:
|
||||
question_id = create_question(
|
||||
db,
|
||||
member_id=member.id,
|
||||
title=body.title,
|
||||
body=body.body,
|
||||
attachment_file_ids=body.attachmentFileIds,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
return {"id": question_id}
|
||||
|
||||
|
||||
@router.post("/uploaded-cases")
|
||||
def submit_uploaded_case(
|
||||
body: dict,
|
||||
principal: dict = Depends(require_member),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
member = _active_member(db, principal)
|
||||
try:
|
||||
row = create_uploaded_case(db, member, body)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
return {"id": row.id, "message": "提交成功"}
|
||||
@@ -0,0 +1,31 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.models.uploaded_case import (
|
||||
KIND_LABELS,
|
||||
OPEN_KINDS,
|
||||
commercial_options,
|
||||
office_options,
|
||||
regions,
|
||||
residential_options,
|
||||
industrial_options,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
def site_health() -> dict:
|
||||
return {"status": "ok", "scope": "site"}
|
||||
|
||||
|
||||
@router.get("/uploaded-case-options")
|
||||
def uploaded_case_options() -> dict:
|
||||
return {
|
||||
"kinds": [{"key": key, "label": label} for key, label in KIND_LABELS.items()],
|
||||
"openKinds": [key for key in KIND_LABELS if key in OPEN_KINDS],
|
||||
"regions": regions(),
|
||||
"residential": residential_options(),
|
||||
"commercial": commercial_options(),
|
||||
"office": office_options(),
|
||||
"industrial": industrial_options(),
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.report_filing import query_filing
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/reports/query")
|
||||
def read_report_query(number: str = "", db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return query_filing(db, number)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
@@ -0,0 +1,23 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.content import settings_view
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/settings")
|
||||
def read_public_settings(db: Session = Depends(get_db)) -> dict:
|
||||
# 只返回门户展示字段。上传上限和登录锁定参数留在管理端。
|
||||
data = settings_view(db)
|
||||
return {
|
||||
"siteName": data["site_name"],
|
||||
"logoUrl": data["logoUrl"],
|
||||
"bannerUrls": data["bannerUrls"],
|
||||
"icpText": data["icp_text"],
|
||||
"policeText": data["police_text"],
|
||||
"friendLinks": data["friend_links"],
|
||||
"linkCirea": data["link_cirea"],
|
||||
"linkHousing": data["link_housing"],
|
||||
}
|
||||
Reference in new issue
Block a user