model.py 2.4 KB

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