model.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. return_url: Mapped[str] = mapped_column(
  48. String(512), nullable=True, comment="同步返回地址"
  49. )
  50. class TenantApiLogModel(ModelMixin, TenantMixin):
  51. """
  52. 租户API调用日志模型
  53. """
  54. __tablename__ = "sys_tenant_api_log"
  55. # 核心字段
  56. api_key_id: Mapped[int] = mapped_column(
  57. Integer,
  58. nullable=True,
  59. index=True,
  60. comment="API Key ID",
  61. )
  62. endpoint: Mapped[str] = mapped_column(
  63. String(256),
  64. nullable=False,
  65. comment="调用的接口",
  66. index=True,
  67. )
  68. method: Mapped[str] = mapped_column(
  69. String(10),
  70. nullable=False,
  71. comment="请求方法",
  72. )
  73. request_ip: Mapped[str] = mapped_column(
  74. String(50),
  75. nullable=False,
  76. comment="请求IP",
  77. index=True,
  78. )
  79. request_data: Mapped[Optional[str]] = mapped_column(
  80. Text,
  81. default=None,
  82. nullable=True,
  83. comment="请求数据(脱敏存储)",
  84. )
  85. response_code: Mapped[int] = mapped_column(
  86. Integer,
  87. nullable=False,
  88. comment="响应码",
  89. index=True,
  90. )
  91. response_time: Mapped[float] = mapped_column(
  92. Integer,
  93. nullable=False,
  94. comment="响应时间(毫秒)",
  95. )