渠道: 启动自动种Step默认渠道(OMO_STEP_API_KEY) + POST /preset/apply

This commit is contained in:
zhiqui
2026-11-15 16:45:29 +08:00
parent 195099a214
commit 6aa3559033
3 changed files with 27 additions and 0 deletions
+16
View File
@@ -32,6 +32,22 @@ async def preset():
return STEP_PRESET return STEP_PRESET
@router.post("/preset/apply")
async def apply_preset(user: User = Depends(current_user), db=Depends(get_db)):
"""一键把 Step Plan 预设落成渠道(已有同名渠道则跳过)。"""
from ...core.config import settings
rows = await repos.ChannelRepo.visible_to(db, user)
for r in rows:
if r.name == STEP_PRESET["name"]:
return {"applied": False, "reason": "已存在同名渠道"}
c = Channel(name=STEP_PRESET["name"], base_url=STEP_PRESET["base_url"],
api_key_enc=settings.step_api_key or "", models=STEP_PRESET["models"],
owner_id=user.id if not user.is_admin else None, visibility="admin")
db.add(c)
await db.commit()
return {"applied": True, "channel": _view(c)}
@router.get("") @router.get("")
async def list_channels(user: User = Depends(current_user), db=Depends(get_db)): async def list_channels(user: User = Depends(current_user), db=Depends(get_db)):
rows = await repos.ChannelRepo.visible_to(db, user) rows = await repos.ChannelRepo.visible_to(db, user)
+2
View File
@@ -8,6 +8,8 @@ class Settings(BaseSettings):
jwt_expire_minutes: int = 60 * 24 * 7 jwt_expire_minutes: int = 60 * 24 * 7
first_admin_user: str = "admin" first_admin_user: str = "admin"
first_admin_pass: str = "maopaoya-admin" first_admin_pass: str = "maopaoya-admin"
# 默认 Step Plan 渠道的 key(为空则种渠道但不填 key,到后台补)
step_api_key: str = ""
class Config: class Config:
env_file = ".env" env_file = ".env"
+9
View File
@@ -38,6 +38,15 @@ async def startup():
password_hash=security.hash_password(settings.first_admin_pass), password_hash=security.hash_password(settings.first_admin_pass),
kdf_salt=salt.hex(), is_admin=True)) kdf_salt=salt.hex(), is_admin=True))
await db.commit() await db.commit()
# 默认渠道:空库时种 Step Plan 预设(key 走 OMO_STEP_API_KEY)
from .models.entities import Channel
from .core.config import STEP_PRESET
chans = (await db.execute(__import__('sqlalchemy').select(Channel))).scalars().all()
if not chans:
db.add(Channel(name=STEP_PRESET["name"], base_url=STEP_PRESET["base_url"],
api_key_enc=settings.step_api_key, models=STEP_PRESET["models"],
owner_id=None, visibility="admin"))
await db.commit()
@app.exception_handler(Exception) @app.exception_handler(Exception)