22
This commit is contained in:
103
app/api/v1/admin_card_keys.py
Normal file
103
app/api/v1/admin_card_keys.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""管理员卡密 CRUD。"""
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, Query
|
||||
|
||||
from app.dependencies import AdminUser, DbSession
|
||||
from app.schemas.admin_card_key import (
|
||||
CardKeyBatchCreateResult,
|
||||
CardKeyCreate,
|
||||
CardKeyOut,
|
||||
CardKeyUpdate,
|
||||
)
|
||||
from app.schemas.common import ApiResponse, PaginatedData
|
||||
from app.services import admin_card_key as card_key_service
|
||||
|
||||
router = APIRouter(prefix="/admin/card-keys", tags=["管理-卡密"])
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[PaginatedData[CardKeyOut]])
|
||||
async def list_card_keys(
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(
|
||||
card_key_service.DEFAULT_PAGE_SIZE,
|
||||
ge=1,
|
||||
le=card_key_service.MAX_PAGE_SIZE,
|
||||
),
|
||||
status: Literal["all", "unused", "used"] | None = Query(
|
||||
default="all",
|
||||
description="筛选:all 全部 / unused 未使用 / used 已激活",
|
||||
),
|
||||
) -> ApiResponse[PaginatedData[CardKeyOut]]:
|
||||
filter_status = None if status == "all" else status
|
||||
cards, total = await card_key_service.list_card_keys(
|
||||
db,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
status=filter_status,
|
||||
)
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="",
|
||||
data=PaginatedData.build(
|
||||
[CardKeyOut.model_validate(c) for c in cards],
|
||||
total=total,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=ApiResponse[CardKeyBatchCreateResult])
|
||||
async def create_card_keys(
|
||||
body: CardKeyCreate,
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[CardKeyBatchCreateResult]:
|
||||
try:
|
||||
created = await card_key_service.create_card_keys(db, body)
|
||||
except card_key_service.AdminCardKeyError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
items = [CardKeyOut.model_validate(c) for c in created]
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message=f"已生成 {len(items)} 张卡密",
|
||||
data=CardKeyBatchCreateResult(items=items, created_count=len(items)),
|
||||
)
|
||||
|
||||
|
||||
@router.patch("/{card_id}", response_model=ApiResponse[CardKeyOut])
|
||||
async def update_card_key(
|
||||
card_id: int,
|
||||
body: CardKeyUpdate,
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[CardKeyOut]:
|
||||
try:
|
||||
card = await card_key_service.update_card_key(db, card_id, body)
|
||||
except card_key_service.AdminCardKeyError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="卡密已更新",
|
||||
data=CardKeyOut.model_validate(card),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{card_id}", response_model=ApiResponse[None])
|
||||
async def delete_card_key(
|
||||
card_id: int,
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[None]:
|
||||
try:
|
||||
await card_key_service.delete_card_key(db, card_id)
|
||||
except card_key_service.AdminCardKeyError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(ok=True, message="卡密已删除")
|
||||
95
app/api/v1/admin_desktop_configs.py
Normal file
95
app/api/v1/admin_desktop_configs.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""管理员 desktop_configs 键值 CRUD。"""
|
||||
|
||||
from fastapi import APIRouter, Query
|
||||
|
||||
from app.dependencies import AdminUser, DbSession
|
||||
from app.schemas.admin_desktop_config import (
|
||||
DesktopConfigCreate,
|
||||
DesktopConfigOut,
|
||||
DesktopConfigUpdate,
|
||||
)
|
||||
from app.schemas.common import ApiResponse, PaginatedData
|
||||
from app.services import admin_desktop_config as config_service
|
||||
|
||||
router = APIRouter(prefix="/admin/desktop-configs", tags=["管理-桌面配置"])
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[PaginatedData[DesktopConfigOut]])
|
||||
async def list_configs(
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(
|
||||
config_service.DEFAULT_PAGE_SIZE,
|
||||
ge=1,
|
||||
le=config_service.MAX_PAGE_SIZE,
|
||||
),
|
||||
keyword: str | None = Query(default=None, max_length=64, description="按键名模糊搜索"),
|
||||
) -> ApiResponse[PaginatedData[DesktopConfigOut]]:
|
||||
rows, total = await config_service.list_configs(
|
||||
db,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
keyword=keyword,
|
||||
)
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="",
|
||||
data=PaginatedData.build(
|
||||
[DesktopConfigOut.model_validate(r) for r in rows],
|
||||
total=total,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=ApiResponse[DesktopConfigOut])
|
||||
async def create_config(
|
||||
body: DesktopConfigCreate,
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[DesktopConfigOut]:
|
||||
try:
|
||||
row = await config_service.create_config(db, body)
|
||||
except config_service.AdminDesktopConfigError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="配置已创建",
|
||||
data=DesktopConfigOut.model_validate(row),
|
||||
)
|
||||
|
||||
|
||||
@router.patch("/{config_id}", response_model=ApiResponse[DesktopConfigOut])
|
||||
async def update_config(
|
||||
config_id: int,
|
||||
body: DesktopConfigUpdate,
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[DesktopConfigOut]:
|
||||
try:
|
||||
row = await config_service.update_config(db, config_id, body)
|
||||
except config_service.AdminDesktopConfigError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="配置已更新",
|
||||
data=DesktopConfigOut.model_validate(row),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{config_id}", response_model=ApiResponse[None])
|
||||
async def delete_config(
|
||||
config_id: int,
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[None]:
|
||||
try:
|
||||
await config_service.delete_config(db, config_id)
|
||||
except config_service.AdminDesktopConfigError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(ok=True, message="配置已删除")
|
||||
91
app/api/v1/admin_users.py
Normal file
91
app/api/v1/admin_users.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""管理员用户 CRUD。"""
|
||||
|
||||
from fastapi import APIRouter, Query
|
||||
|
||||
from app.dependencies import AdminUser, DbSession
|
||||
from app.schemas.admin_user import AdminUserCreate, AdminUserOut, AdminUserUpdate
|
||||
from app.schemas.common import ApiResponse, PaginatedData
|
||||
from app.services import admin_user as admin_user_service
|
||||
|
||||
router = APIRouter(prefix="/admin/users", tags=["管理-用户"])
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[PaginatedData[AdminUserOut]])
|
||||
async def list_users(
|
||||
_admin: AdminUser,
|
||||
db: DbSession,
|
||||
page: int = Query(1, ge=1, description="页码,从 1 开始"),
|
||||
page_size: int = Query(
|
||||
admin_user_service.DEFAULT_PAGE_SIZE,
|
||||
ge=1,
|
||||
le=admin_user_service.MAX_PAGE_SIZE,
|
||||
description="每页条数",
|
||||
),
|
||||
) -> ApiResponse[PaginatedData[AdminUserOut]]:
|
||||
users, total = await admin_user_service.list_users(db, page=page, page_size=page_size)
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="",
|
||||
data=PaginatedData.build(
|
||||
[AdminUserOut.model_validate(u) for u in users],
|
||||
total=total,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=ApiResponse[AdminUserOut])
|
||||
async def create_user(
|
||||
body: AdminUserCreate,
|
||||
admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[AdminUserOut]:
|
||||
try:
|
||||
user = await admin_user_service.create_user(db, body)
|
||||
except admin_user_service.AdminUserError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="用户已创建",
|
||||
data=AdminUserOut.model_validate(user),
|
||||
)
|
||||
|
||||
|
||||
@router.patch("/{user_id}", response_model=ApiResponse[AdminUserOut])
|
||||
async def update_user(
|
||||
user_id: int,
|
||||
body: AdminUserUpdate,
|
||||
admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[AdminUserOut]:
|
||||
try:
|
||||
user = await admin_user_service.update_user(
|
||||
db,
|
||||
user_id,
|
||||
body,
|
||||
actor_id=admin.id,
|
||||
)
|
||||
except admin_user_service.AdminUserError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(
|
||||
ok=True,
|
||||
message="用户已更新",
|
||||
data=AdminUserOut.model_validate(user),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{user_id}", response_model=ApiResponse[None])
|
||||
async def delete_user(
|
||||
user_id: int,
|
||||
admin: AdminUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[None]:
|
||||
try:
|
||||
await admin_user_service.delete_user(db, user_id, actor_id=admin.id)
|
||||
except admin_user_service.AdminUserError as exc:
|
||||
return ApiResponse(ok=False, message=exc.message)
|
||||
|
||||
return ApiResponse(ok=True, message="用户已删除")
|
||||
19
app/api/v1/app_config.py
Normal file
19
app/api/v1/app_config.py
Normal file
@@ -0,0 +1,19 @@
|
||||
"""已登录用户拉取 desktop_configs 表全部键值。"""
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.dependencies import CurrentUser, DbSession
|
||||
from app.schemas.app_config import AppConfigMap
|
||||
from app.schemas.common import ApiResponse
|
||||
from app.services import desktop_config as desktop_config_service
|
||||
|
||||
router = APIRouter(prefix="/appConfig", tags=["appConfig"])
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[AppConfigMap])
|
||||
async def get_app_config(
|
||||
_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[AppConfigMap]:
|
||||
items = await desktop_config_service.list_all_as_map(db)
|
||||
return ApiResponse(ok=True, message="", data=items)
|
||||
47
app/api/v1/nodejs_scripts.py
Normal file
47
app/api/v1/nodejs_scripts.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Node.js 流水线脚本:按文件名下发源码,供桌面端拉取后在 Node 子进程中执行。"""
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.schemas.common import ApiResponse
|
||||
|
||||
router = APIRouter(prefix="/nodejs-scripts", tags=["nodejs"])
|
||||
|
||||
# pythonbackend/scripts/nodejs/*.js
|
||||
_SCRIPTS_DIR = Path(__file__).resolve().parents[3] / "scripts" / "nodejs"
|
||||
|
||||
_SAFE_NAME = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9._-]*\.js$")
|
||||
|
||||
|
||||
def _validate_name(name: str) -> str | None:
|
||||
if not name or not _SAFE_NAME.fullmatch(name.strip()):
|
||||
return None
|
||||
return name.strip()
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[dict])
|
||||
async def get_nodejs_script(name: str) -> ApiResponse[dict]:
|
||||
safe = _validate_name(name)
|
||||
if not safe:
|
||||
return ApiResponse(ok=False, message="无效的脚本名称", data=None)
|
||||
path = _SCRIPTS_DIR / safe
|
||||
try:
|
||||
base_resolved = _SCRIPTS_DIR.resolve()
|
||||
resolved = path.resolve()
|
||||
resolved.relative_to(base_resolved)
|
||||
except (OSError, ValueError):
|
||||
return ApiResponse(ok=False, message="无效的脚本路径", data=None)
|
||||
if not path.is_file():
|
||||
return ApiResponse(ok=False, message=f"脚本不存在: {safe}", data=None)
|
||||
source = path.read_text(encoding="utf-8")
|
||||
return ApiResponse(ok=True, message="", data={"source": source})
|
||||
|
||||
|
||||
@router.get("/list", response_model=ApiResponse[dict])
|
||||
async def list_nodejs_scripts() -> ApiResponse[dict]:
|
||||
if not _SCRIPTS_DIR.is_dir():
|
||||
return ApiResponse(ok=True, message="", data={"names": []})
|
||||
names = sorted(p.name for p in _SCRIPTS_DIR.glob("*.js") if p.is_file())
|
||||
return ApiResponse(ok=True, message="", data={"names": names})
|
||||
@@ -1,7 +1,20 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1 import auth, quickjs_scripts
|
||||
from app.api.v1 import (
|
||||
admin_card_keys,
|
||||
admin_desktop_configs,
|
||||
admin_users,
|
||||
app_config,
|
||||
auth,
|
||||
nodejs_scripts,
|
||||
quickjs_scripts,
|
||||
)
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(auth.router)
|
||||
api_router.include_router(admin_users.router)
|
||||
api_router.include_router(admin_card_keys.router)
|
||||
api_router.include_router(admin_desktop_configs.router)
|
||||
api_router.include_router(app_config.router)
|
||||
api_router.include_router(quickjs_scripts.router)
|
||||
api_router.include_router(nodejs_scripts.router)
|
||||
|
||||
Reference in New Issue
Block a user