Initial commit: 浼佷笟寰俊 AI 鏈哄櫒浜哄姪鐞?MVP

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
bujie9527
2026-02-05 16:36:32 +08:00
commit 59275ed4dc
126 changed files with 9120 additions and 0 deletions

69
deploy/scripts/seed.py Normal file
View File

@@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""
初始化管理员账号。从项目根目录的 .env 读取 ADMIN_USERNAME、ADMIN_PASSWORD可选
未设置则使用默认 admin / admin。需先执行 Alembic 迁移。
用法(在项目根目录):
python deploy/scripts/seed.py
cd backend && PYTHONPATH=. python ../deploy/scripts/seed.py
"""
import os
import sys
import uuid
# 允许从项目根或 backend 运行
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "backend"))
from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "..", ".env"))
from sqlalchemy import create_engine, select, text
from sqlalchemy.orm import sessionmaker
# 同步连接,供脚本使用
DATABASE_URL_SYNC = os.getenv(
"DATABASE_URL_SYNC",
"postgresql://wecom:wecom_secret@localhost:5432/wecom_ai",
)
def main():
import bcrypt
username = os.getenv("ADMIN_USERNAME", "admin")
password = os.getenv("ADMIN_PASSWORD", "admin")
password_hash = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
engine = create_engine(DATABASE_URL_SYNC)
Session = sessionmaker(bind=engine)
with Session() as session:
# 检查 users 表是否存在
try:
session.execute(text("SELECT 1 FROM users LIMIT 1"))
except Exception:
print("表 users 不存在,请先执行: cd backend && alembic upgrade head")
sys.exit(1)
# 是否已存在该用户名
r = session.execute(
text("SELECT id FROM users WHERE username = :u"),
{"u": username},
)
if r.one_or_none():
print(f"用户 {username} 已存在,跳过创建。")
return
user_id = uuid.uuid4()
session.execute(
text(
"INSERT INTO users (id, username, password_hash, role, is_active, created_at) "
"VALUES (:id, :u, :p, 'admin', true, NOW())"
),
{"id": user_id, "u": username, "p": password_hash},
)
session.commit()
print(f"已创建管理员: username={username}")
if __name__ == "__main__":
main()