model.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from datetime import datetime
  2. from typing import Optional
  3. from sqlalchemy import String, DateTime, ForeignKey, Text, Integer
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from app.common.enums import PermissionFilterStrategy
  6. from app.core.base_model import ModelMixin, TenantMixin
  7. class TenantApiKeyModel(ModelMixin, TenantMixin):
  8. """
  9. 租户API Key模型
  10. """
  11. __tablename__ = "sys_tenant_api_key"
  12. __permission_strategy__ = PermissionFilterStrategy.ENTERPRISE_BASED
  13. # 核心字段
  14. api_key: Mapped[str] = mapped_column(
  15. String(256),
  16. nullable=False,
  17. unique=True,
  18. comment="API Key",
  19. index=True,
  20. )
  21. api_secret: Mapped[str] = mapped_column(
  22. String(256),
  23. nullable=False,
  24. comment="API Secret",
  25. )
  26. expired_at: Mapped[Optional[datetime]] = mapped_column(
  27. DateTime,
  28. default=None,
  29. nullable=True,
  30. comment="过期时间",
  31. index=True,
  32. )
  33. last_used_at: Mapped[Optional[datetime]] = mapped_column(
  34. DateTime,
  35. default=None,
  36. nullable=True,
  37. comment="最后使用时间",
  38. )
  39. # 关联字段
  40. tenant_id: Mapped[int] = mapped_column(
  41. Integer,
  42. ForeignKey("sys_tenant.id", ondelete="CASCADE", onupdate="CASCADE"),
  43. nullable=False,
  44. index=True,
  45. comment="租户ID",
  46. )
  47. class TenantApiLogModel(ModelMixin, TenantMixin):
  48. """
  49. 租户API调用日志模型
  50. """
  51. __tablename__ = "sys_tenant_api_log"
  52. # 核心字段
  53. api_key_id: Mapped[int] = mapped_column(
  54. Integer,
  55. nullable=True,
  56. index=True,
  57. comment="API Key ID",
  58. )
  59. endpoint: Mapped[str] = mapped_column(
  60. String(256),
  61. nullable=False,
  62. comment="调用的接口",
  63. index=True,
  64. )
  65. method: Mapped[str] = mapped_column(
  66. String(10),
  67. nullable=False,
  68. comment="请求方法",
  69. )
  70. request_ip: Mapped[str] = mapped_column(
  71. String(50),
  72. nullable=False,
  73. comment="请求IP",
  74. index=True,
  75. )
  76. request_data: Mapped[Optional[str]] = mapped_column(
  77. Text,
  78. default=None,
  79. nullable=True,
  80. comment="请求数据(脱敏存储)",
  81. )
  82. response_code: Mapped[int] = mapped_column(
  83. Integer,
  84. nullable=False,
  85. comment="响应码",
  86. index=True,
  87. )
  88. response_time: Mapped[float] = mapped_column(
  89. Integer,
  90. nullable=False,
  91. comment="响应时间(毫秒)",
  92. )