chore: 初始化项目结构与登录鉴权模块

后端 FastAPI 六层结构(api/services/repositories/models/schemas/core),
统一响应信封、全局异常、日志脱敏;6 张表经 Alembic 迁移建库,SQL 留存
db/migration;登录鉴权含图形验证码(Redis GETDEL 防重放)、JWT、登录限流、
登出黑名单(TTL=令牌剩余有效期)、登录日志。

前端 Vue3 + TS + Element Plus 骨架,含登录页、路由守卫、请求封装与 Vite 代理。

工程配置:uv 锁定 Python 3.12、ruff 检查与格式化、editorconfig、gitattributes。

测试 184 项(单元 155 / 集成 29)全绿,浏览器端到端 8 项通过。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
liuwenxinandClaude Code committed 2026-10-09 18:46:36 +08:00
commit 273ebf4657
101 files changed
+8725

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
"""数据访问层:只负责数据读写。
分层约束:
- **只 flush,不 commit** —— 事务边界由 `services/` 层控制
- 不抛业务异常;查不到数据返回 None 或空集合,由 service 决定如何处置
- 不在此层做权限判断或业务规则校验
"""
@@ -0,0 +1,38 @@
"""登录日志数据访问。"""
from sqlalchemy.orm import Session
from app.models import LoginLog
# user_agent 列宽,超长需截断否则插入报错
_USER_AGENT_MAX_LEN = 255
def add(
session: Session,
*,
user_id: int | None,
username: str,
ip: str | None,
user_agent: str | None,
success: bool,
fail_reason: str | None = None,
) -> None:
"""写入一条登录日志。只 flush 不 commit。
**绝不记录密码** —— 明文与哈希都不写。日志是最容易泄漏凭据的地方。
:param user_id: 账号不存在时传 None(列本身可空)
:param username: 用户名原样记录,便于账号不存在时也能追溯
"""
session.add(
LoginLog(
user_id=user_id,
username=username,
ip=ip,
user_agent=user_agent[:_USER_AGENT_MAX_LEN] if user_agent else None,
success=1 if success else 0,
fail_reason=fail_reason,
)
)
session.flush()
@@ -0,0 +1,36 @@
"""用户与角色数据访问。
本层只 flush 不 commit —— 事务边界由 services 层控制。
"""
from datetime import datetime
from sqlalchemy import select
from sqlalchemy.orm import Session
from app.models import User
def get_by_username(session: Session, username: str) -> User | None:
"""按用户名查询用户。不存在返回 None。"""
return session.execute(select(User).where(User.username == username)).scalar_one_or_none()
def get_by_id(session: Session, user_id: int) -> User | None:
"""按主键查询用户。不存在返回 None。"""
return session.get(User, user_id)
def get_role_codes(user: User) -> list[str]:
"""取用户的角色编码列表。
依赖 `User.user_roles` 的 `lazy="selectin"` 预加载(见模型定义),
故此处遍历不会触发 N+1 查询。
"""
return [link.role.role_code for link in user.user_roles if link.role is not None]
def touch_last_login(session: Session, user: User, moment: datetime) -> None:
"""更新最后登录时间。只 flush,提交由 service 决定。"""
user.last_login_time = moment
session.flush()