由于本次代码变更内容为空,无法生成有效的提交信息。请提供具体的代码变更内容以便生成合适的提交信息。登录、微信登录等认证功能- 添加管理员登录功能

- 实现个人资料更新和密码修改- 配置数据库连接和 Alembic 迁移
- 添加健康检查和系统统计接口
- 实现自定义错误处理和响应格式
- 配置 FastAPI 应用和中间件
This commit is contained in:
ylweng
2025-09-12 00:57:52 +08:00
parent 67aef9a9ee
commit 4db35e91d4
18 changed files with 1763 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# 导入模型
from app.models.user import Base
# 导入配置
from app.core.config import settings
# 确保Python能够找到app模块
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
# 这是Alembic Config对象它提供对.ini文件中值的访问
config = context.config
# 解释配置文件并设置日志记录器
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# 添加你的模型元数据对象
target_metadata = Base.metadata
# 其他值来自config可以通过以下方式定义
# my_important_option = config.get_main_option("my_important_option")
# ... 等等。
def run_migrations_offline() -> None:
"""'offline'模式下运行迁移。
这配置了上下文只需要一个URL并且不要求引擎可用。
跳过引擎创建甚至不需要DBAPI可用。
调用context.execute()来执行迁移。
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""'online'模式下运行迁移。
在这种情况下我们创建了一个Engine并将其与迁移上下文关联。
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@@ -0,0 +1,62 @@
"""initial migration
Revision ID: 0001
Revises:
Create Date: 2025-09-11 16:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '0001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# 创建用户表
op.create_table(
'users',
sa.Column('id', sa.Integer, primary_key=True, index=True),
sa.Column('username', sa.String(50), unique=True, index=True, nullable=False),
sa.Column('password_hash', sa.String(255), nullable=False),
sa.Column('user_type', sa.Enum('farmer', 'merchant', 'admin', 'super_admin'), server_default='farmer'),
sa.Column('real_name', sa.String(50)),
sa.Column('nickname', sa.String(50)),
sa.Column('avatar_url', sa.String(255)),
sa.Column('email', sa.String(100), unique=True, index=True),
sa.Column('phone', sa.String(20), unique=True, index=True),
sa.Column('gender', sa.Enum('male', 'female', 'other'), server_default='other'),
sa.Column('birthday', sa.DateTime),
sa.Column('status', sa.Enum('active', 'inactive'), server_default='active'),
sa.Column('wechat_openid', sa.String(100), unique=True, index=True),
sa.Column('wechat_unionid', sa.String(100), unique=True, index=True),
sa.Column('level', sa.Integer, server_default='1'),
sa.Column('created_at', sa.DateTime, server_default=sa.func.now()),
sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now()),
sa.Column('last_login', sa.DateTime)
)
# 创建管理员表
op.create_table(
'admins',
sa.Column('id', sa.Integer, primary_key=True, index=True),
sa.Column('username', sa.String(50), unique=True, index=True, nullable=False),
sa.Column('password', sa.String(255), nullable=False),
sa.Column('email', sa.String(100), unique=True, index=True),
sa.Column('nickname', sa.String(50)),
sa.Column('avatar', sa.String(255)),
sa.Column('role', sa.Enum('admin', 'super_admin'), server_default='admin'),
sa.Column('status', sa.Enum('active', 'inactive'), server_default='active'),
sa.Column('last_login', sa.DateTime),
sa.Column('created_at', sa.DateTime, server_default=sa.func.now()),
sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now())
)
def downgrade():
op.drop_table('admins')
op.drop_table('users')