feat(backend): 实现多租户数据隔离与权限管理架构

refactor(backend): 重构模型基类支持租户与客户隔离
feat(backend): 添加客户模块相关模型、CRUD和参数校验
docs(backend): 新增SaaS数据隔离设计方案文档
refactor(backend): 优化日志模块并添加类型注解
fix(backend): 修正字典模块查询参数移除creator字段

style(frontend): 统一按钮组件代码格式
fix(frontend): 修复表格序号计算逻辑
chore(frontend): 更新lint脚本使用pnpm替代npm
This commit is contained in:
zhangtao
2025-11-23 18:59:43 +08:00
parent 749c97f4e3
commit 9a39a64faf
54 changed files with 5952 additions and 588 deletions
+54 -16
View File
@@ -1,28 +1,66 @@
# -*- coding: utf-8 -*-
from typing import Optional
from typing import TYPE_CHECKING
from sqlalchemy import String, Integer, Text
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import relationship, Mapped, mapped_column
from app.core.base_model import CreatorMixin
from app.core.base_model import ModelMixin, UserMixin, TenantMixin, CustomerMixin
if TYPE_CHECKING:
from app.api.v1.module_system.tenant.model import TenantModel
from app.api.v1.module_system.customer.model import CustomerModel
from app.api.v1.module_system.user.model import UserModel
class OperationLogModel(CreatorMixin):
"""
系统日志
class OperationLogModel(ModelMixin, UserMixin, TenantMixin, CustomerMixin):
"""
__tablename__ = "system_log"
__table_args__ = ({'comment': '系统日志表'})
__loader_options__ = ["creator"]
系统日志模型
日志隔离策略:
===========
- 日志记录操作人的实际租户和客户信息
- tenant_id: 记录操作人所属租户(必填)
- customer_id: 记录操作人所属客户(如果是客户用户)
用于审计和追踪:
- 系统用户操作: tenant_id=1, customer_id=NULL
- 租户用户操作: tenant_id>1, customer_id=NULL
- 客户用户操作: tenant_id>1, customer_id>1
日志类型:
- 1: 登录日志
- 2: 操作日志
"""
__tablename__: str = "system_log"
__table_args__: dict[str, str] = ({'comment': '系统日志表'})
__loader_options__: list[str] = ["creator"]
type: Mapped[int] = mapped_column(Integer, comment="日志类型(1登录日志 2操作日志)")
request_path: Mapped[str] = mapped_column(String(255), comment="请求路径")
request_method: Mapped[str] = mapped_column(String(10), comment="请求方式")
request_payload: Mapped[Optional[str]] = mapped_column(Text, comment="请求体")
request_ip: Mapped[Optional[str]] = mapped_column(String(50), comment="请求IP地址")
login_location: Mapped[Optional[str]] = mapped_column(String(255), comment="登录位置")
request_os: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, comment="操作系统")
request_browser: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, comment="浏览器")
request_payload: Mapped[str | None] = mapped_column(Text, comment="请求体")
request_ip: Mapped[str | None] = mapped_column(String(50), comment="请求IP地址")
login_location: Mapped[str | None] = mapped_column(String(255), comment="登录位置")
request_os: Mapped[str | None] = mapped_column(String(64), nullable=True, comment="操作系统")
request_browser: Mapped[str | None] = mapped_column(String(64), nullable=True, comment="浏览器")
response_code: Mapped[int] = mapped_column(Integer, comment="响应状态码")
response_json: Mapped[Optional[str]] = mapped_column(Text, nullable=True, comment="响应体")
process_time: Mapped[Optional[str]] = mapped_column(String(20), nullable=True, comment="处理时间")
response_json: Mapped[str | None] = mapped_column(Text, nullable=True, comment="响应体")
process_time: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="处理时间")
# 关联关系 (继承自UserMixin, TenantMixin, CustomerMixin)
tenant: Mapped["TenantModel"] = relationship(
foreign_keys="OperationLogModel.tenant_id",
lazy="selectin"
)
customer: Mapped["CustomerModel | None"] = relationship(
foreign_keys="OperationLogModel.customer_id",
lazy="selectin"
)
created_by: Mapped["UserModel | None"] = relationship(
foreign_keys="OperationLogModel.created_id",
lazy="selectin"
)
updated_by: Mapped["UserModel | None"] = relationship(
foreign_keys="OperationLogModel.updated_id",
lazy="selectin"
)