refactor(server): 移除鉴权并归档遗留路由至 legacy/
- 删除 auth.py 与 deps.py,各路由去除 get_current_user 依赖 - collection.py 更名为 materials.py,冻结链路(ozon/publish/shops/categories)移入 legacy/ - 扩展默认生图服务端口并入 8800 并自动迁移旧配置,水印默认文案改为 Panda Store - 新增 docs/v2.1/backend-structure.md 后端结构盘点文档
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
"""Ozon 类目/属性字典代理(服务端持店铺凭证调用 Ozon,前端不直连)。"""
|
||||
# ⚠️ 冻结代码(Ozon API 直传链路):保留不维护,V2.1 起接口保留但不再投入。
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from core.security import decrypt_secret
|
||||
from db import get_db
|
||||
from legacy.models import Shop
|
||||
from legacy.services.ozon_client import OzonClient, OzonAPIError
|
||||
|
||||
router = APIRouter(prefix="/api/categories", tags=["categories"])
|
||||
|
||||
|
||||
class ShopRef(BaseModel):
|
||||
shop_id: str
|
||||
lang: str = "ZH_HANS" # 中文类目
|
||||
|
||||
|
||||
async def _client(shop_id: str, db: AsyncSession) -> OzonClient:
|
||||
shop = await db.get(Shop, UUID(shop_id))
|
||||
if shop is None:
|
||||
raise HTTPException(status_code=404, detail="店铺不存在")
|
||||
return OzonClient(decrypt_secret(shop.client_id_enc), decrypt_secret(shop.api_key_enc))
|
||||
|
||||
|
||||
def _unwrap(result: dict) -> dict:
|
||||
return result.get("result", result)
|
||||
|
||||
|
||||
@router.post("/tree")
|
||||
async def category_tree(
|
||||
body: ShopRef,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
client = await _client(body.shop_id, db)
|
||||
try:
|
||||
result = await client.post("/v1/description-category/tree", {"language": body.lang})
|
||||
return _unwrap(result)
|
||||
except OzonAPIError as exc:
|
||||
raise HTTPException(status_code=502, detail=exc.detail)
|
||||
|
||||
|
||||
class AttributeQuery(BaseModel):
|
||||
shop_id: str
|
||||
type_id: int
|
||||
lang: str = "ZH_HANS"
|
||||
|
||||
|
||||
@router.post("/{category_id}/attributes")
|
||||
async def category_attributes(
|
||||
category_id: int,
|
||||
body: AttributeQuery,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
client = await _client(body.shop_id, db)
|
||||
try:
|
||||
result = await client.post(
|
||||
"/v1/description-category/attribute",
|
||||
{
|
||||
"description_category_id": category_id,
|
||||
"type_id": body.type_id,
|
||||
"language": body.lang,
|
||||
},
|
||||
)
|
||||
return _unwrap(result)
|
||||
except OzonAPIError as exc:
|
||||
raise HTTPException(status_code=502, detail=exc.detail)
|
||||
|
||||
|
||||
class ValueQuery(BaseModel):
|
||||
shop_id: str
|
||||
category_id: int
|
||||
type_id: int
|
||||
q: str | None = None
|
||||
limit: int = 100
|
||||
last_value_id: int | None = None
|
||||
lang: str = "ZH_HANS"
|
||||
|
||||
|
||||
@router.post("/attribute/{attribute_id}/values")
|
||||
async def attribute_values(
|
||||
attribute_id: int,
|
||||
body: ValueQuery,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
client = await _client(body.shop_id, db)
|
||||
try:
|
||||
if body.q and len(body.q) >= 2:
|
||||
result = await client.post(
|
||||
"/v1/description-category/attribute/values/search",
|
||||
{
|
||||
"attribute_id": attribute_id,
|
||||
"description_category_id": body.category_id,
|
||||
"type_id": body.type_id,
|
||||
"limit": body.limit,
|
||||
"value": body.q,
|
||||
},
|
||||
)
|
||||
else:
|
||||
result = await client.post(
|
||||
"/v1/description-category/attribute/values",
|
||||
{
|
||||
"attribute_id": attribute_id,
|
||||
"description_category_id": body.category_id,
|
||||
"type_id": body.type_id,
|
||||
"limit": body.limit,
|
||||
"last_value_id": body.last_value_id or 0,
|
||||
"language": body.lang,
|
||||
},
|
||||
)
|
||||
return result # values 返回 {result, has_next}
|
||||
except OzonAPIError as exc:
|
||||
raise HTTPException(status_code=502, detail=exc.detail)
|
||||
@@ -0,0 +1,5 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/api/ozon", tags=["ozon"])
|
||||
|
||||
# Phase 3: Ozon Seller API product upload
|
||||
@@ -0,0 +1,181 @@
|
||||
"""发布端点:提交 ImportProductsV3 + 后台轮询回填。"""
|
||||
# ⚠️ 冻结代码(Ozon API 直传链路):保留不维护,V2.1 起接口保留但不再投入。
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime, timezone
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from core.security import decrypt_secret
|
||||
from db import get_db, get_session_factory
|
||||
from models import Product
|
||||
from legacy.models import PublishTask, Shop
|
||||
from models.enums import PublishStatus, Stage
|
||||
from legacy.services.ozon_client import OzonClient, OzonAPIError
|
||||
from legacy.services.publish import build_import_item, validate_ready
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["publish"])
|
||||
|
||||
|
||||
class PublishRequest(BaseModel):
|
||||
shop_id: str
|
||||
|
||||
|
||||
def _client(shop: Shop) -> OzonClient:
|
||||
return OzonClient(decrypt_secret(shop.client_id_enc), decrypt_secret(shop.api_key_enc))
|
||||
|
||||
|
||||
@router.post("/products/{product_id}/publish")
|
||||
async def publish_product(
|
||||
product_id: str,
|
||||
body: PublishRequest,
|
||||
background: BackgroundTasks,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
product = await db.get(Product, UUID(product_id))
|
||||
if product is None:
|
||||
raise HTTPException(status_code=404, detail="商品不存在")
|
||||
shop = await db.get(Shop, UUID(body.shop_id))
|
||||
if shop is None:
|
||||
raise HTTPException(status_code=404, detail="店铺不存在")
|
||||
|
||||
missing = validate_ready(product)
|
||||
if missing:
|
||||
raise HTTPException(status_code=422, detail=f"缺少必填项:{'、'.join(missing)}")
|
||||
|
||||
item = build_import_item(product)
|
||||
client = _client(shop)
|
||||
try:
|
||||
result = await client.post("/v3/product/import", {"items": [item]})
|
||||
except OzonAPIError as exc:
|
||||
raise HTTPException(status_code=502, detail=exc.detail)
|
||||
|
||||
task_id = (result.get("result") or {}).get("task_id")
|
||||
if not task_id:
|
||||
raise HTTPException(status_code=502, detail=f"Ozon 未返回 task_id:{result}")
|
||||
|
||||
task = PublishTask(
|
||||
product_id=product.id,
|
||||
shop_id=shop.id,
|
||||
ozon_task_id=int(task_id),
|
||||
status=PublishStatus.pending,
|
||||
request_payload=item,
|
||||
)
|
||||
db.add(task)
|
||||
product.stage = Stage.publishing
|
||||
await db.commit()
|
||||
await db.refresh(task)
|
||||
|
||||
background.add_task(_poll, str(task.id))
|
||||
return {"task_id": str(task.id), "ozon_task_id": task.ozon_task_id}
|
||||
|
||||
|
||||
async def _poll(task_id: str) -> None:
|
||||
"""后台轮询 import/info,直到 imported / failed 或超时(约 40s)。"""
|
||||
async with get_session_factory()() as db:
|
||||
task = await db.get(PublishTask, UUID(task_id))
|
||||
if task is None:
|
||||
return
|
||||
shop = await db.get(Shop, task.shop_id)
|
||||
product = await db.get(Product, task.product_id)
|
||||
if shop is None or product is None:
|
||||
return
|
||||
client = _client(shop)
|
||||
|
||||
for attempt in range(8):
|
||||
try:
|
||||
result = await client.post("/v1/product/import/info", {"task_id": task.ozon_task_id})
|
||||
except OzonAPIError as exc:
|
||||
task.status = PublishStatus.failed
|
||||
task.errors = [{"error": exc.detail}]
|
||||
task.completed_at = datetime.now(timezone.utc)
|
||||
product.stage = Stage.failed
|
||||
await db.commit()
|
||||
return
|
||||
|
||||
items = (result.get("result") or {}).get("items") or []
|
||||
item = items[0] if items else {}
|
||||
status = item.get("status", "")
|
||||
product_id = item.get("product_id")
|
||||
errors = item.get("errors") or []
|
||||
|
||||
if status == "imported":
|
||||
task.status = PublishStatus.imported
|
||||
task.response = item
|
||||
task.completed_at = datetime.now(timezone.utc)
|
||||
if product_id:
|
||||
product.ozon_product_id = int(product_id)
|
||||
product.stage = Stage.published
|
||||
product.published_at = datetime.now(timezone.utc)
|
||||
await db.commit()
|
||||
return
|
||||
|
||||
if status == "failed":
|
||||
task.status = PublishStatus.failed
|
||||
task.errors = errors
|
||||
task.response = item
|
||||
task.completed_at = datetime.now(timezone.utc)
|
||||
product.stage = Stage.failed
|
||||
await db.commit()
|
||||
return
|
||||
|
||||
# pending / moderation → 继续等
|
||||
task.status = PublishStatus.moderation if status in ("moderating", "moderation") else PublishStatus.processing
|
||||
if product_id:
|
||||
product.ozon_product_id = int(product_id)
|
||||
await db.commit()
|
||||
await asyncio.sleep(5 * (attempt + 1))
|
||||
|
||||
# 超时未定:保留 processing,前端可刷新
|
||||
task.status = PublishStatus.moderation
|
||||
task.response = item
|
||||
await db.commit()
|
||||
|
||||
|
||||
@router.get("/publish/{task_id}")
|
||||
async def get_publish_task(
|
||||
task_id: str,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
task = await db.get(PublishTask, UUID(task_id))
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="发布任务不存在")
|
||||
return {
|
||||
"id": str(task.id),
|
||||
"product_id": str(task.product_id),
|
||||
"shop_id": str(task.shop_id),
|
||||
"ozon_task_id": task.ozon_task_id,
|
||||
"status": task.status.value,
|
||||
"errors": task.errors,
|
||||
"response": task.response,
|
||||
"created_at": task.created_at,
|
||||
"completed_at": task.completed_at,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/products/{product_id}/publish-history")
|
||||
async def publish_history(
|
||||
product_id: str,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
rows = (await db.scalars(
|
||||
select(PublishTask)
|
||||
.where(PublishTask.product_id == UUID(product_id))
|
||||
.order_by(PublishTask.created_at.desc())
|
||||
)).all()
|
||||
return [
|
||||
{
|
||||
"id": str(t.id),
|
||||
"ozon_task_id": t.ozon_task_id,
|
||||
"status": t.status.value,
|
||||
"errors": t.errors,
|
||||
"created_at": t.created_at,
|
||||
"completed_at": t.completed_at,
|
||||
}
|
||||
for t in rows
|
||||
]
|
||||
@@ -0,0 +1,119 @@
|
||||
"""店铺管理:绑定 Ozon Client-Id/Api-Key(加密落库)+ 连通性校验。"""
|
||||
# ⚠️ 冻结代码(Ozon API 直传链路):保留不维护,V2.1 起接口保留但不再投入。
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from core.security import decrypt_secret, encrypt_secret
|
||||
from db import get_db
|
||||
from legacy.models import Shop
|
||||
from models.enums import ShopStatus
|
||||
from legacy.schemas.shop import ShopCreate, ShopListItem, ShopUpdate
|
||||
from legacy.services.ozon_client import OzonClient, OzonAPIError
|
||||
|
||||
router = APIRouter(prefix="/api/shops", tags=["shops"])
|
||||
|
||||
|
||||
def _mask(client_id: str) -> str:
|
||||
return f"…{client_id[-4:]}" if len(client_id) > 4 else "…"
|
||||
|
||||
|
||||
@router.get("", response_model=list[ShopListItem])
|
||||
async def list_shops(
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
rows = (await db.scalars(select(Shop).order_by(Shop.created_at))).all()
|
||||
items = []
|
||||
for s in rows:
|
||||
item = ShopListItem.model_validate(s)
|
||||
try:
|
||||
item.client_id_masked = _mask(decrypt_secret(s.client_id_enc))
|
||||
except Exception: # noqa: BLE001
|
||||
item.client_id_masked = "…"
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
|
||||
@router.post("", response_model=ShopListItem)
|
||||
async def create_shop(
|
||||
body: ShopCreate,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
shop = Shop(
|
||||
name=body.name,
|
||||
client_id_enc=encrypt_secret(body.client_id),
|
||||
api_key_enc=encrypt_secret(body.api_key),
|
||||
currency_code=body.currency_code or "RUB",
|
||||
status=ShopStatus.active,
|
||||
)
|
||||
db.add(shop)
|
||||
await db.commit()
|
||||
await db.refresh(shop)
|
||||
item = ShopListItem.model_validate(shop)
|
||||
item.client_id_masked = _mask(body.client_id)
|
||||
return item
|
||||
|
||||
|
||||
@router.patch("/{shop_id}", response_model=ShopListItem)
|
||||
async def update_shop(
|
||||
shop_id: str,
|
||||
body: ShopUpdate,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
shop = await db.get(Shop, UUID(shop_id))
|
||||
if shop is None:
|
||||
raise HTTPException(status_code=404, detail="店铺不存在")
|
||||
if body.name is not None:
|
||||
shop.name = body.name
|
||||
if body.currency_code is not None:
|
||||
shop.currency_code = body.currency_code
|
||||
if body.client_id:
|
||||
shop.client_id_enc = encrypt_secret(body.client_id)
|
||||
if body.api_key:
|
||||
shop.api_key_enc = encrypt_secret(body.api_key)
|
||||
await db.commit()
|
||||
await db.refresh(shop)
|
||||
item = ShopListItem.model_validate(shop)
|
||||
item.client_id_masked = _mask(decrypt_secret(shop.client_id_enc))
|
||||
return item
|
||||
|
||||
|
||||
@router.delete("/{shop_id}")
|
||||
async def delete_shop(
|
||||
shop_id: str,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
shop = await db.get(Shop, UUID(shop_id))
|
||||
if shop is None:
|
||||
raise HTTPException(status_code=404, detail="店铺不存在")
|
||||
await db.delete(shop)
|
||||
await db.commit()
|
||||
return {"deleted": True}
|
||||
|
||||
|
||||
@router.post("/{shop_id}/test")
|
||||
async def test_shop(
|
||||
shop_id: str,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
shop = await db.get(Shop, UUID(shop_id))
|
||||
if shop is None:
|
||||
raise HTTPException(status_code=404, detail="店铺不存在")
|
||||
client = OzonClient(decrypt_secret(shop.client_id_enc), decrypt_secret(shop.api_key_enc))
|
||||
try:
|
||||
result = await client.test_credentials()
|
||||
except OzonAPIError as exc:
|
||||
shop.status = ShopStatus.invalid
|
||||
await db.commit()
|
||||
return {"ok": False, "error": exc.detail, "roles": []}
|
||||
|
||||
shop.status = ShopStatus.active
|
||||
shop.last_checked_at = datetime.now(timezone.utc)
|
||||
await db.commit()
|
||||
roles = [r.get("name") for r in result.get("roles", [])]
|
||||
return {"ok": True, "roles": roles}
|
||||
Reference in New Issue
Block a user