Add parent WeChat identity mapping

This commit is contained in:
stu2not
2026-06-25 18:53:01 +08:00
parent 10c43a5338
commit a2b873a3c2
6 changed files with 697 additions and 6 deletions

View File

@@ -381,6 +381,32 @@ async def _ensure_parent_wechat_accounts_table(conn) -> None:
)
async def _ensure_parent_wechat_identities_table(conn) -> None:
await conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS parent_wechat_identities (
id BIGINT NOT NULL AUTO_INCREMENT,
user_id BIGINT NOT NULL,
app_id VARCHAR(64) NOT NULL,
account_type VARCHAR(32) NOT NULL,
openid VARCHAR(64) NOT NULL,
unionid VARCHAR(64) NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uq_parent_wechat_identity_app_openid (app_id, account_type, openid),
KEY idx_parent_wechat_identity_user (user_id),
KEY idx_parent_wechat_identity_unionid (unionid),
CONSTRAINT fk_parent_wechat_identity_user
FOREIGN KEY (user_id)
REFERENCES parents (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
"""
)
)
async def _ensure_wechat_mp_bind_states_table(conn) -> None:
await conn.execute(
text(
@@ -428,6 +454,7 @@ async def init_db():
await _ensure_cards_allow_multiple_per_device(conn)
await _ensure_device_family_tables(conn)
await _ensure_parent_wechat_accounts_table(conn)
await _ensure_parent_wechat_identities_table(conn)
await _ensure_wechat_mp_bind_states_table(conn)
await engine.dispose()
session_logger.info("system", "database", "数据库表已成功创建")

View File

@@ -245,6 +245,25 @@ class ParentWechatAccount(Base):
updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
class ParentWechatIdentity(Base):
__tablename__ = "parent_wechat_identities"
__table_args__ = (
UniqueConstraint("app_id", "account_type", "openid", name="uq_parent_wechat_identity_app_openid"),
Index("idx_parent_wechat_identity_user", "user_id"),
Index("idx_parent_wechat_identity_unionid", "unionid"),
{'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_unicode_ci'}
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("parents.user_id"), nullable=False)
app_id: Mapped[str] = mapped_column(String(64), nullable=False)
account_type: Mapped[str] = mapped_column(String(32), nullable=False)
openid: Mapped[str] = mapped_column(String(64), nullable=False)
unionid: Mapped[Optional[str]] = mapped_column(String(64))
created_at: Mapped[Optional[datetime]] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
class WechatMpBindState(Base):
__tablename__ = "wechat_mp_bind_states"
__table_args__ = (