Compare commits
16
Commits
8ab2d869fc
..
main
@@ -42,6 +42,12 @@ Thumbs.db
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Database data
|
||||
backend/data/
|
||||
|
||||
# Virtual environment
|
||||
venv/
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
stages:
|
||||
- lint
|
||||
- test
|
||||
- build
|
||||
|
||||
variables:
|
||||
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
|
||||
NPM_CONFIG_CACHE: "$CI_PROJECT_DIR/.cache/npm"
|
||||
|
||||
# ─── Backend Jobs ────────────────────────────────────────────────
|
||||
|
||||
backend-lint:
|
||||
stage: lint
|
||||
image: python:3.12-slim
|
||||
only:
|
||||
- main
|
||||
- merge_requests
|
||||
cache:
|
||||
key: backend-pip
|
||||
paths:
|
||||
- .cache/pip
|
||||
script:
|
||||
- cd backend
|
||||
- python3 -c "
|
||||
import ast, sys, pathlib;
|
||||
ok = True;
|
||||
for p in sorted(pathlib.Path('.').rglob('*.py')):
|
||||
try:
|
||||
ast.parse(p.read_text());
|
||||
except SyntaxError as e:
|
||||
print(f'SyntaxError in {p}\u003a {e}');
|
||||
ok = False;
|
||||
if not ok:
|
||||
sys.exit(1);
|
||||
print(f'All {len(list(pathlib.Path(\".\").rglob(\"*.py\")))} Python files passed syntax check')
|
||||
"
|
||||
|
||||
backend-test:
|
||||
stage: test
|
||||
image: python:3.12-slim
|
||||
only:
|
||||
- main
|
||||
- merge_requests
|
||||
cache:
|
||||
key: backend-pip
|
||||
paths:
|
||||
- .cache/pip
|
||||
variables:
|
||||
DATABASE_URL: "sqlite+aiosqlite:///./test.db"
|
||||
SECRET_KEY: "ci-test-secret-key"
|
||||
script:
|
||||
- cd backend
|
||||
- pip install -e ".[dev]" aiosqlite "bcrypt<5" --quiet
|
||||
- pytest tests/ -x -q --tb=short
|
||||
|
||||
# ─── Frontend Jobs ───────────────────────────────────────────────
|
||||
|
||||
.frontend-base:
|
||||
image: node:20-slim
|
||||
only:
|
||||
- main
|
||||
- merge_requests
|
||||
cache:
|
||||
key: frontend-npm
|
||||
paths:
|
||||
- .cache/npm
|
||||
- frontend/node_modules
|
||||
before_script:
|
||||
- cd frontend
|
||||
- npm ci --prefer-offline
|
||||
|
||||
frontend-lint:
|
||||
extends: .frontend-base
|
||||
stage: lint
|
||||
script:
|
||||
- npm run lint
|
||||
|
||||
frontend-typecheck:
|
||||
extends: .frontend-base
|
||||
stage: lint
|
||||
script:
|
||||
- npx tsc --noEmit
|
||||
|
||||
frontend-test:
|
||||
extends: .frontend-base
|
||||
stage: test
|
||||
script:
|
||||
- npm test -- --run
|
||||
|
||||
frontend-build:
|
||||
extends: .frontend-base
|
||||
stage: build
|
||||
script:
|
||||
- npm run build
|
||||
@@ -106,9 +106,9 @@ useEffect(() => { loadData() }, [loadData])
|
||||
- 配置项:`AI_PROVIDER`, `AI_API_KEY`, `AI_API_BASE_URL`
|
||||
|
||||
### 文件上传
|
||||
- 腾讯云 COS 直传,前端通过 `useOSSUpload` hook 处理
|
||||
- 流程:`api.getUploadPolicy()` → POST 到 COS → `api.fileUploaded()` 回调
|
||||
- COS 签名:HMAC-SHA1,字段包括 `q-sign-algorithm`、`q-ak`、`q-key-time`、`q-signature`、`policy`
|
||||
- 火山引擎 TOS 直传,前端通过 `useOSSUpload` hook 处理
|
||||
- 流程:`api.getUploadPolicy()` → POST 到 TOS → `api.fileUploaded()` 回调
|
||||
- TOS V4 签名:HMAC-SHA256,字段包括 `x-tos-algorithm`、`x-tos-credential`、`x-tos-date`、`x-tos-signature`、`policy`
|
||||
|
||||
### 实时推送
|
||||
- SSE (Server-Sent Events),端点 `/api/v1/sse/events`
|
||||
|
||||
@@ -30,12 +30,13 @@ AI_PROVIDER=oneapi
|
||||
AI_API_KEY=
|
||||
AI_API_BASE_URL=
|
||||
|
||||
# --- 腾讯云 COS ---
|
||||
COS_SECRET_ID=
|
||||
COS_SECRET_KEY=
|
||||
COS_REGION=ap-guangzhou
|
||||
COS_BUCKET_NAME=miaosi-files-1250000000
|
||||
COS_CDN_DOMAIN=
|
||||
# --- 火山引擎 TOS ---
|
||||
TOS_ACCESS_KEY_ID=
|
||||
TOS_SECRET_ACCESS_KEY=
|
||||
TOS_REGION=cn-beijing
|
||||
TOS_BUCKET_NAME=miaosi-files
|
||||
TOS_ENDPOINT=
|
||||
TOS_CDN_DOMAIN=
|
||||
|
||||
# --- 邮件 SMTP ---
|
||||
SMTP_HOST=
|
||||
|
||||
@@ -22,13 +22,15 @@ def upgrade() -> None:
|
||||
# 创建枚举类型
|
||||
platform_enum = postgresql.ENUM(
|
||||
'douyin', 'xiaohongshu', 'bilibili', 'kuaishou',
|
||||
name='platform_enum'
|
||||
name='platform_enum',
|
||||
create_type=False,
|
||||
)
|
||||
platform_enum.create(op.get_bind(), checkfirst=True)
|
||||
|
||||
task_status_enum = postgresql.ENUM(
|
||||
'pending', 'processing', 'completed', 'failed', 'approved', 'rejected',
|
||||
name='task_status_enum'
|
||||
name='task_status_enum',
|
||||
create_type=False,
|
||||
)
|
||||
task_status_enum.create(op.get_bind(), checkfirst=True)
|
||||
|
||||
|
||||
@@ -17,38 +17,9 @@ depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"manual_tasks",
|
||||
sa.Column("video_uploaded_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
op.alter_column(
|
||||
"manual_tasks",
|
||||
"video_url",
|
||||
existing_type=sa.String(length=2048),
|
||||
nullable=True,
|
||||
)
|
||||
op.add_column(
|
||||
"manual_tasks",
|
||||
sa.Column("script_content", sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"manual_tasks",
|
||||
sa.Column("script_file_url", sa.String(length=2048), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"manual_tasks",
|
||||
sa.Column("script_uploaded_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
# 原 manual_tasks 表已废弃,字段已合并到 003 的 tasks 表中
|
||||
pass
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("manual_tasks", "script_uploaded_at")
|
||||
op.drop_column("manual_tasks", "script_file_url")
|
||||
op.drop_column("manual_tasks", "script_content")
|
||||
op.alter_column(
|
||||
"manual_tasks",
|
||||
"video_url",
|
||||
existing_type=sa.String(length=2048),
|
||||
nullable=False,
|
||||
)
|
||||
op.drop_column("manual_tasks", "video_uploaded_at")
|
||||
pass
|
||||
|
||||
@@ -22,7 +22,8 @@ def upgrade() -> None:
|
||||
# 创建枚举类型
|
||||
user_role_enum = postgresql.ENUM(
|
||||
'brand', 'agency', 'creator',
|
||||
name='user_role_enum'
|
||||
name='user_role_enum',
|
||||
create_type=False,
|
||||
)
|
||||
user_role_enum.create(op.get_bind(), checkfirst=True)
|
||||
|
||||
@@ -30,10 +31,15 @@ def upgrade() -> None:
|
||||
'script_upload', 'script_ai_review', 'script_agency_review', 'script_brand_review',
|
||||
'video_upload', 'video_ai_review', 'video_agency_review', 'video_brand_review',
|
||||
'completed', 'rejected',
|
||||
name='task_stage_enum'
|
||||
name='task_stage_enum',
|
||||
create_type=False,
|
||||
)
|
||||
task_stage_enum.create(op.get_bind(), checkfirst=True)
|
||||
|
||||
# 扩展 task_status_enum:添加 Task 模型需要的值
|
||||
op.execute("ALTER TYPE task_status_enum ADD VALUE IF NOT EXISTS 'passed'")
|
||||
op.execute("ALTER TYPE task_status_enum ADD VALUE IF NOT EXISTS 'force_passed'")
|
||||
|
||||
# 用户表
|
||||
op.create_table(
|
||||
'users',
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
"""添加平台规则表
|
||||
|
||||
Revision ID: 006
|
||||
Revises: 005
|
||||
Create Date: 2026-02-10
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '006'
|
||||
down_revision: Union[str, None] = '005'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
'platform_rules',
|
||||
sa.Column('id', sa.String(64), primary_key=True),
|
||||
sa.Column('tenant_id', sa.String(64), sa.ForeignKey('tenants.id', ondelete='CASCADE'), nullable=False, index=True),
|
||||
sa.Column('brand_id', sa.String(64), nullable=False, index=True),
|
||||
sa.Column('platform', sa.String(50), nullable=False, index=True),
|
||||
sa.Column('document_url', sa.String(2048), nullable=False),
|
||||
sa.Column('document_name', sa.String(512), nullable=False),
|
||||
sa.Column('parsed_rules', sa.JSON().with_variant(postgresql.JSONB, 'postgresql'), nullable=True),
|
||||
sa.Column('status', sa.String(20), nullable=False, default='draft', index=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table('platform_rules')
|
||||
@@ -0,0 +1,26 @@
|
||||
"""添加 Brief 代理商附件字段
|
||||
|
||||
Revision ID: 007
|
||||
Revises: 006
|
||||
Create Date: 2026-02-10
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '007'
|
||||
down_revision: Union[str, None] = '006'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column('briefs', sa.Column('agency_attachments', sa.JSON(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column('briefs', 'agency_attachments')
|
||||
@@ -0,0 +1,26 @@
|
||||
"""添加项目发布平台字段
|
||||
|
||||
Revision ID: 008
|
||||
Revises: 007
|
||||
Create Date: 2026-02-10
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '008'
|
||||
down_revision: Union[str, None] = '007'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column('projects', sa.Column('platform', sa.String(50), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column('projects', 'platform')
|
||||
@@ -0,0 +1,25 @@
|
||||
"""add min_selling_points to briefs
|
||||
|
||||
Revision ID: 261778c01ef8
|
||||
Revises: 008
|
||||
Create Date: 2026-02-11 18:16:59.557746
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '261778c01ef8'
|
||||
down_revision: Union[str, None] = '008'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column('briefs', sa.Column('min_selling_points', sa.Integer(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column('briefs', 'min_selling_points')
|
||||
+295
-1
@@ -1,8 +1,12 @@
|
||||
"""
|
||||
Brief API
|
||||
项目 Brief 文档的 CRUD
|
||||
项目 Brief 文档的 CRUD + AI 解析
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import selectinload
|
||||
@@ -16,10 +20,13 @@ from app.api.deps import get_current_user
|
||||
from app.schemas.brief import (
|
||||
BriefCreateRequest,
|
||||
BriefUpdateRequest,
|
||||
AgencyBriefUpdateRequest,
|
||||
BriefResponse,
|
||||
)
|
||||
from app.services.auth import generate_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/projects/{project_id}/brief", tags=["Brief"])
|
||||
|
||||
|
||||
@@ -74,6 +81,7 @@ def _brief_to_response(brief: Brief) -> BriefResponse:
|
||||
file_url=brief.file_url,
|
||||
file_name=brief.file_name,
|
||||
selling_points=brief.selling_points,
|
||||
min_selling_points=brief.min_selling_points,
|
||||
blacklist_words=brief.blacklist_words,
|
||||
competitors=brief.competitors,
|
||||
brand_tone=brief.brand_tone,
|
||||
@@ -81,6 +89,7 @@ def _brief_to_response(brief: Brief) -> BriefResponse:
|
||||
max_duration=brief.max_duration,
|
||||
other_requirements=brief.other_requirements,
|
||||
attachments=brief.attachments,
|
||||
agency_attachments=brief.agency_attachments,
|
||||
created_at=brief.created_at,
|
||||
updated_at=brief.updated_at,
|
||||
)
|
||||
@@ -137,6 +146,7 @@ async def create_brief(
|
||||
max_duration=request.max_duration,
|
||||
other_requirements=request.other_requirements,
|
||||
attachments=request.attachments,
|
||||
agency_attachments=request.agency_attachments,
|
||||
)
|
||||
db.add(brief)
|
||||
await db.flush()
|
||||
@@ -180,3 +190,287 @@ async def update_brief(
|
||||
await db.refresh(brief)
|
||||
|
||||
return _brief_to_response(brief)
|
||||
|
||||
|
||||
@router.patch("/agency-attachments", response_model=BriefResponse)
|
||||
async def update_brief_agency_attachments(
|
||||
project_id: str,
|
||||
request: AgencyBriefUpdateRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""更新 Brief 代理商配置(代理商操作)
|
||||
|
||||
代理商可更新:agency_attachments、selling_points、blacklist_words。
|
||||
不能修改品牌方设置的核心 Brief 内容(文件、时长、竞品等)。
|
||||
"""
|
||||
# 权限检查:代理商必须属于该项目
|
||||
result = await db.execute(
|
||||
select(Project)
|
||||
.options(selectinload(Project.brand), selectinload(Project.agencies))
|
||||
.where(Project.id == project_id)
|
||||
)
|
||||
project = result.scalar_one_or_none()
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="项目不存在")
|
||||
|
||||
if current_user.role == UserRole.AGENCY:
|
||||
agency_result = await db.execute(
|
||||
select(Agency).where(Agency.user_id == current_user.id)
|
||||
)
|
||||
agency = agency_result.scalar_one_or_none()
|
||||
if not agency or agency not in project.agencies:
|
||||
raise HTTPException(status_code=403, detail="无权访问此项目")
|
||||
elif current_user.role == UserRole.BRAND:
|
||||
# 品牌方也可以更新代理商附件
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.user_id == current_user.id)
|
||||
)
|
||||
brand = brand_result.scalar_one_or_none()
|
||||
if not brand or project.brand_id != brand.id:
|
||||
raise HTTPException(status_code=403, detail="无权访问此项目")
|
||||
else:
|
||||
raise HTTPException(status_code=403, detail="无权修改代理商附件")
|
||||
|
||||
# 获取 Brief
|
||||
brief_result = await db.execute(
|
||||
select(Brief)
|
||||
.options(selectinload(Brief.project))
|
||||
.where(Brief.project_id == project_id)
|
||||
)
|
||||
brief = brief_result.scalar_one_or_none()
|
||||
if not brief:
|
||||
raise HTTPException(status_code=404, detail="Brief 不存在")
|
||||
|
||||
# 更新代理商可编辑的字段
|
||||
update_fields = request.model_dump(exclude_unset=True)
|
||||
for field, value in update_fields.items():
|
||||
setattr(brief, field, value)
|
||||
|
||||
await db.flush()
|
||||
await db.refresh(brief)
|
||||
|
||||
return _brief_to_response(brief)
|
||||
|
||||
|
||||
# ==================== AI 解析 ====================
|
||||
|
||||
class BriefParseResponse(BaseModel):
|
||||
"""Brief AI 解析响应"""
|
||||
product_name: str = ""
|
||||
target_audience: str = ""
|
||||
content_requirements: str = ""
|
||||
selling_points: list[dict] = []
|
||||
blacklist_words: list[dict] = []
|
||||
|
||||
|
||||
@router.post("/parse", response_model=BriefParseResponse)
|
||||
async def parse_brief_with_ai(
|
||||
project_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
AI 解析 Brief 文档
|
||||
|
||||
从品牌方上传的 Brief 文件中提取结构化信息:
|
||||
- 产品名称
|
||||
- 目标人群
|
||||
- 内容要求
|
||||
- 卖点建议
|
||||
- 违禁词建议
|
||||
"""
|
||||
# 权限检查(代理商需要属于该项目)
|
||||
project = await _get_project_with_permission(project_id, current_user, db)
|
||||
|
||||
# 获取 Brief
|
||||
result = await db.execute(
|
||||
select(Brief)
|
||||
.options(selectinload(Brief.project))
|
||||
.where(Brief.project_id == project_id)
|
||||
)
|
||||
brief = result.scalar_one_or_none()
|
||||
if not brief:
|
||||
raise HTTPException(status_code=404, detail="Brief 不存在,请先让品牌方创建 Brief")
|
||||
|
||||
# 收集所有可解析的文档 URL
|
||||
documents: list[dict] = [] # [{"url": ..., "name": ...}]
|
||||
|
||||
if brief.file_url and brief.file_name:
|
||||
documents.append({"url": brief.file_url, "name": brief.file_name})
|
||||
|
||||
if brief.attachments:
|
||||
for att in brief.attachments:
|
||||
if att.get("url") and att.get("name"):
|
||||
documents.append({"url": att["url"], "name": att["name"]})
|
||||
|
||||
if not documents:
|
||||
raise HTTPException(status_code=400, detail="Brief 没有可解析的文件")
|
||||
|
||||
# 提取文本(每个文档限时 60 秒)
|
||||
import asyncio
|
||||
from app.services.document_parser import DocumentParser
|
||||
|
||||
all_texts = []
|
||||
for doc in documents:
|
||||
try:
|
||||
text = await asyncio.wait_for(
|
||||
DocumentParser.download_and_parse(doc["url"], doc["name"]),
|
||||
timeout=60.0,
|
||||
)
|
||||
if text and text.strip():
|
||||
all_texts.append(f"=== {doc['name']} ===\n{text}")
|
||||
logger.info(f"成功解析文档 {doc['name']},提取 {len(text)} 字符")
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"解析文档 {doc['name']} 超时(60s),已跳过")
|
||||
except Exception as e:
|
||||
logger.warning(f"解析文档 {doc['name']} 失败: {e}")
|
||||
|
||||
if not all_texts:
|
||||
raise HTTPException(status_code=400, detail="所有文档均解析失败,无法提取文本内容")
|
||||
|
||||
combined_text = "\n\n".join(all_texts)
|
||||
|
||||
# 截断过长文本
|
||||
max_chars = 15000
|
||||
if len(combined_text) > max_chars:
|
||||
combined_text = combined_text[:max_chars] + "\n...(内容已截断)"
|
||||
|
||||
# 获取 AI 客户端
|
||||
from app.services.ai_service import AIServiceFactory
|
||||
|
||||
tenant_id = project.brand_id or "default"
|
||||
ai_client = await AIServiceFactory.get_client(tenant_id, db)
|
||||
if not ai_client:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="AI 服务未配置,请在品牌方设置中配置 AI 服务",
|
||||
)
|
||||
|
||||
config = await AIServiceFactory.get_config(tenant_id, db)
|
||||
text_model = "gpt-4o"
|
||||
if config and config.models:
|
||||
text_model = config.models.get("text", "gpt-4o")
|
||||
|
||||
# AI 解析
|
||||
prompt = f"""你是营销内容合规审核专家。请从以下品牌方 Brief 文档中提取结构化信息。
|
||||
|
||||
文档内容:
|
||||
{combined_text}
|
||||
|
||||
请以 JSON 格式返回,不要包含其他内容:
|
||||
{{
|
||||
"product_name": "产品名称",
|
||||
"target_audience": "目标人群描述",
|
||||
"content_requirements": "内容创作要求的简要总结",
|
||||
"selling_points": [
|
||||
{{"content": "卖点1", "priority": "core"}},
|
||||
{{"content": "卖点2", "priority": "recommended"}},
|
||||
{{"content": "卖点3", "priority": "reference"}}
|
||||
],
|
||||
"blacklist_words": [
|
||||
{{"word": "违禁词1", "reason": "原因"}},
|
||||
{{"word": "违禁词2", "reason": "原因"}}
|
||||
]
|
||||
}}
|
||||
|
||||
说明:
|
||||
- product_name: 从文档中识别的产品/品牌名称
|
||||
- target_audience: 目标消费人群
|
||||
- content_requirements: 对达人创作内容的要求(时长、风格、场景等)
|
||||
- selling_points: 产品卖点,priority 说明:
|
||||
- "core": 核心卖点,品牌方重点关注,建议优先传达
|
||||
- "recommended": 推荐卖点,建议提及
|
||||
- "reference": 参考信息,不要求出现在脚本中
|
||||
- blacklist_words: 从文档中识别的需要避免的词语(绝对化用语、竞品名、敏感词等)"""
|
||||
|
||||
last_error = None
|
||||
for attempt in range(2):
|
||||
try:
|
||||
response = await ai_client.chat_completion(
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
model=text_model,
|
||||
temperature=0.2 if attempt == 0 else 0.1,
|
||||
max_tokens=2000,
|
||||
)
|
||||
|
||||
# 提取 JSON
|
||||
logger.info(f"AI 原始响应 (attempt={attempt}): {response.content[:500]}")
|
||||
content = _extract_json_from_response(response.content)
|
||||
logger.info(f"提取的 JSON: {content[:500]}")
|
||||
parsed = json.loads(content)
|
||||
|
||||
return BriefParseResponse(
|
||||
product_name=parsed.get("product_name", ""),
|
||||
target_audience=parsed.get("target_audience", ""),
|
||||
content_requirements=parsed.get("content_requirements", ""),
|
||||
selling_points=parsed.get("selling_points", []),
|
||||
blacklist_words=parsed.get("blacklist_words", []),
|
||||
)
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
last_error = e
|
||||
logger.warning(f"AI 返回内容非 JSON (attempt={attempt}): {e}, raw={response.content[:300]}")
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.error(f"AI 解析 Brief 失败: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"AI 解析失败: {str(e)[:200]}")
|
||||
|
||||
# 两次都失败
|
||||
logger.error(f"AI 解析 Brief JSON 格式错误,两次重试均失败: {last_error}")
|
||||
raise HTTPException(status_code=500, detail="AI 解析结果格式错误,请重试")
|
||||
|
||||
|
||||
def _extract_json_from_response(raw: str) -> str:
|
||||
"""从 AI 响应中提取 JSON 内容(处理 markdown 代码块、中文引号等)"""
|
||||
import re
|
||||
text = raw.strip()
|
||||
|
||||
# 移除 markdown ```json ... ``` 代码块包裹
|
||||
m = re.search(r'```(?:json)?\s*\n(.*?)```', text, re.DOTALL)
|
||||
if m:
|
||||
text = m.group(1).strip()
|
||||
|
||||
# 尝试找到第一个 { 和最后一个 }
|
||||
first_brace = text.find("{")
|
||||
last_brace = text.rfind("}")
|
||||
if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
|
||||
text = text[first_brace:last_brace + 1]
|
||||
|
||||
# 清理中文引号等特殊字符
|
||||
text = _sanitize_json_string(text)
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def _sanitize_json_string(text: str) -> str:
|
||||
"""
|
||||
清理 AI 返回的 JSON 文本中的中文引号等特殊字符。
|
||||
中文引号 "" 在 JSON 字符串值内会破坏解析。
|
||||
"""
|
||||
result = []
|
||||
in_string = False
|
||||
i = 0
|
||||
while i < len(text):
|
||||
ch = text[i]
|
||||
if ch == '\\' and in_string and i + 1 < len(text):
|
||||
result.append(ch)
|
||||
result.append(text[i + 1])
|
||||
i += 2
|
||||
continue
|
||||
if ch == '"' and not in_string:
|
||||
in_string = True
|
||||
result.append(ch)
|
||||
elif ch == '"' and in_string:
|
||||
in_string = False
|
||||
result.append(ch)
|
||||
elif in_string and ch in '\u201c\u201d\u300c\u300d':
|
||||
# 中文引号 "" 和「」 → 单引号
|
||||
result.append("'")
|
||||
elif not in_string and ch in '\u201c\u201d':
|
||||
# JSON 结构层的中文引号 → 英文双引号
|
||||
result.append('"')
|
||||
else:
|
||||
result.append(ch)
|
||||
i += 1
|
||||
return ''.join(result)
|
||||
|
||||
@@ -23,6 +23,7 @@ from app.schemas.project import (
|
||||
AgencySummary,
|
||||
)
|
||||
from app.services.auth import generate_id
|
||||
from app.services.message_service import create_message
|
||||
|
||||
router = APIRouter(prefix="/projects", tags=["项目"])
|
||||
|
||||
@@ -46,6 +47,7 @@ async def _project_to_response(project: Project, db: AsyncSession) -> ProjectRes
|
||||
id=project.id,
|
||||
name=project.name,
|
||||
description=project.description,
|
||||
platform=project.platform,
|
||||
brand_id=project.brand_id,
|
||||
brand_name=project.brand.name if project.brand else None,
|
||||
status=project.status,
|
||||
@@ -72,6 +74,7 @@ async def create_project(
|
||||
brand_id=brand.id,
|
||||
name=request.name,
|
||||
description=request.description,
|
||||
platform=request.platform,
|
||||
start_date=request.start_date,
|
||||
deadline=request.deadline,
|
||||
status="active",
|
||||
@@ -79,7 +82,7 @@ async def create_project(
|
||||
db.add(project)
|
||||
await db.flush()
|
||||
|
||||
# 分配代理商
|
||||
# 分配代理商(直接 INSERT 关联表,避免 async 懒加载问题)
|
||||
if request.agency_ids:
|
||||
for agency_id in request.agency_ids:
|
||||
result = await db.execute(
|
||||
@@ -87,7 +90,12 @@ async def create_project(
|
||||
)
|
||||
agency = result.scalar_one_or_none()
|
||||
if agency:
|
||||
project.agencies.append(agency)
|
||||
await db.execute(
|
||||
project_agency_association.insert().values(
|
||||
project_id=project.id,
|
||||
agency_id=agency.id,
|
||||
)
|
||||
)
|
||||
await db.flush()
|
||||
|
||||
await db.refresh(project)
|
||||
@@ -100,6 +108,40 @@ async def create_project(
|
||||
)
|
||||
project = result.scalar_one()
|
||||
|
||||
# 给品牌方用户发送项目创建成功消息
|
||||
brand_user_result = await db.execute(
|
||||
select(User).where(User.id == brand.user_id)
|
||||
)
|
||||
brand_user = brand_user_result.scalar_one_or_none()
|
||||
if brand_user:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_user.id,
|
||||
type="system_notice",
|
||||
title="项目创建成功",
|
||||
content=f"您的项目「{project.name}」已创建成功",
|
||||
related_project_id=project.id,
|
||||
)
|
||||
|
||||
# 给被分配的代理商发送新项目通知
|
||||
if project.agencies:
|
||||
for agency in project.agencies:
|
||||
agency_user_result = await db.execute(
|
||||
select(User).where(User.id == agency.user_id)
|
||||
)
|
||||
agency_user = agency_user_result.scalar_one_or_none()
|
||||
if agency_user:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=agency_user.id,
|
||||
type="new_task",
|
||||
title="新项目分配",
|
||||
content=f"品牌方「{brand.name}」将您加入了项目「{project.name}」",
|
||||
related_project_id=project.id,
|
||||
sender_name=brand.name,
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
return await _project_to_response(project, db)
|
||||
|
||||
|
||||
@@ -248,6 +290,8 @@ async def update_project(
|
||||
project.name = request.name
|
||||
if request.description is not None:
|
||||
project.description = request.description
|
||||
if request.platform is not None:
|
||||
project.platform = request.platform
|
||||
if request.start_date is not None:
|
||||
project.start_date = request.start_date
|
||||
if request.deadline is not None:
|
||||
@@ -281,6 +325,7 @@ async def assign_agencies(
|
||||
if project.brand_id != brand.id:
|
||||
raise HTTPException(status_code=403, detail="无权操作此项目")
|
||||
|
||||
newly_assigned = []
|
||||
for agency_id in request.agency_ids:
|
||||
agency_result = await db.execute(
|
||||
select(Agency).where(Agency.id == agency_id)
|
||||
@@ -288,10 +333,29 @@ async def assign_agencies(
|
||||
agency = agency_result.scalar_one_or_none()
|
||||
if agency and agency not in project.agencies:
|
||||
project.agencies.append(agency)
|
||||
newly_assigned.append(agency)
|
||||
|
||||
await db.flush()
|
||||
await db.refresh(project)
|
||||
|
||||
# 给新分配的代理商发送通知
|
||||
for agency in newly_assigned:
|
||||
agency_user_result = await db.execute(
|
||||
select(User).where(User.id == agency.user_id)
|
||||
)
|
||||
agency_user = agency_user_result.scalar_one_or_none()
|
||||
if agency_user:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=agency_user.id,
|
||||
type="new_task",
|
||||
title="新项目分配",
|
||||
content=f"品牌方「{brand.name}」将您加入了项目「{project.name}」",
|
||||
related_project_id=project.id,
|
||||
sender_name=brand.name,
|
||||
)
|
||||
|
||||
await db.commit()
|
||||
return await _project_to_response(project, db)
|
||||
|
||||
|
||||
|
||||
+586
-21
@@ -2,8 +2,10 @@
|
||||
规则管理 API
|
||||
违禁词库、白名单、竞品库、平台规则
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import uuid
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query, status
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional
|
||||
from sqlalchemy import select, and_
|
||||
@@ -11,7 +13,19 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import get_db
|
||||
from app.models.tenant import Tenant
|
||||
from app.models.rule import ForbiddenWord, WhitelistItem, Competitor
|
||||
from app.models.rule import ForbiddenWord, WhitelistItem, Competitor, PlatformRule, RuleStatus
|
||||
from app.schemas.rules import (
|
||||
PlatformRuleParseRequest,
|
||||
PlatformRuleParseResponse,
|
||||
PlatformRuleConfirmRequest,
|
||||
PlatformRuleResponse as PlatformRuleDBResponse,
|
||||
PlatformRuleListResponse as PlatformRuleDBListResponse,
|
||||
ParsedRulesData,
|
||||
)
|
||||
from app.services.document_parser import DocumentParser
|
||||
from app.services.ai_service import AIServiceFactory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/rules", tags=["rules"])
|
||||
|
||||
@@ -117,10 +131,14 @@ _platform_rules = {
|
||||
"xiaohongshu": {
|
||||
"platform": "xiaohongshu",
|
||||
"rules": [
|
||||
{"type": "forbidden_word", "words": ["最好", "绝对", "100%"]},
|
||||
{"type": "forbidden_word", "words": [
|
||||
"最好", "绝对", "100%", "第一", "最佳", "国家级", "顶级",
|
||||
"万能", "神器", "秒杀", "碾压", "永久", "根治",
|
||||
"一次见效", "立竿见影", "无副作用",
|
||||
]},
|
||||
],
|
||||
"version": "2024.01",
|
||||
"updated_at": "2024-01-10T00:00:00Z",
|
||||
"version": "2024.06",
|
||||
"updated_at": "2024-06-15T00:00:00Z",
|
||||
},
|
||||
"bilibili": {
|
||||
"platform": "bilibili",
|
||||
@@ -322,6 +340,33 @@ async def add_to_whitelist(
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/whitelist/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_whitelist_item(
|
||||
item_id: str,
|
||||
x_tenant_id: str = Header(..., alias="X-Tenant-ID"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""删除白名单项"""
|
||||
result = await db.execute(
|
||||
select(WhitelistItem).where(
|
||||
and_(
|
||||
WhitelistItem.id == item_id,
|
||||
WhitelistItem.tenant_id == x_tenant_id,
|
||||
)
|
||||
)
|
||||
)
|
||||
item = result.scalar_one_or_none()
|
||||
|
||||
if not item:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"白名单项不存在: {item_id}",
|
||||
)
|
||||
|
||||
await db.delete(item)
|
||||
await db.flush()
|
||||
|
||||
|
||||
# ==================== 竞品库 ====================
|
||||
|
||||
@router.get("/competitors", response_model=CompetitorListResponse)
|
||||
@@ -441,33 +486,496 @@ async def get_platform_rules(platform: str) -> PlatformRuleResponse:
|
||||
# ==================== 规则冲突检测 ====================
|
||||
|
||||
@router.post("/validate", response_model=RuleValidateResponse)
|
||||
async def validate_rules(request: RuleValidateRequest) -> RuleValidateResponse:
|
||||
"""检测 Brief 与平台规则冲突"""
|
||||
async def validate_rules(
|
||||
request: RuleValidateRequest,
|
||||
x_tenant_id: str = Header(..., alias="X-Tenant-ID"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> RuleValidateResponse:
|
||||
"""检测 Brief 与平台规则冲突(合并 DB 规则 + 硬编码兜底)"""
|
||||
conflicts = []
|
||||
|
||||
platform_rule = _platform_rules.get(request.platform)
|
||||
if not platform_rule:
|
||||
return RuleValidateResponse(conflicts=[])
|
||||
# 1. 收集违禁词:DB active 规则优先,硬编码兜底
|
||||
db_rules = await get_active_platform_rules(
|
||||
x_tenant_id, request.brand_id, request.platform, db
|
||||
)
|
||||
forbidden_words: set[str] = set()
|
||||
min_seconds: Optional[int] = None
|
||||
max_seconds: Optional[int] = None
|
||||
|
||||
# 检查 required_phrases 是否包含违禁词
|
||||
required_phrases = request.brief_rules.get("required_phrases", [])
|
||||
platform_forbidden = []
|
||||
for rule in platform_rule.get("rules", []):
|
||||
if db_rules:
|
||||
forbidden_words.update(db_rules.get("forbidden_words", []))
|
||||
duration = db_rules.get("duration") or {}
|
||||
min_seconds = duration.get("min_seconds")
|
||||
max_seconds = duration.get("max_seconds")
|
||||
|
||||
# 硬编码兜底
|
||||
hardcoded = _platform_rules.get(request.platform, {})
|
||||
for rule in hardcoded.get("rules", []):
|
||||
if rule.get("type") == "forbidden_word":
|
||||
platform_forbidden.extend(rule.get("words", []))
|
||||
forbidden_words.update(rule.get("words", []))
|
||||
elif rule.get("type") == "duration" and min_seconds is None:
|
||||
if rule.get("min_seconds") is not None:
|
||||
min_seconds = rule["min_seconds"]
|
||||
if rule.get("max_seconds") is not None and max_seconds is None:
|
||||
max_seconds = rule["max_seconds"]
|
||||
|
||||
for phrase in required_phrases:
|
||||
for word in platform_forbidden:
|
||||
if word in phrase:
|
||||
# 2. 检查卖点/必选短语与违禁词冲突
|
||||
phrases = list(request.brief_rules.get("required_phrases", []))
|
||||
phrases += list(request.brief_rules.get("selling_points", []))
|
||||
for phrase in phrases:
|
||||
for word in forbidden_words:
|
||||
if word in str(phrase):
|
||||
conflicts.append(RuleConflict(
|
||||
brief_rule=f"要求使用:{phrase}",
|
||||
platform_rule=f"平台禁止:{word}",
|
||||
suggestion=f"Brief 要求的 '{phrase}' 包含平台违禁词 '{word}',建议修改",
|
||||
brief_rule=f"卖点包含:{phrase}",
|
||||
platform_rule=f"{request.platform} 禁止使用:{word}",
|
||||
suggestion=f"卖点 '{phrase}' 包含违禁词 '{word}',建议修改表述",
|
||||
))
|
||||
|
||||
# 3. 检查时长冲突
|
||||
brief_min = request.brief_rules.get("min_duration")
|
||||
brief_max = request.brief_rules.get("max_duration")
|
||||
if min_seconds and brief_max and brief_max < min_seconds:
|
||||
conflicts.append(RuleConflict(
|
||||
brief_rule=f"Brief 最长时长:{brief_max}秒",
|
||||
platform_rule=f"{request.platform} 最短要求:{min_seconds}秒",
|
||||
suggestion=f"Brief 最长 {brief_max}s 低于平台最短要求 {min_seconds}s,视频可能不达标",
|
||||
))
|
||||
if max_seconds and brief_min and brief_min > max_seconds:
|
||||
conflicts.append(RuleConflict(
|
||||
brief_rule=f"Brief 最短时长:{brief_min}秒",
|
||||
platform_rule=f"{request.platform} 最长限制:{max_seconds}秒",
|
||||
suggestion=f"Brief 最短 {brief_min}s 超过平台最长限制 {max_seconds}s,建议调整",
|
||||
))
|
||||
|
||||
return RuleValidateResponse(conflicts=conflicts)
|
||||
|
||||
|
||||
# ==================== 品牌方平台规则(文档上传 + AI 解析) ====================
|
||||
|
||||
def _format_platform_rule(rule: PlatformRule) -> PlatformRuleDBResponse:
|
||||
"""将 ORM 对象转为响应 Schema"""
|
||||
return PlatformRuleDBResponse(
|
||||
id=rule.id,
|
||||
platform=rule.platform,
|
||||
brand_id=rule.brand_id,
|
||||
document_url=rule.document_url,
|
||||
document_name=rule.document_name,
|
||||
parsed_rules=ParsedRulesData(**(rule.parsed_rules or {})),
|
||||
status=rule.status,
|
||||
created_at=rule.created_at.isoformat() if rule.created_at else "",
|
||||
updated_at=rule.updated_at.isoformat() if rule.updated_at else "",
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/platform-rules/parse",
|
||||
response_model=PlatformRuleParseResponse,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
)
|
||||
async def parse_platform_rule_document(
|
||||
request: PlatformRuleParseRequest,
|
||||
x_tenant_id: str = Header(..., alias="X-Tenant-ID"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> PlatformRuleParseResponse:
|
||||
"""
|
||||
上传文档并通过 AI 解析平台规则
|
||||
|
||||
流程:
|
||||
1. 下载文档
|
||||
2. 提取纯文本
|
||||
3. AI 解析出结构化规则
|
||||
4. 存入 DB (status=draft)
|
||||
5. 返回解析结果供品牌方确认
|
||||
"""
|
||||
await _ensure_tenant_exists(x_tenant_id, db)
|
||||
|
||||
# 1. 尝试提取文本;对图片型 PDF 走视觉解析
|
||||
document_text = ""
|
||||
image_b64_list: list[str] = []
|
||||
|
||||
try:
|
||||
# 先检查是否为图片型 PDF
|
||||
image_b64_list = await DocumentParser.download_and_get_images(
|
||||
request.document_url, request.document_name,
|
||||
) or []
|
||||
except Exception as e:
|
||||
logger.warning(f"图片 PDF 检测失败,回退文本模式: {e}")
|
||||
|
||||
if not image_b64_list:
|
||||
# 非图片 PDF 或检测失败,走文本提取
|
||||
try:
|
||||
document_text = await DocumentParser.download_and_parse(
|
||||
request.document_url, request.document_name,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"文档解析失败: {e}")
|
||||
raise HTTPException(status_code=400, detail=f"文档下载或解析失败: {e}")
|
||||
|
||||
if not document_text.strip():
|
||||
raise HTTPException(status_code=400, detail="文档内容为空,无法解析")
|
||||
|
||||
# 2. AI 解析(图片模式 or 文本模式)
|
||||
if image_b64_list:
|
||||
parsed_rules = await _ai_parse_platform_rules_vision(
|
||||
x_tenant_id, request.platform, image_b64_list, db,
|
||||
)
|
||||
else:
|
||||
parsed_rules = await _ai_parse_platform_rules(x_tenant_id, request.platform, document_text, db)
|
||||
|
||||
# 3. 存入 DB (draft)
|
||||
rule_id = f"pr-{uuid.uuid4().hex[:8]}"
|
||||
rule = PlatformRule(
|
||||
id=rule_id,
|
||||
tenant_id=x_tenant_id,
|
||||
brand_id=request.brand_id,
|
||||
platform=request.platform,
|
||||
document_url=request.document_url,
|
||||
document_name=request.document_name,
|
||||
parsed_rules=parsed_rules,
|
||||
status=RuleStatus.DRAFT.value,
|
||||
)
|
||||
db.add(rule)
|
||||
await db.flush()
|
||||
|
||||
return PlatformRuleParseResponse(
|
||||
id=rule.id,
|
||||
platform=rule.platform,
|
||||
brand_id=rule.brand_id,
|
||||
document_url=rule.document_url,
|
||||
document_name=rule.document_name,
|
||||
parsed_rules=ParsedRulesData(**parsed_rules),
|
||||
status=rule.status,
|
||||
)
|
||||
|
||||
|
||||
@router.put(
|
||||
"/platform-rules/{rule_id}/confirm",
|
||||
response_model=PlatformRuleDBResponse,
|
||||
)
|
||||
async def confirm_platform_rule(
|
||||
rule_id: str,
|
||||
request: PlatformRuleConfirmRequest,
|
||||
x_tenant_id: str = Header(..., alias="X-Tenant-ID"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> PlatformRuleDBResponse:
|
||||
"""
|
||||
确认/编辑平台规则解析结果
|
||||
|
||||
将 draft 状态的规则设为 active,同时将同 (tenant_id, brand_id, platform) 下
|
||||
已有的 active 规则设为 inactive。
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(PlatformRule).where(
|
||||
and_(
|
||||
PlatformRule.id == rule_id,
|
||||
PlatformRule.tenant_id == x_tenant_id,
|
||||
)
|
||||
)
|
||||
)
|
||||
rule = result.scalar_one_or_none()
|
||||
if not rule:
|
||||
raise HTTPException(status_code=404, detail=f"规则不存在: {rule_id}")
|
||||
|
||||
# 将同 (tenant_id, brand_id, platform) 下已有的 active 规则设为 inactive
|
||||
existing_active = await db.execute(
|
||||
select(PlatformRule).where(
|
||||
and_(
|
||||
PlatformRule.tenant_id == x_tenant_id,
|
||||
PlatformRule.brand_id == rule.brand_id,
|
||||
PlatformRule.platform == rule.platform,
|
||||
PlatformRule.status == RuleStatus.ACTIVE.value,
|
||||
PlatformRule.id != rule_id,
|
||||
)
|
||||
)
|
||||
)
|
||||
for old_rule in existing_active.scalars().all():
|
||||
old_rule.status = RuleStatus.INACTIVE.value
|
||||
|
||||
# 更新当前规则
|
||||
rule.parsed_rules = request.parsed_rules.model_dump()
|
||||
rule.status = RuleStatus.ACTIVE.value
|
||||
await db.flush()
|
||||
await db.refresh(rule)
|
||||
|
||||
return _format_platform_rule(rule)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/platform-rules",
|
||||
response_model=PlatformRuleDBListResponse,
|
||||
)
|
||||
async def list_brand_platform_rules(
|
||||
brand_id: Optional[str] = Query(None),
|
||||
platform: Optional[str] = Query(None),
|
||||
rule_status: Optional[str] = Query(None, alias="status"),
|
||||
x_tenant_id: str = Header(..., alias="X-Tenant-ID"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> PlatformRuleDBListResponse:
|
||||
"""查询品牌方的平台规则列表"""
|
||||
query = select(PlatformRule).where(PlatformRule.tenant_id == x_tenant_id)
|
||||
|
||||
if brand_id:
|
||||
query = query.where(PlatformRule.brand_id == brand_id)
|
||||
if platform:
|
||||
query = query.where(PlatformRule.platform == platform)
|
||||
if rule_status:
|
||||
query = query.where(PlatformRule.status == rule_status)
|
||||
|
||||
result = await db.execute(query.order_by(PlatformRule.created_at.desc()))
|
||||
rules = result.scalars().all()
|
||||
|
||||
return PlatformRuleDBListResponse(
|
||||
items=[_format_platform_rule(r) for r in rules],
|
||||
total=len(rules),
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/platform-rules/{rule_id}",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
)
|
||||
async def delete_platform_rule(
|
||||
rule_id: str,
|
||||
x_tenant_id: str = Header(..., alias="X-Tenant-ID"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""删除平台规则"""
|
||||
result = await db.execute(
|
||||
select(PlatformRule).where(
|
||||
and_(
|
||||
PlatformRule.id == rule_id,
|
||||
PlatformRule.tenant_id == x_tenant_id,
|
||||
)
|
||||
)
|
||||
)
|
||||
rule = result.scalar_one_or_none()
|
||||
if not rule:
|
||||
raise HTTPException(status_code=404, detail=f"规则不存在: {rule_id}")
|
||||
|
||||
await db.delete(rule)
|
||||
await db.flush()
|
||||
|
||||
|
||||
async def _ai_parse_platform_rules(
|
||||
tenant_id: str,
|
||||
platform: str,
|
||||
document_text: str,
|
||||
db: AsyncSession,
|
||||
) -> dict:
|
||||
"""
|
||||
使用 AI 将文档文本解析为结构化平台规则
|
||||
|
||||
AI 失败时返回空规则结构(降级为手动编辑)
|
||||
"""
|
||||
try:
|
||||
ai_client = await AIServiceFactory.get_client(tenant_id, db)
|
||||
if not ai_client:
|
||||
logger.warning(f"租户 {tenant_id} 未配置 AI 服务,返回空规则")
|
||||
return _empty_parsed_rules()
|
||||
|
||||
config = await AIServiceFactory.get_config(tenant_id, db)
|
||||
if not config:
|
||||
return _empty_parsed_rules()
|
||||
|
||||
text_model = config.models.get("text", "gpt-4o")
|
||||
|
||||
# 截断过长文本(避免超出 token 限制)
|
||||
max_chars = 15000
|
||||
if len(document_text) > max_chars:
|
||||
document_text = document_text[:max_chars] + "\n...(文档内容已截断)"
|
||||
|
||||
prompt = f"""你是平台广告合规规则分析专家。请从以下 {platform} 平台规则文档中提取结构化规则。
|
||||
|
||||
文档内容:
|
||||
{document_text}
|
||||
|
||||
请以 JSON 格式返回,不要包含其他内容:
|
||||
{{
|
||||
"forbidden_words": ["违禁词1", "违禁词2"],
|
||||
"restricted_words": [{{"word": "xx", "condition": "使用条件", "suggestion": "替换建议"}}],
|
||||
"duration": {{"min_seconds": 7, "max_seconds": null}},
|
||||
"content_requirements": ["必须展示产品正面", "需要口播品牌名"],
|
||||
"other_rules": [{{"rule": "规则名称", "description": "详细说明"}}]
|
||||
}}
|
||||
|
||||
注意:
|
||||
- forbidden_words: 明确禁止使用的词语
|
||||
- restricted_words: 有条件限制的词语
|
||||
- duration: 视频时长要求,如果文档未提及则为 null
|
||||
- content_requirements: 内容上的硬性要求
|
||||
- other_rules: 不属于以上分类的其他规则
|
||||
- 如果某项没有提取到内容,使用空数组或 null
|
||||
- 重要:JSON 字符串值中不要使用中文引号(""),使用单引号或直接省略"""
|
||||
|
||||
response = await ai_client.chat_completion(
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
model=text_model,
|
||||
temperature=0.2,
|
||||
max_tokens=2000,
|
||||
)
|
||||
|
||||
# 解析 AI 响应
|
||||
content = _extract_json_from_ai_response(response.content)
|
||||
parsed = json.loads(content)
|
||||
|
||||
# 校验并补全字段
|
||||
return {
|
||||
"forbidden_words": parsed.get("forbidden_words", []),
|
||||
"restricted_words": parsed.get("restricted_words", []),
|
||||
"duration": parsed.get("duration"),
|
||||
"content_requirements": parsed.get("content_requirements", []),
|
||||
"other_rules": parsed.get("other_rules", []),
|
||||
}
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.warning(f"AI 返回内容非 JSON,降级为空规则: {e}")
|
||||
return _empty_parsed_rules()
|
||||
except Exception as e:
|
||||
logger.error(f"AI 解析平台规则失败: {e}")
|
||||
return _empty_parsed_rules()
|
||||
|
||||
|
||||
async def _ai_parse_platform_rules_vision(
|
||||
tenant_id: str,
|
||||
platform: str,
|
||||
image_b64_list: list[str],
|
||||
db: AsyncSession,
|
||||
) -> dict:
|
||||
"""
|
||||
使用 AI 视觉模型从 PDF 页面图片中提取结构化平台规则。
|
||||
用于扫描件/截图型 PDF。
|
||||
"""
|
||||
try:
|
||||
ai_client = await AIServiceFactory.get_client(tenant_id, db)
|
||||
if not ai_client:
|
||||
logger.warning(f"租户 {tenant_id} 未配置 AI 服务,返回空规则")
|
||||
return _empty_parsed_rules()
|
||||
|
||||
config = await AIServiceFactory.get_config(tenant_id, db)
|
||||
if not config:
|
||||
return _empty_parsed_rules()
|
||||
|
||||
vision_model = config.models.get("vision", config.models.get("text", "gpt-4o"))
|
||||
|
||||
# 构建多模态消息
|
||||
content: list[dict] = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": f"""你是平台广告合规规则分析专家。以下是 {platform} 平台规则文档的页面截图。
|
||||
请仔细阅读所有页面,从中提取结构化规则。
|
||||
|
||||
请以 JSON 格式返回,不要包含其他内容:
|
||||
{{
|
||||
"forbidden_words": ["违禁词1", "违禁词2"],
|
||||
"restricted_words": [{{"word": "xx", "condition": "使用条件", "suggestion": "替换建议"}}],
|
||||
"duration": {{"min_seconds": 7, "max_seconds": null}},
|
||||
"content_requirements": ["必须展示产品正面", "需要口播品牌名"],
|
||||
"other_rules": [{{"rule": "规则名称", "description": "详细说明"}}]
|
||||
}}
|
||||
|
||||
注意:
|
||||
- forbidden_words: 明确禁止使用的词语
|
||||
- restricted_words: 有条件限制的词语
|
||||
- duration: 视频时长要求,如果文档未提及则为 null
|
||||
- content_requirements: 内容上的硬性要求
|
||||
- other_rules: 不属于以上分类的其他规则
|
||||
- 如果某项没有提取到内容,使用空数组或 null
|
||||
- 重要:JSON 字符串值中不要使用中文引号(\u201c\u201d),使用单引号或直接省略""",
|
||||
}
|
||||
]
|
||||
for b64 in image_b64_list:
|
||||
content.append({
|
||||
"type": "image_url",
|
||||
"image_url": {"url": f"data:image/png;base64,{b64}"},
|
||||
})
|
||||
|
||||
response = await ai_client.chat_completion(
|
||||
messages=[{"role": "user", "content": content}],
|
||||
model=vision_model,
|
||||
temperature=0.2,
|
||||
max_tokens=3000,
|
||||
)
|
||||
|
||||
# 解析 AI 响应
|
||||
resp_content = _extract_json_from_ai_response(response.content)
|
||||
parsed = json.loads(resp_content)
|
||||
return {
|
||||
"forbidden_words": parsed.get("forbidden_words", []),
|
||||
"restricted_words": parsed.get("restricted_words", []),
|
||||
"duration": parsed.get("duration"),
|
||||
"content_requirements": parsed.get("content_requirements", []),
|
||||
"other_rules": parsed.get("other_rules", []),
|
||||
}
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.warning(f"AI 视觉解析返回内容非 JSON,降级为空规则: {e}")
|
||||
return _empty_parsed_rules()
|
||||
except Exception as e:
|
||||
logger.error(f"AI 视觉解析平台规则失败: {e}")
|
||||
return _empty_parsed_rules()
|
||||
|
||||
|
||||
def _extract_json_from_ai_response(raw: str) -> str:
|
||||
"""
|
||||
从 AI 响应中提取并清理 JSON 文本。
|
||||
处理:markdown 代码块包裹、中文引号等。
|
||||
"""
|
||||
import re
|
||||
text = raw.strip()
|
||||
# 去掉 markdown ```json ... ``` 包裹
|
||||
m = re.search(r'```(?:json)?\s*\n(.*?)```', text, re.DOTALL)
|
||||
if m:
|
||||
text = m.group(1).strip()
|
||||
return _sanitize_json_string(text)
|
||||
|
||||
|
||||
def _sanitize_json_string(text: str) -> str:
|
||||
"""
|
||||
清理 AI 返回的 JSON 文本中的中文引号等特殊字符。
|
||||
中文引号 "" 在 JSON 字符串值内会破坏解析。
|
||||
"""
|
||||
import re
|
||||
result = []
|
||||
in_string = False
|
||||
i = 0
|
||||
while i < len(text):
|
||||
ch = text[i]
|
||||
if ch == '\\' and in_string and i + 1 < len(text):
|
||||
result.append(ch)
|
||||
result.append(text[i + 1])
|
||||
i += 2
|
||||
continue
|
||||
if ch == '"' and not in_string:
|
||||
in_string = True
|
||||
result.append(ch)
|
||||
elif ch == '"' and in_string:
|
||||
in_string = False
|
||||
result.append(ch)
|
||||
elif in_string and ch in '\u201c\u201d\u300c\u300d':
|
||||
# 中文引号 "" 和「」 → 单引号
|
||||
result.append("'")
|
||||
elif not in_string and ch in '\u201c\u201d':
|
||||
# JSON 结构层的中文引号 → 英文双引号
|
||||
result.append('"')
|
||||
else:
|
||||
result.append(ch)
|
||||
i += 1
|
||||
return ''.join(result)
|
||||
|
||||
|
||||
def _empty_parsed_rules() -> dict:
|
||||
"""返回空的解析规则结构"""
|
||||
return {
|
||||
"forbidden_words": [],
|
||||
"restricted_words": [],
|
||||
"duration": None,
|
||||
"content_requirements": [],
|
||||
"other_rules": [],
|
||||
}
|
||||
|
||||
|
||||
# ==================== 辅助函数(供其他模块调用) ====================
|
||||
|
||||
async def get_whitelist_for_brand(
|
||||
@@ -533,3 +1041,60 @@ async def get_forbidden_words_for_tenant(
|
||||
}
|
||||
for w in words
|
||||
]
|
||||
|
||||
|
||||
async def get_competitors_for_brand(
|
||||
tenant_id: str,
|
||||
brand_id: str,
|
||||
db: AsyncSession,
|
||||
) -> list[dict]:
|
||||
"""
|
||||
获取品牌方配置的竞品列表
|
||||
|
||||
Returns:
|
||||
[{"name": "竞品名", "keywords": ["关键词1", ...]}]
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(Competitor).where(
|
||||
and_(
|
||||
Competitor.tenant_id == tenant_id,
|
||||
Competitor.brand_id == brand_id,
|
||||
)
|
||||
)
|
||||
)
|
||||
competitors = result.scalars().all()
|
||||
return [
|
||||
{
|
||||
"name": c.name,
|
||||
"keywords": c.keywords or [],
|
||||
}
|
||||
for c in competitors
|
||||
]
|
||||
|
||||
|
||||
async def get_active_platform_rules(
|
||||
tenant_id: str,
|
||||
brand_id: str,
|
||||
platform: str,
|
||||
db: AsyncSession,
|
||||
) -> Optional[dict]:
|
||||
"""
|
||||
获取品牌方在该平台的生效规则 (active)
|
||||
|
||||
Returns:
|
||||
parsed_rules dict 或 None(没有上传规则时)
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(PlatformRule).where(
|
||||
and_(
|
||||
PlatformRule.tenant_id == tenant_id,
|
||||
PlatformRule.brand_id == brand_id,
|
||||
PlatformRule.platform == platform,
|
||||
PlatformRule.status == RuleStatus.ACTIVE.value,
|
||||
)
|
||||
)
|
||||
)
|
||||
rule = result.scalar_one_or_none()
|
||||
if not rule:
|
||||
return None
|
||||
return rule.parsed_rules
|
||||
|
||||
+811
-103
File diff suppressed because it is too large
Load Diff
+612
-19
@@ -2,13 +2,15 @@
|
||||
任务 API
|
||||
实现完整的审核任务流程
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.database import get_db
|
||||
from app.database import get_db, AsyncSessionLocal
|
||||
from app.models.user import User, UserRole
|
||||
from app.models.task import Task, TaskStage, TaskStatus
|
||||
from app.models.project import Project
|
||||
@@ -41,6 +43,7 @@ from app.services.task_service import (
|
||||
check_task_permission,
|
||||
upload_script,
|
||||
upload_video,
|
||||
complete_ai_review,
|
||||
agency_review,
|
||||
brand_review,
|
||||
submit_appeal,
|
||||
@@ -53,10 +56,404 @@ from app.services.task_service import (
|
||||
)
|
||||
from app.api.sse import notify_new_task, notify_task_updated, notify_review_decision
|
||||
from app.services.message_service import create_message
|
||||
from app.models.brief import Brief
|
||||
from app.schemas.review import ScriptReviewRequest, Platform
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/tasks", tags=["任务"])
|
||||
|
||||
|
||||
async def _run_script_ai_review(task_id: str, tenant_id: str):
|
||||
"""
|
||||
后台执行脚本 AI 审核
|
||||
|
||||
- 获取 Brief 信息(卖点、黑名单词)
|
||||
- 调用 review_script 进行审核
|
||||
- 保存审核结果并推进任务阶段
|
||||
- 发送 SSE 通知
|
||||
"""
|
||||
from app.api.scripts import review_script
|
||||
|
||||
async with AsyncSessionLocal() as db:
|
||||
try:
|
||||
task = await get_task_by_id(db, task_id)
|
||||
if not task or task.stage.value != "script_ai_review":
|
||||
logger.warning(f"任务 {task_id} 不在 AI 审核阶段,跳过")
|
||||
return
|
||||
|
||||
# 获取项目信息
|
||||
project_result = await db.execute(
|
||||
select(Project).where(Project.id == task.project_id)
|
||||
)
|
||||
project = project_result.scalar_one_or_none()
|
||||
if not project:
|
||||
logger.error(f"任务 {task_id} 对应的项目不存在")
|
||||
return
|
||||
|
||||
# 获取 Brief
|
||||
brief_result = await db.execute(
|
||||
select(Brief).where(Brief.project_id == project.id)
|
||||
)
|
||||
brief = brief_result.scalar_one_or_none()
|
||||
|
||||
# 构建审核请求
|
||||
platform = project.platform or "douyin"
|
||||
selling_points = brief.selling_points if brief else None
|
||||
blacklist_words = brief.blacklist_words if brief else None
|
||||
min_selling_points = brief.min_selling_points if brief else None
|
||||
|
||||
request = ScriptReviewRequest(
|
||||
content=" ", # 占位,实际内容从 file_url 解析
|
||||
platform=Platform(platform),
|
||||
brand_id=project.brand_id,
|
||||
selling_points=selling_points,
|
||||
min_selling_points=min_selling_points,
|
||||
blacklist_words=blacklist_words,
|
||||
file_url=task.script_file_url,
|
||||
file_name=task.script_file_name,
|
||||
)
|
||||
|
||||
# 调用审核逻辑
|
||||
result = await review_script(
|
||||
request=request,
|
||||
x_tenant_id=tenant_id,
|
||||
db=db,
|
||||
)
|
||||
|
||||
# 保存审核结果
|
||||
task = await get_task_by_id(db, task_id)
|
||||
task = await complete_ai_review(
|
||||
db=db,
|
||||
task=task,
|
||||
review_type="script",
|
||||
score=result.score,
|
||||
result=result.model_dump(),
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
ai_auto_rejected = task.script_ai_result and task.script_ai_result.get("ai_auto_rejected")
|
||||
logger.info(f"任务 {task_id} AI 审核完成,得分: {result.score},自动驳回: {ai_auto_rejected}")
|
||||
|
||||
if ai_auto_rejected:
|
||||
# AI 自动驳回:只通知达人
|
||||
try:
|
||||
creator_result = await db.execute(
|
||||
select(Creator).where(Creator.id == task.creator_id)
|
||||
)
|
||||
creator_obj = creator_result.scalar_one_or_none()
|
||||
if creator_obj:
|
||||
reject_reason = task.script_ai_result.get("ai_reject_reason", "")
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[creator_obj.user_id],
|
||||
data={"action": "ai_auto_rejected", "stage": task.stage.value, "score": result.score},
|
||||
)
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=creator_obj.user_id,
|
||||
type="task",
|
||||
title="脚本未通过 AI 审核",
|
||||
content=f"任务「{task.name}」未通过 AI 审核({result.score} 分),原因:{reject_reason}。请修改后重新上传。",
|
||||
related_task_id=task.id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 正常通过:SSE 通知达人和代理商 + 消息通知代理商
|
||||
try:
|
||||
user_ids = []
|
||||
creator_result = await db.execute(
|
||||
select(Creator).where(Creator.id == task.creator_id)
|
||||
)
|
||||
creator_obj = creator_result.scalar_one_or_none()
|
||||
if creator_obj:
|
||||
user_ids.append(creator_obj.user_id)
|
||||
|
||||
agency_result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
agency_obj = agency_result.scalar_one_or_none()
|
||||
if agency_obj:
|
||||
user_ids.append(agency_obj.user_id)
|
||||
|
||||
if user_ids:
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=user_ids,
|
||||
data={"action": "ai_review_completed", "stage": task.stage.value, "score": result.score},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
ag_result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
ag_obj = ag_result.scalar_one_or_none()
|
||||
if ag_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=ag_obj.user_id,
|
||||
type="task",
|
||||
title="脚本 AI 审核完成",
|
||||
content=f"任务「{task.name}」AI 审核完成,综合得分 {result.score} 分,请审核。",
|
||||
related_task_id=task.id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# AI 未配置时通知品牌方
|
||||
if not result.ai_available:
|
||||
try:
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == project.brand_id)
|
||||
)
|
||||
brand_obj = brand_result.scalar_one_or_none()
|
||||
if brand_obj and brand_obj.user_id:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_obj.user_id,
|
||||
type="task",
|
||||
title="AI 审核降级运行",
|
||||
content=f"任务「{task.name}」的 AI 审核已降级运行(仅关键词检测),请前往「AI 配置」完成设置以获得更精准的审核结果。",
|
||||
related_task_id=task.id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"任务 {task_id} AI 审核失败: {e}", exc_info=True)
|
||||
await db.rollback()
|
||||
# AI 审核异常时通知品牌方(rollback 后重新开始事务)
|
||||
try:
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == tenant_id)
|
||||
)
|
||||
brand_obj = brand_result.scalar_one_or_none()
|
||||
if brand_obj and brand_obj.user_id:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_obj.user_id,
|
||||
type="task",
|
||||
title="AI 审核异常",
|
||||
content=f"任务 AI 审核过程中出错,审核结果可能不完整,请检查 AI 服务配置。错误信息:{str(e)[:100]}",
|
||||
related_task_id=task_id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
async def _run_video_ai_review(task_id: str, tenant_id: str):
|
||||
"""
|
||||
后台执行视频 AI 审核
|
||||
|
||||
复用脚本审核的完整规则检测链(违禁词/竞品/平台规则/白名单/AI深度分析)。
|
||||
审核内容来源:已通过审核的脚本文本 + 视频文件(如可解析)。
|
||||
"""
|
||||
from app.api.scripts import review_script
|
||||
|
||||
async with AsyncSessionLocal() as db:
|
||||
try:
|
||||
await asyncio.sleep(2) # 模拟处理延迟
|
||||
|
||||
task = await get_task_by_id(db, task_id)
|
||||
if not task or task.stage.value != "video_ai_review":
|
||||
logger.warning(f"任务 {task_id} 不在视频 AI 审核阶段,跳过")
|
||||
return
|
||||
|
||||
# 获取项目信息
|
||||
project_result = await db.execute(
|
||||
select(Project).where(Project.id == task.project_id)
|
||||
)
|
||||
project = project_result.scalar_one_or_none()
|
||||
if not project:
|
||||
logger.error(f"任务 {task_id} 对应的项目不存在")
|
||||
return
|
||||
|
||||
# 获取 Brief
|
||||
brief_result = await db.execute(
|
||||
select(Brief).where(Brief.project_id == project.id)
|
||||
)
|
||||
brief = brief_result.scalar_one_or_none()
|
||||
|
||||
platform = project.platform or "douyin"
|
||||
selling_points = brief.selling_points if brief else None
|
||||
blacklist_words = brief.blacklist_words if brief else None
|
||||
min_selling_points = brief.min_selling_points if brief else None
|
||||
|
||||
# 使用脚本内容作为审核基础(视频 ASR 尚未实现,先复用脚本文本)
|
||||
script_content = ""
|
||||
if task.script_file_url and task.script_file_name:
|
||||
# 脚本文件可用,复用
|
||||
pass # review_script 会自动解析 file_url
|
||||
|
||||
request = ScriptReviewRequest(
|
||||
content=script_content or " ",
|
||||
platform=Platform(platform),
|
||||
brand_id=project.brand_id,
|
||||
selling_points=selling_points,
|
||||
min_selling_points=min_selling_points,
|
||||
blacklist_words=blacklist_words,
|
||||
file_url=task.script_file_url,
|
||||
file_name=task.script_file_name,
|
||||
)
|
||||
|
||||
# 调用完整审核逻辑(竞品/违禁词/平台规则/白名单/AI深度分析全部参与)
|
||||
result = await review_script(
|
||||
request=request,
|
||||
x_tenant_id=tenant_id,
|
||||
db=db,
|
||||
)
|
||||
|
||||
video_score = result.score
|
||||
video_result = {
|
||||
"score": video_score,
|
||||
"summary": result.summary,
|
||||
"violations": [v.model_dump() for v in result.violations],
|
||||
"soft_warnings": [w.model_dump() for w in result.soft_warnings],
|
||||
"dimensions": result.dimensions.model_dump(),
|
||||
"selling_point_matches": [sp.model_dump() for sp in result.selling_point_matches],
|
||||
}
|
||||
|
||||
task = await get_task_by_id(db, task_id)
|
||||
task = await complete_ai_review(
|
||||
db=db,
|
||||
task=task,
|
||||
review_type="video",
|
||||
score=video_score,
|
||||
result=video_result,
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
ai_auto_rejected = task.video_ai_result and task.video_ai_result.get("ai_auto_rejected")
|
||||
logger.info(f"任务 {task_id} 视频 AI 审核完成,得分: {video_score},自动驳回: {ai_auto_rejected}")
|
||||
|
||||
if ai_auto_rejected:
|
||||
# AI 自动驳回:只通知达人
|
||||
try:
|
||||
creator_result = await db.execute(
|
||||
select(Creator).where(Creator.id == task.creator_id)
|
||||
)
|
||||
creator_obj = creator_result.scalar_one_or_none()
|
||||
if creator_obj:
|
||||
reject_reason = task.video_ai_result.get("ai_reject_reason", "")
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[creator_obj.user_id],
|
||||
data={"action": "ai_auto_rejected", "stage": task.stage.value, "score": video_score},
|
||||
)
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=creator_obj.user_id,
|
||||
type="task",
|
||||
title="视频未通过 AI 审核",
|
||||
content=f"任务「{task.name}」视频未通过 AI 审核({video_score} 分),原因:{reject_reason}。请修改后重新上传。",
|
||||
related_task_id=task.id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 正常通过:SSE 通知达人和代理商 + 消息通知代理商
|
||||
try:
|
||||
user_ids = []
|
||||
creator_result = await db.execute(
|
||||
select(Creator).where(Creator.id == task.creator_id)
|
||||
)
|
||||
creator_obj = creator_result.scalar_one_or_none()
|
||||
if creator_obj:
|
||||
user_ids.append(creator_obj.user_id)
|
||||
|
||||
agency_result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
agency_obj = agency_result.scalar_one_or_none()
|
||||
if agency_obj:
|
||||
user_ids.append(agency_obj.user_id)
|
||||
|
||||
if user_ids:
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=user_ids,
|
||||
data={"action": "ai_review_completed", "stage": task.stage.value, "score": video_score},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
ag_result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
ag_obj = ag_result.scalar_one_or_none()
|
||||
if ag_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=ag_obj.user_id,
|
||||
type="task",
|
||||
title="视频 AI 审核完成",
|
||||
content=f"任务「{task.name}」视频 AI 审核完成,得分 {video_score} 分,请审核。",
|
||||
related_task_id=task.id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# AI 未配置时通知品牌方
|
||||
if not result.ai_available:
|
||||
try:
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == project.brand_id)
|
||||
)
|
||||
brand_obj = brand_result.scalar_one_or_none()
|
||||
if brand_obj and brand_obj.user_id:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_obj.user_id,
|
||||
type="task",
|
||||
title="视频 AI 审核降级运行",
|
||||
content=f"任务「{task.name}」的视频 AI 审核已降级运行(仅关键词检测),请前往「AI 配置」完成设置以获得更精准的审核结果。",
|
||||
related_task_id=task.id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"任务 {task_id} 视频 AI 审核失败: {e}", exc_info=True)
|
||||
await db.rollback()
|
||||
# AI 审核异常时通知品牌方(rollback 后重新开始事务)
|
||||
try:
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == tenant_id)
|
||||
)
|
||||
brand_obj = brand_result.scalar_one_or_none()
|
||||
if brand_obj and brand_obj.user_id:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_obj.user_id,
|
||||
type="task",
|
||||
title="视频 AI 审核异常",
|
||||
content=f"任务视频 AI 审核过程中出错,审核结果可能不完整,请检查 AI 服务配置。错误信息:{str(e)[:100]}",
|
||||
related_task_id=task_id,
|
||||
sender_name="系统",
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _task_to_response(task: Task) -> TaskResponse:
|
||||
"""将数据库模型转换为响应模型"""
|
||||
return TaskResponse(
|
||||
@@ -68,6 +465,7 @@ def _task_to_response(task: Task) -> TaskResponse:
|
||||
id=task.project.id,
|
||||
name=task.project.name,
|
||||
brand_name=task.project.brand.name if task.project.brand else None,
|
||||
platform=task.project.platform,
|
||||
),
|
||||
agency=AgencyInfo(
|
||||
id=task.agency.id,
|
||||
@@ -174,30 +572,64 @@ async def create_new_task(
|
||||
# 重新加载关联
|
||||
task = await get_task_by_id(db, task.id)
|
||||
|
||||
# 提取通知所需的值(commit 后 ORM 对象会过期,提前缓存)
|
||||
_task_id = task.id
|
||||
_task_name = task.name
|
||||
_project_id = task.project.id
|
||||
_project_name = task.project.name
|
||||
_project_brand_id = task.project.brand_id
|
||||
_agency_name = agency.name
|
||||
_creator_user_id = creator.user_id
|
||||
_creator_name = creator.name or creator.id
|
||||
|
||||
# 创建消息 + SSE 通知达人有新任务
|
||||
try:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=creator.user_id,
|
||||
user_id=_creator_user_id,
|
||||
type="new_task",
|
||||
title="新任务分配",
|
||||
content=f"您有新的任务「{task.name}」,来自项目「{task.project.name}」",
|
||||
related_task_id=task.id,
|
||||
related_project_id=task.project.id,
|
||||
sender_name=agency.name,
|
||||
content=f"您有新的任务「{_task_name}」,来自项目「{_project_name}」",
|
||||
related_task_id=_task_id,
|
||||
related_project_id=_project_id,
|
||||
sender_name=_agency_name,
|
||||
)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
logger.warning(f"创建达人通知消息失败: {e}")
|
||||
|
||||
# 通知品牌方:代理商给项目添加了达人
|
||||
try:
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == _project_brand_id)
|
||||
)
|
||||
brand = brand_result.scalar_one_or_none()
|
||||
if brand and brand.user_id:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand.user_id,
|
||||
type="new_task",
|
||||
title="达人加入项目",
|
||||
content=f"代理商「{_agency_name}」将达人「{_creator_name}」加入项目「{_project_name}」,任务:{_task_name}",
|
||||
related_task_id=_task_id,
|
||||
related_project_id=_project_id,
|
||||
sender_name=_agency_name,
|
||||
)
|
||||
await db.commit()
|
||||
else:
|
||||
logger.warning(f"品牌方不存在或无 user_id: brand_id={_project_brand_id}")
|
||||
except Exception as e:
|
||||
logger.warning(f"创建品牌方通知消息失败: {e}")
|
||||
|
||||
try:
|
||||
await notify_new_task(
|
||||
task_id=task.id,
|
||||
creator_user_id=creator.user_id,
|
||||
task_name=task.name,
|
||||
project_name=task.project.name,
|
||||
task_id=_task_id,
|
||||
creator_user_id=_creator_user_id,
|
||||
task_name=_task_name,
|
||||
project_name=_project_name,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e:
|
||||
logger.warning(f"SSE 通知失败: {e}")
|
||||
|
||||
return _task_to_response(task)
|
||||
|
||||
@@ -210,6 +642,7 @@ async def list_tasks(
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
stage: Optional[TaskStage] = Query(None),
|
||||
project_id: Optional[str] = Query(None),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
@@ -242,7 +675,7 @@ async def list_tasks(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="代理商信息不存在",
|
||||
)
|
||||
tasks, total = await list_tasks_for_agency(db, agency.id, page, page_size, stage)
|
||||
tasks, total = await list_tasks_for_agency(db, agency.id, page, page_size, stage, project_id)
|
||||
|
||||
elif current_user.role == UserRole.BRAND:
|
||||
result = await db.execute(
|
||||
@@ -254,7 +687,7 @@ async def list_tasks(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="品牌方信息不存在",
|
||||
)
|
||||
tasks, total = await list_tasks_for_brand(db, brand.id, page, page_size, stage)
|
||||
tasks, total = await list_tasks_for_brand(db, brand.id, page, page_size, stage, project_id)
|
||||
|
||||
else:
|
||||
raise HTTPException(
|
||||
@@ -394,13 +827,23 @@ async def upload_task_script(
|
||||
# 重新加载关联
|
||||
task = await get_task_by_id(db, task.id)
|
||||
|
||||
# SSE 通知代理商脚本已上传
|
||||
# 通知代理商脚本已上传(消息 + SSE)
|
||||
try:
|
||||
result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
agency_obj = result.scalar_one_or_none()
|
||||
if agency_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=agency_obj.user_id,
|
||||
type="task",
|
||||
title="达人已上传脚本",
|
||||
content=f"任务「{task.name}」的脚本已上传,等待 AI 审核。",
|
||||
related_task_id=task.id,
|
||||
sender_name=creator.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[agency_obj.user_id],
|
||||
@@ -409,6 +852,18 @@ async def upload_task_script(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 获取 tenant_id (品牌方 ID) 并在后台触发 AI 审核
|
||||
try:
|
||||
project_result = await db.execute(
|
||||
select(Project).where(Project.id == task.project_id)
|
||||
)
|
||||
project = project_result.scalar_one_or_none()
|
||||
if project:
|
||||
asyncio.create_task(_run_script_ai_review(task.id, project.brand_id))
|
||||
logger.info(f"已触发任务 {task.id} 的后台 AI 审核")
|
||||
except Exception as e:
|
||||
logger.error(f"触发 AI 审核失败: {e}")
|
||||
|
||||
return _task_to_response(task)
|
||||
|
||||
|
||||
@@ -457,13 +912,23 @@ async def upload_task_video(
|
||||
# 重新加载关联
|
||||
task = await get_task_by_id(db, task.id)
|
||||
|
||||
# SSE 通知代理商视频已上传
|
||||
# 通知代理商视频已上传(消息 + SSE)
|
||||
try:
|
||||
result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
agency_obj = result.scalar_one_or_none()
|
||||
if agency_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=agency_obj.user_id,
|
||||
type="task",
|
||||
title="达人已上传视频",
|
||||
content=f"任务「{task.name}」的视频已上传,等待 AI 审核。",
|
||||
related_task_id=task.id,
|
||||
sender_name=creator.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[agency_obj.user_id],
|
||||
@@ -472,6 +937,18 @@ async def upload_task_video(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 获取 tenant_id 并在后台触发视频 AI 审核
|
||||
try:
|
||||
project_result = await db.execute(
|
||||
select(Project).where(Project.id == task.project_id)
|
||||
)
|
||||
project = project_result.scalar_one_or_none()
|
||||
if project:
|
||||
asyncio.create_task(_run_video_ai_review(task.id, project.brand_id))
|
||||
logger.info(f"已触发任务 {task.id} 的后台视频 AI 审核")
|
||||
except Exception as e:
|
||||
logger.error(f"触发视频 AI 审核失败: {e}")
|
||||
|
||||
return _task_to_response(task)
|
||||
|
||||
|
||||
@@ -615,6 +1092,59 @@ async def review_script(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 代理商通过 → 通知品牌方有新内容待审核
|
||||
try:
|
||||
if current_user.role == UserRole.AGENCY and request.action in ("pass", "force_pass"):
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == task.project.brand_id)
|
||||
)
|
||||
brand_obj = brand_result.scalar_one_or_none()
|
||||
if brand_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_obj.user_id,
|
||||
type="task",
|
||||
title="新脚本待审核",
|
||||
content=f"任务「{task.name}」脚本已通过代理商审核,请进行品牌终审。",
|
||||
related_task_id=task.id,
|
||||
sender_name=current_user.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[brand_obj.user_id],
|
||||
data={"action": "script_pending_brand_review", "stage": task.stage.value},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 品牌方审核 → 通知代理商结果
|
||||
try:
|
||||
if current_user.role == UserRole.BRAND:
|
||||
ag_result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
ag_obj = ag_result.scalar_one_or_none()
|
||||
if ag_obj:
|
||||
action_text = {"pass": "通过", "reject": "驳回"}.get(request.action, request.action)
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=ag_obj.user_id,
|
||||
type="task",
|
||||
title=f"脚本品牌终审{action_text}",
|
||||
content=f"任务「{task.name}」脚本品牌终审已{action_text}" + (f",评语:{request.comment}" if request.comment else ""),
|
||||
related_task_id=task.id,
|
||||
sender_name=current_user.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[ag_obj.user_id],
|
||||
data={"action": f"script_brand_{request.action}", "stage": task.stage.value},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return _task_to_response(task)
|
||||
|
||||
|
||||
@@ -755,6 +1285,59 @@ async def review_video(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 代理商通过 → 通知品牌方有视频待审核
|
||||
try:
|
||||
if current_user.role == UserRole.AGENCY and request.action in ("pass", "force_pass"):
|
||||
brand_result = await db.execute(
|
||||
select(Brand).where(Brand.id == task.project.brand_id)
|
||||
)
|
||||
brand_obj = brand_result.scalar_one_or_none()
|
||||
if brand_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=brand_obj.user_id,
|
||||
type="task",
|
||||
title="新视频待审核",
|
||||
content=f"任务「{task.name}」视频已通过代理商审核,请进行品牌终审。",
|
||||
related_task_id=task.id,
|
||||
sender_name=current_user.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[brand_obj.user_id],
|
||||
data={"action": "video_pending_brand_review", "stage": task.stage.value},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 品牌方审核 → 通知代理商结果
|
||||
try:
|
||||
if current_user.role == UserRole.BRAND:
|
||||
ag_result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
ag_obj = ag_result.scalar_one_or_none()
|
||||
if ag_obj:
|
||||
action_text = {"pass": "通过", "reject": "驳回"}.get(request.action, request.action)
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=ag_obj.user_id,
|
||||
type="task",
|
||||
title=f"视频品牌终审{action_text}",
|
||||
content=f"任务「{task.name}」视频品牌终审已{action_text}" + (f",评语:{request.comment}" if request.comment else ""),
|
||||
related_task_id=task.id,
|
||||
sender_name=current_user.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[ag_obj.user_id],
|
||||
data={"action": f"video_brand_{request.action}", "stage": task.stage.value},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return _task_to_response(task)
|
||||
|
||||
|
||||
@@ -803,13 +1386,23 @@ async def submit_task_appeal(
|
||||
# 重新加载关联
|
||||
task = await get_task_by_id(db, task.id)
|
||||
|
||||
# SSE 通知代理商有新申诉
|
||||
# 通知代理商有新申诉(消息 + SSE)
|
||||
try:
|
||||
result = await db.execute(
|
||||
select(Agency).where(Agency.id == task.agency_id)
|
||||
)
|
||||
agency_obj = result.scalar_one_or_none()
|
||||
if agency_obj:
|
||||
await create_message(
|
||||
db=db,
|
||||
user_id=agency_obj.user_id,
|
||||
type="task",
|
||||
title="达人提交申诉",
|
||||
content=f"任务「{task.name}」的达人提交了申诉:{request.reason}",
|
||||
related_task_id=task.id,
|
||||
sender_name=creator.name,
|
||||
)
|
||||
await db.commit()
|
||||
await notify_task_updated(
|
||||
task_id=task.id,
|
||||
user_ids=[agency_obj.user_id],
|
||||
|
||||
+231
-13
@@ -1,12 +1,13 @@
|
||||
"""
|
||||
文件上传 API
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from urllib.parse import quote
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile, File, Form, status
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
|
||||
from app.services.oss import generate_upload_policy, get_file_url
|
||||
from app.services.oss import generate_upload_policy, get_file_url, generate_presigned_url
|
||||
from app.config import settings
|
||||
from app.models.user import User
|
||||
from app.api.deps import get_current_user
|
||||
@@ -21,11 +22,11 @@ class UploadPolicyRequest(BaseModel):
|
||||
|
||||
|
||||
class UploadPolicyResponse(BaseModel):
|
||||
"""COS 直传凭证响应"""
|
||||
q_sign_algorithm: str
|
||||
q_ak: str
|
||||
q_key_time: str
|
||||
q_signature: str
|
||||
"""TOS 直传凭证响应"""
|
||||
x_tos_algorithm: str
|
||||
x_tos_credential: str
|
||||
x_tos_date: str
|
||||
x_tos_signature: str
|
||||
policy: str
|
||||
host: str
|
||||
dir: str
|
||||
@@ -56,9 +57,9 @@ async def get_upload_policy(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
获取 COS 直传凭证
|
||||
获取 TOS 直传凭证
|
||||
|
||||
前端使用此凭证直接上传文件到腾讯云 COS,无需经过后端。
|
||||
前端使用此凭证直接上传文件到火山引擎 TOS,无需经过后端。
|
||||
|
||||
文件类型说明:
|
||||
- script: 脚本文档 (docx, pdf, xlsx, txt, pptx)
|
||||
@@ -92,10 +93,10 @@ async def get_upload_policy(
|
||||
)
|
||||
|
||||
return UploadPolicyResponse(
|
||||
q_sign_algorithm=policy["q_sign_algorithm"],
|
||||
q_ak=policy["q_ak"],
|
||||
q_key_time=policy["q_key_time"],
|
||||
q_signature=policy["q_signature"],
|
||||
x_tos_algorithm=policy["x_tos_algorithm"],
|
||||
x_tos_credential=policy["x_tos_credential"],
|
||||
x_tos_date=policy["x_tos_date"],
|
||||
x_tos_signature=policy["x_tos_signature"],
|
||||
policy=policy["policy"],
|
||||
host=policy["host"],
|
||||
dir=policy["dir"],
|
||||
@@ -123,3 +124,220 @@ async def file_uploaded(
|
||||
file_size=request.file_size,
|
||||
file_type=request.file_type,
|
||||
)
|
||||
|
||||
|
||||
class SignedUrlResponse(BaseModel):
|
||||
"""签名 URL 响应"""
|
||||
signed_url: str
|
||||
expire_seconds: int
|
||||
|
||||
|
||||
@router.get("/sign-url", response_model=SignedUrlResponse)
|
||||
async def get_signed_url(
|
||||
url: str = Query(..., description="文件的原始 URL 或 file_key"),
|
||||
expire: int = Query(3600, ge=60, le=43200, description="有效期(秒),默认1小时,最长12小时"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
获取私有桶文件的预签名访问 URL
|
||||
|
||||
前端在展示/下载文件前调用此接口,获取带签名的临时访问链接。
|
||||
支持传入完整 URL 或 file_key。
|
||||
"""
|
||||
from app.services.oss import parse_file_key_from_url
|
||||
|
||||
# 如果传入的是完整 URL,先解析出 file_key
|
||||
file_key = url
|
||||
if url.startswith("http"):
|
||||
file_key = parse_file_key_from_url(url)
|
||||
|
||||
if not file_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="无效的文件路径",
|
||||
)
|
||||
|
||||
try:
|
||||
signed_url = generate_presigned_url(file_key, expire_seconds=expire)
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=str(e),
|
||||
)
|
||||
|
||||
return SignedUrlResponse(
|
||||
signed_url=signed_url,
|
||||
expire_seconds=expire,
|
||||
)
|
||||
|
||||
|
||||
def _get_tos_object(file_key: str) -> tuple[bytes, str]:
|
||||
"""
|
||||
从 TOS 获取文件内容和文件名(内部工具函数)
|
||||
|
||||
Returns:
|
||||
(content, filename)
|
||||
"""
|
||||
import tos as tos_sdk
|
||||
|
||||
region = settings.TOS_REGION
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{region}.volces.com"
|
||||
client = tos_sdk.TosClientV2(
|
||||
ak=settings.TOS_ACCESS_KEY_ID,
|
||||
sk=settings.TOS_SECRET_ACCESS_KEY,
|
||||
endpoint=f"https://{endpoint}",
|
||||
region=region,
|
||||
)
|
||||
resp = client.get_object(bucket=settings.TOS_BUCKET_NAME, key=file_key)
|
||||
content = resp.read()
|
||||
|
||||
# 从 file_key 提取文件名,去掉时间戳前缀
|
||||
filename = file_key.split("/")[-1]
|
||||
if "_" in filename and filename.split("_")[0].isdigit():
|
||||
filename = filename.split("_", 1)[1]
|
||||
|
||||
return content, filename
|
||||
|
||||
|
||||
def _resolve_file_key(url: str) -> str:
|
||||
"""从 URL 或 file_key 解析出实际 file_key"""
|
||||
from app.services.oss import parse_file_key_from_url
|
||||
|
||||
file_key = url
|
||||
if url.startswith("http"):
|
||||
file_key = parse_file_key_from_url(url)
|
||||
return file_key
|
||||
|
||||
|
||||
def _guess_content_type(filename: str) -> str:
|
||||
"""根据文件名猜测 MIME 类型"""
|
||||
ext = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
|
||||
mime_map = {
|
||||
"pdf": "application/pdf",
|
||||
"jpg": "image/jpeg",
|
||||
"jpeg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"gif": "image/gif",
|
||||
"webp": "image/webp",
|
||||
"mp4": "video/mp4",
|
||||
"mov": "video/quicktime",
|
||||
"webm": "video/webm",
|
||||
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
"txt": "text/plain",
|
||||
}
|
||||
return mime_map.get(ext, "application/octet-stream")
|
||||
|
||||
|
||||
@router.get("/download")
|
||||
async def download_file(
|
||||
url: str = Query(..., description="文件的原始 URL 或 file_key"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
代理下载文件 — 后端获取 TOS 文件后返回给前端,
|
||||
设置 Content-Disposition: attachment 确保浏览器触发下载。
|
||||
"""
|
||||
file_key = _resolve_file_key(url)
|
||||
if not file_key:
|
||||
raise HTTPException(status_code=400, detail="无效的文件路径")
|
||||
|
||||
try:
|
||||
content, filename = _get_tos_object(file_key)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=f"下载文件失败: {e}")
|
||||
|
||||
from fastapi.responses import Response
|
||||
encoded_filename = quote(filename)
|
||||
return Response(
|
||||
content=content,
|
||||
media_type="application/octet-stream",
|
||||
headers={
|
||||
"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/preview")
|
||||
async def preview_file(
|
||||
url: str = Query(..., description="文件的原始 URL 或 file_key"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
代理预览文件 — 后端获取 TOS 文件后返回给前端,
|
||||
设置正确的 Content-Type 让浏览器可以直接渲染(PDF / 图片等)。
|
||||
"""
|
||||
file_key = _resolve_file_key(url)
|
||||
if not file_key:
|
||||
raise HTTPException(status_code=400, detail="无效的文件路径")
|
||||
|
||||
try:
|
||||
content, filename = _get_tos_object(file_key)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=f"获取文件失败: {e}")
|
||||
|
||||
from fastapi.responses import Response
|
||||
content_type = _guess_content_type(filename)
|
||||
return Response(
|
||||
content=content,
|
||||
media_type=content_type,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/proxy", response_model=FileUploadedResponse)
|
||||
async def proxy_upload(
|
||||
file: UploadFile = File(...),
|
||||
file_type: str = Form("general"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
后端代理上传(用于本地开发 / 浏览器无法直连 TOS 的场景)
|
||||
|
||||
前端把文件 POST 到此接口,后端使用 TOS SDK 上传到对象存储。
|
||||
"""
|
||||
import io
|
||||
import tos as tos_sdk
|
||||
|
||||
if not settings.TOS_ACCESS_KEY_ID or not settings.TOS_SECRET_ACCESS_KEY:
|
||||
raise HTTPException(status_code=500, detail="TOS 配置未设置")
|
||||
|
||||
now = datetime.now()
|
||||
base_dir = f"uploads/{now.year}/{now.month:02d}"
|
||||
type_dirs = {"script": "scripts", "video": "videos", "image": "images"}
|
||||
sub_dir = type_dirs.get(file_type, "files")
|
||||
file_key = f"{base_dir}/{sub_dir}/{int(now.timestamp())}_{file.filename}"
|
||||
|
||||
content = await file.read()
|
||||
content_type = file.content_type or "application/octet-stream"
|
||||
|
||||
region = settings.TOS_REGION
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{region}.volces.com"
|
||||
|
||||
try:
|
||||
client = tos_sdk.TosClientV2(
|
||||
ak=settings.TOS_ACCESS_KEY_ID,
|
||||
sk=settings.TOS_SECRET_ACCESS_KEY,
|
||||
endpoint=f"https://{endpoint}",
|
||||
region=region,
|
||||
)
|
||||
client.put_object(
|
||||
bucket=settings.TOS_BUCKET_NAME,
|
||||
key=file_key,
|
||||
content=io.BytesIO(content),
|
||||
content_type=content_type,
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=f"TOS 上传失败: {str(e)[:200]}",
|
||||
)
|
||||
|
||||
url = get_file_url(file_key)
|
||||
return FileUploadedResponse(
|
||||
url=url,
|
||||
file_key=file_key,
|
||||
file_name=file.filename or "unknown",
|
||||
file_size=len(content),
|
||||
file_type=file_type,
|
||||
)
|
||||
|
||||
@@ -32,12 +32,13 @@ class Settings(BaseSettings):
|
||||
AI_API_KEY: str = "" # 中转服务商的 API Key
|
||||
AI_API_BASE_URL: str = "" # 中转服务商的 Base URL,如 https://api.oneinall.ai/v1
|
||||
|
||||
# 腾讯云 COS 配置
|
||||
COS_SECRET_ID: str = ""
|
||||
COS_SECRET_KEY: str = ""
|
||||
COS_REGION: str = "ap-guangzhou"
|
||||
COS_BUCKET_NAME: str = "miaosi-files-1250000000"
|
||||
COS_CDN_DOMAIN: str = "" # CDN 自定义域名,空则用 COS 源站
|
||||
# 火山引擎 TOS 配置
|
||||
TOS_ACCESS_KEY_ID: str = ""
|
||||
TOS_SECRET_ACCESS_KEY: str = ""
|
||||
TOS_REGION: str = "cn-beijing"
|
||||
TOS_BUCKET_NAME: str = "miaosi-files"
|
||||
TOS_ENDPOINT: str = "" # 自定义 Endpoint,空则用默认 tos-cn-{region}.volces.com
|
||||
TOS_CDN_DOMAIN: str = "" # CDN 自定义域名,空则用 TOS 源站
|
||||
|
||||
# 邮件 SMTP
|
||||
SMTP_HOST: str = ""
|
||||
|
||||
+3
-2
@@ -54,8 +54,9 @@ class SecurityHeadersMiddleware(BaseHTTPMiddleware):
|
||||
|
||||
app.add_middleware(SecurityHeadersMiddleware)
|
||||
|
||||
# Rate limiting
|
||||
app.add_middleware(RateLimitMiddleware, default_limit=60, window_seconds=60)
|
||||
# Rate limiting (仅生产环境启用)
|
||||
if _is_production:
|
||||
app.add_middleware(RateLimitMiddleware, default_limit=60, window_seconds=60)
|
||||
|
||||
# 注册路由
|
||||
app.include_router(health.router, prefix="/api/v1")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
简单的速率限制中间件
|
||||
基于内存的滑动窗口计数器
|
||||
速率限制中间件
|
||||
基于内存的滑动窗口计数器,支持按路径自定义限制和标准响应头。
|
||||
"""
|
||||
import time
|
||||
from collections import defaultdict
|
||||
@@ -14,50 +14,111 @@ class RateLimitMiddleware(BaseHTTPMiddleware):
|
||||
速率限制中间件
|
||||
|
||||
- 默认: 60 次/分钟 per IP
|
||||
- 登录/注册: 10 次/分钟 per IP
|
||||
- 按路径配置不同限制 (path_limits)
|
||||
- 返回标准 X-RateLimit-* 响应头
|
||||
"""
|
||||
|
||||
def __init__(self, app, default_limit: int = 60, window_seconds: int = 60):
|
||||
# Path-specific rate limits (requests per window).
|
||||
# Paths not listed here fall back to ``default_limit``.
|
||||
DEFAULT_PATH_LIMITS: dict[str, int] = {
|
||||
# Auth endpoints — prevent brute-force / abuse
|
||||
"/api/v1/auth/login": 10,
|
||||
"/api/v1/auth/register": 10,
|
||||
"/api/v1/auth/send-code": 5,
|
||||
"/api/v1/auth/reset-password": 5,
|
||||
# Upload — bandwidth / storage cost
|
||||
"/api/v1/upload/policy": 30,
|
||||
# AI review — service cost + compute
|
||||
"/api/v1/scripts/review": 10,
|
||||
"/api/v1/videos/review": 5,
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app,
|
||||
default_limit: int = 60,
|
||||
window_seconds: int = 60,
|
||||
path_limits: dict[str, int] | None = None,
|
||||
):
|
||||
super().__init__(app)
|
||||
self.default_limit = default_limit
|
||||
self.window_seconds = window_seconds
|
||||
self.requests: dict[str, list[float]] = defaultdict(list)
|
||||
# Stricter limits for auth endpoints
|
||||
self.strict_paths = {"/api/v1/auth/login", "/api/v1/auth/register"}
|
||||
self.strict_limit = 10
|
||||
# Merge caller-supplied overrides on top of the built-in defaults.
|
||||
self.path_limits: dict[str, int] = {**self.DEFAULT_PATH_LIMITS}
|
||||
if path_limits:
|
||||
self.path_limits.update(path_limits)
|
||||
|
||||
def _get_limit(self, path: str) -> int:
|
||||
"""Return the rate limit for *path*, falling back to *default_limit*."""
|
||||
return self.path_limits.get(path, self.default_limit)
|
||||
|
||||
def _make_key(self, client_ip: str, path: str) -> str:
|
||||
"""Build the bucket key.
|
||||
|
||||
Paths with a custom limit are bucketed per-IP per-path so that
|
||||
hitting one endpoint does not consume the quota of another.
|
||||
Default paths share a single per-IP bucket.
|
||||
"""
|
||||
if path in self.path_limits:
|
||||
return f"{client_ip}:{path}"
|
||||
return client_ip
|
||||
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
client_ip = request.client.host if request.client else "unknown"
|
||||
path = request.url.path
|
||||
now = time.time()
|
||||
|
||||
# Determine rate limit
|
||||
if path in self.strict_paths:
|
||||
key = f"{client_ip}:{path}"
|
||||
limit = self.strict_limit
|
||||
else:
|
||||
key = client_ip
|
||||
limit = self.default_limit
|
||||
limit = self._get_limit(path)
|
||||
key = self._make_key(client_ip, path)
|
||||
|
||||
# Clean old entries
|
||||
# Clean old entries outside the sliding window
|
||||
window_start = now - self.window_seconds
|
||||
self.requests[key] = [t for t in self.requests[key] if t > window_start]
|
||||
|
||||
current_count = len(self.requests[key])
|
||||
remaining = max(0, limit - current_count)
|
||||
|
||||
# Seconds until the oldest request in the window expires
|
||||
if self.requests[key]:
|
||||
reset_seconds = int(self.requests[key][0] - window_start)
|
||||
else:
|
||||
reset_seconds = self.window_seconds
|
||||
|
||||
# Build common rate-limit headers
|
||||
rate_headers = {
|
||||
"X-RateLimit-Limit": str(limit),
|
||||
"X-RateLimit-Remaining": str(max(0, remaining - 1) if remaining > 0 else 0),
|
||||
"X-RateLimit-Reset": str(reset_seconds),
|
||||
}
|
||||
|
||||
# Check limit
|
||||
if len(self.requests[key]) >= limit:
|
||||
if current_count >= limit:
|
||||
return JSONResponse(
|
||||
status_code=429,
|
||||
content={"detail": "请求过于频繁,请稍后再试"},
|
||||
headers={
|
||||
"X-RateLimit-Limit": str(limit),
|
||||
"X-RateLimit-Remaining": "0",
|
||||
"X-RateLimit-Reset": str(reset_seconds),
|
||||
"Retry-After": str(reset_seconds),
|
||||
},
|
||||
)
|
||||
|
||||
# Record request
|
||||
self.requests[key].append(now)
|
||||
|
||||
# Periodic cleanup (every 1000 requests to this key)
|
||||
# Periodic cleanup (keep memory bounded)
|
||||
if len(self.requests) > 10000:
|
||||
self._cleanup(now)
|
||||
|
||||
response = await call_next(request)
|
||||
|
||||
# Attach rate-limit headers to successful responses
|
||||
response.headers["X-RateLimit-Limit"] = rate_headers["X-RateLimit-Limit"]
|
||||
response.headers["X-RateLimit-Remaining"] = rate_headers["X-RateLimit-Remaining"]
|
||||
response.headers["X-RateLimit-Reset"] = rate_headers["X-RateLimit-Reset"]
|
||||
|
||||
return response
|
||||
|
||||
def _cleanup(self, now: float):
|
||||
|
||||
@@ -10,7 +10,7 @@ from app.models.task import Task, TaskStage, TaskStatus
|
||||
from app.models.brief import Brief
|
||||
from app.models.ai_config import AIConfig
|
||||
from app.models.review import ReviewTask, Platform
|
||||
from app.models.rule import ForbiddenWord, WhitelistItem, Competitor
|
||||
from app.models.rule import ForbiddenWord, WhitelistItem, Competitor, PlatformRule, RuleStatus
|
||||
from app.models.audit_log import AuditLog
|
||||
from app.models.message import Message
|
||||
# 保留 Tenant 兼容旧代码,但新代码应使用 Brand
|
||||
@@ -44,6 +44,8 @@ __all__ = [
|
||||
"ForbiddenWord",
|
||||
"WhitelistItem",
|
||||
"Competitor",
|
||||
"PlatformRule",
|
||||
"RuleStatus",
|
||||
# 审计日志
|
||||
"AuditLog",
|
||||
# 消息
|
||||
|
||||
@@ -30,9 +30,12 @@ class Brief(Base, TimestampMixin):
|
||||
file_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
|
||||
# 解析后的结构化内容
|
||||
# 卖点要求: [{"content": "SPF50+", "required": true}, ...]
|
||||
# 卖点要求: [{"content": "SPF50+", "priority": "core"}, ...]
|
||||
selling_points: Mapped[Optional[list]] = mapped_column(JSONType, nullable=True)
|
||||
|
||||
# 代理商要求至少体现的卖点条数(0 或 None 表示不限制)
|
||||
min_selling_points: Mapped[Optional[int]] = mapped_column(nullable=True)
|
||||
|
||||
# 违禁词: [{"word": "最好", "reason": "绝对化用语"}, ...]
|
||||
blacklist_words: Mapped[Optional[list]] = mapped_column(JSONType, nullable=True)
|
||||
|
||||
@@ -49,10 +52,14 @@ class Brief(Base, TimestampMixin):
|
||||
# 其他要求(自由文本)
|
||||
other_requirements: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
|
||||
# 附件文档(代理商上传的参考资料)
|
||||
# 附件文档(品牌方上传的参考资料)
|
||||
# [{"id": "af1", "name": "达人拍摄指南.pdf", "url": "...", "size": "1.5MB"}, ...]
|
||||
attachments: Mapped[Optional[list]] = mapped_column(JSONType, nullable=True)
|
||||
|
||||
# 代理商附件(代理商上传的补充资料,与品牌方 attachments 分开存储)
|
||||
# [{"id": "af1", "name": "达人拍摄指南.pdf", "url": "...", "size": "1.5MB"}, ...]
|
||||
agency_attachments: Mapped[Optional[list]] = mapped_column(JSONType, nullable=True)
|
||||
|
||||
# 关联
|
||||
project: Mapped["Project"] = relationship("Project", back_populates="brief")
|
||||
|
||||
|
||||
@@ -45,6 +45,9 @@ class Project(Base, TimestampMixin):
|
||||
start_date: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
deadline: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
# 发布平台 (douyin/xiaohongshu/bilibili/kuaishou 等)
|
||||
platform: Mapped[Optional[str]] = mapped_column(String(50), nullable=True, default=None)
|
||||
|
||||
# 状态
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(20),
|
||||
|
||||
@@ -47,7 +47,7 @@ class ReviewTask(Base, TimestampMixin):
|
||||
# 视频信息
|
||||
video_url: Mapped[str] = mapped_column(String(2048), nullable=False)
|
||||
platform: Mapped[Platform] = mapped_column(
|
||||
SQLEnum(Platform, name="platform_enum"),
|
||||
SQLEnum(Platform, name="platform_enum", values_callable=lambda x: [e.value for e in x]),
|
||||
nullable=False,
|
||||
)
|
||||
brand_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
@@ -55,7 +55,7 @@ class ReviewTask(Base, TimestampMixin):
|
||||
|
||||
# 审核状态
|
||||
status: Mapped[TaskStatus] = mapped_column(
|
||||
SQLEnum(TaskStatus, name="task_status_enum"),
|
||||
SQLEnum(TaskStatus, name="task_status_enum", values_callable=lambda x: [e.value for e in x]),
|
||||
default=TaskStatus.PENDING,
|
||||
nullable=False,
|
||||
index=True,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""
|
||||
规则模型
|
||||
违禁词、白名单、竞品
|
||||
违禁词、白名单、竞品、平台规则
|
||||
"""
|
||||
import enum
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from sqlalchemy import String, Text, ForeignKey
|
||||
from app.models.types import JSONType
|
||||
@@ -13,6 +14,13 @@ if TYPE_CHECKING:
|
||||
from app.models.tenant import Tenant
|
||||
|
||||
|
||||
class RuleStatus(str, enum.Enum):
|
||||
"""平台规则状态"""
|
||||
DRAFT = "draft" # AI 解析完成,待确认
|
||||
ACTIVE = "active" # 品牌方已确认,生效中
|
||||
INACTIVE = "inactive" # 已停用
|
||||
|
||||
|
||||
class ForbiddenWord(Base, TimestampMixin):
|
||||
"""违禁词表"""
|
||||
__tablename__ = "forbidden_words"
|
||||
@@ -83,3 +91,36 @@ class Competitor(Base, TimestampMixin):
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Competitor(name={self.name}, brand_id={self.brand_id})>"
|
||||
|
||||
|
||||
class PlatformRule(Base, TimestampMixin):
|
||||
"""平台规则表 — 品牌方上传文档 + AI 解析"""
|
||||
__tablename__ = "platform_rules"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
tenant_id: Mapped[str] = mapped_column(
|
||||
String(64),
|
||||
ForeignKey("tenants.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
brand_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
platform: Mapped[str] = mapped_column(String(50), nullable=False, index=True)
|
||||
|
||||
# 文档信息
|
||||
document_url: Mapped[str] = mapped_column(String(2048), nullable=False)
|
||||
document_name: Mapped[str] = mapped_column(String(512), nullable=False)
|
||||
|
||||
# AI 解析结果(JSON)
|
||||
parsed_rules: Mapped[Optional[dict]] = mapped_column(JSONType, nullable=True)
|
||||
|
||||
# 状态
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(20), nullable=False, default=RuleStatus.DRAFT.value, index=True,
|
||||
)
|
||||
|
||||
# 关联
|
||||
tenant: Mapped["Tenant"] = relationship("Tenant", back_populates="platform_rules")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<PlatformRule(id={self.id}, platform={self.platform}, status={self.status})>"
|
||||
|
||||
@@ -70,7 +70,7 @@ class Task(Base, TimestampMixin):
|
||||
|
||||
# 当前阶段
|
||||
stage: Mapped[TaskStage] = mapped_column(
|
||||
SQLEnum(TaskStage, name="task_stage_enum"),
|
||||
SQLEnum(TaskStage, name="task_stage_enum", values_callable=lambda x: [e.value for e in x]),
|
||||
default=TaskStage.SCRIPT_UPLOAD,
|
||||
nullable=False,
|
||||
index=True,
|
||||
@@ -88,7 +88,7 @@ class Task(Base, TimestampMixin):
|
||||
|
||||
# 脚本代理商审核
|
||||
script_agency_status: Mapped[Optional[TaskStatus]] = mapped_column(
|
||||
SQLEnum(TaskStatus, name="task_status_enum"),
|
||||
SQLEnum(TaskStatus, name="task_status_enum", values_callable=lambda x: [e.value for e in x]),
|
||||
nullable=True,
|
||||
)
|
||||
script_agency_comment: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
@@ -97,7 +97,7 @@ class Task(Base, TimestampMixin):
|
||||
|
||||
# 脚本品牌方终审
|
||||
script_brand_status: Mapped[Optional[TaskStatus]] = mapped_column(
|
||||
SQLEnum(TaskStatus, name="task_status_enum", create_type=False),
|
||||
SQLEnum(TaskStatus, name="task_status_enum", create_type=False, values_callable=lambda x: [e.value for e in x]),
|
||||
nullable=True,
|
||||
)
|
||||
script_brand_comment: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
@@ -118,7 +118,7 @@ class Task(Base, TimestampMixin):
|
||||
|
||||
# 视频代理商审核
|
||||
video_agency_status: Mapped[Optional[TaskStatus]] = mapped_column(
|
||||
SQLEnum(TaskStatus, name="task_status_enum", create_type=False),
|
||||
SQLEnum(TaskStatus, name="task_status_enum", create_type=False, values_callable=lambda x: [e.value for e in x]),
|
||||
nullable=True,
|
||||
)
|
||||
video_agency_comment: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
@@ -127,7 +127,7 @@ class Task(Base, TimestampMixin):
|
||||
|
||||
# 视频品牌方终审
|
||||
video_brand_status: Mapped[Optional[TaskStatus]] = mapped_column(
|
||||
SQLEnum(TaskStatus, name="task_status_enum", create_type=False),
|
||||
SQLEnum(TaskStatus, name="task_status_enum", create_type=False, values_callable=lambda x: [e.value for e in x]),
|
||||
nullable=True,
|
||||
)
|
||||
video_brand_comment: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
|
||||
@@ -10,7 +10,7 @@ from app.models.base import Base, TimestampMixin
|
||||
if TYPE_CHECKING:
|
||||
from app.models.ai_config import AIConfig
|
||||
from app.models.review import ReviewTask
|
||||
from app.models.rule import ForbiddenWord, WhitelistItem, Competitor
|
||||
from app.models.rule import ForbiddenWord, WhitelistItem, Competitor, PlatformRule
|
||||
|
||||
|
||||
class Tenant(Base, TimestampMixin):
|
||||
@@ -48,5 +48,11 @@ class Tenant(Base, TimestampMixin):
|
||||
back_populates="tenant",
|
||||
lazy="selectin",
|
||||
)
|
||||
platform_rules: Mapped[list["PlatformRule"]] = relationship(
|
||||
"PlatformRule",
|
||||
back_populates="tenant",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Tenant(id={self.id}, name={self.name})>"
|
||||
|
||||
@@ -37,7 +37,7 @@ class User(Base, TimestampMixin):
|
||||
|
||||
# 角色
|
||||
role: Mapped[UserRole] = mapped_column(
|
||||
SQLEnum(UserRole, name="user_role_enum"),
|
||||
SQLEnum(UserRole, name="user_role_enum", values_callable=lambda x: [e.value for e in x]),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
"""
|
||||
Brief 相关 Schema
|
||||
|
||||
卖点格式 (selling_points: List[dict]):
|
||||
新格式: {"content": "卖点内容", "priority": "core|recommended|reference"}
|
||||
旧格式: {"content": "卖点内容", "required": true|false}
|
||||
兼容规则: required=true → priority="core", required=false → priority="recommended"
|
||||
"""
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
@@ -20,6 +25,7 @@ class BriefCreateRequest(BaseModel):
|
||||
max_duration: Optional[int] = None
|
||||
other_requirements: Optional[str] = None
|
||||
attachments: Optional[List[dict]] = None
|
||||
agency_attachments: Optional[List[dict]] = None
|
||||
|
||||
|
||||
class BriefUpdateRequest(BaseModel):
|
||||
@@ -34,6 +40,17 @@ class BriefUpdateRequest(BaseModel):
|
||||
max_duration: Optional[int] = None
|
||||
other_requirements: Optional[str] = None
|
||||
attachments: Optional[List[dict]] = None
|
||||
agency_attachments: Optional[List[dict]] = None
|
||||
|
||||
|
||||
class AgencyBriefUpdateRequest(BaseModel):
|
||||
"""代理商更新 Brief 请求(允许更新代理商附件 + 卖点 + 违禁词 + AI解析内容)"""
|
||||
agency_attachments: Optional[List[dict]] = None
|
||||
selling_points: Optional[List[dict]] = None
|
||||
min_selling_points: Optional[int] = None
|
||||
blacklist_words: Optional[List[dict]] = None
|
||||
brand_tone: Optional[str] = None
|
||||
other_requirements: Optional[str] = None
|
||||
|
||||
|
||||
# ===== 响应 =====
|
||||
@@ -46,6 +63,7 @@ class BriefResponse(BaseModel):
|
||||
file_url: Optional[str] = None
|
||||
file_name: Optional[str] = None
|
||||
selling_points: Optional[List[dict]] = None
|
||||
min_selling_points: Optional[int] = None
|
||||
blacklist_words: Optional[List[dict]] = None
|
||||
competitors: Optional[List[str]] = None
|
||||
brand_tone: Optional[str] = None
|
||||
@@ -53,6 +71,7 @@ class BriefResponse(BaseModel):
|
||||
max_duration: Optional[int] = None
|
||||
other_requirements: Optional[str] = None
|
||||
attachments: Optional[List[dict]] = None
|
||||
agency_attachments: Optional[List[dict]] = None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ class ProjectCreateRequest(BaseModel):
|
||||
"""创建项目请求(品牌方操作)"""
|
||||
name: str = Field(..., min_length=1, max_length=255)
|
||||
description: Optional[str] = None
|
||||
platform: Optional[str] = None
|
||||
start_date: Optional[datetime] = None
|
||||
deadline: Optional[datetime] = None
|
||||
agency_ids: Optional[List[str]] = None # 分配的代理商 ID 列表
|
||||
@@ -21,6 +22,7 @@ class ProjectUpdateRequest(BaseModel):
|
||||
"""更新项目请求"""
|
||||
name: Optional[str] = Field(None, min_length=1, max_length=255)
|
||||
description: Optional[str] = None
|
||||
platform: Optional[str] = None
|
||||
start_date: Optional[datetime] = None
|
||||
deadline: Optional[datetime] = None
|
||||
status: Optional[str] = Field(None, pattern="^(active|completed|archived)$")
|
||||
@@ -45,6 +47,7 @@ class ProjectResponse(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
platform: Optional[str] = None
|
||||
brand_id: str
|
||||
brand_name: Optional[str] = None
|
||||
status: str
|
||||
|
||||
@@ -91,6 +91,7 @@ class Violation(BaseModel):
|
||||
content: str = Field(..., description="违规内容")
|
||||
severity: RiskLevel = Field(..., description="严重程度")
|
||||
suggestion: str = Field(..., description="修改建议")
|
||||
dimension: Optional[str] = Field(None, description="所属维度: legal/platform/brand_safety/brief_match")
|
||||
|
||||
# 文本审核字段
|
||||
position: Optional[Position] = Field(None, description="文本位置(脚本审核)")
|
||||
@@ -101,6 +102,45 @@ class Violation(BaseModel):
|
||||
source: Optional[ViolationSource] = Field(None, description="违规来源(视频审核)")
|
||||
|
||||
|
||||
# ==================== 多维度审核 ====================
|
||||
|
||||
class ReviewDimension(BaseModel):
|
||||
"""审核维度评分"""
|
||||
score: int = Field(..., ge=0, le=100)
|
||||
passed: bool
|
||||
issue_count: int = 0
|
||||
|
||||
|
||||
class ReviewDimensions(BaseModel):
|
||||
"""四维度审核结果"""
|
||||
legal: ReviewDimension # 法规合规(违禁词、功效词、Brief黑名单词)
|
||||
platform: ReviewDimension # 平台规则
|
||||
brand_safety: ReviewDimension # 品牌安全(竞品、其他品牌词)
|
||||
brief_match: ReviewDimension # Brief 匹配度(卖点覆盖)
|
||||
|
||||
|
||||
class SellingPointMatch(BaseModel):
|
||||
"""卖点匹配结果"""
|
||||
content: str
|
||||
priority: str # "core" | "recommended" | "reference"
|
||||
matched: bool
|
||||
evidence: Optional[str] = None # AI 给出的匹配依据
|
||||
|
||||
|
||||
class BriefMatchDetail(BaseModel):
|
||||
"""Brief 匹配度评分详情"""
|
||||
# 卖点覆盖
|
||||
total_points: int = Field(0, description="需要检查的卖点总数(core + recommended)")
|
||||
matched_points: int = Field(0, description="实际匹配的卖点数")
|
||||
required_points: int = Field(0, description="代理商要求至少体现的卖点条数(min_selling_points)")
|
||||
coverage_score: int = Field(0, ge=0, le=100, description="卖点覆盖率得分")
|
||||
# AI 整体匹配分析
|
||||
overall_score: int = Field(0, ge=0, le=100, description="整体 Brief 匹配度得分")
|
||||
highlights: list[str] = Field(default_factory=list, description="内容亮点(AI 分析)")
|
||||
issues: list[str] = Field(default_factory=list, description="问题点(AI 分析)")
|
||||
explanation: str = Field("", description="评分说明(一句话总结)")
|
||||
|
||||
|
||||
# ==================== 脚本预审 ====================
|
||||
|
||||
class ScriptReviewRequest(BaseModel):
|
||||
@@ -108,8 +148,12 @@ class ScriptReviewRequest(BaseModel):
|
||||
content: str = Field(..., min_length=1, description="脚本内容")
|
||||
platform: Platform = Field(..., description="投放平台")
|
||||
brand_id: str = Field(..., description="品牌 ID")
|
||||
required_points: Optional[list[str]] = Field(None, description="必要卖点列表")
|
||||
selling_points: Optional[list[dict]] = Field(None, description="卖点列表 [{content, priority}]")
|
||||
min_selling_points: Optional[int] = Field(None, ge=0, description="代理商要求至少体现的卖点条数")
|
||||
blacklist_words: Optional[list[dict]] = Field(None, description="Brief 黑名单词 [{word, reason}]")
|
||||
soft_risk_context: Optional[SoftRiskContext] = Field(None, description="软性风控上下文")
|
||||
file_url: Optional[str] = Field(None, description="脚本文件 URL(用于自动解析文本和提取图片)")
|
||||
file_name: Optional[str] = Field(None, description="原始文件名(用于判断格式)")
|
||||
|
||||
|
||||
class ScriptReviewResponse(BaseModel):
|
||||
@@ -117,16 +161,22 @@ class ScriptReviewResponse(BaseModel):
|
||||
脚本预审响应
|
||||
|
||||
结构:
|
||||
- score: 合规分数 0-100
|
||||
- score: 加权总分(向后兼容)
|
||||
- summary: 整体摘要
|
||||
- violations: 违规项列表,每项包含 suggestion
|
||||
- missing_points: 遗漏的卖点(可选)
|
||||
- dimensions: 四维度评分(法规/平台/品牌安全/Brief匹配)
|
||||
- selling_point_matches: 卖点匹配详情
|
||||
- violations: 违规项列表,每项带 dimension 标签
|
||||
- missing_points: 遗漏的核心卖点(向后兼容)
|
||||
"""
|
||||
score: int = Field(..., ge=0, le=100, description="合规分数")
|
||||
score: int = Field(..., ge=0, le=100, description="加权总分")
|
||||
summary: str = Field(..., description="审核摘要")
|
||||
dimensions: ReviewDimensions = Field(..., description="四维度评分")
|
||||
selling_point_matches: list[SellingPointMatch] = Field(default_factory=list, description="卖点匹配详情")
|
||||
brief_match_detail: Optional[BriefMatchDetail] = Field(None, description="Brief 匹配度评分详情")
|
||||
violations: list[Violation] = Field(default_factory=list, description="违规项列表")
|
||||
missing_points: Optional[list[str]] = Field(None, description="遗漏的卖点")
|
||||
missing_points: Optional[list[str]] = Field(None, description="遗漏的核心卖点")
|
||||
soft_warnings: list[SoftRiskWarning] = Field(default_factory=list, description="软性风控提示")
|
||||
ai_available: bool = Field(True, description="AI 服务是否可用(False 表示降级为纯关键词检测)")
|
||||
|
||||
|
||||
# ==================== 视频审核 ====================
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
平台规则相关 Schema
|
||||
"""
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class PlatformRuleParseRequest(BaseModel):
|
||||
"""上传文档并解析"""
|
||||
document_url: str = Field(..., description="TOS 上传后的文件 URL")
|
||||
document_name: str = Field(..., description="原始文件名(用于判断格式)")
|
||||
platform: str = Field(..., description="目标平台 (douyin/xiaohongshu/bilibili/kuaishou)")
|
||||
brand_id: str = Field(..., description="品牌 ID")
|
||||
|
||||
|
||||
class ParsedRulesData(BaseModel):
|
||||
"""AI 解析出的结构化规则"""
|
||||
forbidden_words: list[str] = Field(default_factory=list, description="违禁词列表")
|
||||
restricted_words: list[dict] = Field(
|
||||
default_factory=list,
|
||||
description="限制词 [{word, condition, suggestion}]",
|
||||
)
|
||||
duration: Optional[dict] = Field(
|
||||
None,
|
||||
description="时长要求 {min_seconds, max_seconds}",
|
||||
)
|
||||
content_requirements: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="内容要求(如'必须展示产品')",
|
||||
)
|
||||
other_rules: list[dict] = Field(
|
||||
default_factory=list,
|
||||
description="其他规则 [{rule, description}]",
|
||||
)
|
||||
|
||||
|
||||
class PlatformRuleParseResponse(BaseModel):
|
||||
"""解析响应(draft 状态)"""
|
||||
id: str
|
||||
platform: str
|
||||
brand_id: str
|
||||
document_url: str
|
||||
document_name: str
|
||||
parsed_rules: ParsedRulesData
|
||||
status: str
|
||||
|
||||
|
||||
class PlatformRuleConfirmRequest(BaseModel):
|
||||
"""确认/编辑解析结果"""
|
||||
parsed_rules: ParsedRulesData = Field(..., description="品牌方可能修改过的规则")
|
||||
|
||||
|
||||
class PlatformRuleResponse(BaseModel):
|
||||
"""完整响应"""
|
||||
id: str
|
||||
platform: str
|
||||
brand_id: str
|
||||
document_url: str
|
||||
document_name: str
|
||||
parsed_rules: ParsedRulesData
|
||||
status: str
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
|
||||
class PlatformRuleListResponse(BaseModel):
|
||||
"""列表响应"""
|
||||
items: list[PlatformRuleResponse]
|
||||
total: int
|
||||
@@ -87,6 +87,7 @@ class ProjectInfo(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
brand_name: Optional[str] = None
|
||||
platform: Optional[str] = None
|
||||
|
||||
|
||||
class TaskResponse(BaseModel):
|
||||
|
||||
@@ -48,9 +48,12 @@ class OpenAICompatibleClient:
|
||||
base_url: str,
|
||||
api_key: str,
|
||||
provider: str = "openai",
|
||||
timeout: float = 60.0,
|
||||
timeout: float = 180.0,
|
||||
):
|
||||
self.base_url = base_url.rstrip("/")
|
||||
# 自动补全 /v1 后缀(OpenAI SDK 需要完整路径)
|
||||
if not self.base_url.endswith("/v1"):
|
||||
self.base_url = self.base_url + "/v1"
|
||||
self.api_key = api_key
|
||||
self.provider = provider
|
||||
self.timeout = timeout
|
||||
|
||||
@@ -53,18 +53,24 @@ class AIServiceFactory:
|
||||
)
|
||||
config = result.scalar_one_or_none()
|
||||
|
||||
if not config:
|
||||
return None
|
||||
|
||||
# 解密 API Key
|
||||
api_key = decrypt_api_key(config.api_key_encrypted)
|
||||
|
||||
# 创建客户端
|
||||
client = OpenAICompatibleClient(
|
||||
base_url=config.base_url,
|
||||
api_key=api_key,
|
||||
provider=config.provider,
|
||||
)
|
||||
if config:
|
||||
# 解密 API Key
|
||||
api_key = decrypt_api_key(config.api_key_encrypted)
|
||||
client = OpenAICompatibleClient(
|
||||
base_url=config.base_url,
|
||||
api_key=api_key,
|
||||
provider=config.provider,
|
||||
)
|
||||
else:
|
||||
# 回退到全局 .env 配置
|
||||
from app.config import settings
|
||||
if not settings.AI_API_KEY or not settings.AI_API_BASE_URL:
|
||||
return None
|
||||
client = OpenAICompatibleClient(
|
||||
base_url=settings.AI_API_BASE_URL,
|
||||
api_key=settings.AI_API_KEY,
|
||||
provider=settings.AI_PROVIDER,
|
||||
)
|
||||
|
||||
# 缓存客户端
|
||||
cls._cache[cache_key] = client
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
import secrets
|
||||
from jose import jwt, JWTError
|
||||
import jwt
|
||||
from jwt.exceptions import PyJWTError as JWTError
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
@@ -0,0 +1,349 @@
|
||||
"""
|
||||
文档解析服务
|
||||
从 PDF/Word/Excel 文档中提取纯文本
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DocumentParser:
|
||||
"""从文档中提取纯文本"""
|
||||
|
||||
@staticmethod
|
||||
async def download_and_parse(document_url: str, document_name: str) -> str:
|
||||
"""
|
||||
下载文档并解析为纯文本
|
||||
|
||||
优先使用 TOS SDK 直接下载(私有桶无需签名),
|
||||
回退到 HTTP 预签名 URL 下载。
|
||||
|
||||
Args:
|
||||
document_url: 文档 URL (TOS)
|
||||
document_name: 原始文件名(用于判断格式)
|
||||
|
||||
Returns:
|
||||
提取的纯文本
|
||||
"""
|
||||
tmp_path: Optional[str] = None
|
||||
try:
|
||||
ext = document_name.rsplit(".", 1)[-1].lower() if "." in document_name else ""
|
||||
|
||||
# 优先用 TOS SDK 直接下载(后端有 AK/SK,无需签名 URL)
|
||||
content = await DocumentParser._download_via_tos_sdk(document_url)
|
||||
|
||||
if content is None:
|
||||
# 回退:生成预签名 URL 后用 HTTP 下载
|
||||
content = await DocumentParser._download_via_signed_url(document_url)
|
||||
|
||||
# 跳过过大的文件(>20MB),解析可能非常慢且阻塞
|
||||
if len(content) > 20 * 1024 * 1024:
|
||||
logger.warning(f"文件 {document_name} 过大 ({len(content)//1024//1024}MB),已跳过")
|
||||
return ""
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{ext}") as tmp:
|
||||
tmp.write(content)
|
||||
tmp_path = tmp.name
|
||||
|
||||
# 文件解析可能很慢(CPU 密集),放到线程池执行
|
||||
return await asyncio.to_thread(DocumentParser.parse_file, tmp_path, document_name)
|
||||
finally:
|
||||
if tmp_path and os.path.exists(tmp_path):
|
||||
os.unlink(tmp_path)
|
||||
|
||||
# 图片提取限制
|
||||
MAX_IMAGES = 10
|
||||
MAX_IMAGE_SIZE = 2 * 1024 * 1024 # 2MB per image base64
|
||||
|
||||
@staticmethod
|
||||
async def download_and_get_images(document_url: str, document_name: str) -> Optional[list[str]]:
|
||||
"""
|
||||
下载文档并提取嵌入的图片,返回 base64 编码列表。
|
||||
|
||||
支持格式:
|
||||
- PDF: 图片型 PDF 转页面图片
|
||||
- DOCX: 提取 word/media/ 中的嵌入图片
|
||||
- XLSX: 提取 worksheet 中的嵌入图片
|
||||
|
||||
Returns:
|
||||
base64 图片列表,无图片时返回 None
|
||||
"""
|
||||
ext = document_name.rsplit(".", 1)[-1].lower() if "." in document_name else ""
|
||||
if ext not in ("pdf", "doc", "docx", "xls", "xlsx"):
|
||||
return None
|
||||
|
||||
tmp_path: Optional[str] = None
|
||||
try:
|
||||
file_content = await DocumentParser._download_via_tos_sdk(document_url)
|
||||
if file_content is None:
|
||||
file_content = await DocumentParser._download_via_signed_url(document_url)
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{ext}") as tmp:
|
||||
tmp.write(file_content)
|
||||
tmp_path = tmp.name
|
||||
|
||||
if ext == "pdf":
|
||||
if DocumentParser.is_image_pdf(tmp_path):
|
||||
return DocumentParser.pdf_to_images_base64(tmp_path)
|
||||
return None
|
||||
elif ext in ("doc", "docx"):
|
||||
images = await asyncio.to_thread(DocumentParser._extract_docx_images, tmp_path)
|
||||
return images if images else None
|
||||
elif ext in ("xls", "xlsx"):
|
||||
images = await asyncio.to_thread(DocumentParser._extract_xlsx_images, tmp_path)
|
||||
return images if images else None
|
||||
return None
|
||||
finally:
|
||||
if tmp_path and os.path.exists(tmp_path):
|
||||
os.unlink(tmp_path)
|
||||
|
||||
@staticmethod
|
||||
async def _download_via_tos_sdk(document_url: str) -> Optional[bytes]:
|
||||
"""通过 TOS SDK 直接下载文件(私有桶安全访问),在线程池中执行避免阻塞"""
|
||||
def _sync_download() -> Optional[bytes]:
|
||||
try:
|
||||
from app.config import settings
|
||||
from app.services.oss import parse_file_key_from_url
|
||||
import tos as tos_sdk
|
||||
|
||||
if not settings.TOS_ACCESS_KEY_ID or not settings.TOS_SECRET_ACCESS_KEY:
|
||||
logger.debug("TOS SDK: AK/SK 未配置,跳过")
|
||||
return None
|
||||
|
||||
file_key = parse_file_key_from_url(document_url)
|
||||
if not file_key or file_key == document_url:
|
||||
logger.debug(f"TOS SDK: 无法从 URL 解析 file_key: {document_url}")
|
||||
return None
|
||||
|
||||
region = settings.TOS_REGION
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{region}.volces.com"
|
||||
|
||||
client = tos_sdk.TosClientV2(
|
||||
ak=settings.TOS_ACCESS_KEY_ID,
|
||||
sk=settings.TOS_SECRET_ACCESS_KEY,
|
||||
endpoint=f"https://{endpoint}",
|
||||
region=region,
|
||||
)
|
||||
resp = client.get_object(bucket=settings.TOS_BUCKET_NAME, key=file_key)
|
||||
data = resp.read()
|
||||
logger.info(f"TOS SDK: 下载成功, key={file_key}, size={len(data)}")
|
||||
return data
|
||||
except Exception as e:
|
||||
logger.warning(f"TOS SDK 下载失败,将回退 HTTP: {e}")
|
||||
return None
|
||||
|
||||
return await asyncio.to_thread(_sync_download)
|
||||
|
||||
@staticmethod
|
||||
async def _download_via_signed_url(document_url: str) -> bytes:
|
||||
"""生成预签名 URL 后通过 HTTP 下载"""
|
||||
from app.services.oss import generate_presigned_url, parse_file_key_from_url
|
||||
|
||||
file_key = parse_file_key_from_url(document_url)
|
||||
signed_url = generate_presigned_url(file_key, expire_seconds=300)
|
||||
logger.info(f"HTTP 签名 URL 下载: key={file_key}")
|
||||
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
resp = await client.get(signed_url)
|
||||
resp.raise_for_status()
|
||||
logger.info(f"HTTP 下载成功: {len(resp.content)} bytes")
|
||||
return resp.content
|
||||
|
||||
@staticmethod
|
||||
def parse_file(file_path: str, file_name: str) -> str:
|
||||
"""
|
||||
根据扩展名选择解析器,返回纯文本
|
||||
|
||||
Args:
|
||||
file_path: 本地文件路径
|
||||
file_name: 原始文件名
|
||||
|
||||
Returns:
|
||||
提取的纯文本
|
||||
"""
|
||||
ext = file_name.rsplit(".", 1)[-1].lower() if "." in file_name else ""
|
||||
|
||||
if ext == "pdf":
|
||||
return DocumentParser._parse_pdf(file_path)
|
||||
elif ext in ("doc", "docx"):
|
||||
return DocumentParser._parse_docx(file_path)
|
||||
elif ext in ("xls", "xlsx"):
|
||||
return DocumentParser._parse_xlsx(file_path)
|
||||
elif ext == "txt":
|
||||
return DocumentParser._parse_txt(file_path)
|
||||
else:
|
||||
raise ValueError(f"不支持的文件格式: {ext}")
|
||||
|
||||
@staticmethod
|
||||
def _parse_pdf(path: str) -> str:
|
||||
"""PyMuPDF 提取 PDF 文本,回退 pdfplumber"""
|
||||
import fitz
|
||||
|
||||
texts = []
|
||||
doc = fitz.open(path)
|
||||
for page in doc:
|
||||
text = page.get_text()
|
||||
if text and text.strip():
|
||||
texts.append(text.strip())
|
||||
doc.close()
|
||||
|
||||
result = "\n".join(texts)
|
||||
|
||||
# 如果 PyMuPDF 提取文本太少,回退 pdfplumber
|
||||
if len(result.strip()) < 100:
|
||||
try:
|
||||
import pdfplumber
|
||||
texts2 = []
|
||||
with pdfplumber.open(path) as pdf:
|
||||
for page in pdf.pages:
|
||||
text = page.extract_text()
|
||||
if text:
|
||||
texts2.append(text)
|
||||
fallback = "\n".join(texts2)
|
||||
if len(fallback.strip()) > len(result.strip()):
|
||||
result = fallback
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def pdf_to_images_base64(path: str, max_pages: int = 5, dpi: int = 150) -> list[str]:
|
||||
"""
|
||||
将 PDF 页面渲染为图片并返回 base64 编码列表。
|
||||
用于处理扫描件/图片型 PDF。
|
||||
"""
|
||||
import fitz
|
||||
import base64
|
||||
|
||||
images = []
|
||||
doc = fitz.open(path)
|
||||
for i, page in enumerate(doc):
|
||||
if i >= max_pages:
|
||||
break
|
||||
zoom = dpi / 72
|
||||
mat = fitz.Matrix(zoom, zoom)
|
||||
pix = page.get_pixmap(matrix=mat)
|
||||
img_bytes = pix.tobytes("png")
|
||||
b64 = base64.b64encode(img_bytes).decode()
|
||||
images.append(b64)
|
||||
doc.close()
|
||||
return images
|
||||
|
||||
@staticmethod
|
||||
def is_image_pdf(path: str) -> bool:
|
||||
"""判断 PDF 是否为扫描件/图片型(文本内容极少)"""
|
||||
import fitz
|
||||
|
||||
doc = fitz.open(path)
|
||||
total_text = ""
|
||||
for page in doc:
|
||||
total_text += page.get_text()
|
||||
doc.close()
|
||||
# 去掉页码等噪音后,有效文字少于 200 字符视为图片 PDF
|
||||
cleaned = "".join(c for c in total_text if c.strip())
|
||||
return len(cleaned) < 200
|
||||
|
||||
@staticmethod
|
||||
def _parse_docx(path: str) -> str:
|
||||
"""python-docx 提取 Word 文本"""
|
||||
from docx import Document
|
||||
|
||||
doc = Document(path)
|
||||
texts = []
|
||||
for para in doc.paragraphs:
|
||||
if para.text.strip():
|
||||
texts.append(para.text)
|
||||
# 也提取表格内容
|
||||
for table in doc.tables:
|
||||
for row in table.rows:
|
||||
row_text = "\t".join(cell.text.strip() for cell in row.cells if cell.text.strip())
|
||||
if row_text:
|
||||
texts.append(row_text)
|
||||
return "\n".join(texts)
|
||||
|
||||
@staticmethod
|
||||
def _parse_xlsx(path: str) -> str:
|
||||
"""openpyxl 提取 Excel 文本(所有 sheet 拼接)"""
|
||||
from openpyxl import load_workbook
|
||||
|
||||
wb = load_workbook(path, read_only=True, data_only=True)
|
||||
texts = []
|
||||
for sheet in wb.worksheets:
|
||||
for row in sheet.iter_rows(values_only=True):
|
||||
row_text = "\t".join(str(cell) for cell in row if cell is not None)
|
||||
if row_text.strip():
|
||||
texts.append(row_text)
|
||||
wb.close()
|
||||
return "\n".join(texts)
|
||||
|
||||
@staticmethod
|
||||
def _parse_txt(path: str) -> str:
|
||||
"""纯文本文件"""
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
@staticmethod
|
||||
def _extract_docx_images(path: str) -> list[str]:
|
||||
"""从 DOCX 文件中提取嵌入图片(DOCX 本质是 ZIP,图片在 word/media/ 目录)"""
|
||||
import zipfile
|
||||
import base64
|
||||
|
||||
images = []
|
||||
image_exts = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp"}
|
||||
|
||||
try:
|
||||
with zipfile.ZipFile(path, "r") as zf:
|
||||
for name in zf.namelist():
|
||||
if not name.startswith("word/media/"):
|
||||
continue
|
||||
ext = os.path.splitext(name)[1].lower()
|
||||
if ext not in image_exts:
|
||||
continue
|
||||
img_data = zf.read(name)
|
||||
b64 = base64.b64encode(img_data).decode()
|
||||
if len(b64) > DocumentParser.MAX_IMAGE_SIZE:
|
||||
logger.debug(f"跳过过大图片: {name} ({len(b64)} bytes)")
|
||||
continue
|
||||
images.append(b64)
|
||||
if len(images) >= DocumentParser.MAX_IMAGES:
|
||||
break
|
||||
except Exception as e:
|
||||
logger.warning(f"提取 DOCX 图片失败: {e}")
|
||||
|
||||
return images
|
||||
|
||||
@staticmethod
|
||||
def _extract_xlsx_images(path: str) -> list[str]:
|
||||
"""从 XLSX 文件中提取嵌入图片(通过 openpyxl 的 _images 属性)"""
|
||||
import base64
|
||||
|
||||
images = []
|
||||
try:
|
||||
from openpyxl import load_workbook
|
||||
wb = load_workbook(path, read_only=False)
|
||||
for sheet in wb.worksheets:
|
||||
for img in getattr(sheet, "_images", []):
|
||||
try:
|
||||
img_data = img._data()
|
||||
b64 = base64.b64encode(img_data).decode()
|
||||
if len(b64) > DocumentParser.MAX_IMAGE_SIZE:
|
||||
continue
|
||||
images.append(b64)
|
||||
if len(images) >= DocumentParser.MAX_IMAGES:
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if len(images) >= DocumentParser.MAX_IMAGES:
|
||||
break
|
||||
wb.close()
|
||||
except Exception as e:
|
||||
logger.warning(f"提取 XLSX 图片失败: {e}")
|
||||
|
||||
return images
|
||||
+158
-61
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
腾讯云 COS 服务 — 表单直传签名
|
||||
火山引擎 TOS (Volcengine Object Storage) 服务 — 表单直传签名 (V4)
|
||||
"""
|
||||
import time
|
||||
import hmac
|
||||
@@ -7,7 +7,7 @@ import base64
|
||||
import hashlib
|
||||
import json
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from app.config import settings
|
||||
|
||||
|
||||
@@ -17,90 +17,94 @@ def generate_upload_policy(
|
||||
upload_dir: Optional[str] = None,
|
||||
) -> dict:
|
||||
"""
|
||||
生成前端直传 COS 所需的 Policy 和签名
|
||||
生成前端直传 TOS 所需的 Policy 和签名 (V4 HMAC-SHA256)
|
||||
|
||||
COS 表单直传签名流程:
|
||||
1. key_time = "{start_time};{end_time}"
|
||||
2. sign_key = HMAC-SHA1(secret_key, key_time)
|
||||
3. policy JSON → Base64 编码
|
||||
4. string_to_sign = SHA1(policy_base64)
|
||||
5. signature = HMAC-SHA1(sign_key, string_to_sign)
|
||||
TOS 表单直传签名流程 (PostObject):
|
||||
1. 构建 policy JSON → Base64 编码
|
||||
2. 派生签名密钥: kDate → kRegion → kService → kSigning
|
||||
3. signature = HMAC-SHA256(kSigning, policy_base64)
|
||||
|
||||
Returns:
|
||||
{
|
||||
"q_sign_algorithm": "sha1",
|
||||
"q_ak": "SecretId",
|
||||
"q_key_time": "{start};{end}",
|
||||
"q_signature": "...",
|
||||
"x_tos_algorithm": "TOS4-HMAC-SHA256",
|
||||
"x_tos_credential": "AKIDxxxx/20260210/cn-beijing/tos/request",
|
||||
"x_tos_date": "20260210T120000Z",
|
||||
"x_tos_signature": "...",
|
||||
"policy": "base64 encoded policy",
|
||||
"host": "https://bucket.cos.region.myqcloud.com",
|
||||
"host": "https://bucket.tos-cn-beijing.volces.com",
|
||||
"dir": "uploads/2026/02/",
|
||||
"expire": 1234567890,
|
||||
}
|
||||
"""
|
||||
if not settings.COS_SECRET_ID or not settings.COS_SECRET_KEY:
|
||||
raise ValueError("COS 配置未设置")
|
||||
if not settings.TOS_ACCESS_KEY_ID or not settings.TOS_SECRET_ACCESS_KEY:
|
||||
raise ValueError("TOS 配置未设置")
|
||||
|
||||
# 计算时间范围
|
||||
start_time = int(time.time())
|
||||
end_time = start_time + expire_seconds
|
||||
# 计算时间
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
date_stamp = now_utc.strftime("%Y%m%d") # 20260210
|
||||
tos_date = now_utc.strftime("%Y%m%dT%H%M%SZ") # 20260210T120000Z
|
||||
expire_time = int(time.time()) + expire_seconds
|
||||
expiration = datetime.fromtimestamp(expire_time, tz=timezone.utc).strftime(
|
||||
"%Y-%m-%dT%H:%M:%S.000Z"
|
||||
)
|
||||
|
||||
# key_time: "{start};{end}"
|
||||
key_time = f"{start_time};{end_time}"
|
||||
# Credential scope
|
||||
region = settings.TOS_REGION
|
||||
credential = f"{settings.TOS_ACCESS_KEY_ID}/{date_stamp}/{region}/tos/request"
|
||||
|
||||
# 默认上传目录:uploads/年/月/
|
||||
if upload_dir is None:
|
||||
now = datetime.now()
|
||||
upload_dir = f"uploads/{now.year}/{now.month:02d}/"
|
||||
|
||||
# 1. sign_key = HMAC-SHA1(secret_key, key_time)
|
||||
sign_key = hmac.new(
|
||||
settings.COS_SECRET_KEY.encode(),
|
||||
key_time.encode(),
|
||||
hashlib.sha1,
|
||||
).hexdigest()
|
||||
|
||||
# 2. 构建 Policy(COS 表单上传 Policy 格式)
|
||||
# 1. 构建 Policy
|
||||
policy_dict = {
|
||||
"expiration": datetime.utcfromtimestamp(end_time).strftime(
|
||||
"%Y-%m-%dT%H:%M:%S.000Z"
|
||||
),
|
||||
"expiration": expiration,
|
||||
"conditions": [
|
||||
{"bucket": settings.COS_BUCKET_NAME},
|
||||
{"bucket": settings.TOS_BUCKET_NAME},
|
||||
["starts-with", "$key", upload_dir],
|
||||
{"q-sign-algorithm": "sha1"},
|
||||
{"q-ak": settings.COS_SECRET_ID},
|
||||
{"q-sign-time": key_time},
|
||||
{"x-tos-algorithm": "TOS4-HMAC-SHA256"},
|
||||
{"x-tos-credential": credential},
|
||||
{"x-tos-date": tos_date},
|
||||
["content-length-range", 0, max_size_mb * 1024 * 1024],
|
||||
],
|
||||
}
|
||||
|
||||
# 3. Base64 编码 Policy
|
||||
# 2. Base64 编码 Policy
|
||||
policy_json = json.dumps(policy_dict)
|
||||
policy_base64 = base64.b64encode(policy_json.encode()).decode()
|
||||
|
||||
# 4. string_to_sign = SHA1(policy_base64)
|
||||
string_to_sign = hashlib.sha1(policy_base64.encode()).hexdigest()
|
||||
# 3. 派生签名密钥 (V4 Signing Key)
|
||||
k_date = hmac.new(
|
||||
f"TOS4{settings.TOS_SECRET_ACCESS_KEY}".encode(),
|
||||
date_stamp.encode(),
|
||||
hashlib.sha256,
|
||||
).digest()
|
||||
|
||||
# 5. signature = HMAC-SHA1(sign_key, string_to_sign)
|
||||
k_region = hmac.new(k_date, region.encode(), hashlib.sha256).digest()
|
||||
k_service = hmac.new(k_region, b"tos", hashlib.sha256).digest()
|
||||
k_signing = hmac.new(k_service, b"request", hashlib.sha256).digest()
|
||||
|
||||
# 4. signature = HMAC-SHA256(kSigning, policy_base64)
|
||||
signature = hmac.new(
|
||||
sign_key.encode(),
|
||||
string_to_sign.encode(),
|
||||
hashlib.sha1,
|
||||
k_signing,
|
||||
policy_base64.encode(),
|
||||
hashlib.sha256,
|
||||
).hexdigest()
|
||||
|
||||
# 构建 Host
|
||||
host = f"https://{settings.COS_BUCKET_NAME}.cos.{settings.COS_REGION}.myqcloud.com"
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{region}.volces.com"
|
||||
host = f"https://{settings.TOS_BUCKET_NAME}.{endpoint}"
|
||||
|
||||
return {
|
||||
"q_sign_algorithm": "sha1",
|
||||
"q_ak": settings.COS_SECRET_ID,
|
||||
"q_key_time": key_time,
|
||||
"q_signature": signature,
|
||||
"x_tos_algorithm": "TOS4-HMAC-SHA256",
|
||||
"x_tos_credential": credential,
|
||||
"x_tos_date": tos_date,
|
||||
"x_tos_signature": signature,
|
||||
"policy": policy_base64,
|
||||
"host": host,
|
||||
"dir": upload_dir,
|
||||
"expire": end_time,
|
||||
"expire": expire_time,
|
||||
}
|
||||
|
||||
|
||||
@@ -108,18 +112,19 @@ def get_file_url(file_key: str) -> str:
|
||||
"""
|
||||
获取文件的访问 URL
|
||||
|
||||
优先使用 CDN 域名,否则用 COS 源站域名。
|
||||
优先使用 CDN 域名,否则用 TOS 源站域名。
|
||||
|
||||
Args:
|
||||
file_key: 文件在 COS 中的 key,如 "uploads/2026/02/video.mp4"
|
||||
file_key: 文件在 TOS 中的 key,如 "uploads/2026/02/video.mp4"
|
||||
|
||||
Returns:
|
||||
完整的访问 URL
|
||||
"""
|
||||
if settings.COS_CDN_DOMAIN:
|
||||
host = settings.COS_CDN_DOMAIN
|
||||
if settings.TOS_CDN_DOMAIN:
|
||||
host = settings.TOS_CDN_DOMAIN
|
||||
else:
|
||||
host = f"https://{settings.COS_BUCKET_NAME}.cos.{settings.COS_REGION}.myqcloud.com"
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{settings.TOS_REGION}.volces.com"
|
||||
host = f"https://{settings.TOS_BUCKET_NAME}.{endpoint}"
|
||||
|
||||
# 确保 host 以 https:// 开头
|
||||
if not host.startswith("http"):
|
||||
@@ -134,27 +139,119 @@ def get_file_url(file_key: str) -> str:
|
||||
return f"{host}/{file_key}"
|
||||
|
||||
|
||||
def generate_presigned_url(
|
||||
file_key: str,
|
||||
expire_seconds: int = 3600,
|
||||
) -> str:
|
||||
"""
|
||||
为私有桶中的文件生成预签名访问 URL (TOS V4 Query String Auth)
|
||||
|
||||
签名流程:
|
||||
1. 构建 CanonicalRequest
|
||||
2. 构建 StringToSign
|
||||
3. 用派生密钥签名
|
||||
4. 拼接查询参数
|
||||
|
||||
Args:
|
||||
file_key: 文件在 TOS 中的 key
|
||||
expire_seconds: URL 有效期(秒),默认 1 小时
|
||||
|
||||
Returns:
|
||||
预签名 URL
|
||||
"""
|
||||
if not settings.TOS_ACCESS_KEY_ID or not settings.TOS_SECRET_ACCESS_KEY:
|
||||
raise ValueError("TOS 配置未设置")
|
||||
|
||||
from urllib.parse import quote
|
||||
|
||||
region = settings.TOS_REGION
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{region}.volces.com"
|
||||
bucket = settings.TOS_BUCKET_NAME
|
||||
host = f"{bucket}.{endpoint}"
|
||||
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
date_stamp = now_utc.strftime("%Y%m%d")
|
||||
tos_date = now_utc.strftime("%Y%m%dT%H%M%SZ")
|
||||
credential = f"{settings.TOS_ACCESS_KEY_ID}/{date_stamp}/{region}/tos/request"
|
||||
|
||||
# 对 file_key 中的路径段分别编码
|
||||
encoded_key = "/".join(quote(seg, safe="") for seg in file_key.split("/"))
|
||||
|
||||
# 查询参数(按字母序排列)
|
||||
query_params = (
|
||||
f"X-Tos-Algorithm=TOS4-HMAC-SHA256"
|
||||
f"&X-Tos-Credential={quote(credential, safe='')}"
|
||||
f"&X-Tos-Date={tos_date}"
|
||||
f"&X-Tos-Expires={expire_seconds}"
|
||||
f"&X-Tos-SignedHeaders=host"
|
||||
)
|
||||
|
||||
# CanonicalRequest
|
||||
canonical_request = (
|
||||
f"GET\n"
|
||||
f"/{encoded_key}\n"
|
||||
f"{query_params}\n"
|
||||
f"host:{host}\n"
|
||||
f"\n"
|
||||
f"host\n"
|
||||
f"UNSIGNED-PAYLOAD"
|
||||
)
|
||||
|
||||
# StringToSign
|
||||
canonical_request_hash = hashlib.sha256(canonical_request.encode()).hexdigest()
|
||||
string_to_sign = (
|
||||
f"TOS4-HMAC-SHA256\n"
|
||||
f"{tos_date}\n"
|
||||
f"{date_stamp}/{region}/tos/request\n"
|
||||
f"{canonical_request_hash}"
|
||||
)
|
||||
|
||||
# 派生签名密钥
|
||||
k_date = hmac.new(
|
||||
f"TOS4{settings.TOS_SECRET_ACCESS_KEY}".encode(),
|
||||
date_stamp.encode(),
|
||||
hashlib.sha256,
|
||||
).digest()
|
||||
k_region = hmac.new(k_date, region.encode(), hashlib.sha256).digest()
|
||||
k_service = hmac.new(k_region, b"tos", hashlib.sha256).digest()
|
||||
k_signing = hmac.new(k_service, b"request", hashlib.sha256).digest()
|
||||
|
||||
# 计算签名
|
||||
signature = hmac.new(
|
||||
k_signing,
|
||||
string_to_sign.encode(),
|
||||
hashlib.sha256,
|
||||
).hexdigest()
|
||||
|
||||
return (
|
||||
f"https://{host}/{encoded_key}"
|
||||
f"?{query_params}"
|
||||
f"&X-Tos-Signature={signature}"
|
||||
)
|
||||
|
||||
|
||||
def parse_file_key_from_url(url: str) -> str:
|
||||
"""
|
||||
从完整 URL 解析出文件 key
|
||||
|
||||
Args:
|
||||
url: 完整的 COS URL
|
||||
url: 完整的 TOS URL
|
||||
|
||||
Returns:
|
||||
文件 key
|
||||
"""
|
||||
# 尝试移除 CDN 域名
|
||||
if settings.COS_CDN_DOMAIN:
|
||||
cdn = settings.COS_CDN_DOMAIN.rstrip("/")
|
||||
if settings.TOS_CDN_DOMAIN:
|
||||
cdn = settings.TOS_CDN_DOMAIN.rstrip("/")
|
||||
if not cdn.startswith("http"):
|
||||
cdn = f"https://{cdn}"
|
||||
if url.startswith(cdn):
|
||||
return url[len(cdn):].lstrip("/")
|
||||
|
||||
# 尝试移除 COS 源站域名
|
||||
cos_host = f"https://{settings.COS_BUCKET_NAME}.cos.{settings.COS_REGION}.myqcloud.com"
|
||||
if url.startswith(cos_host):
|
||||
return url[len(cos_host):].lstrip("/")
|
||||
# 尝试移除 TOS 源站域名
|
||||
endpoint = settings.TOS_ENDPOINT or f"tos-cn-{settings.TOS_REGION}.volces.com"
|
||||
tos_host = f"https://{settings.TOS_BUCKET_NAME}.{endpoint}"
|
||||
if url.startswith(tos_host):
|
||||
return url[len(tos_host):].lstrip("/")
|
||||
|
||||
return url
|
||||
|
||||
@@ -195,6 +195,42 @@ async def upload_video(
|
||||
return task
|
||||
|
||||
|
||||
AI_AUTO_REJECT_SCORE = 40
|
||||
|
||||
|
||||
def _check_ai_auto_reject(score: int, result: dict) -> tuple[bool, str]:
|
||||
"""
|
||||
判断 AI 审核结果是否应自动驳回
|
||||
|
||||
触发条件(任一):
|
||||
1. 法规合规维度存在 HIGH 级违规(违禁词/功效词)
|
||||
2. 品牌安全维度存在 HIGH 级违规(竞品提及)
|
||||
3. 总分 < 40
|
||||
"""
|
||||
reasons = []
|
||||
violations = result.get("violations", [])
|
||||
|
||||
# 条件1: 法规 HIGH
|
||||
high_legal = [v for v in violations if v.get("dimension") == "legal" and v.get("severity") == "high"]
|
||||
if high_legal:
|
||||
words = [v.get("content", "") for v in high_legal[:5]]
|
||||
reasons.append(f"法规违规:{', '.join(words)}")
|
||||
|
||||
# 条件2: 品牌安全 HIGH
|
||||
high_brand = [v for v in violations if v.get("dimension") == "brand_safety" and v.get("severity") == "high"]
|
||||
if high_brand:
|
||||
words = [v.get("content", "") for v in high_brand[:5]]
|
||||
reasons.append(f"品牌安全违规:{', '.join(words)}")
|
||||
|
||||
# 条件3: 总分过低
|
||||
if score < AI_AUTO_REJECT_SCORE:
|
||||
reasons.append(f"综合评分 {score} 分,低于合格线 {AI_AUTO_REJECT_SCORE} 分")
|
||||
|
||||
if reasons:
|
||||
return True, ";".join(reasons)
|
||||
return False, ""
|
||||
|
||||
|
||||
async def complete_ai_review(
|
||||
db: AsyncSession,
|
||||
task: Task,
|
||||
@@ -206,9 +242,16 @@ async def complete_ai_review(
|
||||
完成 AI 审核
|
||||
|
||||
- 更新 AI 审核结果
|
||||
- 状态流转到代理商审核
|
||||
- 自动驳回:法规/品牌安全 HIGH 违规或总分 < 40 → 回到上传阶段
|
||||
- 正常:流转到代理商审核
|
||||
"""
|
||||
now = datetime.now(timezone.utc)
|
||||
auto_rejected, reject_reason = _check_ai_auto_reject(score, result)
|
||||
|
||||
# 将自动驳回信息写入 result,前端可据此展示
|
||||
if auto_rejected:
|
||||
result["ai_auto_rejected"] = True
|
||||
result["ai_reject_reason"] = reject_reason
|
||||
|
||||
if review_type == "script":
|
||||
if task.stage != TaskStage.SCRIPT_AI_REVIEW:
|
||||
@@ -217,7 +260,11 @@ async def complete_ai_review(
|
||||
task.script_ai_score = score
|
||||
task.script_ai_result = result
|
||||
task.script_ai_reviewed_at = now
|
||||
task.stage = TaskStage.SCRIPT_AGENCY_REVIEW
|
||||
|
||||
if auto_rejected:
|
||||
task.stage = TaskStage.SCRIPT_UPLOAD
|
||||
else:
|
||||
task.stage = TaskStage.SCRIPT_AGENCY_REVIEW
|
||||
|
||||
elif review_type == "video":
|
||||
if task.stage != TaskStage.VIDEO_AI_REVIEW:
|
||||
@@ -226,7 +273,11 @@ async def complete_ai_review(
|
||||
task.video_ai_score = score
|
||||
task.video_ai_result = result
|
||||
task.video_ai_reviewed_at = now
|
||||
task.stage = TaskStage.VIDEO_AGENCY_REVIEW
|
||||
|
||||
if auto_rejected:
|
||||
task.stage = TaskStage.VIDEO_UPLOAD
|
||||
else:
|
||||
task.stage = TaskStage.VIDEO_AGENCY_REVIEW
|
||||
|
||||
else:
|
||||
raise ValueError(f"不支持的审核类型: {review_type}")
|
||||
@@ -459,6 +510,7 @@ async def list_tasks_for_agency(
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
stage: Optional[TaskStage] = None,
|
||||
project_id: Optional[str] = None,
|
||||
) -> Tuple[List[Task], int]:
|
||||
"""获取代理商的任务列表"""
|
||||
query = (
|
||||
@@ -473,6 +525,8 @@ async def list_tasks_for_agency(
|
||||
|
||||
if stage:
|
||||
query = query.where(Task.stage == stage)
|
||||
if project_id:
|
||||
query = query.where(Task.project_id == project_id)
|
||||
|
||||
query = query.order_by(Task.created_at.desc())
|
||||
|
||||
@@ -480,6 +534,8 @@ async def list_tasks_for_agency(
|
||||
count_query = select(func.count(Task.id)).where(Task.agency_id == agency_id)
|
||||
if stage:
|
||||
count_query = count_query.where(Task.stage == stage)
|
||||
if project_id:
|
||||
count_query = count_query.where(Task.project_id == project_id)
|
||||
count_result = await db.execute(count_query)
|
||||
total = count_result.scalar() or 0
|
||||
|
||||
@@ -497,12 +553,17 @@ async def list_tasks_for_brand(
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
stage: Optional[TaskStage] = None,
|
||||
project_id: Optional[str] = None,
|
||||
) -> Tuple[List[Task], int]:
|
||||
"""获取品牌方的任务列表(通过项目关联)"""
|
||||
# 先获取品牌方的所有项目
|
||||
project_ids_query = select(Project.id).where(Project.brand_id == brand_id)
|
||||
project_ids_result = await db.execute(project_ids_query)
|
||||
project_ids = [row[0] for row in project_ids_result.all()]
|
||||
if project_id:
|
||||
# 指定了项目 ID,直接筛选该项目的任务
|
||||
project_ids = [project_id]
|
||||
else:
|
||||
# 未指定项目,获取品牌方的所有项目
|
||||
project_ids_query = select(Project.id).where(Project.brand_id == brand_id)
|
||||
project_ids_result = await db.execute(project_ids_query)
|
||||
project_ids = [row[0] for row in project_ids_result.all()]
|
||||
|
||||
if not project_ids:
|
||||
return [], 0
|
||||
|
||||
@@ -14,7 +14,7 @@ from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.config import settings
|
||||
from app.models.review import ReviewTask, TaskStatus as DBTaskStatus
|
||||
from app.models.rule import ForbiddenWord, Competitor
|
||||
from app.models.rule import ForbiddenWord, Competitor, PlatformRule, RuleStatus
|
||||
from app.models.ai_config import AIConfig
|
||||
from app.services.video_download import VideoDownloadService, DownloadResult
|
||||
from app.services.keyframe import KeyFrameExtractor, ExtractionResult
|
||||
@@ -81,6 +81,7 @@ async def complete_review(
|
||||
summary: str,
|
||||
violations: list[dict],
|
||||
status: DBTaskStatus = DBTaskStatus.COMPLETED,
|
||||
soft_warnings: Optional[list[dict]] = None,
|
||||
):
|
||||
"""完成审核"""
|
||||
result = await db.execute(
|
||||
@@ -94,6 +95,8 @@ async def complete_review(
|
||||
task.score = score
|
||||
task.summary = summary
|
||||
task.violations = violations
|
||||
if soft_warnings is not None:
|
||||
task.soft_warnings = soft_warnings
|
||||
task.completed_at = datetime.now(timezone.utc)
|
||||
await db.commit()
|
||||
|
||||
@@ -153,6 +156,24 @@ async def get_competitors(db: AsyncSession, tenant_id: str, brand_id: str) -> li
|
||||
return [row[0] for row in result.fetchall()]
|
||||
|
||||
|
||||
async def get_platform_forbidden_words(
|
||||
db: AsyncSession, tenant_id: str, brand_id: str, platform: str,
|
||||
) -> list[str]:
|
||||
"""从 DB 获取品牌方在该平台的 active 规则中的违禁词"""
|
||||
result = await db.execute(
|
||||
select(PlatformRule).where(
|
||||
PlatformRule.tenant_id == tenant_id,
|
||||
PlatformRule.brand_id == brand_id,
|
||||
PlatformRule.platform == platform,
|
||||
PlatformRule.status == RuleStatus.ACTIVE.value,
|
||||
)
|
||||
)
|
||||
rule = result.scalar_one_or_none()
|
||||
if not rule or not rule.parsed_rules:
|
||||
return []
|
||||
return rule.parsed_rules.get("forbidden_words", [])
|
||||
|
||||
|
||||
async def process_video_review(
|
||||
review_id: str,
|
||||
tenant_id: str,
|
||||
@@ -199,6 +220,13 @@ async def process_video_review(
|
||||
|
||||
# 获取规则
|
||||
forbidden_words = await get_forbidden_words(db, tenant_id)
|
||||
# 合并平台规则中的违禁词
|
||||
platform_fw = await get_platform_forbidden_words(db, tenant_id, brand_id, platform)
|
||||
existing_set = set(forbidden_words)
|
||||
for w in platform_fw:
|
||||
if w not in existing_set:
|
||||
forbidden_words.append(w)
|
||||
existing_set.add(w)
|
||||
competitors = await get_competitors(db, tenant_id, brand_id)
|
||||
|
||||
# 初始化 AI 服务
|
||||
@@ -281,16 +309,37 @@ async def process_video_review(
|
||||
)
|
||||
all_violations.extend(subtitle_violations)
|
||||
|
||||
# 6. 计算分数和生成报告
|
||||
# 6. 分流 violations / soft_warnings
|
||||
await update_review_progress(db, review_id, 90, "生成报告")
|
||||
score = review_service.calculate_score(all_violations)
|
||||
|
||||
if not all_violations:
|
||||
hard_violations = []
|
||||
soft_warnings_data = []
|
||||
|
||||
for v in all_violations:
|
||||
v_type = v.get("type", "")
|
||||
if v_type in ("forbidden_word", "efficacy_claim", "competitor_logo", "brand_safety"):
|
||||
hard_violations.append(v)
|
||||
elif v_type in ("duration_short", "mention_missing"):
|
||||
soft_warnings_data.append({
|
||||
"code": f"video_{v_type}",
|
||||
"message": v.get("content", ""),
|
||||
"action_required": "note",
|
||||
"blocking": False,
|
||||
"context": {"suggestion": v.get("suggestion", "")},
|
||||
})
|
||||
else:
|
||||
hard_violations.append(v) # 默认当硬性违规
|
||||
|
||||
# 计算分数(仅硬性违规影响分数)
|
||||
score = review_service.calculate_score(hard_violations)
|
||||
|
||||
if not hard_violations:
|
||||
summary = "视频内容合规,未发现违规项"
|
||||
if soft_warnings_data:
|
||||
summary += f"({len(soft_warnings_data)} 条提醒)"
|
||||
else:
|
||||
high_count = sum(1 for v in all_violations if v.get("risk_level") == "high")
|
||||
medium_count = sum(1 for v in all_violations if v.get("risk_level") == "medium")
|
||||
summary = f"发现 {len(all_violations)} 处违规"
|
||||
high_count = sum(1 for v in hard_violations if v.get("risk_level") == "high")
|
||||
summary = f"发现 {len(hard_violations)} 处违规"
|
||||
if high_count > 0:
|
||||
summary += f"({high_count} 处高风险)"
|
||||
|
||||
@@ -300,7 +349,8 @@ async def process_video_review(
|
||||
review_id,
|
||||
score=score,
|
||||
summary=summary,
|
||||
violations=all_violations,
|
||||
violations=hard_violations,
|
||||
soft_warnings=soft_warnings_data if soft_warnings_data else None,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -9,6 +9,8 @@ services:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-postgres}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-miaosi}
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- ./data/postgres:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
@@ -21,6 +23,8 @@ services:
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
container_name: miaosi-redis
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- ./data/redis:/data
|
||||
healthcheck:
|
||||
@@ -82,7 +86,8 @@ services:
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
celery-worker: {}
|
||||
celery-worker:
|
||||
condition: service_started
|
||||
command: celery -A app.celery_app beat -l info
|
||||
|
||||
# Next.js 前端
|
||||
|
||||
@@ -14,13 +14,19 @@ dependencies = [
|
||||
"httpx>=0.26.0",
|
||||
"pydantic[email]>=2.5.0",
|
||||
"pydantic-settings>=2.0.0",
|
||||
"python-jose>=3.3.0",
|
||||
"PyJWT>=2.8.0",
|
||||
"passlib>=1.7.4",
|
||||
"alembic>=1.13.0",
|
||||
"cryptography>=42.0.0",
|
||||
"openai>=1.12.0",
|
||||
"cachetools>=5.3.0",
|
||||
"sse-starlette>=2.0.0",
|
||||
"pdfplumber>=0.10.0",
|
||||
"python-docx>=1.1.0",
|
||||
"openpyxl>=3.1.0",
|
||||
"PyMuPDF>=1.24.0",
|
||||
"tos>=2.7.0",
|
||||
"socksio>=1.0.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
@@ -57,6 +63,7 @@ markers = [
|
||||
]
|
||||
filterwarnings = [
|
||||
"ignore::DeprecationWarning",
|
||||
"ignore::jwt.warnings.InsecureKeyLengthWarning",
|
||||
]
|
||||
|
||||
[tool.coverage.run]
|
||||
|
||||
+10
-10
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
# ===========================
|
||||
# PostgreSQL 每日备份脚本
|
||||
# 备份到本地 + 上传到腾讯云 COS
|
||||
# 备份到本地 + 上传到火山引擎 TOS
|
||||
# ===========================
|
||||
# 配合 crontab 使用:
|
||||
# 0 3 * * * /path/to/backup.sh >> /var/log/miaosi-backup.log 2>&1
|
||||
@@ -15,9 +15,9 @@ POSTGRES_USER="${POSTGRES_USER:-postgres}"
|
||||
POSTGRES_DB="${POSTGRES_DB:-miaosi}"
|
||||
RETAIN_DAYS="${RETAIN_DAYS:-7}"
|
||||
|
||||
# COS 备份桶(需要先安装 coscli 并配置好凭证)
|
||||
COS_BACKUP_BUCKET="${COS_BACKUP_BUCKET:-}"
|
||||
COS_BACKUP_PREFIX="${COS_BACKUP_PREFIX:-backups/postgres}"
|
||||
# TOS 备份桶(需要先安装 tosutil 并配置好凭证)
|
||||
TOS_BACKUP_BUCKET="${TOS_BACKUP_BUCKET:-}"
|
||||
TOS_BACKUP_PREFIX="${TOS_BACKUP_PREFIX:-backups/postgres}"
|
||||
|
||||
# ---- 执行 ----
|
||||
DATE=$(date +%Y%m%d_%H%M%S)
|
||||
@@ -32,13 +32,13 @@ docker exec "$POSTGRES_CONTAINER" pg_dump -U "$POSTGRES_USER" "$POSTGRES_DB" | g
|
||||
|
||||
echo "[$(date)] 本地备份完成: ${BACKUP_DIR}/${FILENAME}"
|
||||
|
||||
# 2. 上传到 COS(如果配置了备份桶)
|
||||
if [ -n "$COS_BACKUP_BUCKET" ]; then
|
||||
if command -v coscli &> /dev/null; then
|
||||
coscli cp "${BACKUP_DIR}/${FILENAME}" "cos://${COS_BACKUP_BUCKET}/${COS_BACKUP_PREFIX}/${FILENAME}"
|
||||
echo "[$(date)] 已上传到 COS: ${COS_BACKUP_BUCKET}/${COS_BACKUP_PREFIX}/${FILENAME}"
|
||||
# 2. 上传到 TOS(如果配置了备份桶)
|
||||
if [ -n "$TOS_BACKUP_BUCKET" ]; then
|
||||
if command -v tosutil &> /dev/null; then
|
||||
tosutil cp "${BACKUP_DIR}/${FILENAME}" "tos://${TOS_BACKUP_BUCKET}/${TOS_BACKUP_PREFIX}/${FILENAME}"
|
||||
echo "[$(date)] 已上传到 TOS: ${TOS_BACKUP_BUCKET}/${TOS_BACKUP_PREFIX}/${FILENAME}"
|
||||
else
|
||||
echo "[$(date)] 警告: coscli 未安装,跳过 COS 上传"
|
||||
echo "[$(date)] 警告: tosutil 未安装,跳过 TOS 上传"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
+99
-9
@@ -165,6 +165,7 @@ async def seed_data() -> None:
|
||||
brand_id=BRAND_ID,
|
||||
name="2026春季新品推广",
|
||||
description="春季新品防晒霜推广活动,面向 18-35 岁女性用户,重点投放抖音和小红书平台",
|
||||
platform="douyin",
|
||||
start_date=NOW,
|
||||
deadline=NOW + timedelta(days=30),
|
||||
status="active",
|
||||
@@ -188,9 +189,10 @@ async def seed_data() -> None:
|
||||
id=BRIEF_ID,
|
||||
project_id=PROJECT_ID,
|
||||
selling_points=[
|
||||
{"content": "SPF50+ PA++++,超强防晒", "required": True},
|
||||
{"content": "轻薄不油腻,适合日常通勤", "required": True},
|
||||
{"content": "添加玻尿酸成分,防晒同时保湿", "required": False},
|
||||
{"content": "SPF50+ PA++++,超强防晒", "priority": "core"},
|
||||
{"content": "轻薄不油腻,适合日常通勤", "priority": "core"},
|
||||
{"content": "添加玻尿酸成分,防晒同时保湿", "priority": "recommended"},
|
||||
{"content": "获得皮肤科医生推荐", "priority": "reference"},
|
||||
],
|
||||
blacklist_words=[
|
||||
{"word": "最好", "reason": "绝对化用语"},
|
||||
@@ -199,6 +201,7 @@ async def seed_data() -> None:
|
||||
],
|
||||
competitors=["安耐晒", "怡思丁", "薇诺娜"],
|
||||
brand_tone="年轻、活力、专业、可信赖",
|
||||
min_selling_points=2,
|
||||
min_duration=30,
|
||||
max_duration=60,
|
||||
other_requirements="请在视频中展示产品实际使用效果,包含户外场景拍摄",
|
||||
@@ -235,8 +238,38 @@ async def seed_data() -> None:
|
||||
script_ai_result={
|
||||
"score": 85,
|
||||
"summary": "脚本整体符合要求,卖点覆盖充分",
|
||||
"issues": [
|
||||
{"type": "soft_warning", "content": "建议增加产品成分说明"},
|
||||
"dimensions": {
|
||||
"legal": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"platform": {"score": 85, "passed": True, "issue_count": 1},
|
||||
"brand_safety": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"brief_match": {"score": 80, "passed": True, "issue_count": 1},
|
||||
},
|
||||
"selling_point_matches": [
|
||||
{"content": "SPF50+ PA++++,超强防晒", "priority": "core", "matched": True, "evidence": "脚本中提到了SPF50+防晒参数"},
|
||||
{"content": "轻薄不油腻,适合日常通勤", "priority": "core", "matched": True, "evidence": "提到了轻薄质地不油腻"},
|
||||
{"content": "添加玻尿酸成分,防晒同时保湿", "priority": "recommended", "matched": False, "evidence": "未提及玻尿酸成分"},
|
||||
],
|
||||
"brief_match_detail": {
|
||||
"total_points": 3,
|
||||
"matched_points": 2,
|
||||
"required_points": 2,
|
||||
"coverage_score": 100,
|
||||
"overall_score": 75,
|
||||
"highlights": [
|
||||
"防晒参数描述准确,SPF50+ PA++++完整提及",
|
||||
"产品使用场景贴合Brief要求的日常通勤场景",
|
||||
],
|
||||
"issues": [
|
||||
"缺少玻尿酸保湿成分的说明,建议补充产品成分亮点",
|
||||
"脚本中使用了\"神器\"等夸张用语,需替换为更客观的表述",
|
||||
],
|
||||
"explanation": "脚本覆盖了2/2条要求卖点,核心卖点全部匹配。整体内容方向正确,但部分细节可优化。",
|
||||
},
|
||||
"violations": [
|
||||
{"type": "forbidden_word", "content": "神器", "severity": "medium", "suggestion": "建议替换为\"好物\"", "dimension": "platform"},
|
||||
],
|
||||
"soft_warnings": [
|
||||
{"type": "suggestion", "content": "建议增加产品成分说明", "suggestion": "可提及玻尿酸等核心成分"},
|
||||
],
|
||||
},
|
||||
script_ai_reviewed_at=NOW - timedelta(hours=1),
|
||||
@@ -257,7 +290,33 @@ async def seed_data() -> None:
|
||||
script_ai_result={
|
||||
"score": 92,
|
||||
"summary": "脚本质量优秀,完全符合 Brief 要求",
|
||||
"issues": [],
|
||||
"dimensions": {
|
||||
"legal": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"platform": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"brand_safety": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"brief_match": {"score": 90, "passed": True, "issue_count": 0},
|
||||
},
|
||||
"selling_point_matches": [
|
||||
{"content": "SPF50+ PA++++,超强防晒", "priority": "core", "matched": True, "evidence": "脚本完整提及防晒参数"},
|
||||
{"content": "轻薄不油腻,适合日常通勤", "priority": "core", "matched": True, "evidence": "详细描述了质地体验"},
|
||||
{"content": "添加玻尿酸成分,防晒同时保湿", "priority": "recommended", "matched": True, "evidence": "提及了玻尿酸保湿功能"},
|
||||
],
|
||||
"brief_match_detail": {
|
||||
"total_points": 3,
|
||||
"matched_points": 3,
|
||||
"required_points": 2,
|
||||
"coverage_score": 100,
|
||||
"overall_score": 90,
|
||||
"highlights": [
|
||||
"所有核心和推荐卖点均完整覆盖",
|
||||
"产品使用场景自然,与Brief要求高度一致",
|
||||
"成分说明准确,玻尿酸保湿功能表述清晰",
|
||||
],
|
||||
"issues": [],
|
||||
"explanation": "脚本覆盖了3/2条要求卖点(超出要求),与Brief整体匹配度优秀。",
|
||||
},
|
||||
"violations": [],
|
||||
"soft_warnings": [],
|
||||
},
|
||||
script_ai_reviewed_at=NOW - timedelta(days=2),
|
||||
script_agency_status=TaskStatus.PASSED,
|
||||
@@ -282,7 +341,19 @@ async def seed_data() -> None:
|
||||
script_file_name="防晒霜种草脚本v4.pdf",
|
||||
script_uploaded_at=NOW - timedelta(days=7),
|
||||
script_ai_score=90,
|
||||
script_ai_result={"score": 90, "summary": "符合要求", "issues": []},
|
||||
script_ai_result={
|
||||
"score": 90,
|
||||
"summary": "符合要求",
|
||||
"dimensions": {
|
||||
"legal": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"platform": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"brand_safety": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"brief_match": {"score": 85, "passed": True, "issue_count": 0},
|
||||
},
|
||||
"selling_point_matches": [],
|
||||
"violations": [],
|
||||
"soft_warnings": [],
|
||||
},
|
||||
script_ai_reviewed_at=NOW - timedelta(days=7),
|
||||
script_agency_status=TaskStatus.PASSED,
|
||||
script_agency_comment="通过",
|
||||
@@ -297,7 +368,19 @@ async def seed_data() -> None:
|
||||
video_duration=45,
|
||||
video_uploaded_at=NOW - timedelta(days=5),
|
||||
video_ai_score=88,
|
||||
video_ai_result={"score": 88, "summary": "视频质量良好", "issues": []},
|
||||
video_ai_result={
|
||||
"score": 88,
|
||||
"summary": "视频质量良好",
|
||||
"dimensions": {
|
||||
"legal": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"platform": {"score": 100, "passed": True, "issue_count": 0},
|
||||
"brand_safety": {"score": 85, "passed": True, "issue_count": 0},
|
||||
"brief_match": {"score": 80, "passed": True, "issue_count": 0},
|
||||
},
|
||||
"selling_point_matches": [],
|
||||
"violations": [],
|
||||
"soft_warnings": [],
|
||||
},
|
||||
video_ai_reviewed_at=NOW - timedelta(days=5),
|
||||
video_agency_status=TaskStatus.PASSED,
|
||||
video_agency_comment="视频效果好",
|
||||
@@ -320,10 +403,17 @@ async def seed_data() -> None:
|
||||
ForbiddenWord(id="FW100003", tenant_id=TENANT_ID, word="最好", category="绝对化用语", severity="medium"),
|
||||
ForbiddenWord(id="FW100004", tenant_id=TENANT_ID, word="第一", category="绝对化用语", severity="medium"),
|
||||
ForbiddenWord(id="FW100005", tenant_id=TENANT_ID, word="纯天然", category="虚假宣传", severity="medium"),
|
||||
# 功效词(品牌方可自行增删)
|
||||
ForbiddenWord(id="FW100006", tenant_id=TENANT_ID, word="根治", category="功效词", severity="high"),
|
||||
ForbiddenWord(id="FW100007", tenant_id=TENANT_ID, word="治愈", category="功效词", severity="high"),
|
||||
ForbiddenWord(id="FW100008", tenant_id=TENANT_ID, word="治疗", category="功效词", severity="high"),
|
||||
ForbiddenWord(id="FW100009", tenant_id=TENANT_ID, word="药效", category="功效词", severity="high"),
|
||||
ForbiddenWord(id="FW100010", tenant_id=TENANT_ID, word="疗效", category="功效词", severity="high"),
|
||||
ForbiddenWord(id="FW100011", tenant_id=TENANT_ID, word="特效", category="功效词", severity="high"),
|
||||
]
|
||||
db.add_all(forbidden_words)
|
||||
await db.flush()
|
||||
print(" ✓ 违禁词已创建: 5 条")
|
||||
print(" ✓ 违禁词已创建: 11 条(含 6 条功效词)")
|
||||
|
||||
competitors = [
|
||||
Competitor(id="CP100001", tenant_id=TENANT_ID, brand_id=BRAND_ID, name="安耐晒", keywords=["安耐晒", "ANESSA", "资生堂防晒"]),
|
||||
|
||||
@@ -25,7 +25,7 @@ alembic upgrade head
|
||||
|
||||
# 填充种子数据
|
||||
echo "填充种子数据..."
|
||||
python -m scripts.seed
|
||||
python3 -m scripts.seed
|
||||
|
||||
echo ""
|
||||
echo "=== 基础服务已启动 ==="
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
"""
|
||||
消息 API 测试
|
||||
覆盖: GET /messages, GET /messages/unread-count, PUT /messages/{id}/read, PUT /messages/read-all
|
||||
"""
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.services.message_service import create_message
|
||||
|
||||
|
||||
MESSAGES_URL = "/api/v1/messages"
|
||||
REGISTER_URL = "/api/v1/auth/register"
|
||||
|
||||
|
||||
def _auth(token: str) -> dict:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
async def _register(client: AsyncClient, role: str, name: str, email: str) -> dict:
|
||||
resp = await client.post(REGISTER_URL, json={
|
||||
"email": email,
|
||||
"password": "Test1234!",
|
||||
"name": name,
|
||||
"role": role,
|
||||
"email_code": "000000",
|
||||
})
|
||||
assert resp.status_code == 201, resp.text
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def _seed_messages(
|
||||
db: AsyncSession,
|
||||
user_id: str,
|
||||
count: int = 5,
|
||||
msg_type: str = "system",
|
||||
is_read: bool = False,
|
||||
) -> list:
|
||||
"""在数据库中直接创建消息(绕过 API)"""
|
||||
msgs = []
|
||||
for i in range(count):
|
||||
m = await create_message(
|
||||
db=db,
|
||||
user_id=user_id,
|
||||
type=msg_type,
|
||||
title=f"测试消息 {i+1}",
|
||||
content=f"消息内容 {i+1}",
|
||||
related_task_id=f"TK{100000+i}",
|
||||
sender_name="系统",
|
||||
)
|
||||
if is_read:
|
||||
m.is_read = True
|
||||
msgs.append(m)
|
||||
await db.commit()
|
||||
return msgs
|
||||
|
||||
|
||||
# ==================== GET /messages ====================
|
||||
|
||||
|
||||
class TestGetMessages:
|
||||
"""消息列表"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_messages(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "空消息", "empty-msg@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.get(MESSAGES_URL, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
body = resp.json()
|
||||
assert body["items"] == []
|
||||
assert body["total"] == 0
|
||||
assert body["page"] == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_messages(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "消息列表", "list-msg@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
await _seed_messages(test_db_session, user_id, count=3)
|
||||
|
||||
resp = await client.get(MESSAGES_URL, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
body = resp.json()
|
||||
assert body["total"] == 3
|
||||
assert len(body["items"]) == 3
|
||||
|
||||
# 验证消息结构
|
||||
msg = body["items"][0]
|
||||
assert "id" in msg
|
||||
assert "type" in msg
|
||||
assert "title" in msg
|
||||
assert "content" in msg
|
||||
assert "is_read" in msg
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pagination(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "分页测试", "page-msg@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
await _seed_messages(test_db_session, user_id, count=15)
|
||||
|
||||
# 第 1 页
|
||||
resp1 = await client.get(
|
||||
MESSAGES_URL, params={"page": 1, "page_size": 10}, headers=_auth(token),
|
||||
)
|
||||
assert resp1.status_code == 200
|
||||
body1 = resp1.json()
|
||||
assert len(body1["items"]) == 10
|
||||
assert body1["total"] == 15
|
||||
assert body1["page"] == 1
|
||||
|
||||
# 第 2 页
|
||||
resp2 = await client.get(
|
||||
MESSAGES_URL, params={"page": 2, "page_size": 10}, headers=_auth(token),
|
||||
)
|
||||
assert resp2.status_code == 200
|
||||
body2 = resp2.json()
|
||||
assert len(body2["items"]) == 5
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_filter_by_read_status(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "已读过滤", "read-filter@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
await _seed_messages(test_db_session, user_id, count=3, is_read=False)
|
||||
await _seed_messages(test_db_session, user_id, count=2, is_read=True)
|
||||
|
||||
# 只看未读
|
||||
resp = await client.get(
|
||||
MESSAGES_URL, params={"is_read": False}, headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["total"] == 3
|
||||
for m in body["items"]:
|
||||
assert m["is_read"] is False
|
||||
|
||||
# 只看已读
|
||||
resp2 = await client.get(
|
||||
MESSAGES_URL, params={"is_read": True}, headers=_auth(token),
|
||||
)
|
||||
assert resp2.status_code == 200
|
||||
assert resp2.json()["total"] == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_filter_by_type(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "类型过滤", "type-filter@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
await _seed_messages(test_db_session, user_id, count=3, msg_type="new_task")
|
||||
await _seed_messages(test_db_session, user_id, count=2, msg_type="system")
|
||||
|
||||
resp = await client.get(
|
||||
MESSAGES_URL, params={"type": "new_task"}, headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["total"] == 3
|
||||
for m in body["items"]:
|
||||
assert m["type"] == "new_task"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_messages_isolation(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
"""用户只能看到自己的消息"""
|
||||
data_a = await _register(client, "brand", "用户A", "user-a@test.com")
|
||||
data_b = await _register(client, "agency", "用户B", "user-b@test.com")
|
||||
|
||||
await _seed_messages(test_db_session, data_a["user"]["id"], count=3)
|
||||
await _seed_messages(test_db_session, data_b["user"]["id"], count=5)
|
||||
|
||||
resp = await client.get(MESSAGES_URL, headers=_auth(data_a["access_token"]))
|
||||
assert resp.json()["total"] == 3
|
||||
|
||||
resp2 = await client.get(MESSAGES_URL, headers=_auth(data_b["access_token"]))
|
||||
assert resp2.json()["total"] == 5
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_messages_unauthenticated(self, client: AsyncClient):
|
||||
resp = await client.get(MESSAGES_URL)
|
||||
assert resp.status_code in (401, 403)
|
||||
|
||||
|
||||
# ==================== GET /messages/unread-count ====================
|
||||
|
||||
|
||||
class TestUnreadCount:
|
||||
"""未读消息数"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unread_count_zero(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "零未读", "zero-unread@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.get(f"{MESSAGES_URL}/unread-count", headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["count"] == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unread_count(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "未读计数", "unread-count@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
await _seed_messages(test_db_session, user_id, count=5, is_read=False)
|
||||
await _seed_messages(test_db_session, user_id, count=3, is_read=True)
|
||||
|
||||
resp = await client.get(f"{MESSAGES_URL}/unread-count", headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["count"] == 5
|
||||
|
||||
|
||||
# ==================== PUT /messages/{id}/read ====================
|
||||
|
||||
|
||||
class TestMarkAsRead:
|
||||
"""标记单条消息已读"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_as_read(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "标记已读", "mark-read@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
msgs = await _seed_messages(test_db_session, user_id, count=1)
|
||||
msg_id = msgs[0].id
|
||||
|
||||
resp = await client.put(f"{MESSAGES_URL}/{msg_id}/read", headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
# 验证未读数减少
|
||||
count_resp = await client.get(f"{MESSAGES_URL}/unread-count", headers=_auth(token))
|
||||
assert count_resp.json()["count"] == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_nonexistent_message(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "不存在", "nonexist-msg@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(f"{MESSAGES_URL}/MSG999999/read", headers=_auth(token))
|
||||
assert resp.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_other_users_message(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
"""不能标记别人的消息"""
|
||||
data_a = await _register(client, "brand", "用户A标记", "mark-a@test.com")
|
||||
data_b = await _register(client, "agency", "用户B标记", "mark-b@test.com")
|
||||
|
||||
msgs = await _seed_messages(test_db_session, data_a["user"]["id"], count=1)
|
||||
msg_id = msgs[0].id
|
||||
|
||||
# 用户B尝试标记用户A的消息
|
||||
resp = await client.put(
|
||||
f"{MESSAGES_URL}/{msg_id}/read",
|
||||
headers=_auth(data_b["access_token"]),
|
||||
)
|
||||
assert resp.status_code == 404 # 看不到别人的消息,返回 404
|
||||
|
||||
|
||||
# ==================== PUT /messages/read-all ====================
|
||||
|
||||
|
||||
class TestMarkAllAsRead:
|
||||
"""标记所有消息已读"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_all_as_read(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
data = await _register(client, "brand", "全部已读", "all-read@test.com")
|
||||
token = data["access_token"]
|
||||
user_id = data["user"]["id"]
|
||||
|
||||
await _seed_messages(test_db_session, user_id, count=5)
|
||||
|
||||
resp = await client.put(f"{MESSAGES_URL}/read-all", headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["count"] == 5
|
||||
|
||||
# 验证未读数为 0
|
||||
count_resp = await client.get(f"{MESSAGES_URL}/unread-count", headers=_auth(token))
|
||||
assert count_resp.json()["count"] == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_all_no_messages(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "无消息全读", "no-msg-all@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(f"{MESSAGES_URL}/read-all", headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["count"] == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mark_all_only_affects_own(self, client: AsyncClient, test_db_session: AsyncSession):
|
||||
"""全部已读只影响自己的消息"""
|
||||
data_a = await _register(client, "brand", "全读A", "all-own-a@test.com")
|
||||
data_b = await _register(client, "agency", "全读B", "all-own-b@test.com")
|
||||
|
||||
await _seed_messages(test_db_session, data_a["user"]["id"], count=3)
|
||||
await _seed_messages(test_db_session, data_b["user"]["id"], count=4)
|
||||
|
||||
# A 全部已读
|
||||
await client.put(f"{MESSAGES_URL}/read-all", headers=_auth(data_a["access_token"]))
|
||||
|
||||
# B 的未读数不受影响
|
||||
count_resp = await client.get(
|
||||
f"{MESSAGES_URL}/unread-count", headers=_auth(data_b["access_token"]),
|
||||
)
|
||||
assert count_resp.json()["count"] == 4
|
||||
@@ -0,0 +1,281 @@
|
||||
"""
|
||||
用户资料 API 测试
|
||||
覆盖: GET /profile, PUT /profile, PUT /profile/password
|
||||
"""
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
|
||||
PROFILE_URL = "/api/v1/profile"
|
||||
REGISTER_URL = "/api/v1/auth/register"
|
||||
|
||||
|
||||
def _auth(token: str) -> dict:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
async def _register(client: AsyncClient, role: str, name: str, email: str) -> dict:
|
||||
resp = await client.post(REGISTER_URL, json={
|
||||
"email": email,
|
||||
"password": "Test1234!",
|
||||
"name": name,
|
||||
"role": role,
|
||||
"email_code": "000000",
|
||||
})
|
||||
assert resp.status_code == 201, resp.text
|
||||
return resp.json()
|
||||
|
||||
|
||||
# ==================== GET /profile ====================
|
||||
|
||||
|
||||
class TestGetProfile:
|
||||
"""获取用户资料"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_brand_profile(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "测试品牌", "brand-profile@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.get(PROFILE_URL, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
body = resp.json()
|
||||
assert body["name"] == "测试品牌"
|
||||
assert body["role"] == "brand"
|
||||
assert body["email"] == "brand-profile@test.com"
|
||||
assert body["brand"] is not None
|
||||
assert body["brand"]["name"] == "测试品牌"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_agency_profile(self, client: AsyncClient):
|
||||
data = await _register(client, "agency", "测试代理商", "agency-profile@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.get(PROFILE_URL, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
body = resp.json()
|
||||
assert body["role"] == "agency"
|
||||
assert body["agency"] is not None
|
||||
assert body["agency"]["name"] == "测试代理商"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_creator_profile(self, client: AsyncClient):
|
||||
data = await _register(client, "creator", "测试达人", "creator-profile@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.get(PROFILE_URL, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
body = resp.json()
|
||||
assert body["role"] == "creator"
|
||||
assert body["creator"] is not None
|
||||
assert body["creator"]["name"] == "测试达人"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_profile_unauthenticated(self, client: AsyncClient):
|
||||
resp = await client.get(PROFILE_URL)
|
||||
assert resp.status_code in (401, 403)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_profile_invalid_token(self, client: AsyncClient):
|
||||
resp = await client.get(PROFILE_URL, headers=_auth("invalid-token"))
|
||||
assert resp.status_code in (401, 403)
|
||||
|
||||
|
||||
# ==================== PUT /profile ====================
|
||||
|
||||
|
||||
class TestUpdateProfile:
|
||||
"""更新用户资料"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_brand_name(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "原始品牌", "brand-update@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
PROFILE_URL,
|
||||
json={"name": "新品牌名称"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["name"] == "新品牌名称"
|
||||
assert body["brand"]["name"] == "新品牌名称"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_brand_contact(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "品牌联系人", "brand-contact@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
PROFILE_URL,
|
||||
json={
|
||||
"description": "品牌描述",
|
||||
"contact_name": "张三",
|
||||
"contact_phone": "13800000001",
|
||||
"contact_email": "zhangsan@brand.com",
|
||||
},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["brand"]["description"] == "品牌描述"
|
||||
assert body["brand"]["contact_name"] == "张三"
|
||||
assert body["brand"]["contact_phone"] == "13800000001"
|
||||
assert body["brand"]["contact_email"] == "zhangsan@brand.com"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_agency_profile(self, client: AsyncClient):
|
||||
data = await _register(client, "agency", "代理商", "agency-update@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
PROFILE_URL,
|
||||
json={
|
||||
"name": "新代理商名",
|
||||
"description": "专业MCN机构",
|
||||
"contact_name": "李四",
|
||||
},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["name"] == "新代理商名"
|
||||
assert body["agency"]["name"] == "新代理商名"
|
||||
assert body["agency"]["description"] == "专业MCN机构"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_creator_profile(self, client: AsyncClient):
|
||||
data = await _register(client, "creator", "达人", "creator-update@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
PROFILE_URL,
|
||||
json={
|
||||
"name": "新达人名",
|
||||
"bio": "美食博主",
|
||||
"douyin_account": "douyin123",
|
||||
"xiaohongshu_account": "xhs456",
|
||||
"bilibili_account": "bili789",
|
||||
},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["name"] == "新达人名"
|
||||
assert body["creator"]["name"] == "新达人名"
|
||||
assert body["creator"]["bio"] == "美食博主"
|
||||
assert body["creator"]["douyin_account"] == "douyin123"
|
||||
assert body["creator"]["xiaohongshu_account"] == "xhs456"
|
||||
assert body["creator"]["bilibili_account"] == "bili789"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_phone_and_avatar(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "头像测试", "avatar-test@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
PROFILE_URL,
|
||||
json={
|
||||
"phone": "13900000000",
|
||||
"avatar": "https://example.com/avatar.png",
|
||||
},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["phone"] == "13900000000"
|
||||
assert body["avatar"] == "https://example.com/avatar.png"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_empty_body(self, client: AsyncClient):
|
||||
"""空请求体不应报错"""
|
||||
data = await _register(client, "brand", "空更新测试", "empty-update@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(PROFILE_URL, json={}, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_profile_unauthenticated(self, client: AsyncClient):
|
||||
resp = await client.put(PROFILE_URL, json={"name": "hack"})
|
||||
assert resp.status_code in (401, 403)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_persists(self, client: AsyncClient):
|
||||
"""更新后重新 GET 应返回最新数据"""
|
||||
data = await _register(client, "creator", "持久化测试", "persist@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
await client.put(
|
||||
PROFILE_URL,
|
||||
json={"bio": "更新后的简介"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
|
||||
resp = await client.get(PROFILE_URL, headers=_auth(token))
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["creator"]["bio"] == "更新后的简介"
|
||||
|
||||
|
||||
# ==================== PUT /profile/password ====================
|
||||
|
||||
|
||||
class TestChangePassword:
|
||||
"""修改密码"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_change_password_success(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "密码测试", "pwd-change@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
f"{PROFILE_URL}/password",
|
||||
json={"old_password": "Test1234!", "new_password": "NewPass5678!"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
assert "密码修改成功" in resp.json()["message"]
|
||||
|
||||
# 用新密码登录
|
||||
login_resp = await client.post("/api/v1/auth/login", json={
|
||||
"email": "pwd-change@test.com",
|
||||
"password": "NewPass5678!",
|
||||
})
|
||||
assert login_resp.status_code == 200
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_change_password_wrong_old(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "错误密码", "wrong-pwd@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
f"{PROFILE_URL}/password",
|
||||
json={"old_password": "WrongPassword!", "new_password": "NewPass!"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert "原密码" in resp.json()["detail"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_change_password_too_short(self, client: AsyncClient):
|
||||
data = await _register(client, "brand", "短密码", "short-pwd@test.com")
|
||||
token = data["access_token"]
|
||||
|
||||
resp = await client.put(
|
||||
f"{PROFILE_URL}/password",
|
||||
json={"old_password": "Test1234!", "new_password": "12345"},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 422 # Pydantic validation
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_change_password_unauthenticated(self, client: AsyncClient):
|
||||
resp = await client.put(
|
||||
f"{PROFILE_URL}/password",
|
||||
json={"old_password": "a", "new_password": "b"},
|
||||
)
|
||||
assert resp.status_code in (401, 403)
|
||||
@@ -1,8 +1,10 @@
|
||||
"""
|
||||
规则管理 API 测试 (TDD - 红色阶段)
|
||||
测试覆盖: 违禁词库、白名单、竞品库、平台规则
|
||||
规则管理 API 测试
|
||||
测试覆盖: 违禁词库、白名单、竞品库、平台规则、品牌方平台规则 CRUD
|
||||
"""
|
||||
import json
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from httpx import AsyncClient
|
||||
|
||||
from app.schemas.review import ScriptReviewResponse, ViolationType
|
||||
@@ -343,7 +345,7 @@ class TestRuleConflictDetection:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_detect_brief_platform_conflict(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""检测 Brief 与平台规则冲突"""
|
||||
"""检测 Brief 与平台规则冲突(required_phrases)"""
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
@@ -351,7 +353,7 @@ class TestRuleConflictDetection:
|
||||
"brand_id": brand_id,
|
||||
"platform": "douyin",
|
||||
"brief_rules": {
|
||||
"required_phrases": ["绝对有效"], # 可能违反平台规则
|
||||
"required_phrases": ["绝对有效"],
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -383,3 +385,669 @@ class TestRuleConflictDetection:
|
||||
assert "brief_rule" in conflict
|
||||
assert "platform_rule" in conflict
|
||||
assert "suggestion" in conflict
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_selling_points_conflict_detection(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""selling_points 字段也参与冲突检测"""
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "douyin",
|
||||
"brief_rules": {
|
||||
"selling_points": ["100%纯天然成分", "绝对安全"],
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
assert len(data["conflicts"]) >= 2 # "100%" 和 "绝对" 都命中
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_conflict_returns_empty(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""无冲突时返回空列表"""
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "douyin",
|
||||
"brief_rules": {
|
||||
"selling_points": ["温和护肤", "适合敏感肌"],
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
assert data["conflicts"] == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_duration_conflict_brief_max_below_platform_min(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""Brief 最长时长低于平台最短要求"""
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "douyin", # 硬编码 min_seconds=7
|
||||
"brief_rules": {
|
||||
"max_duration": 5,
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
assert len(data["conflicts"]) >= 1
|
||||
assert any("时长" in c["brief_rule"] for c in data["conflicts"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_db_rules_participate_in_conflict_detection(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""DB 中 active 的规则参与冲突检测"""
|
||||
headers = {"X-Tenant-ID": tenant_id}
|
||||
|
||||
# 创建并确认一条包含自定义违禁词的 DB 平台规则
|
||||
create_resp = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
rule_id = create_resp.json()["id"]
|
||||
|
||||
custom_rules = {
|
||||
"forbidden_words": ["自定义违禁词ABC"],
|
||||
"restricted_words": [],
|
||||
"duration": {"min_seconds": 15, "max_seconds": 120},
|
||||
"content_requirements": [],
|
||||
"other_rules": [],
|
||||
}
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers=headers,
|
||||
json={"parsed_rules": custom_rules},
|
||||
)
|
||||
|
||||
# 验证 DB 违禁词参与检测
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers=headers,
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "douyin",
|
||||
"brief_rules": {
|
||||
"selling_points": ["这个自定义违禁词ABC很好"],
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
assert len(data["conflicts"]) >= 1
|
||||
assert any("自定义违禁词ABC" in c["suggestion"] for c in data["conflicts"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_db_duration_conflict(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""DB 规则中的时长限制参与检测"""
|
||||
headers = {"X-Tenant-ID": tenant_id}
|
||||
|
||||
# 创建 DB 规则:max_seconds=60
|
||||
create_resp = await _create_platform_rule(client, tenant_id, brand_id, platform="xiaohongshu")
|
||||
rule_id = create_resp.json()["id"]
|
||||
|
||||
custom_rules = {
|
||||
"forbidden_words": [],
|
||||
"restricted_words": [],
|
||||
"duration": {"min_seconds": 10, "max_seconds": 60},
|
||||
"content_requirements": [],
|
||||
"other_rules": [],
|
||||
}
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers=headers,
|
||||
json={"parsed_rules": custom_rules},
|
||||
)
|
||||
|
||||
# Brief 最短时长 90s > 平台最长 60s → 冲突
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers=headers,
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "xiaohongshu",
|
||||
"brief_rules": {
|
||||
"min_duration": 90,
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
assert len(data["conflicts"]) >= 1
|
||||
assert any("最长限制" in c["platform_rule"] for c in data["conflicts"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_db_and_hardcoded_rules_merge(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""DB 规则与硬编码规则合并检测"""
|
||||
headers = {"X-Tenant-ID": tenant_id}
|
||||
|
||||
# DB 规则只包含自定义违禁词
|
||||
create_resp = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
rule_id = create_resp.json()["id"]
|
||||
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers=headers,
|
||||
json={"parsed_rules": {
|
||||
"forbidden_words": ["DB专属词"],
|
||||
"restricted_words": [],
|
||||
"duration": None,
|
||||
"content_requirements": [],
|
||||
"other_rules": [],
|
||||
}},
|
||||
)
|
||||
|
||||
# selling_points 同时包含 DB 违禁词和硬编码违禁词
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers=headers,
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "douyin",
|
||||
"brief_rules": {
|
||||
"selling_points": ["这是DB专属词内容", "最好的选择"],
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
# 应同时检出 DB 违禁词和硬编码违禁词
|
||||
suggestions = [c["suggestion"] for c in data["conflicts"]]
|
||||
assert any("DB专属词" in s for s in suggestions)
|
||||
assert any("最好" in s for s in suggestions)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unknown_platform_returns_empty(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""未知平台返回空冲突(无硬编码规则,无 DB 规则)"""
|
||||
response = await client.post(
|
||||
"/api/v1/rules/validate",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"brand_id": brand_id,
|
||||
"platform": "unknown_platform",
|
||||
"brief_rules": {
|
||||
"selling_points": ["最好的产品"],
|
||||
}
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
assert data["conflicts"] == []
|
||||
|
||||
|
||||
# ==================== 品牌方平台规则(文档上传 + AI 解析) ====================
|
||||
|
||||
# Mock AI 解析返回的规则数据
|
||||
MOCK_PARSED_RULES = {
|
||||
"forbidden_words": ["绝对有效", "最强", "第一"],
|
||||
"restricted_words": [
|
||||
{"word": "推荐", "condition": "不能用于医疗产品", "suggestion": "建议改为'供参考'"}
|
||||
],
|
||||
"duration": {"min_seconds": 7, "max_seconds": 60},
|
||||
"content_requirements": ["必须展示产品正面", "需口播品牌名"],
|
||||
"other_rules": [
|
||||
{"rule": "字幕要求", "description": "视频必须添加中文字幕"}
|
||||
],
|
||||
}
|
||||
|
||||
MOCK_AI_JSON_RESPONSE = json.dumps(MOCK_PARSED_RULES, ensure_ascii=False)
|
||||
|
||||
|
||||
def _mock_ai_client_for_parse():
|
||||
"""创建用于文档解析的 mock AI 客户端"""
|
||||
client = MagicMock()
|
||||
client.chat_completion = AsyncMock(return_value=MagicMock(
|
||||
content=MOCK_AI_JSON_RESPONSE,
|
||||
))
|
||||
client.close = AsyncMock()
|
||||
return client
|
||||
|
||||
|
||||
async def _create_platform_rule(
|
||||
client: AsyncClient,
|
||||
tenant_id: str,
|
||||
brand_id: str,
|
||||
platform: str = "douyin",
|
||||
document_name: str = "规则文档.pdf",
|
||||
) -> dict:
|
||||
"""辅助函数:创建一条 draft 平台规则"""
|
||||
with patch(
|
||||
"app.api.rules.DocumentParser.download_and_parse",
|
||||
new_callable=AsyncMock,
|
||||
return_value="这是平台规则文档内容...",
|
||||
), patch(
|
||||
"app.api.rules.AIServiceFactory.get_client",
|
||||
new_callable=AsyncMock,
|
||||
return_value=_mock_ai_client_for_parse(),
|
||||
), patch(
|
||||
"app.api.rules.AIServiceFactory.get_config",
|
||||
new_callable=AsyncMock,
|
||||
return_value=MagicMock(models={"text": "gpt-4o"}),
|
||||
):
|
||||
resp = await client.post(
|
||||
"/api/v1/rules/platform-rules/parse",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"document_url": "https://tos.example.com/rules.pdf",
|
||||
"document_name": document_name,
|
||||
"platform": platform,
|
||||
"brand_id": brand_id,
|
||||
},
|
||||
)
|
||||
return resp
|
||||
|
||||
|
||||
class TestBrandPlatformRuleParse:
|
||||
"""品牌方平台规则 — 上传文档 + AI 解析"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_returns_201_draft(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""上传文档解析返回 201,状态为 draft"""
|
||||
resp = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
assert resp.status_code == 201
|
||||
|
||||
data = resp.json()
|
||||
assert data["status"] == "draft"
|
||||
assert data["platform"] == "douyin"
|
||||
assert data["brand_id"] == brand_id
|
||||
assert data["id"].startswith("pr-")
|
||||
assert data["document_name"] == "规则文档.pdf"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_returns_parsed_rules(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""解析后返回结构化规则"""
|
||||
resp = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
data = resp.json()
|
||||
|
||||
rules = data["parsed_rules"]
|
||||
assert "forbidden_words" in rules
|
||||
assert "restricted_words" in rules
|
||||
assert "duration" in rules
|
||||
assert "content_requirements" in rules
|
||||
assert "other_rules" in rules
|
||||
assert len(rules["forbidden_words"]) == 3
|
||||
assert "绝对有效" in rules["forbidden_words"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_empty_document_returns_400(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""空文档返回 400"""
|
||||
with patch(
|
||||
"app.api.rules.DocumentParser.download_and_parse",
|
||||
new_callable=AsyncMock,
|
||||
return_value=" ",
|
||||
):
|
||||
resp = await client.post(
|
||||
"/api/v1/rules/platform-rules/parse",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"document_url": "https://tos.example.com/empty.pdf",
|
||||
"document_name": "empty.pdf",
|
||||
"platform": "douyin",
|
||||
"brand_id": brand_id,
|
||||
},
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert "内容为空" in resp.json()["detail"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_unsupported_format_returns_400(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""不支持的文件格式返回 400"""
|
||||
with patch(
|
||||
"app.api.rules.DocumentParser.download_and_parse",
|
||||
new_callable=AsyncMock,
|
||||
side_effect=ValueError("不支持的文件格式: zip"),
|
||||
):
|
||||
resp = await client.post(
|
||||
"/api/v1/rules/platform-rules/parse",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"document_url": "https://tos.example.com/file.zip",
|
||||
"document_name": "file.zip",
|
||||
"platform": "douyin",
|
||||
"brand_id": brand_id,
|
||||
},
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_ai_failure_returns_empty_rules(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""AI 解析失败时返回空规则结构(降级处理)"""
|
||||
with patch(
|
||||
"app.api.rules.DocumentParser.download_and_parse",
|
||||
new_callable=AsyncMock,
|
||||
return_value="文档内容...",
|
||||
), patch(
|
||||
"app.api.rules.AIServiceFactory.get_client",
|
||||
new_callable=AsyncMock,
|
||||
return_value=None,
|
||||
):
|
||||
resp = await client.post(
|
||||
"/api/v1/rules/platform-rules/parse",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"document_url": "https://tos.example.com/rules.pdf",
|
||||
"document_name": "rules.pdf",
|
||||
"platform": "douyin",
|
||||
"brand_id": brand_id,
|
||||
},
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
rules = resp.json()["parsed_rules"]
|
||||
assert rules["forbidden_words"] == []
|
||||
assert rules["content_requirements"] == []
|
||||
assert rules["duration"] is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parse_multiple_platforms(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""同一品牌方可以上传不同平台的规则"""
|
||||
r1 = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
r2 = await _create_platform_rule(client, tenant_id, brand_id, platform="xiaohongshu")
|
||||
|
||||
assert r1.status_code == 201
|
||||
assert r2.status_code == 201
|
||||
assert r1.json()["platform"] == "douyin"
|
||||
assert r2.json()["platform"] == "xiaohongshu"
|
||||
|
||||
|
||||
class TestBrandPlatformRuleConfirm:
|
||||
"""品牌方平台规则 — 确认/生效"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_confirm_sets_active(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""确认规则后状态变为 active"""
|
||||
# 先创建 draft
|
||||
create_resp = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = create_resp.json()["id"]
|
||||
|
||||
# 确认
|
||||
confirm_resp = await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={
|
||||
"parsed_rules": MOCK_PARSED_RULES,
|
||||
},
|
||||
)
|
||||
assert confirm_resp.status_code == 200
|
||||
data = confirm_resp.json()
|
||||
assert data["status"] == "active"
|
||||
assert data["id"] == rule_id
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_confirm_with_edited_rules(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""品牌方修改后确认"""
|
||||
create_resp = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = create_resp.json()["id"]
|
||||
|
||||
edited_rules = {
|
||||
"forbidden_words": ["绝对有效", "最强", "第一", "新增的违禁词"],
|
||||
"restricted_words": [],
|
||||
"duration": {"min_seconds": 10, "max_seconds": 120},
|
||||
"content_requirements": ["必须展示产品"],
|
||||
"other_rules": [],
|
||||
}
|
||||
|
||||
confirm_resp = await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={"parsed_rules": edited_rules},
|
||||
)
|
||||
assert confirm_resp.status_code == 200
|
||||
data = confirm_resp.json()
|
||||
assert "新增的违禁词" in data["parsed_rules"]["forbidden_words"]
|
||||
assert data["parsed_rules"]["duration"]["min_seconds"] == 10
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_confirm_deactivates_old_rule(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""确认新规则后旧的 active 规则变 inactive"""
|
||||
# 创建并确认第一条规则
|
||||
r1 = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
rule1_id = r1.json()["id"]
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule1_id}/confirm",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
|
||||
# 创建并确认第二条规则(同品牌同平台)
|
||||
r2 = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
rule2_id = r2.json()["id"]
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule2_id}/confirm",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
|
||||
# 查询所有规则 — rule1 应该变 inactive,rule2 应该 active
|
||||
list_resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}&platform=douyin",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
rules = list_resp.json()["items"]
|
||||
rule1 = next(r for r in rules if r["id"] == rule1_id)
|
||||
rule2 = next(r for r in rules if r["id"] == rule2_id)
|
||||
|
||||
assert rule1["status"] == "inactive"
|
||||
assert rule2["status"] == "active"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_confirm_nonexistent_rule_returns_404(self, client: AsyncClient, tenant_id: str):
|
||||
"""确认不存在的规则返回 404"""
|
||||
resp = await client.put(
|
||||
"/api/v1/rules/platform-rules/pr-nonexist/confirm",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_confirm_cross_tenant_returns_404(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""不同租户确认规则返回 404(租户隔离)"""
|
||||
create_resp = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = create_resp.json()["id"]
|
||||
|
||||
resp = await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers={"X-Tenant-ID": "other-tenant-xxx"},
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
class TestBrandPlatformRuleList:
|
||||
"""品牌方平台规则 — 列表查询"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_empty_returns_200(self, client: AsyncClient, tenant_id: str):
|
||||
"""没有规则时返回空列表"""
|
||||
resp = await client.get(
|
||||
"/api/v1/rules/platform-rules",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["items"] == []
|
||||
assert data["total"] == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_returns_created_rules(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""创建规则后列表包含该规则"""
|
||||
await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
await _create_platform_rule(client, tenant_id, brand_id, platform="xiaohongshu")
|
||||
|
||||
resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
data = resp.json()
|
||||
assert data["total"] == 2
|
||||
platforms = {r["platform"] for r in data["items"]}
|
||||
assert platforms == {"douyin", "xiaohongshu"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_filter_by_platform(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""按平台筛选"""
|
||||
await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
await _create_platform_rule(client, tenant_id, brand_id, platform="xiaohongshu")
|
||||
|
||||
resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}&platform=douyin",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
data = resp.json()
|
||||
assert data["total"] == 1
|
||||
assert data["items"][0]["platform"] == "douyin"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_filter_by_status(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""按状态筛选"""
|
||||
r = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = r.json()["id"]
|
||||
|
||||
# 确认一条
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}/confirm",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
|
||||
# 再创建一条 draft
|
||||
await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
|
||||
# 只查 active
|
||||
resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}&status=active",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
active_rules = resp.json()["items"]
|
||||
assert all(r["status"] == "active" for r in active_rules)
|
||||
|
||||
# 只查 draft
|
||||
resp2 = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}&status=draft",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
draft_rules = resp2.json()["items"]
|
||||
assert all(r["status"] == "draft" for r in draft_rules)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_tenant_isolation(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""租户隔离:不同租户看不到彼此的规则"""
|
||||
await _create_platform_rule(client, tenant_id, brand_id)
|
||||
|
||||
resp = await client.get(
|
||||
"/api/v1/rules/platform-rules",
|
||||
headers={"X-Tenant-ID": "another-tenant-yyy"},
|
||||
)
|
||||
assert resp.json()["total"] == 0
|
||||
|
||||
|
||||
class TestBrandPlatformRuleDelete:
|
||||
"""品牌方平台规则 — 删除"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_returns_204(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""删除规则返回 204"""
|
||||
r = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = r.json()["id"]
|
||||
|
||||
resp = await client.delete(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
assert resp.status_code == 204
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_actually_removes(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""删除后列表中不再包含该规则"""
|
||||
r = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = r.json()["id"]
|
||||
|
||||
await client.delete(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
|
||||
resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
ids = [r["id"] for r in resp.json()["items"]]
|
||||
assert rule_id not in ids
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_nonexistent_returns_404(self, client: AsyncClient, tenant_id: str):
|
||||
"""删除不存在的规则返回 404"""
|
||||
resp = await client.delete(
|
||||
"/api/v1/rules/platform-rules/pr-nonexist",
|
||||
headers={"X-Tenant-ID": tenant_id},
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_cross_tenant_returns_404(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""不同租户删除规则返回 404(租户隔离)"""
|
||||
r = await _create_platform_rule(client, tenant_id, brand_id)
|
||||
rule_id = r.json()["id"]
|
||||
|
||||
resp = await client.delete(
|
||||
f"/api/v1/rules/platform-rules/{rule_id}",
|
||||
headers={"X-Tenant-ID": "other-tenant-zzz"},
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
class TestBrandPlatformRuleLifecycle:
|
||||
"""品牌方平台规则 — 完整生命周期"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_lifecycle(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
"""完整流程: 上传解析 → 确认生效 → 重新上传 → 旧规则停用"""
|
||||
headers = {"X-Tenant-ID": tenant_id}
|
||||
|
||||
# 1. 上传并解析
|
||||
r1 = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
assert r1.status_code == 201
|
||||
rule1_id = r1.json()["id"]
|
||||
assert r1.json()["status"] == "draft"
|
||||
|
||||
# 2. 确认生效
|
||||
confirm_resp = await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule1_id}/confirm",
|
||||
headers=headers,
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
assert confirm_resp.json()["status"] == "active"
|
||||
|
||||
# 3. 重新上传新规则
|
||||
r2 = await _create_platform_rule(client, tenant_id, brand_id, platform="douyin")
|
||||
rule2_id = r2.json()["id"]
|
||||
assert r2.json()["status"] == "draft"
|
||||
|
||||
# 4. 确认新规则
|
||||
await client.put(
|
||||
f"/api/v1/rules/platform-rules/{rule2_id}/confirm",
|
||||
headers=headers,
|
||||
json={"parsed_rules": MOCK_PARSED_RULES},
|
||||
)
|
||||
|
||||
# 5. 验证旧规则自动停用
|
||||
list_resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}&platform=douyin",
|
||||
headers=headers,
|
||||
)
|
||||
rules = list_resp.json()["items"]
|
||||
rule1 = next(r for r in rules if r["id"] == rule1_id)
|
||||
rule2 = next(r for r in rules if r["id"] == rule2_id)
|
||||
assert rule1["status"] == "inactive"
|
||||
assert rule2["status"] == "active"
|
||||
|
||||
# 6. 删除旧规则
|
||||
del_resp = await client.delete(
|
||||
f"/api/v1/rules/platform-rules/{rule1_id}",
|
||||
headers=headers,
|
||||
)
|
||||
assert del_resp.status_code == 204
|
||||
|
||||
# 7. 验证只剩新规则
|
||||
final_resp = await client.get(
|
||||
f"/api/v1/rules/platform-rules?brand_id={brand_id}&platform=douyin",
|
||||
headers=headers,
|
||||
)
|
||||
assert final_resp.json()["total"] == 1
|
||||
assert final_resp.json()["items"][0]["id"] == rule2_id
|
||||
|
||||
@@ -215,7 +215,11 @@ class TestSellingPointCheck:
|
||||
"content": "这个产品很好用",
|
||||
"platform": "douyin",
|
||||
"brand_id": brand_id,
|
||||
"required_points": ["功效说明", "使用方法", "品牌名称"],
|
||||
"selling_points": [
|
||||
{"content": "功效说明", "priority": "core"},
|
||||
{"content": "使用方法", "priority": "core"},
|
||||
{"content": "品牌名称", "priority": "recommended"},
|
||||
],
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
@@ -223,6 +227,9 @@ class TestSellingPointCheck:
|
||||
|
||||
assert parsed.missing_points is not None
|
||||
assert isinstance(parsed.missing_points, list)
|
||||
# 验证多维度评分存在
|
||||
assert parsed.dimensions is not None
|
||||
assert parsed.dimensions.brief_match is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_all_points_covered(self, client: AsyncClient, tenant_id: str, brand_id: str):
|
||||
@@ -234,7 +241,11 @@ class TestSellingPointCheck:
|
||||
"content": "品牌A的护肤精华,每天早晚各用一次,可以让肌肤更水润",
|
||||
"platform": "douyin",
|
||||
"brand_id": brand_id,
|
||||
"required_points": ["品牌名称", "使用方法", "功效说明"],
|
||||
"selling_points": [
|
||||
{"content": "护肤精华", "priority": "core"},
|
||||
{"content": "早晚各用一次", "priority": "core"},
|
||||
{"content": "肌肤更水润", "priority": "recommended"},
|
||||
],
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
|
||||
@@ -149,7 +149,7 @@ function mapTaskToAppeal(task: TaskResponse): Appeal {
|
||||
taskTitle: task.name,
|
||||
creatorId: task.creator.id,
|
||||
creatorName: task.creator.name,
|
||||
platform: 'douyin', // Backend does not expose platform on task; default for now
|
||||
platform: task.project?.platform || 'douyin',
|
||||
type,
|
||||
contentType,
|
||||
reason: task.appeal_reason || '申诉',
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -141,7 +141,7 @@ export default function AgencyBriefsPage() {
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
brandName: project.brand_name || '未知品牌',
|
||||
platform: 'douyin', // 后端暂无 platform 字段,默认值
|
||||
platform: project.platform || 'douyin',
|
||||
status: hasBrief ? 'configured' : 'pending',
|
||||
uploadedAt: project.created_at.split('T')[0],
|
||||
configuredAt: hasBrief ? brief.updated_at.split('T')[0] : null,
|
||||
@@ -156,7 +156,7 @@ export default function AgencyBriefsPage() {
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
brandName: project.brand_name || '未知品牌',
|
||||
platform: 'douyin',
|
||||
platform: project.platform || 'douyin',
|
||||
status: 'pending',
|
||||
uploadedAt: project.created_at.split('T')[0],
|
||||
configuredAt: null,
|
||||
@@ -204,8 +204,8 @@ export default function AgencyBriefsPage() {
|
||||
{/* 页面标题 */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-text-primary">Brief 配置</h1>
|
||||
<p className="text-sm text-text-secondary mt-1">配置项目 Brief,设置审核规则</p>
|
||||
<h1 className="text-2xl font-bold text-text-primary">任务配置</h1>
|
||||
<p className="text-sm text-text-secondary mt-1">配置项目 Brief,分配达人任务</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<span className="px-3 py-1.5 bg-yellow-500/20 text-yellow-400 rounded-lg font-medium">
|
||||
|
||||
@@ -245,7 +245,7 @@ export default function AgencyCreatorsPage() {
|
||||
id: task.id,
|
||||
name: task.name,
|
||||
projectName: task.project?.name || '-',
|
||||
platform: 'douyin', // 后端暂未返回平台信息,默认
|
||||
platform: task.project?.platform || 'douyin',
|
||||
stage: mapBackendStage(task.stage),
|
||||
appealRemaining: task.appeal_count,
|
||||
appealUsed: task.is_appeal ? 1 : 0,
|
||||
@@ -477,15 +477,36 @@ export default function AgencyCreatorsPage() {
|
||||
setOpenMenuId(null)
|
||||
}
|
||||
|
||||
// 确认分配项目
|
||||
const handleConfirmAssign = () => {
|
||||
// 确认分配项目(创建任务)
|
||||
const handleConfirmAssign = async () => {
|
||||
const projectList = USE_MOCK ? mockProjects : projects
|
||||
if (assignModal.creator && selectedProject) {
|
||||
const project = projectList.find(p => p.id === selectedProject)
|
||||
if (!assignModal.creator || !selectedProject) return
|
||||
|
||||
const project = projectList.find(p => p.id === selectedProject)
|
||||
|
||||
if (USE_MOCK) {
|
||||
toast.success(`已将达人「${assignModal.creator.name}」分配到项目「${project?.name}」`)
|
||||
setAssignModal({ open: false, creator: null })
|
||||
setSelectedProject('')
|
||||
return
|
||||
}
|
||||
|
||||
setSubmitting(true)
|
||||
try {
|
||||
await api.createTask({
|
||||
project_id: selectedProject,
|
||||
creator_id: assignModal.creator.creatorId,
|
||||
})
|
||||
toast.success(`已将达人「${assignModal.creator.name}」分配到项目「${project?.name}」`)
|
||||
setAssignModal({ open: false, creator: null })
|
||||
setSelectedProject('')
|
||||
await fetchData() // 刷新列表
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : '分配失败'
|
||||
toast.error(message)
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
}
|
||||
setAssignModal({ open: false, creator: null })
|
||||
setSelectedProject('')
|
||||
}
|
||||
|
||||
// 骨架屏
|
||||
@@ -761,43 +782,45 @@ export default function AgencyCreatorsPage() {
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-text-tertiary">{creator.joinedAt}</td>
|
||||
<td className="px-6 py-4">
|
||||
<div className="relative">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setOpenMenuId(openMenuId === creator.id ? null : creator.id)}
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenAssign(creator)}
|
||||
className="px-3 py-1.5 text-xs font-medium text-accent-indigo bg-accent-indigo/10 hover:bg-accent-indigo/20 rounded-lg transition-colors flex items-center gap-1.5"
|
||||
>
|
||||
<MoreVertical size={16} />
|
||||
</Button>
|
||||
{/* 下拉菜单 */}
|
||||
{openMenuId === creator.id && (
|
||||
<div className="absolute right-0 top-full mt-1 w-40 bg-bg-card rounded-xl shadow-lg border border-border-subtle z-10 overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenRemark(creator)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-text-primary hover:bg-bg-elevated flex items-center gap-2"
|
||||
>
|
||||
<MessageSquareText size={14} className="text-text-secondary" />
|
||||
{creator.remark ? '编辑备注' : '添加备注'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenAssign(creator)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-text-primary hover:bg-bg-elevated flex items-center gap-2"
|
||||
>
|
||||
<FolderPlus size={14} className="text-text-secondary" />
|
||||
分配到项目
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenDelete(creator)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-accent-coral hover:bg-accent-coral/10 flex items-center gap-2"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
移除达人
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<FolderPlus size={13} />
|
||||
分配项目
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenDelete(creator)}
|
||||
className="p-1.5 text-text-tertiary hover:text-accent-coral hover:bg-accent-coral/10 rounded-lg transition-colors"
|
||||
title="移除达人"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
<div className="relative">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpenMenuId(openMenuId === creator.id ? null : creator.id)}
|
||||
className="p-1.5 text-text-tertiary hover:text-text-primary hover:bg-bg-elevated rounded-lg transition-colors"
|
||||
title="更多操作"
|
||||
>
|
||||
<MoreVertical size={14} />
|
||||
</button>
|
||||
{openMenuId === creator.id && (
|
||||
<div className="absolute right-0 top-full mt-1 w-36 bg-bg-card rounded-xl shadow-lg border border-border-subtle z-10 overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenRemark(creator)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-text-primary hover:bg-bg-elevated flex items-center gap-2"
|
||||
>
|
||||
<MessageSquareText size={14} className="text-text-secondary" />
|
||||
{creator.remark ? '编辑备注' : '添加备注'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -1042,8 +1065,8 @@ export default function AgencyCreatorsPage() {
|
||||
<Button variant="ghost" onClick={() => { setAssignModal({ open: false, creator: null }); setSelectedProject(''); }}>
|
||||
取消
|
||||
</Button>
|
||||
<Button onClick={handleConfirmAssign} disabled={!selectedProject}>
|
||||
<FolderPlus size={16} />
|
||||
<Button onClick={handleConfirmAssign} disabled={!selectedProject || submitting}>
|
||||
{submitting ? <Loader2 size={16} className="animate-spin" /> : <FolderPlus size={16} />}
|
||||
确认分配
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -45,6 +45,11 @@ type MessageType =
|
||||
| 'task_deadline' // 任务截止提醒
|
||||
| 'brand_brief_updated' // 品牌方更新了Brief
|
||||
| 'system_notice' // 系统通知
|
||||
| 'new_task' // 新任务
|
||||
| 'pass' // 审核通过
|
||||
| 'reject' // 审核驳回
|
||||
| 'force_pass' // 强制通过
|
||||
| 'approve' // 审核批准
|
||||
|
||||
interface Message {
|
||||
id: string
|
||||
@@ -299,19 +304,31 @@ export default function AgencyMessagesPage() {
|
||||
}
|
||||
try {
|
||||
const res = await api.getMessages({ page: 1, page_size: 50 })
|
||||
const mapped: Message[] = res.items.map(item => ({
|
||||
id: item.id,
|
||||
type: (item.type || 'system_notice') as MessageType,
|
||||
title: item.title,
|
||||
content: item.content,
|
||||
time: item.created_at ? new Date(item.created_at).toLocaleString('zh-CN', { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' }) : '',
|
||||
read: item.is_read,
|
||||
icon: Bell,
|
||||
iconColor: 'text-text-secondary',
|
||||
bgColor: 'bg-bg-elevated',
|
||||
taskId: item.related_task_id || undefined,
|
||||
projectId: item.related_project_id || undefined,
|
||||
}))
|
||||
const typeIconMap: Record<string, { icon: typeof Bell; iconColor: string; bgColor: string }> = {
|
||||
new_task: { icon: FileText, iconColor: 'text-accent-indigo', bgColor: 'bg-accent-indigo/20' },
|
||||
pass: { icon: CheckCircle, iconColor: 'text-accent-green', bgColor: 'bg-accent-green/20' },
|
||||
approve: { icon: CheckCircle, iconColor: 'text-accent-green', bgColor: 'bg-accent-green/20' },
|
||||
reject: { icon: XCircle, iconColor: 'text-accent-coral', bgColor: 'bg-accent-coral/20' },
|
||||
force_pass: { icon: CheckCircle, iconColor: 'text-accent-amber', bgColor: 'bg-accent-amber/20' },
|
||||
system_notice: { icon: Bell, iconColor: 'text-text-secondary', bgColor: 'bg-bg-elevated' },
|
||||
}
|
||||
const defaultIcon = { icon: Bell, iconColor: 'text-text-secondary', bgColor: 'bg-bg-elevated' }
|
||||
const mapped: Message[] = res.items.map(item => {
|
||||
const iconCfg = typeIconMap[item.type] || defaultIcon
|
||||
return {
|
||||
id: item.id,
|
||||
type: (item.type || 'system_notice') as MessageType,
|
||||
title: item.title,
|
||||
content: item.content,
|
||||
time: item.created_at ? new Date(item.created_at).toLocaleString('zh-CN', { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' }) : '',
|
||||
read: item.is_read,
|
||||
icon: iconCfg.icon,
|
||||
iconColor: iconCfg.iconColor,
|
||||
bgColor: iconCfg.bgColor,
|
||||
taskId: item.related_task_id || undefined,
|
||||
projectId: item.related_project_id || undefined,
|
||||
}
|
||||
})
|
||||
setMessages(mapped)
|
||||
} catch {
|
||||
// 加载失败保持 mock 数据
|
||||
|
||||
@@ -96,8 +96,12 @@ function getTaskUrgencyLevel(task: TaskResponse): string {
|
||||
}
|
||||
|
||||
function getTaskUrgencyTitle(task: TaskResponse): string {
|
||||
const type = task.stage.includes('video') ? '视频' : '脚本'
|
||||
return `${task.creator.name}${type} - ${task.name}`
|
||||
return `${task.project.name} · ${task.name}`
|
||||
}
|
||||
|
||||
function getPlatformLabel(platform?: string | null): string {
|
||||
const map: Record<string, string> = { douyin: '抖音', xiaohongshu: '小红书', bilibili: 'B站', kuaishou: '快手' }
|
||||
return platform ? (map[platform] || platform) : ''
|
||||
}
|
||||
|
||||
function getTaskTimeAgo(dateStr: string): string {
|
||||
@@ -182,13 +186,19 @@ export default function AgencyDashboard() {
|
||||
if (loading || !stats) return <DashboardSkeleton />
|
||||
|
||||
// Build urgent todos from pending tasks (top 3)
|
||||
const urgentTodos = pendingTasks.slice(0, 3).map(task => ({
|
||||
id: task.id,
|
||||
title: getTaskUrgencyTitle(task),
|
||||
description: task.project.name,
|
||||
time: getTaskTimeAgo(task.updated_at),
|
||||
level: getTaskUrgencyLevel(task),
|
||||
}))
|
||||
const urgentTodos = pendingTasks.slice(0, 3).map(task => {
|
||||
const type = task.stage.includes('video') ? '视频' : '脚本'
|
||||
const platformLabel = getPlatformLabel(task.project.platform)
|
||||
const brandLabel = task.project.brand_name || ''
|
||||
const desc = [task.creator.name, brandLabel, platformLabel, type].filter(Boolean).join(' · ')
|
||||
return {
|
||||
id: task.id,
|
||||
title: getTaskUrgencyTitle(task),
|
||||
description: desc,
|
||||
time: getTaskTimeAgo(task.updated_at),
|
||||
level: getTaskUrgencyLevel(task),
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-6 min-h-0">
|
||||
@@ -316,6 +326,9 @@ export default function AgencyDashboard() {
|
||||
{project.brand_name && (
|
||||
<span className="text-xs text-text-tertiary">({project.brand_name})</span>
|
||||
)}
|
||||
{project.platform && (
|
||||
<span className="text-xs px-1.5 py-0.5 rounded bg-accent-indigo/10 text-accent-indigo">{getPlatformLabel(project.platform)}</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-sm text-text-secondary">
|
||||
{project.task_count} 个任务
|
||||
@@ -356,6 +369,7 @@ export default function AgencyDashboard() {
|
||||
<th className="pb-3 font-medium">类型</th>
|
||||
<th className="pb-3 font-medium">达人</th>
|
||||
<th className="pb-3 font-medium">品牌</th>
|
||||
<th className="pb-3 font-medium">平台</th>
|
||||
<th className="pb-3 font-medium">AI评分</th>
|
||||
<th className="pb-3 font-medium">提交时间</th>
|
||||
<th className="pb-3 font-medium">操作</th>
|
||||
@@ -369,7 +383,9 @@ export default function AgencyDashboard() {
|
||||
<tr key={task.id} className="border-b border-border-subtle last:border-0 hover:bg-bg-elevated">
|
||||
<td className="py-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="font-medium text-text-primary">{task.name}</div>
|
||||
<div>
|
||||
<div className="font-medium text-text-primary">{task.project.name} · {task.name}</div>
|
||||
</div>
|
||||
{task.is_appeal && (
|
||||
<span className="px-1.5 py-0.5 text-xs bg-accent-amber/20 text-accent-amber rounded">
|
||||
申诉
|
||||
@@ -385,7 +401,8 @@ export default function AgencyDashboard() {
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 text-text-secondary">{task.creator.name}</td>
|
||||
<td className="py-4 text-text-secondary">{task.project.brand_name || task.project.name}</td>
|
||||
<td className="py-4 text-text-secondary">{task.project.brand_name || '-'}</td>
|
||||
<td className="py-4 text-text-secondary">{getPlatformLabel(task.project.platform) || '-'}</td>
|
||||
<td className="py-4">
|
||||
{aiScore != null ? (
|
||||
<span className={`font-medium ${
|
||||
@@ -409,7 +426,7 @@ export default function AgencyDashboard() {
|
||||
)
|
||||
}) : (
|
||||
<tr>
|
||||
<td colSpan={7} className="py-8 text-center text-text-tertiary">暂无待审核任务</td>
|
||||
<td colSpan={8} className="py-8 text-center text-text-tertiary">暂无待审核任务</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
|
||||
@@ -1,577 +1,60 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useRouter, useParams } from 'next/navigation'
|
||||
import { useToast } from '@/components/ui/Toast'
|
||||
import { ArrowLeft, Play, Pause, AlertTriangle, Shield, Radio, Loader2 } from 'lucide-react'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card'
|
||||
import { Button } from '@/components/ui/Button'
|
||||
import { SuccessTag, WarningTag, ErrorTag } from '@/components/ui/Tag'
|
||||
import { Modal, ConfirmModal } from '@/components/ui/Modal'
|
||||
import { ReviewSteps, getAgencyReviewSteps } from '@/components/ui/ReviewSteps'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
import type { TaskResponse, AIReviewResult } from '@/types/task'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
|
||||
// ==================== Mock 数据 ====================
|
||||
const mockTask: TaskResponse = {
|
||||
id: 'task-001',
|
||||
name: '夏日护肤推广',
|
||||
sequence: 1,
|
||||
stage: 'script_agency_review',
|
||||
project: { id: 'proj-001', name: 'XX品牌618推广', brand_name: 'XX护肤品牌' },
|
||||
agency: { id: 'ag-001', name: '优创代理' },
|
||||
creator: { id: 'cr-001', name: '小美护肤' },
|
||||
script_ai_score: 85,
|
||||
script_ai_result: {
|
||||
score: 85,
|
||||
violations: [
|
||||
{
|
||||
type: '违禁词',
|
||||
content: '效果最好',
|
||||
severity: 'high',
|
||||
suggestion: '建议替换为"效果显著"',
|
||||
timestamp: 15.5,
|
||||
source: 'speech',
|
||||
},
|
||||
{
|
||||
type: '竞品露出',
|
||||
content: '疑似竞品Logo',
|
||||
severity: 'high',
|
||||
suggestion: '需人工确认是否为竞品露出',
|
||||
timestamp: 42.0,
|
||||
source: 'visual',
|
||||
},
|
||||
],
|
||||
soft_warnings: [
|
||||
{ type: '油腻预警', content: '达人表情过于夸张,建议检查', suggestion: '软性风险仅作提示' },
|
||||
],
|
||||
summary: '视频整体合规,发现2处硬性问题和1处舆情提示需人工确认',
|
||||
},
|
||||
video_ai_score: 85,
|
||||
video_ai_result: {
|
||||
score: 85,
|
||||
violations: [
|
||||
{
|
||||
type: '违禁词',
|
||||
content: '效果最好',
|
||||
severity: 'high',
|
||||
suggestion: '建议替换为"效果显著"',
|
||||
timestamp: 15.5,
|
||||
source: 'speech',
|
||||
},
|
||||
{
|
||||
type: '竞品露出',
|
||||
content: '疑似竞品Logo',
|
||||
severity: 'high',
|
||||
suggestion: '需人工确认是否为竞品露出',
|
||||
timestamp: 42.0,
|
||||
source: 'visual',
|
||||
},
|
||||
],
|
||||
soft_warnings: [
|
||||
{ type: '油腻预警', content: '达人表情过于夸张,建议检查', suggestion: '软性风险仅作提示' },
|
||||
],
|
||||
summary: '视频整体合规,发现2处硬性问题和1处舆情提示需人工确认',
|
||||
},
|
||||
appeal_count: 0,
|
||||
is_appeal: false,
|
||||
created_at: '2026-02-03T10:30:00Z',
|
||||
updated_at: '2026-02-03T10:35:00Z',
|
||||
}
|
||||
|
||||
// ==================== 工具函数 ====================
|
||||
|
||||
function getReviewStepStatus(task: TaskResponse): string {
|
||||
if (task.stage.includes('agency_review')) return 'agent_reviewing'
|
||||
if (task.stage.includes('brand_review')) return 'brand_reviewing'
|
||||
if (task.stage === 'completed') return 'completed'
|
||||
return 'agent_reviewing'
|
||||
}
|
||||
|
||||
function formatTimestamp(seconds: number): string {
|
||||
const mins = Math.floor(seconds / 60)
|
||||
const secs = Math.floor(seconds % 60)
|
||||
return `${mins}:${secs.toString().padStart(2, '0')}`
|
||||
}
|
||||
|
||||
// ==================== 子组件 ====================
|
||||
|
||||
function ReviewProgressBar({ taskStatus }: { taskStatus: string }) {
|
||||
const steps = getAgencyReviewSteps(taskStatus)
|
||||
const currentStep = steps.find(s => s.status === 'current')
|
||||
|
||||
return (
|
||||
<Card className="mb-6">
|
||||
<CardContent className="py-4">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<span className="text-sm font-medium text-text-primary">审核流程</span>
|
||||
<span className="text-sm text-accent-indigo font-medium">
|
||||
当前:{currentStep?.label || '代理商审核'}
|
||||
</span>
|
||||
</div>
|
||||
<ReviewSteps steps={steps} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function RiskLevelTag({ level }: { level: string }) {
|
||||
if (level === 'high') return <ErrorTag>高风险</ErrorTag>
|
||||
if (level === 'medium') return <WarningTag>中风险</WarningTag>
|
||||
return <SuccessTag>低风险</SuccessTag>
|
||||
}
|
||||
|
||||
function ReviewSkeleton() {
|
||||
return (
|
||||
<div className="space-y-4 animate-pulse">
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="w-10 h-10 bg-bg-elevated rounded-full" />
|
||||
<div className="space-y-2">
|
||||
<div className="h-6 w-48 bg-bg-elevated rounded" />
|
||||
<div className="h-4 w-64 bg-bg-elevated rounded" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="h-16 bg-bg-elevated rounded-xl" />
|
||||
<div className="grid grid-cols-1 lg:grid-cols-5 gap-6">
|
||||
<div className="lg:col-span-3 space-y-4">
|
||||
<div className="h-64 bg-bg-elevated rounded-xl" />
|
||||
<div className="h-20 bg-bg-elevated rounded-xl" />
|
||||
</div>
|
||||
<div className="lg:col-span-2 space-y-4">
|
||||
<div className="h-48 bg-bg-elevated rounded-xl" />
|
||||
<div className="h-32 bg-bg-elevated rounded-xl" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ==================== 主页面 ====================
|
||||
|
||||
export default function ReviewPage() {
|
||||
/**
|
||||
* Redirect page: detects task type (script/video) and redirects
|
||||
* to the appropriate review detail page.
|
||||
*/
|
||||
export default function ReviewRedirectPage() {
|
||||
const router = useRouter()
|
||||
const params = useParams()
|
||||
const toast = useToast()
|
||||
const taskId = params.id as string
|
||||
const { subscribe } = useSSE()
|
||||
const [error, setError] = useState('')
|
||||
|
||||
const [task, setTask] = useState<TaskResponse | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const [isPlaying, setIsPlaying] = useState(false)
|
||||
const [showApproveModal, setShowApproveModal] = useState(false)
|
||||
const [showRejectModal, setShowRejectModal] = useState(false)
|
||||
const [showForcePassModal, setShowForcePassModal] = useState(false)
|
||||
const [rejectReason, setRejectReason] = useState('')
|
||||
const [forcePassReason, setForcePassReason] = useState('')
|
||||
const [saveAsException, setSaveAsException] = useState(false)
|
||||
const [checkedViolations, setCheckedViolations] = useState<Record<string, boolean>>({})
|
||||
|
||||
const loadTask = useCallback(async () => {
|
||||
useEffect(() => {
|
||||
if (USE_MOCK) {
|
||||
setTask(mockTask)
|
||||
setLoading(false)
|
||||
router.replace(`/agency/review/script/${taskId}`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await api.getTask(taskId)
|
||||
setTask(data)
|
||||
} catch (err) {
|
||||
console.error('Failed to load task:', err)
|
||||
toast.error('加载任务失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [taskId, toast])
|
||||
|
||||
useEffect(() => {
|
||||
loadTask()
|
||||
}, [loadTask])
|
||||
|
||||
useEffect(() => {
|
||||
const unsub1 = subscribe('task_updated', (data: any) => {
|
||||
if (data?.task_id === taskId) loadTask()
|
||||
})
|
||||
const unsub2 = subscribe('review_completed', (data: any) => {
|
||||
if (data?.task_id === taskId) loadTask()
|
||||
})
|
||||
return () => { unsub1(); unsub2() }
|
||||
}, [subscribe, taskId, loadTask])
|
||||
|
||||
if (loading || !task) return <ReviewSkeleton />
|
||||
|
||||
// Determine if this is script or video review
|
||||
const isVideoReview = task.stage.includes('video')
|
||||
const aiResult: AIReviewResult | null | undefined = isVideoReview ? task.video_ai_result : task.script_ai_result
|
||||
const aiScore = isVideoReview ? task.video_ai_score : task.script_ai_score
|
||||
|
||||
const violations = aiResult?.violations || []
|
||||
const softWarnings = aiResult?.soft_warnings || []
|
||||
const aiSummary = aiResult?.summary || '暂无 AI 分析总结'
|
||||
|
||||
const handleApprove = async () => {
|
||||
setSubmitting(true)
|
||||
try {
|
||||
if (!USE_MOCK) {
|
||||
if (isVideoReview) {
|
||||
await api.reviewVideo(taskId, { action: 'pass' })
|
||||
} else {
|
||||
await api.reviewScript(taskId, { action: 'pass' })
|
||||
}
|
||||
async function redirect() {
|
||||
try {
|
||||
const task = await api.getTask(taskId)
|
||||
const isVideo = task.stage.includes('video')
|
||||
const path = isVideo
|
||||
? `/agency/review/video/${taskId}`
|
||||
: `/agency/review/script/${taskId}`
|
||||
router.replace(path)
|
||||
} catch {
|
||||
setError('加载任务失败,请返回重试')
|
||||
}
|
||||
toast.success('审核已通过')
|
||||
setShowApproveModal(false)
|
||||
router.push('/agency/review')
|
||||
} catch (err) {
|
||||
console.error('Failed to approve:', err)
|
||||
toast.error('操作失败,请重试')
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
}
|
||||
redirect()
|
||||
}, [taskId, router])
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center min-h-[50vh] gap-4">
|
||||
<p className="text-text-secondary">{error}</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => router.back()}
|
||||
className="text-accent-indigo hover:underline"
|
||||
>
|
||||
返回
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const handleReject = async () => {
|
||||
if (!rejectReason.trim()) {
|
||||
toast.error('请填写驳回原因')
|
||||
return
|
||||
}
|
||||
setSubmitting(true)
|
||||
try {
|
||||
if (!USE_MOCK) {
|
||||
if (isVideoReview) {
|
||||
await api.reviewVideo(taskId, { action: 'reject', comment: rejectReason })
|
||||
} else {
|
||||
await api.reviewScript(taskId, { action: 'reject', comment: rejectReason })
|
||||
}
|
||||
}
|
||||
toast.success('已驳回')
|
||||
setShowRejectModal(false)
|
||||
router.push('/agency/review')
|
||||
} catch (err) {
|
||||
console.error('Failed to reject:', err)
|
||||
toast.error('操作失败,请重试')
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleForcePass = async () => {
|
||||
if (!forcePassReason.trim()) {
|
||||
toast.error('请填写强制通过原因')
|
||||
return
|
||||
}
|
||||
setSubmitting(true)
|
||||
try {
|
||||
if (!USE_MOCK) {
|
||||
if (isVideoReview) {
|
||||
await api.reviewVideo(taskId, { action: 'force_pass', comment: forcePassReason })
|
||||
} else {
|
||||
await api.reviewScript(taskId, { action: 'force_pass', comment: forcePassReason })
|
||||
}
|
||||
}
|
||||
toast.success('已强制通过')
|
||||
setShowForcePassModal(false)
|
||||
router.push('/agency/review')
|
||||
} catch (err) {
|
||||
console.error('Failed to force pass:', err)
|
||||
toast.error('操作失败,请重试')
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
// 时间线标记
|
||||
const timelineMarkers = [
|
||||
...violations.filter(v => v.timestamp != null).map(v => ({ time: v.timestamp!, type: 'hard' as const })),
|
||||
].sort((a, b) => a.time - b.time)
|
||||
|
||||
const maxTime = Math.max(120, ...timelineMarkers.map(m => m.time + 10))
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* 顶部导航 */}
|
||||
<div className="flex items-center gap-4">
|
||||
<button type="button" onClick={() => router.back()} className="p-2 hover:bg-bg-elevated rounded-full">
|
||||
<ArrowLeft size={20} className="text-text-primary" />
|
||||
</button>
|
||||
<div className="flex-1">
|
||||
<h1 className="text-xl font-bold text-text-primary">{task.name}</h1>
|
||||
<p className="text-sm text-text-secondary">
|
||||
{task.creator.name} · {task.project.brand_name || task.project.name} · {isVideoReview ? '视频审核' : '脚本审核'}
|
||||
</p>
|
||||
</div>
|
||||
{task.is_appeal && (
|
||||
<span className="px-3 py-1 bg-accent-amber/20 text-accent-amber rounded-full text-sm font-medium">
|
||||
申诉重审
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 申诉理由 */}
|
||||
{task.is_appeal && task.appeal_reason && (
|
||||
<Card className="border-accent-amber/30 bg-accent-amber/5">
|
||||
<CardContent className="py-3">
|
||||
<p className="text-sm text-accent-amber font-medium mb-1">申诉理由</p>
|
||||
<p className="text-sm text-text-secondary">{task.appeal_reason}</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* 审核流程进度条 */}
|
||||
<ReviewProgressBar taskStatus={getReviewStepStatus(task)} />
|
||||
|
||||
<div className="grid grid-cols-1 lg:grid-cols-5 gap-6">
|
||||
{/* 左侧:视频/脚本播放器 (3/5) */}
|
||||
<div className="lg:col-span-3 space-y-4">
|
||||
<Card>
|
||||
<CardContent className="p-0">
|
||||
{isVideoReview ? (
|
||||
<div className="aspect-video bg-gray-900 rounded-t-lg flex items-center justify-center relative">
|
||||
<button
|
||||
type="button"
|
||||
className="w-16 h-16 bg-white/20 rounded-full flex items-center justify-center hover:bg-white/30 transition-colors"
|
||||
onClick={() => setIsPlaying(!isPlaying)}
|
||||
>
|
||||
{isPlaying ? <Pause size={32} className="text-white" /> : <Play size={32} className="text-white ml-1" />}
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="aspect-[4/3] bg-bg-elevated rounded-t-lg flex items-center justify-center">
|
||||
<div className="text-center">
|
||||
<p className="text-text-secondary">脚本预览区域</p>
|
||||
<p className="text-sm text-text-tertiary mt-1">{task.script_file_name || '脚本文件'}</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 智能进度条(仅视频且有时间标记时显示) */}
|
||||
{isVideoReview && timelineMarkers.length > 0 && (
|
||||
<div className="p-4 border-t border-border-subtle">
|
||||
<div className="text-sm font-medium text-text-primary mb-3">智能进度条(点击跳转)</div>
|
||||
<div className="relative h-3 bg-bg-elevated rounded-full">
|
||||
{timelineMarkers.map((marker, idx) => (
|
||||
<button
|
||||
key={idx}
|
||||
type="button"
|
||||
className={`absolute top-1/2 -translate-y-1/2 w-4 h-4 rounded-full border-2 border-bg-card shadow-md cursor-pointer transition-transform hover:scale-125 ${
|
||||
marker.type === 'hard' ? 'bg-accent-coral' : 'bg-orange-500'
|
||||
}`}
|
||||
style={{ left: `${(marker.time / maxTime) * 100}%` }}
|
||||
title={`${formatTimestamp(marker.time)} - 硬性问题`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex justify-between text-xs text-text-tertiary mt-1">
|
||||
<span>0:00</span>
|
||||
<span>{formatTimestamp(maxTime)}</span>
|
||||
</div>
|
||||
<div className="flex gap-4 mt-3 text-xs text-text-secondary">
|
||||
<span className="flex items-center gap-1">
|
||||
<span className="w-3 h-3 bg-accent-coral rounded-full" />
|
||||
硬性问题
|
||||
</span>
|
||||
<span className="flex items-center gap-1">
|
||||
<span className="w-3 h-3 bg-orange-500 rounded-full" />
|
||||
舆情提示
|
||||
</span>
|
||||
<span className="flex items-center gap-1">
|
||||
<span className="w-3 h-3 bg-accent-green rounded-full" />
|
||||
卖点覆盖
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* AI 分析总结 */}
|
||||
<Card>
|
||||
<CardContent className="py-4">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<span className="font-medium text-text-primary">AI 分析总结</span>
|
||||
{aiScore != null && (
|
||||
<span className={`text-xl font-bold ${aiScore >= 80 ? 'text-accent-green' : aiScore >= 60 ? 'text-yellow-400' : 'text-accent-coral'}`}>
|
||||
{aiScore}分
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-text-secondary text-sm">{aiSummary}</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* 右侧:AI 检查单 (2/5) */}
|
||||
<div className="lg:col-span-2 space-y-4">
|
||||
{/* 硬性合规 */}
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<Shield size={16} className="text-red-500" />
|
||||
硬性合规 ({violations.length})
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
{violations.length > 0 ? violations.map((v, idx) => {
|
||||
const key = `v-${idx}`
|
||||
return (
|
||||
<div key={key} className={`p-3 rounded-lg border ${checkedViolations[key] ? 'bg-bg-elevated border-border-subtle' : 'bg-accent-coral/10 border-accent-coral/30'}`}>
|
||||
<div className="flex items-start gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={checkedViolations[key] || false}
|
||||
onChange={() => setCheckedViolations((prev) => ({ ...prev, [key]: !prev[key] }))}
|
||||
className="mt-1 accent-accent-indigo"
|
||||
/>
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<ErrorTag>{v.type}</ErrorTag>
|
||||
{v.timestamp != null && (
|
||||
<span className="text-xs text-text-tertiary">{formatTimestamp(v.timestamp)}</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-sm font-medium text-text-primary">「{v.content}」</p>
|
||||
<p className="text-xs text-accent-indigo mt-1">{v.suggestion}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}) : (
|
||||
<div className="text-center py-4 text-text-tertiary text-sm">无硬性违规</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* 舆情雷达 */}
|
||||
{softWarnings.length > 0 && (
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<Radio size={16} className="text-orange-500" />
|
||||
舆情雷达(仅提示)
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{softWarnings.map((w, idx) => (
|
||||
<div key={idx} className="p-3 bg-orange-500/10 rounded-lg border border-orange-500/30">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<WarningTag>{w.type}</WarningTag>
|
||||
</div>
|
||||
<p className="text-sm text-orange-400">{w.content}</p>
|
||||
<p className="text-xs text-text-tertiary mt-1">软性风险仅作提示,不强制拦截</p>
|
||||
</div>
|
||||
))}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 底部决策栏 */}
|
||||
<Card className="sticky bottom-4 shadow-lg">
|
||||
<CardContent className="py-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm text-text-secondary">
|
||||
已检查 {Object.values(checkedViolations).filter(Boolean).length}/{violations.length} 个问题
|
||||
</div>
|
||||
<div className="flex gap-3">
|
||||
<Button variant="danger" onClick={() => setShowRejectModal(true)} disabled={submitting}>
|
||||
驳回
|
||||
</Button>
|
||||
<Button variant="secondary" onClick={() => setShowForcePassModal(true)} disabled={submitting}>
|
||||
强制通过
|
||||
</Button>
|
||||
<Button variant="success" onClick={() => setShowApproveModal(true)} disabled={submitting}>
|
||||
{submitting ? <Loader2 size={16} className="animate-spin" /> : null}
|
||||
通过
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* 通过确认弹窗 */}
|
||||
<ConfirmModal
|
||||
isOpen={showApproveModal}
|
||||
onClose={() => setShowApproveModal(false)}
|
||||
onConfirm={handleApprove}
|
||||
title="确认通过"
|
||||
message={`确定要通过此${isVideoReview ? '视频' : '脚本'}的审核吗?通过后达人将收到通知。`}
|
||||
confirmText="确认通过"
|
||||
/>
|
||||
|
||||
{/* 驳回弹窗 */}
|
||||
<Modal isOpen={showRejectModal} onClose={() => setShowRejectModal(false)} title="驳回审核">
|
||||
<div className="space-y-4">
|
||||
<p className="text-text-secondary text-sm">请填写驳回原因,已勾选的问题将自动打包发送给达人。</p>
|
||||
<div className="p-3 bg-bg-elevated rounded-lg">
|
||||
<p className="text-sm font-medium text-text-primary mb-2">
|
||||
已选问题 ({Object.values(checkedViolations).filter(Boolean).length})
|
||||
</p>
|
||||
{violations.filter((_, idx) => checkedViolations[`v-${idx}`]).map((v, idx) => (
|
||||
<div key={idx} className="text-sm text-text-secondary">- {v.type}: {v.content}</div>
|
||||
))}
|
||||
{Object.values(checkedViolations).filter(Boolean).length === 0 && (
|
||||
<div className="text-sm text-text-tertiary">未选择任何问题</div>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-primary mb-1">补充说明</label>
|
||||
<textarea
|
||||
className="w-full h-24 p-3 border border-border-subtle rounded-lg resize-none bg-bg-elevated text-text-primary focus:outline-none focus:ring-2 focus:ring-accent-indigo"
|
||||
placeholder="请详细说明驳回原因..."
|
||||
value={rejectReason}
|
||||
onChange={(e) => setRejectReason(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex gap-3 justify-end">
|
||||
<Button variant="ghost" onClick={() => setShowRejectModal(false)} disabled={submitting}>取消</Button>
|
||||
<Button variant="danger" onClick={handleReject} disabled={submitting}>
|
||||
{submitting && <Loader2 size={16} className="animate-spin" />}
|
||||
确认驳回
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* 强制通过弹窗 */}
|
||||
<Modal isOpen={showForcePassModal} onClose={() => setShowForcePassModal(false)} title="强制通过">
|
||||
<div className="space-y-4">
|
||||
<div className="p-3 bg-yellow-500/10 rounded-lg border border-yellow-500/30">
|
||||
<p className="text-sm text-yellow-400">
|
||||
<AlertTriangle size={14} className="inline mr-1" />
|
||||
强制通过将跳过所有问题检测,操作将被记录
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-primary mb-1">放行原因(必填)</label>
|
||||
<textarea
|
||||
className="w-full h-24 p-3 border border-border-subtle rounded-lg resize-none bg-bg-elevated text-text-primary focus:outline-none focus:ring-2 focus:ring-accent-indigo"
|
||||
placeholder="例如:达人玩的新梗,品牌方认可"
|
||||
value={forcePassReason}
|
||||
onChange={(e) => setForcePassReason(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<label className="flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={saveAsException}
|
||||
onChange={(e) => setSaveAsException(e.target.checked)}
|
||||
className="rounded accent-accent-indigo"
|
||||
/>
|
||||
<span className="text-sm text-text-secondary">保存为特例(需品牌方确认后生效)</span>
|
||||
</label>
|
||||
<div className="flex gap-3 justify-end">
|
||||
<Button variant="ghost" onClick={() => setShowForcePassModal(false)} disabled={submitting}>取消</Button>
|
||||
<Button onClick={handleForcePass} disabled={submitting}>
|
||||
{submitting && <Loader2 size={16} className="animate-spin" />}
|
||||
确认强制通过
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
<div className="flex items-center justify-center min-h-[50vh]">
|
||||
<Loader2 size={32} className="animate-spin text-accent-indigo" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
import type { TaskResponse } from '@/types/task'
|
||||
|
||||
function platformLabel(id?: string | null): string {
|
||||
if (!id) return ''
|
||||
return getPlatformInfo(id)?.name || id
|
||||
}
|
||||
|
||||
// ==================== Mock 数据 ====================
|
||||
const mockScriptTasks: TaskResponse[] = [
|
||||
{
|
||||
@@ -151,7 +156,10 @@ function ScriptTaskCard({ task, onPreview, toast }: { task: TaskResponse; onPrev
|
||||
<div className="rounded-xl bg-bg-elevated overflow-hidden">
|
||||
{/* 顶部条 */}
|
||||
<div className="px-4 py-1.5 bg-accent-indigo/10 border-b border-accent-indigo/20 flex items-center gap-1.5">
|
||||
<span className="text-xs font-medium text-accent-indigo">{task.project.brand_name || task.project.name}</span>
|
||||
<span className="text-xs font-medium text-accent-indigo">{task.project.brand_name || ''}</span>
|
||||
{task.project.platform && (
|
||||
<span className="text-xs text-text-tertiary">· {platformLabel(task.project.platform)}</span>
|
||||
)}
|
||||
{task.is_appeal && (
|
||||
<span className="ml-auto flex items-center gap-1 px-2 py-0.5 text-xs bg-accent-amber/30 text-accent-amber rounded-full font-medium">
|
||||
<MessageSquareWarning size={12} />
|
||||
@@ -161,12 +169,13 @@ function ScriptTaskCard({ task, onPreview, toast }: { task: TaskResponse; onPrev
|
||||
</div>
|
||||
<div className="p-4">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className={`w-2 h-2 rounded-full ${riskConfig.color}`} />
|
||||
<span className="font-medium text-text-primary">{task.creator.name} · {task.name}</span>
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<div className={`w-2 h-2 rounded-full flex-shrink-0 ${riskConfig.color}`} />
|
||||
<span className="font-medium text-text-primary truncate">{task.project.name} · {task.name}</span>
|
||||
</div>
|
||||
<span className={`text-xs ${riskConfig.textColor}`}>{riskConfig.label}</span>
|
||||
<span className={`text-xs flex-shrink-0 ${riskConfig.textColor}`}>{riskConfig.label}</span>
|
||||
</div>
|
||||
<p className="text-xs text-text-secondary mb-3">达人:{task.creator.name}</p>
|
||||
|
||||
{task.is_appeal && task.appeal_reason && (
|
||||
<div className="mb-3 p-2.5 rounded-lg bg-accent-amber/10 border border-accent-amber/30">
|
||||
@@ -195,7 +204,7 @@ function ScriptTaskCard({ task, onPreview, toast }: { task: TaskResponse; onPrev
|
||||
<Clock size={12} />
|
||||
{new Date(task.created_at).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' })}
|
||||
</span>
|
||||
<Link href={`/agency/review/${task.id}`}>
|
||||
<Link href={`/agency/review/script/${task.id}`}>
|
||||
<Button size="sm" className={`${
|
||||
riskLevel === 'high' ? 'bg-accent-coral hover:bg-accent-coral/80' :
|
||||
riskLevel === 'medium' ? 'bg-accent-amber hover:bg-accent-amber/80' :
|
||||
@@ -227,7 +236,10 @@ function VideoTaskCard({ task, onPreview, toast }: { task: TaskResponse; onPrevi
|
||||
return (
|
||||
<div className="rounded-xl bg-bg-elevated overflow-hidden">
|
||||
<div className="px-4 py-1.5 bg-purple-500/10 border-b border-purple-500/20 flex items-center gap-1.5">
|
||||
<span className="text-xs font-medium text-purple-400">{task.project.brand_name || task.project.name}</span>
|
||||
<span className="text-xs font-medium text-purple-400">{task.project.brand_name || ''}</span>
|
||||
{task.project.platform && (
|
||||
<span className="text-xs text-text-tertiary">· {platformLabel(task.project.platform)}</span>
|
||||
)}
|
||||
{task.is_appeal && (
|
||||
<span className="ml-auto flex items-center gap-1 px-2 py-0.5 text-xs bg-accent-amber/30 text-accent-amber rounded-full font-medium">
|
||||
<MessageSquareWarning size={12} />
|
||||
@@ -237,12 +249,13 @@ function VideoTaskCard({ task, onPreview, toast }: { task: TaskResponse; onPrevi
|
||||
</div>
|
||||
<div className="p-4">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className={`w-2 h-2 rounded-full ${riskConfig.color}`} />
|
||||
<span className="font-medium text-text-primary">{task.creator.name} · {task.name}</span>
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<div className={`w-2 h-2 rounded-full flex-shrink-0 ${riskConfig.color}`} />
|
||||
<span className="font-medium text-text-primary truncate">{task.project.name} · {task.name}</span>
|
||||
</div>
|
||||
<span className={`text-xs ${riskConfig.textColor}`}>{riskConfig.label}</span>
|
||||
<span className={`text-xs flex-shrink-0 ${riskConfig.textColor}`}>{riskConfig.label}</span>
|
||||
</div>
|
||||
<p className="text-xs text-text-secondary mb-3">达人:{task.creator.name}</p>
|
||||
|
||||
{task.is_appeal && task.appeal_reason && (
|
||||
<div className="mb-3 p-2.5 rounded-lg bg-accent-amber/10 border border-accent-amber/30">
|
||||
@@ -274,7 +287,7 @@ function VideoTaskCard({ task, onPreview, toast }: { task: TaskResponse; onPrevi
|
||||
<Clock size={12} />
|
||||
{new Date(task.created_at).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' })}
|
||||
</span>
|
||||
<Link href={`/agency/review/${task.id}`}>
|
||||
<Link href={`/agency/review/video/${task.id}`}>
|
||||
<Button size="sm" className={`${
|
||||
riskLevel === 'high' ? 'bg-accent-coral hover:bg-accent-coral/80' :
|
||||
riskLevel === 'medium' ? 'bg-accent-amber hover:bg-accent-amber/80' :
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
import { FilePreview, FileInfoCard, FilePreviewModal, type FileInfo } from '@/components/ui/FilePreview'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { getPlatformInfo } from '@/lib/platforms'
|
||||
import type { TaskResponse } from '@/types/task'
|
||||
|
||||
// 模拟脚本任务数据
|
||||
@@ -81,6 +82,8 @@ function mapTaskToViewModel(task: TaskResponse) {
|
||||
title: task.name,
|
||||
creatorName: task.creator?.name || '未知达人',
|
||||
projectName: task.project?.name || '未知项目',
|
||||
brandName: task.project?.brand_name || '',
|
||||
platform: task.project?.platform || '',
|
||||
submittedAt: task.script_uploaded_at || task.created_at,
|
||||
aiScore: task.script_ai_score ?? 0,
|
||||
status: task.stage,
|
||||
@@ -107,12 +110,26 @@ function mapTaskToViewModel(task: TaskResponse) {
|
||||
content: v.content,
|
||||
suggestion: v.suggestion,
|
||||
severity: v.severity,
|
||||
dimension: v.dimension,
|
||||
})),
|
||||
complianceChecks: (task.script_ai_result?.soft_warnings || []).map((w) => ({
|
||||
item: w.type,
|
||||
passed: false,
|
||||
note: w.content,
|
||||
})),
|
||||
complianceChecks: (task.script_ai_result?.soft_warnings || []).map((w: any) => {
|
||||
const codeLabels: Record<string, string> = {
|
||||
missing_selling_points: '卖点缺失',
|
||||
tone_mismatch: '语气不符',
|
||||
length_warning: '时长提示',
|
||||
style_warning: '风格提示',
|
||||
sensitive_topic: '敏感话题',
|
||||
audience_mismatch: '受众偏差',
|
||||
}
|
||||
const rawLabel = w.type || w.code || '提示'
|
||||
return {
|
||||
item: codeLabels[rawLabel] || rawLabel,
|
||||
passed: false,
|
||||
note: w.content || w.message || '',
|
||||
}
|
||||
}),
|
||||
dimensions: task.script_ai_result?.dimensions,
|
||||
sellingPointMatches: task.script_ai_result?.selling_point_matches || [],
|
||||
sellingPoints: [] as Array<{ point: string; covered: boolean }>,
|
||||
},
|
||||
aiSummary: task.script_ai_result?.summary || '',
|
||||
@@ -183,6 +200,7 @@ export default function AgencyScriptReviewPage() {
|
||||
const [showFilePreview, setShowFilePreview] = useState(false)
|
||||
const [task, setTask] = useState<ScriptTaskViewModel>(mockScriptTask as unknown as ScriptTaskViewModel)
|
||||
|
||||
|
||||
const loadTask = useCallback(async () => {
|
||||
if (USE_MOCK) return
|
||||
setLoading(true)
|
||||
@@ -294,10 +312,9 @@ export default function AgencyScriptReviewPage() {
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-4 mt-1 text-sm text-text-secondary">
|
||||
<span className="flex items-center gap-1">
|
||||
<User size={14} />
|
||||
{task.creatorName}
|
||||
</span>
|
||||
<span>{task.creatorName}</span>
|
||||
{task.brandName && <span>{task.brandName}</span>}
|
||||
{task.platform && <span>{getPlatformInfo(task.platform)?.name || task.platform}</span>}
|
||||
<span className="flex items-center gap-1">
|
||||
<Clock size={14} />
|
||||
{task.submittedAt}
|
||||
@@ -368,8 +385,7 @@ export default function AgencyScriptReviewPage() {
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<FileText size={18} className="text-accent-indigo" />
|
||||
AI 解析内容
|
||||
<span className="text-xs font-normal text-text-tertiary ml-2">(AI 自动提取的结构化内容)</span>
|
||||
AI 审核分析
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
@@ -378,23 +394,41 @@ export default function AgencyScriptReviewPage() {
|
||||
<div className="text-xs text-accent-indigo font-medium mb-2">AI 总结</div>
|
||||
<p className="text-text-primary">{task.aiSummary}</p>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="text-xs text-accent-indigo font-medium mb-2">开场白</div>
|
||||
<p className="text-text-primary">{task.scriptContent.opening || '(无内容)'}</p>
|
||||
</div>
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="text-xs text-purple-400 font-medium mb-2">产品介绍</div>
|
||||
<p className="text-text-primary">{task.scriptContent.productIntro || '(无内容)'}</p>
|
||||
</div>
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="text-xs text-orange-400 font-medium mb-2">使用演示</div>
|
||||
<p className="text-text-primary">{task.scriptContent.demo || '(无内容)'}</p>
|
||||
</div>
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="text-xs text-accent-green font-medium mb-2">结尾引导</div>
|
||||
<p className="text-text-primary">{task.scriptContent.closing || '(无内容)'}</p>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-sm text-text-tertiary text-center py-4">暂无 AI 分析总结</p>
|
||||
)}
|
||||
{task.aiAnalysis.violations.length > 0 && (
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="text-xs text-accent-coral font-medium mb-2">发现问题 ({task.aiAnalysis.violations.length})</div>
|
||||
<div className="space-y-2">
|
||||
{task.aiAnalysis.violations.map((v) => (
|
||||
<div key={v.id} className="text-sm">
|
||||
<span className="text-accent-coral font-medium">[{v.type}]</span>
|
||||
<span className="text-text-primary ml-1">{v.content}</span>
|
||||
<p className="text-xs text-accent-indigo mt-0.5">{v.suggestion}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{task.aiAnalysis.sellingPointMatches.length > 0 && (
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="text-xs text-accent-green font-medium mb-2">卖点匹配概览</div>
|
||||
<div className="space-y-1">
|
||||
{task.aiAnalysis.sellingPointMatches.map((sp: { content: string; priority: string; matched: boolean; evidence?: string }, idx: number) => (
|
||||
<div key={idx} className="flex items-center gap-2 text-sm">
|
||||
{sp.matched ? <CheckCircle size={14} className="text-accent-green flex-shrink-0" /> : <XCircle size={14} className="text-accent-coral flex-shrink-0" />}
|
||||
<span className="text-text-primary">{sp.content}</span>
|
||||
<span className={`text-xs px-1.5 py-0.5 rounded ${
|
||||
sp.priority === 'core' ? 'bg-accent-coral/20 text-accent-coral' :
|
||||
sp.priority === 'recommended' ? 'bg-accent-amber/20 text-accent-amber' :
|
||||
'bg-bg-page text-text-tertiary'
|
||||
}`}>{sp.priority === 'core' ? '核心' : sp.priority === 'recommended' ? '推荐' : '参考'}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
@@ -414,6 +448,34 @@ export default function AgencyScriptReviewPage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* 维度评分 */}
|
||||
{task.aiAnalysis.dimensions && (
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<Shield size={16} className="text-accent-indigo" />
|
||||
维度评分
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{(['legal', 'platform', 'brand_safety', 'brief_match'] as const).map(key => {
|
||||
const dim = (task.aiAnalysis.dimensions as unknown as Record<string, { score: number; passed: boolean; issue_count: number }>)?.[key]
|
||||
if (!dim) return null
|
||||
const label = { legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }[key]
|
||||
return (
|
||||
<div key={key} className={`flex items-center justify-between p-2 rounded-lg ${dim.passed ? 'bg-accent-green/5' : 'bg-accent-coral/5'}`}>
|
||||
<span className="text-sm text-text-primary">{label}</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`text-sm font-bold ${dim.passed ? 'text-accent-green' : 'text-accent-coral'}`}>{dim.score}</span>
|
||||
{dim.passed ? <CheckCircle size={14} className="text-accent-green" /> : <XCircle size={14} className="text-accent-coral" />}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* 违规检测 */}
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
@@ -427,6 +489,7 @@ export default function AgencyScriptReviewPage() {
|
||||
<div key={v.id} className="p-3 bg-orange-500/10 rounded-lg border border-orange-500/30">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<WarningTag>{v.type}</WarningTag>
|
||||
{v.dimension && <span className="text-xs text-text-tertiary">{{ legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }[v.dimension as string]}</span>}
|
||||
</div>
|
||||
<p className="text-sm text-text-primary">{v.content}</p>
|
||||
<p className="text-xs text-accent-indigo mt-1">{v.suggestion}</p>
|
||||
@@ -438,53 +501,58 @@ export default function AgencyScriptReviewPage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* 合规检查 */}
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<Shield size={16} className="text-accent-indigo" />
|
||||
合规检查
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{task.aiAnalysis.complianceChecks.map((check, idx) => (
|
||||
<div key={idx} className="flex items-start gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{check.passed ? (
|
||||
<CheckCircle size={16} className="text-accent-green flex-shrink-0 mt-0.5" />
|
||||
) : (
|
||||
<XCircle size={16} className="text-accent-coral flex-shrink-0 mt-0.5" />
|
||||
)}
|
||||
<div className="flex-1">
|
||||
<span className="text-sm text-text-primary">{check.item}</span>
|
||||
{/* 舆情提示 */}
|
||||
{task.aiAnalysis.complianceChecks.length > 0 && (
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<AlertTriangle size={16} className="text-orange-500" />
|
||||
舆情提示(仅参考)
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{task.aiAnalysis.complianceChecks.map((check, idx) => (
|
||||
<div key={idx} className="p-3 bg-orange-500/10 rounded-lg border border-orange-500/30">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<WarningTag>{check.item}</WarningTag>
|
||||
</div>
|
||||
{check.note && (
|
||||
<p className="text-xs text-text-tertiary mt-0.5">{check.note}</p>
|
||||
<p className="text-sm text-text-secondary">{check.note}</p>
|
||||
)}
|
||||
<p className="text-xs text-text-tertiary mt-1">软性风险仅作提示,不影响审核结果</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* 卖点覆盖 */}
|
||||
{/* 卖点匹配 */}
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<CheckCircle size={16} className="text-accent-green" />
|
||||
卖点覆盖
|
||||
卖点匹配
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{task.aiAnalysis.sellingPoints.map((sp, idx) => (
|
||||
<div key={idx} className="flex items-center gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{sp.covered ? (
|
||||
<CheckCircle size={16} className="text-accent-green" />
|
||||
) : (
|
||||
<XCircle size={16} className="text-accent-coral" />
|
||||
)}
|
||||
<span className="text-sm text-text-primary">{sp.point}</span>
|
||||
</div>
|
||||
))}
|
||||
{task.aiAnalysis.sellingPoints.length === 0 && (
|
||||
{task.aiAnalysis.sellingPointMatches && task.aiAnalysis.sellingPointMatches.length > 0 ? (
|
||||
task.aiAnalysis.sellingPointMatches.map((sp: { content: string; priority: string; matched: boolean; evidence?: string }, idx: number) => (
|
||||
<div key={idx} className="flex items-start gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{sp.matched ? <CheckCircle size={16} className="text-accent-green flex-shrink-0 mt-0.5" /> : <XCircle size={16} className="text-accent-coral flex-shrink-0 mt-0.5" />}
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm text-text-primary">{sp.content}</span>
|
||||
<span className={`px-1.5 py-0.5 text-xs rounded ${
|
||||
sp.priority === 'core' ? 'bg-accent-coral/20 text-accent-coral' :
|
||||
sp.priority === 'recommended' ? 'bg-accent-amber/20 text-accent-amber' :
|
||||
'bg-bg-page text-text-tertiary'
|
||||
}`}>{sp.priority === 'core' ? '核心' : sp.priority === 'recommended' ? '推荐' : '参考'}</span>
|
||||
</div>
|
||||
{sp.evidence && <p className="text-xs text-text-tertiary mt-0.5">{sp.evidence}</p>}
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<p className="text-sm text-text-tertiary text-center py-4">暂无卖点数据</p>
|
||||
)}
|
||||
</CardContent>
|
||||
@@ -497,7 +565,9 @@ export default function AgencyScriptReviewPage() {
|
||||
<CardContent className="py-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm text-text-secondary">
|
||||
项目:{task.projectName}
|
||||
{task.brandName && <span>{task.brandName} · </span>}
|
||||
{task.projectName}
|
||||
{task.platform && <span> · {getPlatformInfo(task.platform)?.name || task.platform}</span>}
|
||||
</div>
|
||||
<div className="flex gap-3">
|
||||
<Button variant="danger" onClick={() => setShowRejectModal(true)} disabled={submitting}>
|
||||
|
||||
@@ -221,6 +221,7 @@ export default function AgencyVideoReviewPage() {
|
||||
const [videoError, setVideoError] = useState(false)
|
||||
const [task, setTask] = useState<VideoTaskViewModel>(mockVideoTask as unknown as VideoTaskViewModel)
|
||||
|
||||
|
||||
const loadTask = useCallback(async () => {
|
||||
if (USE_MOCK) return
|
||||
setLoading(true)
|
||||
|
||||
@@ -8,8 +8,13 @@ import { Button } from '@/components/ui/Button'
|
||||
import { SuccessTag, WarningTag, ErrorTag, PendingTag } from '@/components/ui/Tag'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { getPlatformInfo } from '@/lib/platforms'
|
||||
import type { TaskResponse, TaskStage } from '@/types/task'
|
||||
|
||||
function getPlatformLabel(platformId: string): string {
|
||||
return getPlatformInfo(platformId)?.name || platformId
|
||||
}
|
||||
|
||||
// ==================== 本地视图模型 ====================
|
||||
interface TaskViewModel {
|
||||
id: string
|
||||
@@ -225,7 +230,7 @@ function mapTaskResponseToViewModel(task: TaskResponse): TaskViewModel {
|
||||
videoTitle: task.name,
|
||||
creatorName: task.creator?.name || '未知达人',
|
||||
brandName: task.project?.brand_name || '未知品牌',
|
||||
platform: '小红书', // 后端暂无 platform 字段
|
||||
platform: task.project?.platform ? getPlatformLabel(task.project.platform) : '未知平台',
|
||||
status,
|
||||
aiScore,
|
||||
finalScore,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
import { Card, CardContent } from '@/components/ui/Card'
|
||||
import { Button } from '@/components/ui/Button'
|
||||
import { Modal } from '@/components/ui/Modal'
|
||||
@@ -69,6 +69,30 @@ export default function AgenciesManagePage() {
|
||||
|
||||
// 操作菜单状态
|
||||
const [openMenuId, setOpenMenuId] = useState<string | null>(null)
|
||||
const [menuPos, setMenuPos] = useState<{ top: number; left: number }>({ top: 0, left: 0 })
|
||||
const menuRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleToggleMenu = (agencyId: string, e: React.MouseEvent<HTMLButtonElement>) => {
|
||||
if (openMenuId === agencyId) {
|
||||
setOpenMenuId(null)
|
||||
return
|
||||
}
|
||||
const rect = e.currentTarget.getBoundingClientRect()
|
||||
setMenuPos({ top: rect.bottom + 4, left: rect.right - 160 }) // 160 = menu width
|
||||
setOpenMenuId(agencyId)
|
||||
}
|
||||
|
||||
// 点击外部关闭菜单
|
||||
useEffect(() => {
|
||||
if (!openMenuId) return
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
|
||||
setOpenMenuId(null)
|
||||
}
|
||||
}
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||
}, [openMenuId])
|
||||
|
||||
// 删除确认弹窗状态
|
||||
const [deleteModal, setDeleteModal] = useState<{ open: boolean; agency: AgencyDetail | null }>({ open: false, agency: null })
|
||||
@@ -351,35 +375,13 @@ export default function AgenciesManagePage() {
|
||||
<StatusTag forcePass={agency.force_pass_enabled} />
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<div className="relative">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setOpenMenuId(openMenuId === agency.id ? null : agency.id)}
|
||||
>
|
||||
<MoreVertical size={16} />
|
||||
</Button>
|
||||
{openMenuId === agency.id && (
|
||||
<div className="absolute right-0 top-full mt-1 w-40 bg-bg-card rounded-xl shadow-lg border border-border-subtle z-10 overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenAssign(agency)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-text-primary hover:bg-bg-elevated flex items-center gap-2"
|
||||
>
|
||||
<FolderPlus size={14} className="text-text-secondary" />
|
||||
分配到项目
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleOpenDelete(agency)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-accent-coral hover:bg-accent-coral/10 flex items-center gap-2"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
移除代理商
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={(e) => handleToggleMenu(agency.id, e)}
|
||||
>
|
||||
<MoreVertical size={16} />
|
||||
</Button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
@@ -396,6 +398,38 @@ export default function AgenciesManagePage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* 操作菜单(fixed 定位,不受 overflow 裁剪) */}
|
||||
{openMenuId && (
|
||||
<div
|
||||
ref={menuRef}
|
||||
className="fixed w-40 bg-bg-card rounded-xl shadow-lg border border-border-subtle z-50 overflow-hidden"
|
||||
style={{ top: menuPos.top, left: menuPos.left }}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
const agency = agencies.find(a => a.id === openMenuId)
|
||||
if (agency) handleOpenAssign(agency)
|
||||
}}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-text-primary hover:bg-bg-elevated flex items-center gap-2"
|
||||
>
|
||||
<FolderPlus size={14} className="text-text-secondary" />
|
||||
分配到项目
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
const agency = agencies.find(a => a.id === openMenuId)
|
||||
if (agency) handleOpenDelete(agency)
|
||||
}}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-accent-coral hover:bg-accent-coral/10 flex items-center gap-2"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
移除代理商
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 邀请代理商弹窗 */}
|
||||
<Modal isOpen={showInviteModal} onClose={handleCloseInviteModal} title="邀请代理商">
|
||||
<div className="space-y-4">
|
||||
@@ -539,13 +573,6 @@ export default function AgenciesManagePage() {
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* 点击其他地方关闭菜单 */}
|
||||
{openMenuId && (
|
||||
<div
|
||||
className="fixed inset-0 z-0"
|
||||
onClick={() => setOpenMenuId(null)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -34,21 +34,67 @@ const providerOptions: { value: AIProvider | string; label: string }[] = [
|
||||
{ value: 'moonshot', label: 'Moonshot' },
|
||||
]
|
||||
|
||||
// Mock 可用模型列表
|
||||
// 预设可用模型列表
|
||||
const mockModels: Record<string, ModelInfo[]> = {
|
||||
text: [
|
||||
// Anthropic Claude
|
||||
{ id: 'claude-opus-4-5-20251101', name: 'Claude Opus 4.5' },
|
||||
{ id: 'claude-sonnet-4-5-20250929', name: 'Claude Sonnet 4.5' },
|
||||
{ id: 'claude-sonnet-4-20250514', name: 'Claude Sonnet 4' },
|
||||
{ id: 'claude-haiku-4-5-20251001', name: 'Claude Haiku 4.5' },
|
||||
// OpenAI
|
||||
{ id: 'gpt-4o', name: 'GPT-4o' },
|
||||
{ id: 'deepseek-chat', name: 'DeepSeek Chat' },
|
||||
{ id: 'gpt-4o-mini', name: 'GPT-4o Mini' },
|
||||
{ id: 'gpt-4-turbo', name: 'GPT-4 Turbo' },
|
||||
{ id: 'o1', name: 'o1' },
|
||||
{ id: 'o3-mini', name: 'o3-mini' },
|
||||
// Google
|
||||
{ id: 'gemini-2.0-flash', name: 'Gemini 2.0 Flash' },
|
||||
{ id: 'gemini-2.0-pro', name: 'Gemini 2.0 Pro' },
|
||||
{ id: 'gemini-1.5-pro', name: 'Gemini 1.5 Pro' },
|
||||
// DeepSeek
|
||||
{ id: 'deepseek-chat', name: 'DeepSeek V3' },
|
||||
{ id: 'deepseek-reasoner', name: 'DeepSeek R1' },
|
||||
// 通义千问
|
||||
{ id: 'qwen-max', name: '通义千问 Max' },
|
||||
{ id: 'qwen-plus', name: '通义千问 Plus' },
|
||||
{ id: 'qwen-turbo', name: '通义千问 Turbo' },
|
||||
// 豆包
|
||||
{ id: 'doubao-pro-256k', name: '豆包 Pro 256K' },
|
||||
{ id: 'doubao-pro-32k', name: '豆包 Pro 32K' },
|
||||
// 智谱
|
||||
{ id: 'glm-4-plus', name: 'GLM-4 Plus' },
|
||||
{ id: 'glm-4', name: 'GLM-4' },
|
||||
// Moonshot
|
||||
{ id: 'moonshot-v1-128k', name: 'Moonshot V1 128K' },
|
||||
{ id: 'moonshot-v1-32k', name: 'Moonshot V1 32K' },
|
||||
],
|
||||
vision: [
|
||||
// Anthropic Claude (原生多模态)
|
||||
{ id: 'claude-opus-4-5-20251101', name: 'Claude Opus 4.5' },
|
||||
{ id: 'claude-sonnet-4-5-20250929', name: 'Claude Sonnet 4.5' },
|
||||
{ id: 'claude-sonnet-4-20250514', name: 'Claude Sonnet 4' },
|
||||
{ id: 'claude-haiku-4-5-20251001', name: 'Claude Haiku 4.5' },
|
||||
// OpenAI
|
||||
{ id: 'gpt-4o', name: 'GPT-4o' },
|
||||
{ id: 'gpt-4o-mini', name: 'GPT-4o Mini' },
|
||||
// Google
|
||||
{ id: 'gemini-2.0-flash', name: 'Gemini 2.0 Flash' },
|
||||
{ id: 'gemini-2.0-pro', name: 'Gemini 2.0 Pro' },
|
||||
{ id: 'gemini-1.5-pro', name: 'Gemini 1.5 Pro' },
|
||||
// 通义千问 VL
|
||||
{ id: 'qwen-vl-max', name: '通义千问 VL Max' },
|
||||
{ id: 'qwen-vl-plus', name: '通义千问 VL Plus' },
|
||||
// 智谱
|
||||
{ id: 'glm-4v-plus', name: 'GLM-4V Plus' },
|
||||
{ id: 'glm-4v', name: 'GLM-4V' },
|
||||
],
|
||||
audio: [
|
||||
{ id: 'whisper-large-v3', name: 'Whisper Large V3' },
|
||||
{ id: 'whisper-large-v3-turbo', name: 'Whisper Large V3 Turbo' },
|
||||
{ id: 'whisper-medium', name: 'Whisper Medium' },
|
||||
{ id: 'sensevoice-v1', name: 'SenseVoice V1 (阿里)' },
|
||||
{ id: 'paraformer-realtime-v2', name: 'Paraformer V2 (阿里)' },
|
||||
],
|
||||
}
|
||||
|
||||
@@ -83,6 +129,9 @@ export default function AIConfigPage() {
|
||||
const [maxTokens, setMaxTokens] = useState(2000)
|
||||
|
||||
const [availableModels, setAvailableModels] = useState<Record<string, ModelInfo[]>>(mockModels)
|
||||
const [customLlmModel, setCustomLlmModel] = useState('')
|
||||
const [customVisionModel, setCustomVisionModel] = useState('')
|
||||
const [customAsrModel, setCustomAsrModel] = useState('')
|
||||
|
||||
const [testResults, setTestResults] = useState<Record<string, { status: TestStatus; latency?: number; error?: string }>>({
|
||||
text: { status: 'idle' },
|
||||
@@ -101,17 +150,41 @@ export default function AIConfigPage() {
|
||||
setBaseUrl(config.base_url)
|
||||
setApiKey('') // API key is masked, don't fill it
|
||||
setIsConfigured(config.is_configured)
|
||||
setLlmModel(config.models.text)
|
||||
setVisionModel(config.models.vision)
|
||||
setAsrModel(config.models.audio)
|
||||
const models = availableModels
|
||||
// 如果后端返回的模型不在预设列表中,设为自定义
|
||||
if (config.models.text && !(models.text || []).some(m => m.id === config.models.text)) {
|
||||
setLlmModel('__custom__')
|
||||
setCustomLlmModel(config.models.text)
|
||||
} else {
|
||||
setLlmModel(config.models.text)
|
||||
}
|
||||
if (config.models.vision && !(models.vision || []).some(m => m.id === config.models.vision)) {
|
||||
setVisionModel('__custom__')
|
||||
setCustomVisionModel(config.models.vision)
|
||||
} else {
|
||||
setVisionModel(config.models.vision)
|
||||
}
|
||||
if (config.models.audio && !(models.audio || []).some(m => m.id === config.models.audio)) {
|
||||
setAsrModel('__custom__')
|
||||
setCustomAsrModel(config.models.audio)
|
||||
} else {
|
||||
setAsrModel(config.models.audio)
|
||||
}
|
||||
setTemperature(config.parameters.temperature)
|
||||
setMaxTokens(config.parameters.max_tokens)
|
||||
if (config.available_models && Object.keys(config.available_models).length > 0) {
|
||||
setAvailableModels(config.available_models)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load AI config:', err)
|
||||
toast.error('加载 AI 配置失败')
|
||||
} catch (err: any) {
|
||||
// 后端 404 返回 "AI 服务未配置" → Axios 拦截器转为 Error(message)
|
||||
// 这是正常的"尚未配置"状态,不弹错误
|
||||
const msg = err?.message || ''
|
||||
if (msg.includes('未配置')) {
|
||||
setIsConfigured(false)
|
||||
} else {
|
||||
console.error('Failed to load AI config:', err)
|
||||
toast.error('加载 AI 配置失败')
|
||||
}
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
@@ -119,6 +192,10 @@ export default function AIConfigPage() {
|
||||
|
||||
useEffect(() => { loadConfig() }, [loadConfig])
|
||||
|
||||
// 获取实际使用的模型 ID(自定义时用输入值)
|
||||
const getActualModel = (selected: string, custom: string) =>
|
||||
selected === '__custom__' ? custom : selected
|
||||
|
||||
const handleTestConnection = async () => {
|
||||
setTestResults({
|
||||
text: { status: 'testing' },
|
||||
@@ -141,7 +218,7 @@ export default function AIConfigPage() {
|
||||
provider: provider as AIProvider,
|
||||
base_url: baseUrl,
|
||||
api_key: apiKey || '***', // use existing key if not changed
|
||||
models: { text: llmModel, vision: visionModel, audio: asrModel },
|
||||
models: { text: getActualModel(llmModel, customLlmModel), vision: getActualModel(visionModel, customVisionModel), audio: getActualModel(asrModel, customAsrModel) },
|
||||
})
|
||||
const newResults: Record<string, { status: TestStatus; latency?: number; error?: string }> = {}
|
||||
for (const [key, r] of Object.entries(result.results)) {
|
||||
@@ -177,7 +254,7 @@ export default function AIConfigPage() {
|
||||
provider: provider as AIProvider,
|
||||
base_url: baseUrl,
|
||||
api_key: apiKey || '***',
|
||||
models: { text: llmModel, vision: visionModel, audio: asrModel },
|
||||
models: { text: getActualModel(llmModel, customLlmModel), vision: getActualModel(visionModel, customVisionModel), audio: getActualModel(asrModel, customAsrModel) },
|
||||
parameters: { temperature, max_tokens: maxTokens },
|
||||
})
|
||||
}
|
||||
@@ -297,32 +374,52 @@ export default function AIConfigPage() {
|
||||
<select
|
||||
className="w-full px-3 py-2 border border-border-subtle rounded-lg focus:outline-none focus:ring-2 focus:ring-accent-indigo bg-bg-card text-text-primary"
|
||||
value={llmModel}
|
||||
onChange={(e) => setLlmModel(e.target.value)}
|
||||
onChange={(e) => { setLlmModel(e.target.value); if (e.target.value !== '__custom__') setCustomLlmModel('') }}
|
||||
>
|
||||
{(availableModels.text || []).map(model => (
|
||||
<option key={model.id} value={model.id}>{model.name}</option>
|
||||
<option key={model.id} value={model.id}>{model.name} ({model.id})</option>
|
||||
))}
|
||||
<option value="__custom__">自定义模型...</option>
|
||||
</select>
|
||||
<p className="text-xs text-text-tertiary mt-2">用于 Brief 解析、语义分析、报告生成</p>
|
||||
{llmModel === '__custom__' && (
|
||||
<input
|
||||
type="text"
|
||||
className="w-full mt-2 px-3 py-2 border border-border-subtle rounded-lg focus:outline-none focus:ring-2 focus:ring-accent-indigo bg-bg-card text-text-primary"
|
||||
value={customLlmModel}
|
||||
onChange={(e) => setCustomLlmModel(e.target.value)}
|
||||
placeholder="输入模型 ID,如 deepseek-chat"
|
||||
/>
|
||||
)}
|
||||
<p className="text-xs text-text-tertiary mt-2">用于 Brief 解析、脚本语义审核、卖点匹配分析</p>
|
||||
</div>
|
||||
|
||||
{/* 视频分析模型 */}
|
||||
{/* 视觉理解模型 */}
|
||||
<div className="p-4 bg-bg-elevated rounded-lg">
|
||||
<div className="flex items-center gap-2 mb-3">
|
||||
<Eye size={16} className="text-accent-green" />
|
||||
<span className="font-medium text-text-primary">视频分析模型 (Vision)</span>
|
||||
<span className="font-medium text-text-primary">视觉理解模型 (Vision)</span>
|
||||
{getTestStatusIcon('vision')}
|
||||
</div>
|
||||
<select
|
||||
className="w-full px-3 py-2 border border-border-subtle rounded-lg focus:outline-none focus:ring-2 focus:ring-accent-indigo bg-bg-card text-text-primary"
|
||||
value={visionModel}
|
||||
onChange={(e) => setVisionModel(e.target.value)}
|
||||
onChange={(e) => { setVisionModel(e.target.value); if (e.target.value !== '__custom__') setCustomVisionModel('') }}
|
||||
>
|
||||
{(availableModels.vision || []).map(model => (
|
||||
<option key={model.id} value={model.id}>{model.name}</option>
|
||||
<option key={model.id} value={model.id}>{model.name} ({model.id})</option>
|
||||
))}
|
||||
<option value="__custom__">自定义模型...</option>
|
||||
</select>
|
||||
<p className="text-xs text-text-tertiary mt-2">用于画面语义分析、场景/风险识别</p>
|
||||
{visionModel === '__custom__' && (
|
||||
<input
|
||||
type="text"
|
||||
className="w-full mt-2 px-3 py-2 border border-border-subtle rounded-lg focus:outline-none focus:ring-2 focus:ring-accent-indigo bg-bg-card text-text-primary"
|
||||
value={customVisionModel}
|
||||
onChange={(e) => setCustomVisionModel(e.target.value)}
|
||||
placeholder="输入模型 ID,如 gpt-4o"
|
||||
/>
|
||||
)}
|
||||
<p className="text-xs text-text-tertiary mt-2">用于脚本文档中的图片审核(竞品 logo、违规画面识别)及视频帧分析</p>
|
||||
</div>
|
||||
|
||||
{/* 音频解析模型 */}
|
||||
@@ -335,12 +432,22 @@ export default function AIConfigPage() {
|
||||
<select
|
||||
className="w-full px-3 py-2 border border-border-subtle rounded-lg focus:outline-none focus:ring-2 focus:ring-accent-indigo bg-bg-card text-text-primary"
|
||||
value={asrModel}
|
||||
onChange={(e) => setAsrModel(e.target.value)}
|
||||
onChange={(e) => { setAsrModel(e.target.value); if (e.target.value !== '__custom__') setCustomAsrModel('') }}
|
||||
>
|
||||
{(availableModels.audio || []).map(model => (
|
||||
<option key={model.id} value={model.id}>{model.name}</option>
|
||||
<option key={model.id} value={model.id}>{model.name} ({model.id})</option>
|
||||
))}
|
||||
<option value="__custom__">自定义模型...</option>
|
||||
</select>
|
||||
{asrModel === '__custom__' && (
|
||||
<input
|
||||
type="text"
|
||||
className="w-full mt-2 px-3 py-2 border border-border-subtle rounded-lg focus:outline-none focus:ring-2 focus:ring-accent-indigo bg-bg-card text-text-primary"
|
||||
value={customAsrModel}
|
||||
onChange={(e) => setCustomAsrModel(e.target.value)}
|
||||
placeholder="输入模型 ID,如 whisper-large-v3"
|
||||
/>
|
||||
)}
|
||||
<p className="text-xs text-text-tertiary mt-2">用于语音转文字、口播内容提取</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
@@ -45,6 +45,10 @@ type MessageType =
|
||||
| 'brief_config_updated' // 代理商更新了Brief配置
|
||||
| 'batch_review_done' // 批量审核完成
|
||||
| 'system_notice' // 系统通知
|
||||
| 'new_task' // 新任务分配
|
||||
| 'pass' // 审核通过
|
||||
| 'reject' // 审核驳回
|
||||
| 'approve' // 审核批准
|
||||
|
||||
type Message = {
|
||||
id: string
|
||||
@@ -80,6 +84,10 @@ const messageConfig: Record<MessageType, {
|
||||
brief_config_updated: { icon: FileText, iconColor: 'text-accent-indigo', bgColor: 'bg-accent-indigo/20' },
|
||||
batch_review_done: { icon: CheckCircle, iconColor: 'text-accent-green', bgColor: 'bg-accent-green/20' },
|
||||
system_notice: { icon: Bell, iconColor: 'text-text-secondary', bgColor: 'bg-bg-elevated' },
|
||||
new_task: { icon: FileText, iconColor: 'text-accent-indigo', bgColor: 'bg-accent-indigo/20' },
|
||||
pass: { icon: CheckCircle, iconColor: 'text-accent-green', bgColor: 'bg-accent-green/20' },
|
||||
reject: { icon: XCircle, iconColor: 'text-accent-coral', bgColor: 'bg-accent-coral/20' },
|
||||
approve: { icon: CheckCircle, iconColor: 'text-accent-green', bgColor: 'bg-accent-green/20' },
|
||||
}
|
||||
|
||||
// 模拟消息数据
|
||||
@@ -412,7 +420,7 @@ export default function BrandMessagesPage() {
|
||||
{/* 消息列表 */}
|
||||
<div className="space-y-3">
|
||||
{filteredMessages.map((message) => {
|
||||
const config = messageConfig[message.type]
|
||||
const config = messageConfig[message.type] || messageConfig.system_notice
|
||||
const Icon = config.icon
|
||||
const platform = message.platform ? getPlatformInfo(message.platform) : null
|
||||
|
||||
|
||||
@@ -22,28 +22,29 @@ import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
import { useToast } from '@/components/ui/Toast'
|
||||
import { getPlatformInfo } from '@/lib/platforms'
|
||||
import type { ProjectResponse } from '@/types/project'
|
||||
|
||||
// ==================== Mock 数据 ====================
|
||||
const mockProjects: ProjectResponse[] = [
|
||||
{
|
||||
id: 'proj-001', name: 'XX品牌618推广', brand_id: 'br-001', brand_name: 'XX品牌',
|
||||
status: 'active', deadline: '2026-06-18', agencies: [],
|
||||
platform: 'douyin', status: 'active', deadline: '2026-06-18', agencies: [],
|
||||
task_count: 20, created_at: '2026-02-01T00:00:00Z', updated_at: '2026-02-05T00:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'proj-002', name: '新品口红系列', brand_id: 'br-001', brand_name: 'XX品牌',
|
||||
status: 'active', deadline: '2026-03-15', agencies: [],
|
||||
platform: 'xiaohongshu', status: 'active', deadline: '2026-03-15', agencies: [],
|
||||
task_count: 12, created_at: '2026-01-15T00:00:00Z', updated_at: '2026-02-01T00:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'proj-003', name: '护肤品秋季活动', brand_id: 'br-001', brand_name: 'XX品牌',
|
||||
status: 'completed', deadline: '2025-11-30', agencies: [],
|
||||
platform: 'bilibili', status: 'completed', deadline: '2025-11-30', agencies: [],
|
||||
task_count: 15, created_at: '2025-08-01T00:00:00Z', updated_at: '2025-11-30T00:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'proj-004', name: '双11预热活动', brand_id: 'br-001', brand_name: 'XX品牌',
|
||||
status: 'active', deadline: '2026-11-11', agencies: [],
|
||||
platform: 'kuaishou', status: 'active', deadline: '2026-11-11', agencies: [],
|
||||
task_count: 18, created_at: '2026-01-10T00:00:00Z', updated_at: '2026-02-04T00:00:00Z',
|
||||
},
|
||||
]
|
||||
@@ -58,11 +59,25 @@ function StatusTag({ status }: { status: string }) {
|
||||
}
|
||||
|
||||
function ProjectCard({ project, onEditDeadline }: { project: ProjectResponse; onEditDeadline: (project: ProjectResponse) => void }) {
|
||||
const platformInfo = project.platform ? getPlatformInfo(project.platform) : null
|
||||
|
||||
return (
|
||||
<Link href={`/brand/projects/${project.id}`}>
|
||||
<Card className="hover:border-accent-indigo/50 transition-colors cursor-pointer h-full overflow-hidden">
|
||||
<div className="px-6 py-2 bg-accent-indigo/10 border-b border-accent-indigo/20 flex items-center justify-between">
|
||||
<span className="text-sm font-medium text-accent-indigo">{project.brand_name || '品牌项目'}</span>
|
||||
<div className={`px-6 py-2 border-b flex items-center justify-between ${
|
||||
platformInfo
|
||||
? `${platformInfo.bgColor} ${platformInfo.borderColor}`
|
||||
: 'bg-accent-indigo/10 border-accent-indigo/20'
|
||||
}`}>
|
||||
<span className={`text-sm font-medium flex items-center gap-1.5 ${
|
||||
platformInfo ? platformInfo.textColor : 'text-accent-indigo'
|
||||
}`}>
|
||||
{platformInfo ? (
|
||||
<><span>{platformInfo.icon}</span>{platformInfo.name}</>
|
||||
) : (
|
||||
project.brand_name || '品牌项目'
|
||||
)}
|
||||
</span>
|
||||
<StatusTag status={project.status} />
|
||||
</div>
|
||||
<CardContent className="p-6 space-y-4">
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
import { useRouter, useParams } from 'next/navigation'
|
||||
import { useToast } from '@/components/ui/Toast'
|
||||
import { Card, CardContent } from '@/components/ui/Card'
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
Plus,
|
||||
Trash2,
|
||||
AlertTriangle,
|
||||
AlertCircle,
|
||||
CheckCircle,
|
||||
Bot,
|
||||
Users,
|
||||
@@ -20,13 +21,28 @@ import {
|
||||
Upload,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
Loader2
|
||||
Loader2,
|
||||
Search,
|
||||
RotateCcw
|
||||
} from 'lucide-react'
|
||||
import { Modal } from '@/components/ui/Modal'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useOSSUpload } from '@/hooks/useOSSUpload'
|
||||
import { USE_MOCK, useAuth } from '@/contexts/AuthContext'
|
||||
import type { RuleConflict } from '@/types/rules'
|
||||
import type { BriefResponse, BriefCreateRequest, SellingPoint, BlacklistWord, BriefAttachment } from '@/types/brief'
|
||||
|
||||
// 单个文件的上传状态
|
||||
interface UploadFileItem {
|
||||
id: string
|
||||
name: string
|
||||
size: string
|
||||
status: 'uploading' | 'success' | 'error'
|
||||
progress: number
|
||||
url?: string
|
||||
error?: string
|
||||
file?: File
|
||||
}
|
||||
|
||||
// ==================== Mock 数据 ====================
|
||||
const mockBrief: BriefResponse = {
|
||||
id: 'bf-001',
|
||||
@@ -81,6 +97,13 @@ const mockRules = {
|
||||
},
|
||||
}
|
||||
|
||||
function formatFileSize(bytes: number): string {
|
||||
if (bytes < 1024) return bytes + 'B'
|
||||
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB'
|
||||
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + 'MB'
|
||||
return (bytes / (1024 * 1024 * 1024)).toFixed(1) + 'GB'
|
||||
}
|
||||
|
||||
// 严格程度选项
|
||||
const strictnessOptions = [
|
||||
{ value: 'low', label: '宽松', description: '仅检测明显违规内容' },
|
||||
@@ -109,8 +132,11 @@ export default function ProjectConfigPage() {
|
||||
const router = useRouter()
|
||||
const params = useParams()
|
||||
const toast = useToast()
|
||||
const { user } = useAuth()
|
||||
const projectId = params.id as string
|
||||
const { upload, isUploading, progress: uploadProgress } = useOSSUpload('general')
|
||||
|
||||
// 附件上传跟踪
|
||||
const [uploadingFiles, setUploadingFiles] = useState<UploadFileItem[]>([])
|
||||
|
||||
// Brief state
|
||||
const [briefExists, setBriefExists] = useState(false)
|
||||
@@ -133,6 +159,82 @@ export default function ProjectConfigPage() {
|
||||
const [isSaving, setIsSaving] = useState(false)
|
||||
const [activeSection, setActiveSection] = useState<string | null>('brief')
|
||||
|
||||
// 规则冲突检测
|
||||
const [isCheckingConflicts, setIsCheckingConflicts] = useState(false)
|
||||
const [showConflictModal, setShowConflictModal] = useState(false)
|
||||
const [conflicts, setConflicts] = useState<RuleConflict[]>([])
|
||||
const [showPlatformSelect, setShowPlatformSelect] = useState(false)
|
||||
|
||||
const platformDropdownRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const platformOptions = [
|
||||
{ value: 'douyin', label: '抖音' },
|
||||
{ value: 'xiaohongshu', label: '小红书' },
|
||||
{ value: 'bilibili', label: 'B站' },
|
||||
]
|
||||
|
||||
// 点击外部关闭平台选择下拉
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (platformDropdownRef.current && !platformDropdownRef.current.contains(e.target as Node)) {
|
||||
setShowPlatformSelect(false)
|
||||
}
|
||||
}
|
||||
if (showPlatformSelect) {
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
}
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||
}, [showPlatformSelect])
|
||||
|
||||
const handleCheckConflicts = async (platform: string) => {
|
||||
setShowPlatformSelect(false)
|
||||
setIsCheckingConflicts(true)
|
||||
|
||||
if (USE_MOCK) {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||
setConflicts([
|
||||
{
|
||||
brief_rule: '卖点包含:100%纯天然成分',
|
||||
platform_rule: `${platform} 禁止使用:100%`,
|
||||
suggestion: "卖点 '100%纯天然成分' 包含违禁词 '100%',建议修改表述",
|
||||
},
|
||||
{
|
||||
brief_rule: 'Brief 最长时长:5秒',
|
||||
platform_rule: `${platform} 最短要求:7秒`,
|
||||
suggestion: 'Brief 最长 5s 低于平台最短要求 7s,视频可能不达标',
|
||||
},
|
||||
])
|
||||
setShowConflictModal(true)
|
||||
setIsCheckingConflicts(false)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const brandId = user?.brand_id || ''
|
||||
const briefRules: Record<string, unknown> = {
|
||||
selling_points: sellingPoints.map(sp => sp.content),
|
||||
min_duration: minDuration,
|
||||
max_duration: maxDuration,
|
||||
}
|
||||
const result = await api.validateRules({
|
||||
brand_id: brandId,
|
||||
platform,
|
||||
brief_rules: briefRules,
|
||||
})
|
||||
setConflicts(result.conflicts)
|
||||
if (result.conflicts.length > 0) {
|
||||
setShowConflictModal(true)
|
||||
} else {
|
||||
toast.success('未发现规则冲突')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('规则冲突检测失败:', err)
|
||||
toast.error('规则冲突检测失败')
|
||||
} finally {
|
||||
setIsCheckingConflicts(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Input fields
|
||||
const [newSellingPoint, setNewSellingPoint] = useState('')
|
||||
const [newBlacklistWord, setNewBlacklistWord] = useState('')
|
||||
@@ -254,32 +356,71 @@ export default function ProjectConfigPage() {
|
||||
setCompetitors(competitors.filter(c => c !== name))
|
||||
}
|
||||
|
||||
// Attachment upload
|
||||
const handleAttachmentUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (!file) return
|
||||
|
||||
// 上传单个附件(独立跟踪进度)
|
||||
const uploadSingleAttachment = async (file: File, fileId: string) => {
|
||||
if (USE_MOCK) {
|
||||
setAttachments([...attachments, {
|
||||
id: `att-${Date.now()}`,
|
||||
name: file.name,
|
||||
url: `mock://${file.name}`,
|
||||
}])
|
||||
for (let p = 20; p <= 80; p += 20) {
|
||||
await new Promise(r => setTimeout(r, 300))
|
||||
setUploadingFiles(prev => prev.map(f => f.id === fileId ? { ...f, progress: p } : f))
|
||||
}
|
||||
await new Promise(r => setTimeout(r, 300))
|
||||
const att: BriefAttachment = { id: fileId, name: file.name, url: `mock://${file.name}`, size: formatFileSize(file.size) }
|
||||
setAttachments(prev => [...prev, att])
|
||||
setUploadingFiles(prev => prev.filter(f => f.id !== fileId))
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await upload(file)
|
||||
setAttachments([...attachments, {
|
||||
id: `att-${Date.now()}`,
|
||||
name: file.name,
|
||||
url: result.url,
|
||||
}])
|
||||
} catch {
|
||||
toast.error('文件上传失败')
|
||||
const result = await api.proxyUpload(file, 'general', (pct) => {
|
||||
setUploadingFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, progress: Math.min(95, Math.round(pct * 0.95)) }
|
||||
: f
|
||||
))
|
||||
})
|
||||
const att: BriefAttachment = { id: fileId, name: file.name, url: result.url, size: formatFileSize(file.size) }
|
||||
setAttachments(prev => [...prev, att])
|
||||
setUploadingFiles(prev => prev.filter(f => f.id !== fileId))
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : '上传失败'
|
||||
setUploadingFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, status: 'error', error: msg }
|
||||
: f
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
const handleAttachmentUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const files = e.target.files
|
||||
if (!files || files.length === 0) return
|
||||
|
||||
const fileList = Array.from(files)
|
||||
e.target.value = ''
|
||||
const newItems: UploadFileItem[] = fileList.map(file => ({
|
||||
id: `att-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
name: file.name,
|
||||
size: formatFileSize(file.size),
|
||||
status: 'uploading' as const,
|
||||
progress: 0,
|
||||
file,
|
||||
}))
|
||||
setUploadingFiles(prev => [...prev, ...newItems])
|
||||
newItems.forEach(item => uploadSingleAttachment(item.file!, item.id))
|
||||
}
|
||||
|
||||
const retryAttachmentUpload = (fileId: string) => {
|
||||
const item = uploadingFiles.find(f => f.id === fileId)
|
||||
if (!item?.file) return
|
||||
setUploadingFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, status: 'uploading', progress: 0, error: undefined }
|
||||
: f
|
||||
))
|
||||
uploadSingleAttachment(item.file, fileId)
|
||||
}
|
||||
|
||||
const removeUploadingFile = (id: string) => {
|
||||
setUploadingFiles(prev => prev.filter(f => f.id !== id))
|
||||
}
|
||||
|
||||
const removeAttachment = (id: string) => {
|
||||
setAttachments(attachments.filter(a => a.id !== id))
|
||||
}
|
||||
@@ -336,19 +477,54 @@ export default function ProjectConfigPage() {
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Button variant="primary" onClick={handleSaveBrief} disabled={isSaving}>
|
||||
{isSaving ? (
|
||||
<>
|
||||
<Loader2 size={16} className="animate-spin" />
|
||||
保存中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save size={16} />
|
||||
保存配置
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="relative" ref={platformDropdownRef}>
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => setShowPlatformSelect(!showPlatformSelect)}
|
||||
disabled={isCheckingConflicts}
|
||||
>
|
||||
{isCheckingConflicts ? (
|
||||
<>
|
||||
<Loader2 size={16} className="animate-spin" />
|
||||
检测中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Search size={16} />
|
||||
检查规则冲突
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
{showPlatformSelect && (
|
||||
<div className="absolute right-0 top-full mt-2 w-40 bg-bg-card border border-border-subtle rounded-xl shadow-lg z-50 overflow-hidden">
|
||||
{platformOptions.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => handleCheckConflicts(opt.value)}
|
||||
className="w-full px-4 py-2.5 text-left text-sm text-text-primary hover:bg-bg-elevated transition-colors"
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button variant="primary" onClick={handleSaveBrief} disabled={isSaving}>
|
||||
{isSaving ? (
|
||||
<>
|
||||
<Loader2 size={16} className="animate-spin" />
|
||||
保存中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save size={16} />
|
||||
保存配置
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Brief配置 */}
|
||||
@@ -514,40 +690,99 @@ export default function ProjectConfigPage() {
|
||||
{/* 参考资料 */}
|
||||
<div>
|
||||
<label className="text-sm text-text-secondary mb-2 block">参考资料</label>
|
||||
<div className="space-y-2">
|
||||
{attachments.map((att) => (
|
||||
<div key={att.id} className="flex items-center gap-3 p-3 rounded-lg bg-bg-elevated">
|
||||
<FileText size={16} className="text-accent-indigo" />
|
||||
<span className="flex-1 text-text-primary">{att.name}</span>
|
||||
{att.size && <span className="text-xs text-text-tertiary">{att.size}</span>}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeAttachment(att.id)}
|
||||
className="p-1 rounded hover:bg-bg-page text-text-tertiary hover:text-accent-coral transition-colors"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
|
||||
<label className="flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg border border-dashed border-border-subtle bg-bg-elevated text-text-primary hover:border-accent-indigo/50 hover:bg-bg-page transition-colors cursor-pointer w-full text-sm mb-3">
|
||||
<Upload size={16} className="text-accent-indigo" />
|
||||
点击上传参考资料(可多选)
|
||||
<input
|
||||
type="file"
|
||||
multiple
|
||||
onChange={handleAttachmentUpload}
|
||||
className="hidden"
|
||||
/>
|
||||
</label>
|
||||
|
||||
{/* 文件列表 */}
|
||||
<div className="border border-border-subtle rounded-lg overflow-hidden">
|
||||
<div className="flex items-center justify-between px-4 py-2 bg-bg-elevated border-b border-border-subtle">
|
||||
<span className="text-xs font-medium text-text-secondary flex items-center gap-1.5">
|
||||
<FileText size={12} className="text-accent-indigo" />
|
||||
附件列表
|
||||
</span>
|
||||
<span className="text-xs text-text-tertiary">
|
||||
{attachments.length + uploadingFiles.filter(f => f.status === 'uploading').length} 个文件
|
||||
{uploadingFiles.some(f => f.status === 'uploading') && (
|
||||
<span className="text-accent-indigo ml-1">· 上传中</span>
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{attachments.length === 0 && uploadingFiles.length === 0 ? (
|
||||
<div className="px-4 py-5 text-center">
|
||||
<p className="text-xs text-text-tertiary">暂无附件</p>
|
||||
</div>
|
||||
))}
|
||||
<label className="flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg border border-border-subtle bg-bg-elevated text-text-primary hover:bg-bg-page transition-colors cursor-pointer w-full text-sm">
|
||||
{isUploading ? (
|
||||
<>
|
||||
<Loader2 size={16} className="animate-spin" />
|
||||
上传中 {uploadProgress}%
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={16} />
|
||||
上传参考资料
|
||||
</>
|
||||
)}
|
||||
<input
|
||||
type="file"
|
||||
onChange={handleAttachmentUpload}
|
||||
className="hidden"
|
||||
disabled={isUploading}
|
||||
/>
|
||||
</label>
|
||||
) : (
|
||||
<div className="divide-y divide-border-subtle">
|
||||
{/* 已完成的文件 */}
|
||||
{attachments.map((att) => (
|
||||
<div key={att.id} className="flex items-center gap-3 px-4 py-2.5">
|
||||
<CheckCircle size={14} className="text-accent-green flex-shrink-0" />
|
||||
<FileText size={14} className="text-text-tertiary flex-shrink-0" />
|
||||
<span className="flex-1 text-sm text-text-primary truncate">{att.name}</span>
|
||||
{att.size && <span className="text-xs text-text-tertiary">{att.size}</span>}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeAttachment(att.id)}
|
||||
className="p-1 rounded hover:bg-bg-elevated text-text-tertiary hover:text-accent-coral transition-colors"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
|
||||
{/* 上传中/失败的文件 */}
|
||||
{uploadingFiles.map((file) => (
|
||||
<div key={file.id} className="px-4 py-2.5">
|
||||
<div className="flex items-center gap-3">
|
||||
{file.status === 'uploading' && (
|
||||
<Loader2 size={14} className="animate-spin text-accent-indigo flex-shrink-0" />
|
||||
)}
|
||||
{file.status === 'error' && (
|
||||
<AlertCircle size={14} className="text-accent-coral flex-shrink-0" />
|
||||
)}
|
||||
<FileText size={14} className="text-text-tertiary flex-shrink-0" />
|
||||
<span className={`flex-1 text-sm truncate ${
|
||||
file.status === 'error' ? 'text-accent-coral' : 'text-text-primary'
|
||||
}`}>{file.name}</span>
|
||||
<span className="text-xs text-text-tertiary whitespace-nowrap min-w-[40px] text-right">
|
||||
{file.status === 'uploading' ? `${file.progress}%` : file.size}
|
||||
</span>
|
||||
{file.status === 'error' && (
|
||||
<button type="button" onClick={() => retryAttachmentUpload(file.id)}
|
||||
className="p-1 rounded hover:bg-bg-elevated text-accent-indigo transition-colors" title="重试">
|
||||
<RotateCcw size={14} />
|
||||
</button>
|
||||
)}
|
||||
{file.status !== 'uploading' && (
|
||||
<button type="button" onClick={() => removeUploadingFile(file.id)}
|
||||
className="p-1 rounded hover:bg-bg-elevated text-text-tertiary hover:text-accent-coral transition-colors" title="删除">
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{file.status === 'uploading' && (
|
||||
<div className="mt-1.5 ml-[28px] h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div className="h-full bg-accent-indigo rounded-full transition-all duration-300"
|
||||
style={{ width: `${file.progress}%` }} />
|
||||
</div>
|
||||
)}
|
||||
{file.status === 'error' && file.error && (
|
||||
<p className="mt-1 ml-[28px] text-xs text-accent-coral">{file.error}</p>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
@@ -757,6 +992,54 @@ export default function ProjectConfigPage() {
|
||||
</CardContent>
|
||||
)}
|
||||
</Card>
|
||||
|
||||
{/* 规则冲突检测结果弹窗 */}
|
||||
<Modal
|
||||
isOpen={showConflictModal}
|
||||
onClose={() => setShowConflictModal(false)}
|
||||
title="规则冲突检测结果"
|
||||
size="lg"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
{conflicts.length === 0 ? (
|
||||
<div className="py-8 text-center">
|
||||
<CheckCircle size={48} className="mx-auto text-accent-green mb-3" />
|
||||
<p className="text-text-primary font-medium">未发现冲突</p>
|
||||
<p className="text-sm text-text-secondary mt-1">Brief 内容与平台规则兼容</p>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="flex items-center gap-2 p-3 bg-accent-amber/10 rounded-lg border border-accent-amber/30">
|
||||
<AlertTriangle size={16} className="text-accent-amber flex-shrink-0" />
|
||||
<p className="text-sm text-accent-amber">
|
||||
发现 {conflicts.length} 处规则冲突,建议在发布前修改
|
||||
</p>
|
||||
</div>
|
||||
{conflicts.map((conflict, index) => (
|
||||
<div key={index} className="p-4 bg-bg-elevated rounded-xl border border-border-subtle space-y-2">
|
||||
<div className="flex items-start gap-2">
|
||||
<span className="text-xs font-medium text-accent-amber bg-accent-amber/15 px-2 py-0.5 rounded">Brief</span>
|
||||
<span className="text-sm text-text-primary">{conflict.brief_rule}</span>
|
||||
</div>
|
||||
<div className="flex items-start gap-2">
|
||||
<span className="text-xs font-medium text-accent-coral bg-accent-coral/15 px-2 py-0.5 rounded">平台</span>
|
||||
<span className="text-sm text-text-primary">{conflict.platform_rule}</span>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 pt-1 border-t border-border-subtle">
|
||||
<span className="text-xs font-medium text-accent-indigo bg-accent-indigo/15 px-2 py-0.5 rounded">建议</span>
|
||||
<span className="text-sm text-text-secondary">{conflict.suggestion}</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
<div className="flex justify-end pt-2">
|
||||
<Button variant="secondary" onClick={() => setShowConflictModal(false)}>
|
||||
关闭
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
Calendar,
|
||||
Users,
|
||||
FileText,
|
||||
Video,
|
||||
|
||||
Clock,
|
||||
CheckCircle,
|
||||
XCircle,
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
Loader2
|
||||
} from 'lucide-react'
|
||||
import { getPlatformInfo } from '@/lib/platforms'
|
||||
import { mapTaskToUI } from '@/lib/taskStageMapper'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
@@ -77,6 +78,24 @@ const mockTasks: TaskResponse[] = [
|
||||
appeal_count: 0, is_appeal: false,
|
||||
created_at: '2026-02-06T10:00:00Z', updated_at: '2026-02-06T10:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'task-004', name: '美白精华测评', sequence: 4,
|
||||
stage: 'script_agency_review',
|
||||
project: { id: 'proj-001', name: 'XX品牌618推广' },
|
||||
agency: { id: 'AG456789', name: '创意无限' },
|
||||
creator: { id: 'cr-004', name: '时尚小王' },
|
||||
appeal_count: 0, is_appeal: false,
|
||||
created_at: '2026-02-07T09:00:00Z', updated_at: '2026-02-07T09:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'task-005', name: '防晒霜种草', sequence: 5,
|
||||
stage: 'script_upload',
|
||||
project: { id: 'proj-001', name: 'XX品牌618推广' },
|
||||
agency: { id: 'AG789012', name: '星耀传媒' },
|
||||
creator: { id: 'cr-001', name: '小美护肤' },
|
||||
appeal_count: 0, is_appeal: false,
|
||||
created_at: '2026-02-07T11:00:00Z', updated_at: '2026-02-07T11:00:00Z',
|
||||
},
|
||||
]
|
||||
|
||||
const mockManagedAgencies: AgencyDetail[] = [
|
||||
@@ -136,6 +155,137 @@ function DetailSkeleton() {
|
||||
)
|
||||
}
|
||||
|
||||
// ==================== 任务进度条 ====================
|
||||
|
||||
const SCRIPT_STEPS = [
|
||||
{ key: 'script_upload', label: '上传' },
|
||||
{ key: 'script_ai_review', label: 'AI' },
|
||||
{ key: 'script_agency_review', label: '代理商' },
|
||||
{ key: 'script_brand_review', label: '品牌' },
|
||||
]
|
||||
|
||||
const VIDEO_STEPS = [
|
||||
{ key: 'video_upload', label: '上传' },
|
||||
{ key: 'video_ai_review', label: 'AI' },
|
||||
{ key: 'video_agency_review', label: '代理商' },
|
||||
{ key: 'video_brand_review', label: '品牌' },
|
||||
]
|
||||
|
||||
function StepDot({ status }: { status: 'done' | 'current' | 'error' | 'pending' }) {
|
||||
const base = 'w-3 h-3 rounded-full border-2 flex-shrink-0'
|
||||
if (status === 'done') return <div className={`${base} bg-accent-green border-accent-green`} />
|
||||
if (status === 'current') return <div className={`${base} bg-accent-indigo border-accent-indigo animate-pulse`} />
|
||||
if (status === 'error') return <div className={`${base} bg-accent-coral border-accent-coral`} />
|
||||
return <div className={`${base} bg-transparent border-border-strong`} />
|
||||
}
|
||||
|
||||
function StepLine({ status }: { status: 'done' | 'pending' | 'error' }) {
|
||||
if (status === 'done') return <div className="w-4 h-0.5 bg-accent-green mx-0.5" />
|
||||
if (status === 'error') return <div className="w-4 h-0.5 bg-accent-coral mx-0.5" />
|
||||
return <div className="w-4 h-0.5 bg-border-strong mx-0.5" />
|
||||
}
|
||||
|
||||
function TaskProgressBar({ task }: { task: TaskResponse }) {
|
||||
const ui = mapTaskToUI(task)
|
||||
|
||||
const scriptStatuses: Array<'done' | 'current' | 'error' | 'pending'> = [
|
||||
ui.scriptStage.submit, ui.scriptStage.ai, ui.scriptStage.agency, ui.scriptStage.brand,
|
||||
]
|
||||
const videoStatuses: Array<'done' | 'current' | 'error' | 'pending'> = [
|
||||
ui.videoStage.submit, ui.videoStage.ai, ui.videoStage.agency, ui.videoStage.brand,
|
||||
]
|
||||
|
||||
const isCompleted = task.stage === 'completed'
|
||||
const isRejected = task.stage === 'rejected'
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-1">
|
||||
{/* 脚本阶段 */}
|
||||
<div className="flex items-center gap-0">
|
||||
<span className="text-[10px] text-text-tertiary mr-1.5 w-6">脚本</span>
|
||||
{SCRIPT_STEPS.map((step, i) => (
|
||||
<div key={step.key} className="flex items-center">
|
||||
<div className="relative group">
|
||||
<StepDot status={scriptStatuses[i]} />
|
||||
<div className="absolute bottom-full left-1/2 -translate-x-1/2 mb-1 px-1.5 py-0.5 bg-bg-card border border-border-subtle rounded text-[10px] text-text-secondary whitespace-nowrap opacity-0 group-hover:opacity-100 pointer-events-none transition-opacity z-10">
|
||||
{step.label}
|
||||
</div>
|
||||
</div>
|
||||
{i < SCRIPT_STEPS.length - 1 && (
|
||||
<StepLine status={scriptStatuses[i] === 'done' ? 'done' : scriptStatuses[i] === 'error' ? 'error' : 'pending'} />
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* 分隔线 */}
|
||||
<div className="w-3 h-0.5 bg-border-subtle mx-0.5" />
|
||||
|
||||
{/* 视频阶段 */}
|
||||
<div className="flex items-center gap-0">
|
||||
<span className="text-[10px] text-text-tertiary mr-1.5 w-6">视频</span>
|
||||
{VIDEO_STEPS.map((step, i) => (
|
||||
<div key={step.key} className="flex items-center">
|
||||
<div className="relative group">
|
||||
<StepDot status={videoStatuses[i]} />
|
||||
<div className="absolute bottom-full left-1/2 -translate-x-1/2 mb-1 px-1.5 py-0.5 bg-bg-card border border-border-subtle rounded text-[10px] text-text-secondary whitespace-nowrap opacity-0 group-hover:opacity-100 pointer-events-none transition-opacity z-10">
|
||||
{step.label}
|
||||
</div>
|
||||
</div>
|
||||
{i < VIDEO_STEPS.length - 1 && (
|
||||
<StepLine status={videoStatuses[i] === 'done' ? 'done' : videoStatuses[i] === 'error' ? 'error' : 'pending'} />
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* 完成标记 */}
|
||||
<div className="ml-1.5">
|
||||
{isCompleted ? (
|
||||
<CheckCircle size={14} className="text-accent-green" />
|
||||
) : isRejected ? (
|
||||
<XCircle size={14} className="text-accent-coral" />
|
||||
) : (
|
||||
<div className="w-3.5 h-3.5" />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
interface TaskGroup {
|
||||
agencyId: string
|
||||
agencyName: string
|
||||
creators: {
|
||||
creatorId: string
|
||||
creatorName: string
|
||||
tasks: TaskResponse[]
|
||||
}[]
|
||||
}
|
||||
|
||||
function groupTasksByAgencyCreator(tasks: TaskResponse[]): TaskGroup[] {
|
||||
const agencyMap = new Map<string, TaskGroup>()
|
||||
|
||||
for (const task of tasks) {
|
||||
if (!agencyMap.has(task.agency.id)) {
|
||||
agencyMap.set(task.agency.id, {
|
||||
agencyId: task.agency.id,
|
||||
agencyName: task.agency.name,
|
||||
creators: [],
|
||||
})
|
||||
}
|
||||
const group = agencyMap.get(task.agency.id)!
|
||||
let creator = group.creators.find(c => c.creatorId === task.creator.id)
|
||||
if (!creator) {
|
||||
creator = { creatorId: task.creator.id, creatorName: task.creator.name, tasks: [] }
|
||||
group.creators.push(creator)
|
||||
}
|
||||
creator.tasks.push(task)
|
||||
}
|
||||
|
||||
return Array.from(agencyMap.values())
|
||||
}
|
||||
|
||||
export default function ProjectDetailPage() {
|
||||
const router = useRouter()
|
||||
const params = useParams()
|
||||
@@ -144,7 +294,7 @@ export default function ProjectDetailPage() {
|
||||
const { subscribe } = useSSE()
|
||||
|
||||
const [project, setProject] = useState<ProjectResponse | null>(null)
|
||||
const [recentTasks, setRecentTasks] = useState<TaskResponse[]>([])
|
||||
const [allTasks, setAllTasks] = useState<TaskResponse[]>([])
|
||||
const [managedAgencies, setManagedAgencies] = useState<AgencyDetail[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
@@ -162,7 +312,7 @@ export default function ProjectDetailPage() {
|
||||
const loadData = useCallback(async () => {
|
||||
if (USE_MOCK) {
|
||||
setProject(mockProject)
|
||||
setRecentTasks(mockTasks)
|
||||
setAllTasks(mockTasks)
|
||||
setManagedAgencies(mockManagedAgencies)
|
||||
setLoading(false)
|
||||
return
|
||||
@@ -171,11 +321,11 @@ export default function ProjectDetailPage() {
|
||||
try {
|
||||
const [projectData, tasksData, agenciesData] = await Promise.all([
|
||||
api.getProject(projectId),
|
||||
api.listTasks(1, 10),
|
||||
api.listTasks(1, 100, undefined, projectId),
|
||||
api.listBrandAgencies(),
|
||||
])
|
||||
setProject(projectData)
|
||||
setRecentTasks(tasksData.items.filter(t => t.project.id === projectId).slice(0, 5))
|
||||
setAllTasks(tasksData.items)
|
||||
setManagedAgencies(agenciesData.items)
|
||||
} catch (err) {
|
||||
console.error('Failed to load project:', err)
|
||||
@@ -337,54 +487,74 @@ export default function ProjectDetailPage() {
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
||||
{/* 最近任务 */}
|
||||
{/* 任务进度 */}
|
||||
<Card className="lg:col-span-2">
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between">
|
||||
<span>最近提交</span>
|
||||
<span>任务进度</span>
|
||||
<Link href="/brand/review">
|
||||
<Button variant="ghost" size="sm">
|
||||
查看全部 <ChevronRight size={16} />
|
||||
审核列表 <ChevronRight size={16} />
|
||||
</Button>
|
||||
</Link>
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{recentTasks.length > 0 ? (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="border-b border-border-subtle text-left text-sm text-text-secondary">
|
||||
<th className="pb-3 font-medium">任务</th>
|
||||
<th className="pb-3 font-medium">达人</th>
|
||||
<th className="pb-3 font-medium">代理商</th>
|
||||
<th className="pb-3 font-medium">状态</th>
|
||||
<th className="pb-3 font-medium">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{recentTasks.map((task) => (
|
||||
<tr key={task.id} className="border-b border-border-subtle last:border-0 hover:bg-bg-elevated">
|
||||
<td className="py-4 font-medium text-text-primary">{task.name}</td>
|
||||
<td className="py-4 text-text-secondary">{task.creator.name}</td>
|
||||
<td className="py-4">
|
||||
<span className="inline-flex items-center gap-1.5 px-2 py-1 rounded-md bg-bg-elevated text-sm">
|
||||
<Building2 size={14} className="text-accent-indigo" />
|
||||
<span className="text-text-secondary">{task.agency.name}</span>
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4"><TaskStatusTag stage={task.stage} /></td>
|
||||
<td className="py-4">
|
||||
<Link href={`/agency/review/${task.id}`}>
|
||||
<Button size="sm" variant={task.stage.includes('review') ? 'primary' : 'secondary'}>
|
||||
{task.stage.includes('review') ? '审核' : '查看'}
|
||||
</Button>
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{allTasks.length > 0 ? (
|
||||
<div className="space-y-4">
|
||||
{/* 图例 */}
|
||||
<div className="flex items-center gap-4 text-[10px] text-text-tertiary pb-2 border-b border-border-subtle">
|
||||
<span className="flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-accent-green inline-block" /> 已完成</span>
|
||||
<span className="flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-accent-indigo inline-block" /> 进行中</span>
|
||||
<span className="flex items-center gap-1"><span className="w-2 h-2 rounded-full border border-border-strong inline-block" /> 待处理</span>
|
||||
<span className="flex items-center gap-1"><span className="w-2 h-2 rounded-full bg-accent-coral inline-block" /> 已驳回</span>
|
||||
</div>
|
||||
|
||||
{groupTasksByAgencyCreator(allTasks).map((group) => (
|
||||
<div key={group.agencyId} className="space-y-2">
|
||||
{/* 代理商标题 */}
|
||||
<div className="flex items-center gap-2">
|
||||
<Building2 size={14} className="text-accent-indigo" />
|
||||
<span className="text-sm font-medium text-text-primary">{group.agencyName}</span>
|
||||
<span className="text-xs text-text-tertiary">
|
||||
({group.creators.reduce((sum, c) => sum + c.tasks.length, 0)} 个任务)
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 达人列表 */}
|
||||
<div className="ml-4 space-y-1">
|
||||
{group.creators.map((creator) => (
|
||||
<div key={creator.creatorId} className="space-y-1">
|
||||
{/* 达人名称 */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<div className="w-5 h-5 rounded-full bg-accent-green/15 flex items-center justify-center">
|
||||
<Users size={10} className="text-accent-green" />
|
||||
</div>
|
||||
<span className="text-xs font-medium text-text-secondary">{creator.creatorName}</span>
|
||||
</div>
|
||||
|
||||
{/* 任务进度条 */}
|
||||
<div className="ml-6 space-y-1.5">
|
||||
{creator.tasks.map((task) => (
|
||||
<div key={task.id} className="flex items-center gap-3 py-1.5 px-2 rounded-lg hover:bg-bg-elevated group/task">
|
||||
<span className="text-xs text-text-primary min-w-[80px] truncate font-medium">{task.name}</span>
|
||||
<TaskProgressBar task={task} />
|
||||
<span className="text-[10px] text-text-tertiary ml-auto hidden group-hover/task:block">
|
||||
<TaskStatusTag stage={task.stage} />
|
||||
</span>
|
||||
<Link href={task.stage.includes('video') ? `/brand/review/video/${task.id}` : `/brand/review/script/${task.id}`}>
|
||||
<button type="button" className="text-xs text-accent-indigo hover:text-accent-indigo/80 opacity-0 group-hover/task:opacity-100 transition-opacity">
|
||||
{task.stage.includes('brand_review') ? '审核' : '查看'}
|
||||
</button>
|
||||
</Link>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-8 text-text-tertiary text-sm">暂无任务</div>
|
||||
|
||||
@@ -12,17 +12,38 @@ import {
|
||||
Calendar,
|
||||
FileText,
|
||||
CheckCircle,
|
||||
X,
|
||||
Users,
|
||||
AlertCircle,
|
||||
Search,
|
||||
Building2,
|
||||
Check,
|
||||
Loader2
|
||||
Loader2,
|
||||
Trash2,
|
||||
RotateCcw
|
||||
} from 'lucide-react'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useOSSUpload } from '@/hooks/useOSSUpload'
|
||||
import { platformOptions } from '@/lib/platforms'
|
||||
import type { AgencyDetail } from '@/types/organization'
|
||||
import type { BriefAttachment } from '@/types/brief'
|
||||
|
||||
// 单个文件的上传状态
|
||||
interface UploadFileItem {
|
||||
id: string
|
||||
name: string
|
||||
size: string
|
||||
rawSize: number
|
||||
status: 'uploading' | 'success' | 'error'
|
||||
progress: number
|
||||
url?: string
|
||||
error?: string
|
||||
file?: File // 保留引用用于重试
|
||||
}
|
||||
|
||||
function formatFileSize(bytes: number): string {
|
||||
if (bytes < 1024) return bytes + 'B'
|
||||
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB'
|
||||
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + 'MB'
|
||||
return (bytes / (1024 * 1024 * 1024)).toFixed(1) + 'GB'
|
||||
}
|
||||
|
||||
// ==================== Mock 数据 ====================
|
||||
const mockAgencies: AgencyDetail[] = [
|
||||
@@ -37,19 +58,25 @@ const mockAgencies: AgencyDetail[] = [
|
||||
export default function CreateProjectPage() {
|
||||
const router = useRouter()
|
||||
const toast = useToast()
|
||||
const { upload, isUploading, progress: uploadProgress } = useOSSUpload('general')
|
||||
|
||||
const [projectName, setProjectName] = useState('')
|
||||
const [description, setDescription] = useState('')
|
||||
const [platform, setPlatform] = useState('douyin')
|
||||
const [deadline, setDeadline] = useState('')
|
||||
const [briefFile, setBriefFile] = useState<File | null>(null)
|
||||
const [briefFileUrl, setBriefFileUrl] = useState<string | null>(null)
|
||||
const [uploadFiles, setUploadFiles] = useState<UploadFileItem[]>([])
|
||||
const [selectedAgencies, setSelectedAgencies] = useState<string[]>([])
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const [agencySearch, setAgencySearch] = useState('')
|
||||
const [agencies, setAgencies] = useState<AgencyDetail[]>([])
|
||||
const [loadingAgencies, setLoadingAgencies] = useState(true)
|
||||
|
||||
// 从成功上传的文件中提取 BriefAttachment
|
||||
const briefFiles: BriefAttachment[] = uploadFiles
|
||||
.filter(f => f.status === 'success' && f.url)
|
||||
.map(f => ({ id: f.id, name: f.name, url: f.url!, size: f.size }))
|
||||
|
||||
const hasUploading = uploadFiles.some(f => f.status === 'uploading')
|
||||
|
||||
useEffect(() => {
|
||||
const loadAgencies = async () => {
|
||||
if (USE_MOCK) {
|
||||
@@ -76,22 +103,85 @@ export default function CreateProjectPage() {
|
||||
agency.id.toLowerCase().includes(agencySearch.toLowerCase())
|
||||
)
|
||||
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (!file) return
|
||||
setBriefFile(file)
|
||||
|
||||
if (!USE_MOCK) {
|
||||
try {
|
||||
const result = await upload(file)
|
||||
setBriefFileUrl(result.url)
|
||||
} catch (err) {
|
||||
toast.error('文件上传失败')
|
||||
setBriefFile(null)
|
||||
// 上传单个文件(独立跟踪进度)
|
||||
const uploadSingleFile = async (file: File, fileId: string) => {
|
||||
if (USE_MOCK) {
|
||||
// Mock:模拟进度
|
||||
for (let p = 20; p <= 80; p += 20) {
|
||||
await new Promise(r => setTimeout(r, 300))
|
||||
setUploadFiles(prev => prev.map(f => f.id === fileId ? { ...f, progress: p } : f))
|
||||
}
|
||||
} else {
|
||||
setBriefFileUrl('mock://brief-file.pdf')
|
||||
await new Promise(r => setTimeout(r, 300))
|
||||
setUploadFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, status: 'success', progress: 100, url: `mock://${file.name}` }
|
||||
: f
|
||||
))
|
||||
toast.success(`${file.name} 上传完成`)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await api.proxyUpload(file, 'general', (pct) => {
|
||||
setUploadFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, progress: Math.min(95, Math.round(pct * 0.95)) }
|
||||
: f
|
||||
))
|
||||
})
|
||||
setUploadFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, status: 'success', progress: 100, url: result.url }
|
||||
: f
|
||||
))
|
||||
toast.success(`${file.name} 上传完成`)
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : '上传失败'
|
||||
setUploadFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, status: 'error', error: msg }
|
||||
: f
|
||||
))
|
||||
toast.error(`${file.name} 上传失败: ${msg}`)
|
||||
}
|
||||
}
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const files = e.target.files
|
||||
if (!files || files.length === 0) return
|
||||
|
||||
const fileList = Array.from(files)
|
||||
e.target.value = ''
|
||||
toast.info(`已选择 ${fileList.length} 个文件,开始上传...`)
|
||||
|
||||
// 立即添加所有文件到列表(uploading 状态)
|
||||
const newItems: UploadFileItem[] = fileList.map(file => ({
|
||||
id: `att-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
name: file.name,
|
||||
size: formatFileSize(file.size),
|
||||
rawSize: file.size,
|
||||
status: 'uploading' as const,
|
||||
progress: 0,
|
||||
file,
|
||||
}))
|
||||
|
||||
setUploadFiles(prev => [...prev, ...newItems])
|
||||
|
||||
// 并发上传所有文件
|
||||
newItems.forEach(item => {
|
||||
uploadSingleFile(item.file!, item.id)
|
||||
})
|
||||
}
|
||||
|
||||
// 重试失败的上传
|
||||
const retryUpload = (fileId: string) => {
|
||||
const item = uploadFiles.find(f => f.id === fileId)
|
||||
if (!item?.file) return
|
||||
setUploadFiles(prev => prev.map(f => f.id === fileId
|
||||
? { ...f, status: 'uploading', progress: 0, error: undefined }
|
||||
: f
|
||||
))
|
||||
uploadSingleFile(item.file, fileId)
|
||||
}
|
||||
|
||||
const removeFile = (id: string) => {
|
||||
setUploadFiles(prev => prev.filter(f => f.id !== id))
|
||||
}
|
||||
|
||||
const toggleAgency = (agencyId: string) => {
|
||||
@@ -116,15 +206,15 @@ export default function CreateProjectPage() {
|
||||
const project = await api.createProject({
|
||||
name: projectName.trim(),
|
||||
description: description.trim() || undefined,
|
||||
platform,
|
||||
deadline,
|
||||
agency_ids: selectedAgencies,
|
||||
})
|
||||
|
||||
// If brief file was uploaded, create brief
|
||||
if (briefFileUrl && briefFile) {
|
||||
// If brief files were uploaded, create brief with attachments
|
||||
if (briefFiles.length > 0) {
|
||||
await api.createBrief(project.id, {
|
||||
file_url: briefFileUrl,
|
||||
file_name: briefFile.name,
|
||||
attachments: briefFiles,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -177,6 +267,35 @@ export default function CreateProjectPage() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 发布平台 */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-primary mb-2">
|
||||
发布平台 <span className="text-accent-coral">*</span>
|
||||
</label>
|
||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
|
||||
{platformOptions.map((p) => {
|
||||
const isSelected = platform === p.id
|
||||
return (
|
||||
<button
|
||||
key={p.id}
|
||||
type="button"
|
||||
onClick={() => setPlatform(p.id)}
|
||||
className={`flex items-center gap-3 px-4 py-3 rounded-xl border-2 transition-all ${
|
||||
isSelected
|
||||
? `${p.borderColor} ${p.bgColor} border-opacity-100`
|
||||
: 'border-border-subtle hover:border-accent-indigo/30'
|
||||
}`}
|
||||
>
|
||||
<span className="text-xl">{p.icon}</span>
|
||||
<span className={`font-medium ${isSelected ? p.textColor : 'text-text-secondary'}`}>
|
||||
{p.name}
|
||||
</span>
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 截止日期 */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-primary mb-2">
|
||||
@@ -195,35 +314,125 @@ export default function CreateProjectPage() {
|
||||
|
||||
{/* Brief 上传 */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-text-primary mb-2">上传 Brief</label>
|
||||
<div className="border-2 border-dashed border-border-subtle rounded-lg p-8 text-center hover:border-accent-indigo/50 transition-colors">
|
||||
{briefFile ? (
|
||||
<div className="flex items-center justify-center gap-3">
|
||||
<FileText size={24} className="text-accent-indigo" />
|
||||
<span className="text-text-primary">{briefFile.name}</span>
|
||||
{isUploading && (
|
||||
<span className="text-xs text-text-tertiary">{uploadProgress}%</span>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => { setBriefFile(null); setBriefFileUrl(null) }}
|
||||
className="p-1 hover:bg-bg-elevated rounded-full"
|
||||
>
|
||||
<X size={16} className="text-text-tertiary" />
|
||||
</button>
|
||||
<label className="block text-sm font-medium text-text-primary mb-2">
|
||||
上传 Brief 文档
|
||||
</label>
|
||||
|
||||
{/* 上传区域 */}
|
||||
<label className="border-2 border-dashed border-border-subtle rounded-lg p-6 text-center hover:border-accent-indigo/50 transition-colors cursor-pointer block mb-3">
|
||||
<Upload size={28} className="mx-auto text-text-tertiary mb-2" />
|
||||
<p className="text-text-secondary text-sm mb-1">
|
||||
{uploadFiles.length > 0 ? '继续添加文件' : '点击上传 Brief 文件(可多选)'}
|
||||
</p>
|
||||
<p className="text-xs text-text-tertiary">支持 PDF、Word、Excel、图片等格式</p>
|
||||
<input
|
||||
type="file"
|
||||
multiple
|
||||
onChange={handleFileChange}
|
||||
className="hidden"
|
||||
/>
|
||||
</label>
|
||||
|
||||
{/* 文件列表(含进度)— 始终显示,空状态也有提示 */}
|
||||
<div className={`border rounded-lg overflow-hidden ${uploadFiles.length > 0 ? 'border-accent-indigo/40 bg-accent-indigo/5' : 'border-border-subtle'}`}>
|
||||
<div className={`flex items-center justify-between px-4 py-2.5 border-b ${uploadFiles.length > 0 ? 'bg-accent-indigo/10 border-accent-indigo/20' : 'bg-bg-elevated border-border-subtle'}`}>
|
||||
<span className="text-sm font-medium text-text-primary flex items-center gap-2">
|
||||
<FileText size={14} className="text-accent-indigo" />
|
||||
已选文件
|
||||
</span>
|
||||
{uploadFiles.length > 0 && (
|
||||
<span className="text-xs text-text-tertiary">
|
||||
{briefFiles.length}/{uploadFiles.length} 完成
|
||||
{uploadFiles.some(f => f.status === 'error') && (
|
||||
<span className="text-accent-coral ml-1">
|
||||
· {uploadFiles.filter(f => f.status === 'error').length} 失败
|
||||
</span>
|
||||
)}
|
||||
{hasUploading && (
|
||||
<span className="text-accent-indigo ml-1">
|
||||
· 上传中...
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{uploadFiles.length === 0 ? (
|
||||
<div className="px-4 py-6 text-center">
|
||||
<p className="text-sm text-text-tertiary">还没有选择文件,点击上方区域选择</p>
|
||||
</div>
|
||||
) : (
|
||||
<label className="cursor-pointer">
|
||||
<Upload size={32} className="mx-auto text-text-tertiary mb-3" />
|
||||
<p className="text-text-secondary mb-1">点击或拖拽上传 Brief 文件</p>
|
||||
<p className="text-xs text-text-tertiary">支持 PDF、Word、Excel 格式</p>
|
||||
<input
|
||||
type="file"
|
||||
accept=".pdf,.doc,.docx,.xls,.xlsx"
|
||||
onChange={handleFileChange}
|
||||
className="hidden"
|
||||
/>
|
||||
</label>
|
||||
<div className="divide-y divide-border-subtle">
|
||||
{uploadFiles.map((file) => (
|
||||
<div key={file.id} className="px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
{/* 状态图标 */}
|
||||
{file.status === 'uploading' && (
|
||||
<Loader2 size={16} className="animate-spin text-accent-indigo flex-shrink-0" />
|
||||
)}
|
||||
{file.status === 'success' && (
|
||||
<CheckCircle size={16} className="text-accent-green flex-shrink-0" />
|
||||
)}
|
||||
{file.status === 'error' && (
|
||||
<AlertCircle size={16} className="text-accent-coral flex-shrink-0" />
|
||||
)}
|
||||
|
||||
{/* 文件图标+文件名 */}
|
||||
<FileText size={14} className="text-text-tertiary flex-shrink-0" />
|
||||
<span className={`flex-1 text-sm truncate ${
|
||||
file.status === 'error' ? 'text-accent-coral' : 'text-text-primary'
|
||||
}`}>
|
||||
{file.name}
|
||||
</span>
|
||||
|
||||
{/* 大小/进度文字 */}
|
||||
<span className="text-xs text-text-tertiary whitespace-nowrap min-w-[48px] text-right">
|
||||
{file.status === 'uploading'
|
||||
? `${file.progress}%`
|
||||
: file.size
|
||||
}
|
||||
</span>
|
||||
|
||||
{/* 操作按钮 */}
|
||||
{file.status === 'error' && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => retryUpload(file.id)}
|
||||
className="p-1 rounded hover:bg-bg-elevated text-accent-indigo transition-colors"
|
||||
title="重试"
|
||||
>
|
||||
<RotateCcw size={14} />
|
||||
</button>
|
||||
)}
|
||||
{file.status !== 'uploading' && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeFile(file.id)}
|
||||
className="p-1 rounded hover:bg-bg-elevated text-text-tertiary hover:text-accent-coral transition-colors"
|
||||
title="删除"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 进度条 */}
|
||||
{file.status === 'uploading' && (
|
||||
<div className="mt-2 ml-[30px] h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div
|
||||
className="h-full bg-accent-indigo rounded-full transition-all duration-300"
|
||||
style={{ width: `${file.progress}%` }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 错误提示 */}
|
||||
{file.status === 'error' && file.error && (
|
||||
<p className="mt-1 ml-[30px] text-xs text-accent-coral">{file.error}</p>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
@@ -311,7 +520,7 @@ export default function CreateProjectPage() {
|
||||
<Button variant="secondary" onClick={() => router.back()}>
|
||||
取消
|
||||
</Button>
|
||||
<Button onClick={handleSubmit} disabled={!isValid || isSubmitting || isUploading}>
|
||||
<Button onClick={handleSubmit} disabled={!isValid || isSubmitting || hasUploading}>
|
||||
{isSubmitting ? (
|
||||
<>
|
||||
<Loader2 size={16} className="animate-spin" />
|
||||
|
||||
@@ -173,12 +173,12 @@ function mapTaskToUI(task: TaskResponse, type: 'script' | 'video'): UITask {
|
||||
// 格式化提交时间
|
||||
const submittedAt = formatDateTime(task.updated_at)
|
||||
|
||||
// 平台信息:后端目前不返回平台字段,默认 douyin
|
||||
const platform = 'douyin'
|
||||
// 平台信息:从项目获取
|
||||
const platform = task.project.platform || ''
|
||||
|
||||
return {
|
||||
id: task.id,
|
||||
title: task.name,
|
||||
title: `${task.project.name} · ${task.name}`,
|
||||
fileName,
|
||||
fileSize: isScript ? '--' : '--',
|
||||
creatorName: task.creator.name,
|
||||
|
||||
@@ -63,7 +63,7 @@ const mockScriptTask = {
|
||||
},
|
||||
aiAnalysis: {
|
||||
violations: [
|
||||
{ id: 'v1', type: '违禁词', content: '神器', suggestion: '建议替换为"好物"或"必备品"', severity: 'medium' },
|
||||
{ id: 'v1', type: '违禁词', content: '神器', suggestion: '建议替换为"好物"或"必备品"', severity: 'medium', dimension: 'legal' },
|
||||
],
|
||||
complianceChecks: [
|
||||
{ item: '品牌名称正确', passed: true },
|
||||
@@ -71,6 +71,17 @@ const mockScriptTask = {
|
||||
{ item: '无绝对化用语', passed: false, note: '"超级好用"建议修改' },
|
||||
{ item: '引导语规范', passed: true },
|
||||
],
|
||||
dimensions: {
|
||||
legal: { score: 85, passed: true, issue_count: 1 },
|
||||
platform: { score: 100, passed: true, issue_count: 0 },
|
||||
brand_safety: { score: 100, passed: true, issue_count: 0 },
|
||||
brief_match: { score: 100, passed: true, issue_count: 0 },
|
||||
},
|
||||
sellingPointMatches: [
|
||||
{ content: 'SPF50+ PA++++', priority: 'core' as const, matched: true, evidence: '脚本提及 SPF50+,PA++++' },
|
||||
{ content: '轻薄质地', priority: 'core' as const, matched: true, evidence: '脚本描述质地轻薄不油腻' },
|
||||
{ content: '延展性好', priority: 'recommended' as const, matched: true, evidence: '脚本演示延展性' },
|
||||
],
|
||||
sellingPoints: [
|
||||
{ point: 'SPF50+ PA++++', covered: true },
|
||||
{ point: '轻薄质地', covered: true },
|
||||
@@ -88,6 +99,7 @@ function mapTaskToView(task: TaskResponse) {
|
||||
content: v.content,
|
||||
suggestion: v.suggestion,
|
||||
severity: v.severity,
|
||||
dimension: v.dimension,
|
||||
}))
|
||||
|
||||
const softWarnings = (task.script_ai_result?.soft_warnings || []).map((w, idx) => ({
|
||||
@@ -138,6 +150,8 @@ function mapTaskToView(task: TaskResponse) {
|
||||
aiAnalysis: {
|
||||
violations,
|
||||
softWarnings,
|
||||
dimensions: task.script_ai_result?.dimensions,
|
||||
sellingPointMatches: task.script_ai_result?.selling_point_matches || [],
|
||||
sellingPoints: [] as Array<{ point: string; covered: boolean }>,
|
||||
},
|
||||
}
|
||||
@@ -236,6 +250,7 @@ export default function BrandScriptReviewPage() {
|
||||
},
|
||||
} : taskData
|
||||
|
||||
|
||||
const handleApprove = async () => {
|
||||
if (USE_MOCK) {
|
||||
setShowApproveModal(false)
|
||||
@@ -492,6 +507,34 @@ export default function BrandScriptReviewPage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* 维度评分 */}
|
||||
{task.aiAnalysis.dimensions && (
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<Shield size={16} className="text-accent-indigo" />
|
||||
维度评分
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{(['legal', 'platform', 'brand_safety', 'brief_match'] as const).map(key => {
|
||||
const dim = (task.aiAnalysis.dimensions as unknown as Record<string, { score: number; passed: boolean; issue_count: number }>)?.[key]
|
||||
if (!dim) return null
|
||||
const label = { legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }[key]
|
||||
return (
|
||||
<div key={key} className={`flex items-center justify-between p-2 rounded-lg ${dim.passed ? 'bg-accent-green/5' : 'bg-accent-coral/5'}`}>
|
||||
<span className="text-sm text-text-primary">{label}</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`text-sm font-bold ${dim.passed ? 'text-accent-green' : 'text-accent-coral'}`}>{dim.score}</span>
|
||||
{dim.passed ? <CheckCircle size={14} className="text-accent-green" /> : <XCircle size={14} className="text-accent-coral" />}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* 违规检测 */}
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
@@ -505,6 +548,7 @@ export default function BrandScriptReviewPage() {
|
||||
<div key={v.id} className="p-3 bg-orange-500/10 rounded-lg border border-orange-500/30">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<WarningTag>{v.type}</WarningTag>
|
||||
{v.dimension && <span className="text-xs text-text-tertiary">{{ legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }[v.dimension as string]}</span>}
|
||||
</div>
|
||||
<p className="text-sm text-text-primary">{v.content}</p>
|
||||
<p className="text-xs text-accent-indigo mt-1">{v.suggestion}</p>
|
||||
@@ -568,26 +612,41 @@ export default function BrandScriptReviewPage() {
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* 卖点覆盖 */}
|
||||
{task.aiAnalysis.sellingPoints.length > 0 && (
|
||||
{/* 卖点匹配 */}
|
||||
{(task.aiAnalysis.sellingPointMatches?.length > 0 || task.aiAnalysis.sellingPoints.length > 0) && (
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<CheckCircle size={16} className="text-accent-green" />
|
||||
卖点覆盖
|
||||
卖点匹配
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{task.aiAnalysis.sellingPoints.map((sp, idx) => (
|
||||
<div key={idx} className="flex items-center gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{sp.covered ? (
|
||||
<CheckCircle size={16} className="text-accent-green" />
|
||||
) : (
|
||||
<XCircle size={16} className="text-accent-coral" />
|
||||
)}
|
||||
<span className="text-sm text-text-primary">{sp.point}</span>
|
||||
</div>
|
||||
))}
|
||||
{task.aiAnalysis.sellingPointMatches && task.aiAnalysis.sellingPointMatches.length > 0 ? (
|
||||
task.aiAnalysis.sellingPointMatches.map((sp: { content: string; priority: string; matched: boolean; evidence?: string }, idx: number) => (
|
||||
<div key={idx} className="flex items-start gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{sp.matched ? <CheckCircle size={16} className="text-accent-green flex-shrink-0 mt-0.5" /> : <XCircle size={16} className="text-accent-coral flex-shrink-0 mt-0.5" />}
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm text-text-primary">{sp.content}</span>
|
||||
<span className={`px-1.5 py-0.5 text-xs rounded ${
|
||||
sp.priority === 'core' ? 'bg-accent-coral/20 text-accent-coral' :
|
||||
sp.priority === 'recommended' ? 'bg-accent-amber/20 text-accent-amber' :
|
||||
'bg-bg-page text-text-tertiary'
|
||||
}`}>{sp.priority === 'core' ? '核心' : sp.priority === 'recommended' ? '推荐' : '参考'}</span>
|
||||
</div>
|
||||
{sp.evidence && <p className="text-xs text-text-tertiary mt-0.5">{sp.evidence}</p>}
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
task.aiAnalysis.sellingPoints.map((sp, idx) => (
|
||||
<div key={idx} className="flex items-center gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{sp.covered ? <CheckCircle size={16} className="text-accent-green" /> : <XCircle size={16} className="text-accent-coral" />}
|
||||
<span className="text-sm text-text-primary">{sp.point}</span>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
@@ -316,6 +316,7 @@ export default function BrandVideoReviewPage() {
|
||||
const [showFilePreview, setShowFilePreview] = useState(false)
|
||||
const [videoError, setVideoError] = useState(false)
|
||||
|
||||
|
||||
// 加载任务数据
|
||||
const loadTask = useCallback(async () => {
|
||||
if (!taskId) return
|
||||
|
||||
+574
-597
File diff suppressed because it is too large
Load Diff
@@ -47,6 +47,9 @@ type MessageType =
|
||||
| 'task_deadline' // 任务截止提醒
|
||||
| 'brief_updated' // Brief更新通知
|
||||
| 'system_notice' // 系统通知
|
||||
| 'reject' // 审核驳回
|
||||
| 'force_pass' // 强制通过
|
||||
| 'approve' // 审核批准
|
||||
|
||||
type Message = {
|
||||
id: string
|
||||
@@ -87,6 +90,9 @@ const messageConfig: Record<MessageType, {
|
||||
task_deadline: { icon: CalendarClock, iconColor: 'text-orange-400', bgColor: 'bg-orange-500/20' },
|
||||
brief_updated: { icon: FileText, iconColor: 'text-accent-indigo', bgColor: 'bg-accent-indigo/20' },
|
||||
system_notice: { icon: Bell, iconColor: 'text-text-secondary', bgColor: 'bg-bg-elevated' },
|
||||
reject: { icon: XCircle, iconColor: 'text-accent-coral', bgColor: 'bg-accent-coral/20' },
|
||||
force_pass: { icon: CheckCircle, iconColor: 'text-accent-amber', bgColor: 'bg-accent-amber/20' },
|
||||
approve: { icon: CheckCircle, iconColor: 'text-accent-green', bgColor: 'bg-accent-green/20' },
|
||||
}
|
||||
|
||||
// 12条消息数据
|
||||
@@ -281,7 +287,7 @@ function MessageCard({
|
||||
onAcceptInvite?: () => void
|
||||
onIgnoreInvite?: () => void
|
||||
}) {
|
||||
const config = messageConfig[message.type]
|
||||
const config = messageConfig[message.type] || messageConfig.system_notice
|
||||
const Icon = config.icon
|
||||
|
||||
return (
|
||||
|
||||
@@ -82,7 +82,7 @@ function mapTaskResponseToUI(task: TaskResponse): Task {
|
||||
id: task.id,
|
||||
title: task.name,
|
||||
description: `${task.project.name} · ${ui.statusLabel}`,
|
||||
platform: 'douyin', // 后端暂无平台字段,默认
|
||||
platform: task.project?.platform || 'douyin',
|
||||
scriptStage: ui.scriptStage,
|
||||
videoStage: ui.videoStage,
|
||||
buttonText: ui.buttonText,
|
||||
|
||||
@@ -32,6 +32,7 @@ type AgencyBriefFile = {
|
||||
size: string
|
||||
uploadedAt: string
|
||||
description?: string
|
||||
url?: string
|
||||
}
|
||||
|
||||
// 页面视图模型
|
||||
@@ -102,20 +103,24 @@ function buildMockViewModel(): BriefViewModel {
|
||||
}
|
||||
|
||||
function buildViewModelFromAPI(task: TaskResponse, brief: BriefResponse): BriefViewModel {
|
||||
// Map attachments to file list
|
||||
const files: AgencyBriefFile[] = (brief.attachments ?? []).map((att, idx) => ({
|
||||
// 优先显示代理商上传的文档,没有则降级到品牌方附件
|
||||
const agencyAtts = brief.agency_attachments ?? []
|
||||
const brandAtts = brief.attachments ?? []
|
||||
const sourceAtts = agencyAtts.length > 0 ? agencyAtts : brandAtts
|
||||
const files: AgencyBriefFile[] = sourceAtts.map((att, idx) => ({
|
||||
id: att.id || `att-${idx}`,
|
||||
name: att.name,
|
||||
size: att.size || '',
|
||||
uploadedAt: brief.updated_at?.split('T')[0] || '',
|
||||
description: undefined,
|
||||
url: att.url,
|
||||
}))
|
||||
|
||||
// Map selling points
|
||||
const sellingPoints = (brief.selling_points ?? []).map((sp, idx) => ({
|
||||
id: `sp-${idx}`,
|
||||
content: sp.content,
|
||||
required: sp.required,
|
||||
required: sp.required ?? (sp.priority === 'core'),
|
||||
}))
|
||||
|
||||
// Map blacklist words
|
||||
@@ -233,12 +238,21 @@ export default function TaskBriefPage() {
|
||||
loadBriefData()
|
||||
}, [loadBriefData])
|
||||
|
||||
const handleDownload = (file: AgencyBriefFile) => {
|
||||
toast.info(`下载文件: ${file.name}`)
|
||||
const handleDownload = async (file: AgencyBriefFile) => {
|
||||
if (USE_MOCK || !file.url) {
|
||||
toast.info(`下载文件: ${file.name}`)
|
||||
return
|
||||
}
|
||||
try {
|
||||
await api.downloadFile(file.url, file.name)
|
||||
} catch {
|
||||
toast.error('下载失败')
|
||||
}
|
||||
}
|
||||
|
||||
const handleDownloadAll = () => {
|
||||
toast.info('下载全部文件')
|
||||
if (!viewModel) return
|
||||
viewModel.files.forEach(f => handleDownload(f))
|
||||
}
|
||||
|
||||
if (loading || !viewModel) {
|
||||
|
||||
@@ -17,7 +17,7 @@ import { cn } from '@/lib/utils'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
import type { TaskResponse, AIReviewResult } from '@/types/task'
|
||||
import type { TaskResponse, AIReviewResult, ReviewDimensions, SellingPointMatchResult, BriefMatchDetail } from '@/types/task'
|
||||
import type { BriefResponse } from '@/types/brief'
|
||||
|
||||
// 前端 UI 使用的任务阶段类型
|
||||
@@ -57,6 +57,15 @@ type TaskData = {
|
||||
rejectionReason?: string
|
||||
submittedAt?: string
|
||||
scriptContent?: string
|
||||
aiResult?: {
|
||||
score: number
|
||||
dimensions?: ReviewDimensions
|
||||
sellingPointMatches?: SellingPointMatchResult[]
|
||||
briefMatchDetail?: BriefMatchDetail
|
||||
violations: Array<{ type: string; content: string; suggestion: string; dimension?: string }>
|
||||
}
|
||||
agencyReview?: { result: 'approved' | 'rejected'; comment: string; reviewer: string; time: string }
|
||||
brandReview?: { result: 'approved' | 'rejected'; comment: string; reviewer: string; time: string }
|
||||
}
|
||||
|
||||
type AgencyBriefFile = {
|
||||
@@ -134,8 +143,9 @@ function mapApiTaskToTaskData(task: TaskResponse): TaskData {
|
||||
// 提取 AI 审核结果中的 issues
|
||||
const aiResult = phase === 'script' ? task.script_ai_result : task.video_ai_result
|
||||
if (aiResult?.violations) {
|
||||
const dimLabels: Record<string, string> = { legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }
|
||||
issues = aiResult.violations.map(v => ({
|
||||
title: v.type,
|
||||
title: v.dimension ? `[${dimLabels[v.dimension] || v.dimension}] ${v.type}` : v.type,
|
||||
description: `${v.content}${v.suggestion ? ` — ${v.suggestion}` : ''}`,
|
||||
timestamp: v.timestamp ? `${v.timestamp}s` : undefined,
|
||||
severity: v.severity === 'warning' ? 'warning' as const : 'error' as const,
|
||||
@@ -144,6 +154,35 @@ function mapApiTaskToTaskData(task: TaskResponse): TaskData {
|
||||
|
||||
const subtitle = `${task.project.name} · ${task.project.brand_name || ''}`
|
||||
|
||||
// AI 审核结果(完整,含维度)
|
||||
const aiResultData = aiResult ? {
|
||||
score: aiResult.score,
|
||||
dimensions: aiResult.dimensions,
|
||||
sellingPointMatches: aiResult.selling_point_matches,
|
||||
briefMatchDetail: aiResult.brief_match_detail,
|
||||
violations: aiResult.violations.map(v => ({ type: v.type, content: v.content, suggestion: v.suggestion, dimension: v.dimension })),
|
||||
} : undefined
|
||||
|
||||
// 代理商审核反馈
|
||||
const agencyStatus = phase === 'script' ? task.script_agency_status : task.video_agency_status
|
||||
const agencyComment = phase === 'script' ? task.script_agency_comment : task.video_agency_comment
|
||||
const agencyReview = agencyStatus && agencyStatus !== 'pending' ? {
|
||||
result: (agencyStatus === 'passed' || agencyStatus === 'force_passed' ? 'approved' : 'rejected') as 'approved' | 'rejected',
|
||||
comment: agencyComment || '',
|
||||
reviewer: task.agency?.name || '代理商',
|
||||
time: task.updated_at,
|
||||
} : undefined
|
||||
|
||||
// 品牌方审核反馈
|
||||
const brandStatus = phase === 'script' ? task.script_brand_status : task.video_brand_status
|
||||
const brandComment = phase === 'script' ? task.script_brand_comment : task.video_brand_comment
|
||||
const brandReview = brandStatus && brandStatus !== 'pending' ? {
|
||||
result: (brandStatus === 'passed' || brandStatus === 'force_passed' ? 'approved' : 'rejected') as 'approved' | 'rejected',
|
||||
comment: brandComment || '',
|
||||
reviewer: '品牌方审核员',
|
||||
time: task.updated_at,
|
||||
} : undefined
|
||||
|
||||
return {
|
||||
id: task.id,
|
||||
title: task.name,
|
||||
@@ -153,6 +192,9 @@ function mapApiTaskToTaskData(task: TaskResponse): TaskData {
|
||||
issues: issues.length > 0 ? issues : undefined,
|
||||
rejectionReason,
|
||||
submittedAt,
|
||||
aiResult: aiResultData,
|
||||
agencyReview,
|
||||
brandReview,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,11 +206,12 @@ const mockBriefData = {
|
||||
{ id: 'af3', name: '品牌视觉参考.pdf', size: '3.2MB', uploadedAt: '2026-02-02', description: '视觉风格和拍摄参考示例' },
|
||||
] as AgencyBriefFile[],
|
||||
sellingPoints: [
|
||||
{ id: 'sp1', content: 'SPF50+ PA++++', required: true },
|
||||
{ id: 'sp2', content: '轻薄质地,不油腻', required: true },
|
||||
{ id: 'sp3', content: '延展性好,易推开', required: false },
|
||||
{ id: 'sp4', content: '适合敏感肌', required: false },
|
||||
{ id: 'sp5', content: '夏日必备防晒', required: true },
|
||||
{ id: 'sp1', content: 'SPF50+ PA++++', priority: 'core' as const },
|
||||
{ id: 'sp2', content: '轻薄质地,不油腻', priority: 'core' as const },
|
||||
{ id: 'sp3', content: '延展性好,易推开', priority: 'recommended' as const },
|
||||
{ id: 'sp4', content: '适合敏感肌', priority: 'recommended' as const },
|
||||
{ id: 'sp5', content: '夏日必备防晒', priority: 'core' as const },
|
||||
{ id: 'sp6', content: '产品成分天然', priority: 'reference' as const },
|
||||
],
|
||||
blacklistWords: [
|
||||
{ id: 'bw1', word: '最好', reason: '绝对化用语' },
|
||||
@@ -278,15 +321,16 @@ function ReviewProgressBar({ task }: { task: TaskData }) {
|
||||
// Brief 组件
|
||||
function AgencyBriefSection({ toast, briefData }: {
|
||||
toast: ReturnType<typeof useToast>
|
||||
briefData: { files: AgencyBriefFile[]; sellingPoints: { id: string; content: string; required: boolean }[]; blacklistWords: { id: string; word: string; reason: string }[] }
|
||||
briefData: { files: AgencyBriefFile[]; sellingPoints: { id: string; content: string; priority: 'core' | 'recommended' | 'reference' }[]; blacklistWords: { id: string; word: string; reason: string }[] }
|
||||
}) {
|
||||
const [isExpanded, setIsExpanded] = useState(true)
|
||||
const [previewFile, setPreviewFile] = useState<AgencyBriefFile | null>(null)
|
||||
|
||||
const handleDownload = (file: AgencyBriefFile) => { toast.info(`下载文件: ${file.name}`) }
|
||||
|
||||
const requiredPoints = briefData.sellingPoints.filter(sp => sp.required)
|
||||
const optionalPoints = briefData.sellingPoints.filter(sp => !sp.required)
|
||||
const corePoints = briefData.sellingPoints.filter(sp => sp.priority === 'core')
|
||||
const recommendedPoints = briefData.sellingPoints.filter(sp => sp.priority === 'recommended')
|
||||
const referencePoints = briefData.sellingPoints.filter(sp => sp.priority === 'reference')
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -337,21 +381,31 @@ function AgencyBriefSection({ toast, briefData }: {
|
||||
<Target className="w-4 h-4 text-accent-green" /> 卖点要求
|
||||
</h4>
|
||||
<div className="space-y-2">
|
||||
{requiredPoints.length > 0 && (
|
||||
{corePoints.length > 0 && (
|
||||
<div className="p-3 bg-accent-coral/10 rounded-xl border border-accent-coral/30">
|
||||
<p className="text-xs text-accent-coral font-medium mb-2">必选卖点(必须提及)</p>
|
||||
<p className="text-xs text-accent-coral font-medium mb-2">核心卖点(建议优先提及)</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{requiredPoints.map((sp) => (
|
||||
{corePoints.map((sp) => (
|
||||
<span key={sp.id} className="px-2 py-1 text-xs bg-accent-coral/20 text-accent-coral rounded-lg">{sp.content}</span>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{optionalPoints.length > 0 && (
|
||||
<div className="p-3 bg-bg-elevated rounded-xl">
|
||||
<p className="text-xs text-text-tertiary font-medium mb-2">可选卖点</p>
|
||||
{recommendedPoints.length > 0 && (
|
||||
<div className="p-3 bg-accent-amber/10 rounded-xl border border-accent-amber/30">
|
||||
<p className="text-xs text-accent-amber font-medium mb-2">推荐卖点(建议提及)</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{optionalPoints.map((sp) => (
|
||||
{recommendedPoints.map((sp) => (
|
||||
<span key={sp.id} className="px-2 py-1 text-xs bg-accent-amber/20 text-accent-amber rounded-lg">{sp.content}</span>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{referencePoints.length > 0 && (
|
||||
<div className="p-3 bg-bg-elevated rounded-xl">
|
||||
<p className="text-xs text-text-tertiary font-medium mb-2">参考信息</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{referencePoints.map((sp) => (
|
||||
<span key={sp.id} className="px-2 py-1 text-xs bg-bg-page text-text-secondary rounded-lg">{sp.content}</span>
|
||||
))}
|
||||
</div>
|
||||
@@ -394,42 +448,290 @@ function AgencyBriefSection({ toast, briefData }: {
|
||||
)
|
||||
}
|
||||
|
||||
function UploadView({ task, toast, briefData }: { task: TaskData; toast: ReturnType<typeof useToast>; briefData: typeof mockBriefData }) {
|
||||
const router = useRouter()
|
||||
const { id } = useParams()
|
||||
const isScript = task.phase === 'script'
|
||||
const uploadPath = isScript ? `/creator/task/${id}/script` : `/creator/task/${id}/video`
|
||||
function FileUploadSection({ taskId, phase, onUploaded }: { taskId: string; phase: 'script' | 'video'; onUploaded: () => void }) {
|
||||
const [file, setFile] = useState<File | null>(null)
|
||||
const [isUploading, setIsUploading] = useState(false)
|
||||
const [progress, setProgress] = useState(0)
|
||||
const [uploadError, setUploadError] = useState<string | null>(null)
|
||||
const toast = useToast()
|
||||
const isScript = phase === 'script'
|
||||
|
||||
const handleUploadClick = () => {
|
||||
router.push(uploadPath)
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const selectedFile = e.target.files?.[0]
|
||||
if (selectedFile) { setFile(selectedFile); setUploadError(null) }
|
||||
}
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!file) return
|
||||
setIsUploading(true); setProgress(0); setUploadError(null)
|
||||
try {
|
||||
if (USE_MOCK) {
|
||||
for (let i = 0; i <= 100; i += 20) { await new Promise(r => setTimeout(r, 400)); setProgress(i) }
|
||||
toast.success(isScript ? '脚本已提交,等待 AI 审核' : '视频已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
} else {
|
||||
const result = await api.proxyUpload(file, isScript ? 'script' : 'video', (pct) => {
|
||||
setProgress(Math.min(90, Math.round(pct * 0.9)))
|
||||
})
|
||||
setProgress(95)
|
||||
if (isScript) {
|
||||
await api.uploadTaskScript(taskId, { file_url: result.url, file_name: result.file_name })
|
||||
} else {
|
||||
await api.uploadTaskVideo(taskId, { file_url: result.url, file_name: result.file_name })
|
||||
}
|
||||
setProgress(100)
|
||||
toast.success(isScript ? '脚本已提交,等待 AI 审核' : '视频已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
}
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : '上传失败'
|
||||
setUploadError(msg); toast.error(msg)
|
||||
} finally { setIsUploading(false) }
|
||||
}
|
||||
|
||||
const formatSize = (bytes: number) => {
|
||||
if (bytes < 1024) return bytes + 'B'
|
||||
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB'
|
||||
return (bytes / (1024 * 1024)).toFixed(1) + 'MB'
|
||||
}
|
||||
|
||||
const acceptTypes = isScript ? '.doc,.docx,.pdf,.txt,.xls,.xlsx' : '.mp4,.mov,.avi,.mkv'
|
||||
const acceptHint = isScript ? '支持 Word、PDF、TXT、Excel 格式' : '支持 MP4/MOV 格式,≤ 100MB'
|
||||
|
||||
return (
|
||||
<div className="bg-bg-card rounded-2xl card-shadow">
|
||||
<div className="flex items-center gap-2 p-4 border-b border-border-subtle">
|
||||
<Upload className="w-5 h-5 text-accent-indigo" />
|
||||
<span className="text-base font-semibold text-text-primary">{isScript ? '上传脚本' : '上传视频'}</span>
|
||||
<span className="ml-auto px-2.5 py-1 rounded-full text-xs font-semibold bg-accent-indigo/15 text-accent-indigo">待提交</span>
|
||||
</div>
|
||||
<div className="p-4 space-y-4">
|
||||
{!file ? (
|
||||
<label className="border-2 border-dashed border-border-subtle rounded-xl p-8 text-center hover:border-accent-indigo/50 transition-colors cursor-pointer block">
|
||||
<Upload className="w-8 h-8 mx-auto text-text-tertiary mb-3" />
|
||||
<p className="text-text-secondary mb-1">点击选择{isScript ? '脚本' : '视频'}文件</p>
|
||||
<p className="text-xs text-text-tertiary">{acceptHint}</p>
|
||||
<input type="file" accept={acceptTypes} onChange={handleFileChange} className="hidden" />
|
||||
</label>
|
||||
) : (
|
||||
<div className="border border-border-subtle rounded-xl overflow-hidden">
|
||||
<div className="px-4 py-2.5 bg-bg-elevated border-b border-border-subtle">
|
||||
<span className="text-xs font-medium text-text-secondary">已选文件</span>
|
||||
</div>
|
||||
<div className="px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
{isUploading ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin text-accent-indigo flex-shrink-0" />
|
||||
) : uploadError ? (
|
||||
<AlertTriangle className="w-4 h-4 text-accent-coral flex-shrink-0" />
|
||||
) : (
|
||||
<CheckCircle className="w-4 h-4 text-accent-green flex-shrink-0" />
|
||||
)}
|
||||
<FileText className="w-4 h-4 text-accent-indigo flex-shrink-0" />
|
||||
<span className="flex-1 text-sm text-text-primary truncate">{file.name}</span>
|
||||
<span className="text-xs text-text-tertiary">{formatSize(file.size)}</span>
|
||||
{!isUploading && (
|
||||
<button type="button" onClick={() => { setFile(null); setUploadError(null) }} className="p-1 hover:bg-bg-elevated rounded">
|
||||
<XCircle className="w-4 h-4 text-text-tertiary" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{isUploading && (
|
||||
<>
|
||||
<div className="mt-2 ml-[30px] h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div className="h-full bg-accent-indigo rounded-full transition-all duration-300" style={{ width: `${progress}%` }} />
|
||||
</div>
|
||||
<p className="mt-1 ml-[30px] text-xs text-text-tertiary">上传中 {progress}%</p>
|
||||
</>
|
||||
)}
|
||||
{uploadError && <p className="mt-1 ml-[30px] text-xs text-accent-coral">{uploadError}</p>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSubmit}
|
||||
disabled={!file || isUploading}
|
||||
className="w-full flex items-center justify-center gap-2 px-6 py-3 rounded-xl bg-gradient-to-r from-accent-indigo to-[#4F46E5] text-white font-semibold hover:opacity-90 transition-opacity disabled:opacity-40 disabled:cursor-not-allowed"
|
||||
>
|
||||
{isUploading ? <><Loader2 className="w-5 h-5 animate-spin" />上传中 {progress}%</> : <><Upload className="w-5 h-5" />{isScript ? '提交脚本' : '提交视频'}</>}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function getDimensionLabel(key: string) {
|
||||
const labels: Record<string, string> = { legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }
|
||||
return labels[key] || key
|
||||
}
|
||||
|
||||
function AIResultDetailSection({ task }: { task: TaskData }) {
|
||||
if (!task.aiResult) return null
|
||||
const { dimensions, sellingPointMatches, briefMatchDetail, violations } = task.aiResult
|
||||
|
||||
return (
|
||||
<div className="bg-bg-card rounded-2xl card-shadow">
|
||||
<div className="flex items-center justify-between p-4 border-b border-border-subtle">
|
||||
<div className="flex items-center gap-2">
|
||||
<Bot className="w-5 h-5 text-accent-indigo" />
|
||||
<span className="text-base font-semibold text-text-primary">AI 审核结果</span>
|
||||
</div>
|
||||
<span className={cn('text-xl font-bold', task.aiResult.score >= 85 ? 'text-accent-green' : task.aiResult.score >= 70 ? 'text-yellow-400' : 'text-accent-coral')}>
|
||||
{task.aiResult.score}分
|
||||
</span>
|
||||
</div>
|
||||
<div className="p-4 space-y-4">
|
||||
{dimensions && (
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
{(['legal', 'platform', 'brand_safety', 'brief_match'] as const).map(key => {
|
||||
const dim = dimensions[key]
|
||||
if (!dim) return null
|
||||
return (
|
||||
<div key={key} className={cn('p-3 rounded-xl border', dim.passed ? 'bg-accent-green/5 border-accent-green/20' : 'bg-accent-coral/5 border-accent-coral/20')}>
|
||||
<div className="flex items-center justify-between mb-1">
|
||||
<span className="text-xs text-text-secondary">{getDimensionLabel(key)}</span>
|
||||
{dim.passed ? <CheckCircle className="w-4 h-4 text-accent-green" /> : <XCircle className="w-4 h-4 text-accent-coral" />}
|
||||
</div>
|
||||
<span className={cn('text-lg font-bold', dim.passed ? (dim.score >= 85 ? 'text-accent-green' : 'text-yellow-400') : 'text-accent-coral')}>{dim.score}</span>
|
||||
{dim.issue_count > 0 && <span className="text-xs text-text-tertiary ml-1">({dim.issue_count} 项问题)</span>}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
{violations.length > 0 && (
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2">
|
||||
<AlertTriangle className="w-4 h-4 text-accent-coral" /> 违规检测 ({violations.length})
|
||||
</h4>
|
||||
<div className="space-y-2">
|
||||
{violations.map((v, idx) => (
|
||||
<div key={idx} className="p-3 bg-accent-coral/10 rounded-xl border border-accent-coral/30">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<span className="px-2 py-0.5 rounded text-xs font-semibold bg-accent-coral/15 text-accent-coral">{v.type}</span>
|
||||
{v.dimension && <span className="text-xs text-text-tertiary">{getDimensionLabel(v.dimension)}</span>}
|
||||
</div>
|
||||
<p className="text-sm text-text-primary">「{v.content}」</p>
|
||||
<p className="text-xs text-accent-indigo mt-1">{v.suggestion}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* Brief 匹配度详情 */}
|
||||
{briefMatchDetail && (
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2">
|
||||
<Target className="w-4 h-4 text-accent-indigo" /> Brief 匹配度分析
|
||||
</h4>
|
||||
<div className="p-3 bg-bg-elevated rounded-xl space-y-3">
|
||||
{/* 评分说明 */}
|
||||
<p className="text-sm text-text-secondary">{briefMatchDetail.explanation}</p>
|
||||
{/* 覆盖率进度条 */}
|
||||
{briefMatchDetail.total_points > 0 && (
|
||||
<div>
|
||||
<div className="flex items-center justify-between text-xs mb-1">
|
||||
<span className="text-text-tertiary">卖点覆盖率</span>
|
||||
<span className="text-text-primary font-medium">{briefMatchDetail.matched_points}/{briefMatchDetail.required_points > 0 ? briefMatchDetail.required_points : briefMatchDetail.total_points} 条</span>
|
||||
</div>
|
||||
<div className="h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div className={cn('h-full rounded-full transition-all', briefMatchDetail.coverage_score >= 80 ? 'bg-accent-green' : briefMatchDetail.coverage_score >= 50 ? 'bg-accent-amber' : 'bg-accent-coral')} style={{ width: `${briefMatchDetail.coverage_score}%` }} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* 亮点 */}
|
||||
{briefMatchDetail.highlights.length > 0 && (
|
||||
<div>
|
||||
<p className="text-xs text-accent-green font-medium mb-1">亮点</p>
|
||||
<div className="space-y-1">
|
||||
{briefMatchDetail.highlights.map((h, i) => (
|
||||
<div key={i} className="flex items-start gap-2">
|
||||
<CheckCircle className="w-3.5 h-3.5 text-accent-green flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-text-secondary">{h}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* 问题点 */}
|
||||
{briefMatchDetail.issues.length > 0 && (
|
||||
<div>
|
||||
<p className="text-xs text-accent-coral font-medium mb-1">可改进</p>
|
||||
<div className="space-y-1">
|
||||
{briefMatchDetail.issues.map((issue, i) => (
|
||||
<div key={i} className="flex items-start gap-2">
|
||||
<AlertTriangle className="w-3.5 h-3.5 text-accent-coral flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-text-secondary">{issue}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{/* 卖点匹配列表 */}
|
||||
{sellingPointMatches && sellingPointMatches.length > 0 && (
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2">
|
||||
<Target className="w-4 h-4 text-accent-green" /> 卖点匹配详情
|
||||
</h4>
|
||||
<div className="space-y-2">
|
||||
{sellingPointMatches.map((sp, idx) => (
|
||||
<div key={idx} className="flex items-start gap-2 p-2.5 rounded-xl bg-bg-elevated">
|
||||
{sp.matched ? <CheckCircle className="w-4 h-4 text-accent-green flex-shrink-0 mt-0.5" /> : <XCircle className="w-4 h-4 text-accent-coral flex-shrink-0 mt-0.5" />}
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm text-text-primary">{sp.content}</span>
|
||||
<span className={cn('px-1.5 py-0.5 text-xs rounded',
|
||||
sp.priority === 'core' ? 'bg-accent-coral/20 text-accent-coral' :
|
||||
sp.priority === 'recommended' ? 'bg-accent-amber/20 text-accent-amber' :
|
||||
'bg-bg-page text-text-tertiary'
|
||||
)}>{sp.priority === 'core' ? '核心' : sp.priority === 'recommended' ? '推荐' : '参考'}</span>
|
||||
</div>
|
||||
{sp.evidence && <p className="text-xs text-text-tertiary mt-0.5">{sp.evidence}</p>}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function ReviewFeedbackCard({ review, type }: { review: { result: string; comment: string; reviewer: string; time: string }; type: 'agency' | 'brand' }) {
|
||||
const isApproved = review.result === 'approved'
|
||||
const title = type === 'agency' ? '代理商审核意见' : '品牌方终审意见'
|
||||
return (
|
||||
<div className={cn('bg-bg-card rounded-2xl card-shadow border', isApproved ? 'border-accent-green/30' : 'border-accent-coral/30')}>
|
||||
<div className="flex items-center gap-2 p-4 border-b border-border-subtle">
|
||||
{isApproved ? <CheckCircle className="w-5 h-5 text-accent-green" /> : <XCircle className="w-5 h-5 text-accent-coral" />}
|
||||
<span className="text-base font-semibold text-text-primary">{title}</span>
|
||||
</div>
|
||||
<div className="p-4">
|
||||
<div className="flex items-center gap-2 mb-2">
|
||||
<span className="font-medium text-text-primary">{review.reviewer}</span>
|
||||
<span className={cn('px-2 py-0.5 rounded text-xs font-semibold', isApproved ? 'bg-accent-green/15 text-accent-green' : 'bg-accent-coral/15 text-accent-coral')}>
|
||||
{isApproved ? '通过' : '驳回'}
|
||||
</span>
|
||||
</div>
|
||||
{review.comment && <p className="text-sm text-text-secondary">{review.comment}</p>}
|
||||
<p className="text-xs text-text-tertiary mt-2">{review.time}</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function UploadView({ task, toast, briefData, onUploaded }: { task: TaskData; toast: ReturnType<typeof useToast>; briefData: typeof mockBriefData; onUploaded: () => void }) {
|
||||
const isScript = task.phase === 'script'
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6 h-full">
|
||||
{isScript && <AgencyBriefSection toast={toast} briefData={briefData} />}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold text-text-primary">{isScript ? '上传脚本' : '上传视频'}</h3>
|
||||
<p className="text-sm text-text-tertiary">{isScript ? '支持粘贴文本或上传文档' : '支持 MP4/MOV 格式,≤ 100MB'}</p>
|
||||
</div>
|
||||
<span className="px-2.5 py-1 rounded-full text-xs font-semibold bg-accent-indigo/15 text-accent-indigo">待提交</span>
|
||||
</div>
|
||||
<div
|
||||
className="flex-1 flex flex-col items-center justify-center gap-5 rounded-2xl border-2 border-dashed transition-colors card-shadow bg-bg-card min-h-[400px] border-border-subtle hover:border-accent-indigo/50 cursor-pointer"
|
||||
onClick={handleUploadClick}
|
||||
>
|
||||
<div className="w-20 h-20 rounded-full bg-accent-indigo/15 flex items-center justify-center">
|
||||
<Upload className="w-10 h-10 text-accent-indigo" />
|
||||
</div>
|
||||
<div className="flex flex-col items-center gap-2 text-center">
|
||||
<p className="text-lg font-semibold text-text-primary">点击进入上传页面</p>
|
||||
<p className="text-sm text-text-tertiary">{isScript ? '支持 .doc、.docx、.txt 格式' : '支持 MP4/MOV 格式,≤ 100MB'}</p>
|
||||
</div>
|
||||
<button type="button" onClick={handleUploadClick} className="flex items-center gap-2 px-8 py-3.5 rounded-xl bg-gradient-to-r from-accent-indigo to-[#4F46E5] text-white font-semibold hover:opacity-90 transition-opacity">
|
||||
<Upload className="w-5 h-5" />
|
||||
{isScript ? '上传脚本文档' : '上传视频文件'}
|
||||
</button>
|
||||
</div>
|
||||
<FileUploadSection taskId={task.id} phase={task.phase} onUploaded={onUploaded} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -489,20 +791,20 @@ function AIReviewingView({ task }: { task: TaskData }) {
|
||||
)
|
||||
}
|
||||
|
||||
function RejectionView({ task, onAppeal }: { task: TaskData; onAppeal: () => void }) {
|
||||
function RejectionView({ task, onAppeal, onReupload }: { task: TaskData; onAppeal: () => void; onReupload: () => void }) {
|
||||
const getTitle = () => {
|
||||
switch (task.stage) {
|
||||
case 'ai_result': return 'AI 审核结果'
|
||||
case 'agency_rejected': return '代理商审核结果'
|
||||
case 'brand_rejected': return '品牌方审核结果'
|
||||
case 'agency_rejected': return '代理商审核驳回'
|
||||
case 'brand_rejected': return '品牌方审核驳回'
|
||||
default: return '审核结果'
|
||||
}
|
||||
}
|
||||
const getStatusText = () => {
|
||||
switch (task.stage) {
|
||||
case 'ai_result': return 'AI 检测到问题'
|
||||
case 'agency_rejected': return '代理商审核驳回'
|
||||
case 'brand_rejected': return '品牌方审核驳回'
|
||||
case 'ai_result': return 'AI 检测到问题,请修改后重新上传'
|
||||
case 'agency_rejected': return '代理商审核驳回,请根据意见修改'
|
||||
case 'brand_rejected': return '品牌方审核驳回,请根据意见修改'
|
||||
default: return '需要修改'
|
||||
}
|
||||
}
|
||||
@@ -510,7 +812,7 @@ function RejectionView({ task, onAppeal }: { task: TaskData; onAppeal: () => voi
|
||||
return (
|
||||
<div className="flex flex-col gap-6 h-full">
|
||||
<ReviewProgressBar task={task} />
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow flex-1 flex flex-col">
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow">
|
||||
<div className="flex items-center gap-3 pb-5 border-b border-border-subtle">
|
||||
<div className="w-12 h-12 rounded-xl bg-accent-coral/15 flex items-center justify-center">
|
||||
<XCircle className="w-6 h-6 text-accent-coral" />
|
||||
@@ -525,35 +827,19 @@ function RejectionView({ task, onAppeal }: { task: TaskData; onAppeal: () => voi
|
||||
<p className="text-sm text-text-secondary leading-relaxed">{task.rejectionReason}</p>
|
||||
</div>
|
||||
)}
|
||||
{task.issues && task.issues.length > 0 && (
|
||||
<div className="py-4 flex flex-col gap-4 flex-1">
|
||||
<span className="text-sm font-semibold text-text-primary">发现 {task.issues.length} 处问题</span>
|
||||
<div className="flex flex-col gap-3">
|
||||
{task.issues.map((issue, index) => (
|
||||
<div key={index} className="bg-bg-elevated rounded-xl p-4 flex flex-col gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={cn('px-2 py-0.5 rounded text-xs font-semibold',
|
||||
issue.severity === 'error' ? 'bg-accent-coral/15 text-accent-coral' : 'bg-amber-500/15 text-amber-500'
|
||||
)}>
|
||||
{issue.severity === 'error' ? '违规' : '建议'}
|
||||
</span>
|
||||
<span className="text-sm font-semibold text-text-primary">{issue.title}</span>
|
||||
</div>
|
||||
<p className="text-[13px] text-text-secondary leading-relaxed">{issue.description}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center justify-between pt-4 border-t border-border-subtle">
|
||||
<div className="flex items-center justify-between pt-4">
|
||||
<button type="button" onClick={onAppeal} className="flex items-center gap-2 px-5 py-2.5 rounded-xl bg-bg-elevated border border-border-subtle text-text-secondary text-sm font-medium hover:bg-bg-page transition-colors">
|
||||
<MessageCircle className="w-[18px] h-[18px]" /> 申诉
|
||||
</button>
|
||||
<button type="button" className="flex items-center gap-2 px-6 py-2.5 rounded-xl bg-accent-green text-white text-sm font-semibold hover:bg-accent-green/90 transition-colors">
|
||||
<button type="button" onClick={onReupload} className="flex items-center gap-2 px-6 py-2.5 rounded-xl bg-accent-green text-white text-sm font-semibold hover:bg-accent-green/90 transition-colors">
|
||||
<Upload className="w-[18px] h-[18px]" /> 重新上传
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{task.stage === 'agency_rejected' && task.agencyReview && <ReviewFeedbackCard review={task.agencyReview} type="agency" />}
|
||||
{task.stage === 'brand_rejected' && task.brandReview && <ReviewFeedbackCard review={task.brandReview} type="brand" />}
|
||||
{task.stage === 'brand_rejected' && task.agencyReview && <ReviewFeedbackCard review={task.agencyReview} type="agency" />}
|
||||
<AIResultDetailSection task={task} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -567,9 +853,14 @@ function WaitingReviewView({ task }: { task: TaskData }) {
|
||||
<div className="flex flex-col gap-6 h-full">
|
||||
<ReviewProgressBar task={task} />
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow">
|
||||
<div className="flex items-center gap-3 mb-4">
|
||||
<FileText className="w-5 h-5 text-text-secondary" />
|
||||
<span className="text-base font-semibold text-text-primary">{task.phase === 'script' ? '脚本提交信息' : '视频提交信息'}</span>
|
||||
<div className="flex items-center gap-4 mb-4">
|
||||
<div className="w-12 h-12 rounded-xl bg-accent-indigo/15 flex items-center justify-center">
|
||||
<Clock className="w-6 h-6 text-accent-indigo" />
|
||||
</div>
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<span className="text-lg font-semibold text-text-primary">{title}</span>
|
||||
<span className="text-sm text-text-secondary">{description}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-bg-elevated rounded-xl p-4 flex flex-col gap-3">
|
||||
<div className="flex items-center justify-between">
|
||||
@@ -600,28 +891,8 @@ function WaitingReviewView({ task }: { task: TaskData }) {
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow flex-1">
|
||||
<div className="flex items-center gap-4 mb-4">
|
||||
<div className="w-12 h-12 rounded-xl bg-accent-indigo/15 flex items-center justify-center">
|
||||
<Clock className="w-6 h-6 text-accent-indigo" />
|
||||
</div>
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<span className="text-lg font-semibold text-text-primary">{title}</span>
|
||||
<span className="text-sm text-text-secondary">{description}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-accent-indigo/10 rounded-xl p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<AlertTriangle className="w-5 h-5 text-accent-indigo flex-shrink-0 mt-0.5" />
|
||||
<div className="flex flex-col gap-1">
|
||||
<span className="text-sm font-medium text-text-primary">温馨提示</span>
|
||||
<span className="text-[13px] text-text-secondary">
|
||||
{isAgency ? '代理商通常会在 1-2 个工作日内完成审核。' : '品牌方终审通常需要 1-3 个工作日。'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{!isAgency && task.agencyReview && <ReviewFeedbackCard review={task.agencyReview} type="agency" />}
|
||||
<AIResultDetailSection task={task} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -631,30 +902,6 @@ function ApprovedView({ task }: { task: TaskData }) {
|
||||
return (
|
||||
<div className="flex flex-col gap-6 h-full">
|
||||
<ReviewProgressBar task={task} />
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow">
|
||||
<div className="flex items-center gap-3 mb-4">
|
||||
<FileText className="w-5 h-5 text-text-secondary" />
|
||||
<span className="text-base font-semibold text-text-primary">{task.phase === 'script' ? '脚本提交信息' : '视频提交信息'}</span>
|
||||
</div>
|
||||
<div className="bg-bg-elevated rounded-xl p-4 flex flex-col gap-3">
|
||||
<div className="flex items-center justify-between"><span className="text-sm text-text-tertiary">提交时间</span><span className="text-sm text-text-primary">{task.submittedAt || '2026-02-01 10:30'}</span></div>
|
||||
<div className="flex items-center justify-between"><span className="text-sm text-text-tertiary">AI审核</span><span className="text-sm text-accent-green font-medium">已通过</span></div>
|
||||
<div className="flex items-center justify-between"><span className="text-sm text-text-tertiary">代理商审核</span><span className="text-sm text-accent-green font-medium">已通过</span></div>
|
||||
<div className="flex items-center justify-between"><span className="text-sm text-text-tertiary">品牌方终审</span><span className="text-sm text-accent-green font-medium">已通过</span></div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<div className="flex items-center gap-3">
|
||||
<CheckCircle className="w-6 h-6 text-accent-green" />
|
||||
<span className="text-lg font-semibold text-text-primary">品牌方审核通过</span>
|
||||
</div>
|
||||
<span className="px-2.5 py-1 rounded-full text-xs font-semibold bg-accent-green/15 text-accent-green">已通过</span>
|
||||
</div>
|
||||
<p className="text-sm text-text-secondary">
|
||||
{isVideoPhase ? '恭喜!视频已通过所有审核,可以发布了' : '脚本已通过品牌方终审,请继续上传视频'}
|
||||
</p>
|
||||
</div>
|
||||
<div className="bg-bg-card rounded-2xl p-6 card-shadow">
|
||||
<div className="flex items-center gap-4 mb-4">
|
||||
<div className="w-12 h-12 rounded-xl bg-accent-green/15 flex items-center justify-center">
|
||||
@@ -677,6 +924,9 @@ function ApprovedView({ task }: { task: TaskData }) {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{task.brandReview && <ReviewFeedbackCard review={task.brandReview} type="brand" />}
|
||||
{task.agencyReview && <ReviewFeedbackCard review={task.agencyReview} type="agency" />}
|
||||
<AIResultDetailSection task={task} />
|
||||
{!isVideoPhase && (
|
||||
<div className="flex justify-center pt-4">
|
||||
<button type="button" className="flex items-center gap-2 px-12 py-4 rounded-xl bg-accent-green text-white text-base font-semibold">
|
||||
@@ -701,6 +951,7 @@ export default function TaskDetailPage() {
|
||||
const [briefData, setBriefData] = useState(mockBriefData)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [showReupload, setShowReupload] = useState(false)
|
||||
|
||||
const loadTask = useCallback(async () => {
|
||||
if (USE_MOCK) {
|
||||
@@ -728,7 +979,7 @@ export default function TaskDetailPage() {
|
||||
sellingPoints: (brief.selling_points || []).map((sp, i) => ({
|
||||
id: `sp-${i}`,
|
||||
content: sp.content,
|
||||
required: sp.required,
|
||||
priority: (sp.priority || (sp.required ? 'core' : 'recommended')) as 'core' | 'recommended' | 'reference',
|
||||
})),
|
||||
blacklistWords: (brief.blacklist_words || []).map((bw, i) => ({
|
||||
id: `bw-${i}`,
|
||||
@@ -762,6 +1013,14 @@ export default function TaskDetailPage() {
|
||||
return () => { unsub1(); unsub2() }
|
||||
}, [subscribe, taskId, loadTask])
|
||||
|
||||
// AI 审核中时轮询(SSE 后备方案)
|
||||
useEffect(() => {
|
||||
if (!taskData || (taskData.stage !== 'ai_reviewing') || USE_MOCK) return
|
||||
const interval = setInterval(() => { loadTask() }, 5000)
|
||||
return () => clearInterval(interval)
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [taskData?.stage, loadTask])
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<ResponsiveLayout role="creator">
|
||||
@@ -793,12 +1052,27 @@ export default function TaskDetailPage() {
|
||||
}
|
||||
|
||||
const renderContent = () => {
|
||||
// 驳回状态下选择重新上传时,显示上传界面
|
||||
if (showReupload && (taskData.stage === 'ai_result' || taskData.stage === 'agency_rejected' || taskData.stage === 'brand_rejected')) {
|
||||
return (
|
||||
<div className="flex flex-col gap-6 h-full">
|
||||
<div className="flex items-center gap-3">
|
||||
<button type="button" onClick={() => setShowReupload(false)} className="flex items-center gap-2 px-3 py-1.5 rounded-lg bg-bg-elevated text-text-secondary text-sm hover:bg-bg-card transition-colors">
|
||||
<ArrowLeft className="w-4 h-4" /> 返回审核详情
|
||||
</button>
|
||||
</div>
|
||||
{taskData.phase === 'script' && <AgencyBriefSection toast={toast} briefData={briefData} />}
|
||||
<FileUploadSection taskId={taskData.id} phase={taskData.phase} onUploaded={() => { setShowReupload(false); loadTask() }} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
switch (taskData.stage) {
|
||||
case 'upload': return <UploadView task={taskData} toast={toast} briefData={briefData} />
|
||||
case 'upload': return <UploadView task={taskData} toast={toast} briefData={briefData} onUploaded={loadTask} />
|
||||
case 'ai_reviewing': return <AIReviewingView task={taskData} />
|
||||
case 'ai_result':
|
||||
case 'agency_rejected':
|
||||
case 'brand_rejected': return <RejectionView task={taskData} onAppeal={handleAppeal} />
|
||||
case 'brand_rejected': return <RejectionView task={taskData} onAppeal={handleAppeal} onReupload={() => setShowReupload(true)} />
|
||||
case 'agency_reviewing':
|
||||
case 'brand_reviewing': return <WaitingReviewView task={taskData} />
|
||||
case 'brand_approved': return <ApprovedView task={taskData} />
|
||||
|
||||
@@ -16,10 +16,17 @@ import { Modal } from '@/components/ui/Modal'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
import { useOSSUpload } from '@/hooks/useOSSUpload'
|
||||
import type { TaskResponse, AIReviewResult } from '@/types/task'
|
||||
import type { TaskResponse, AIReviewResult, ReviewDimensions, SellingPointMatchResult, BriefMatchDetail } from '@/types/task'
|
||||
import type { BriefResponse } from '@/types/brief'
|
||||
|
||||
// ========== 工具函数 ==========
|
||||
function getSellingPointPriority(sp: { priority?: string; required?: boolean }): 'core' | 'recommended' | 'reference' {
|
||||
if (sp.priority) return sp.priority as 'core' | 'recommended' | 'reference'
|
||||
if (sp.required === true) return 'core'
|
||||
if (sp.required === false) return 'recommended'
|
||||
return 'recommended'
|
||||
}
|
||||
|
||||
// ========== 类型 ==========
|
||||
type AgencyBriefFile = { id: string; name: string; size: string; uploadedAt: string; description?: string }
|
||||
|
||||
@@ -28,10 +35,14 @@ type ScriptTaskUI = {
|
||||
brandName: string
|
||||
scriptStatus: string
|
||||
scriptFile: string | null
|
||||
aiAutoRejected?: boolean
|
||||
aiRejectReason?: string
|
||||
aiResult: null | {
|
||||
score: number
|
||||
violations: Array<{ type: string; content: string; suggestion: string }>
|
||||
complianceChecks: Array<{ item: string; passed: boolean; note?: string }>
|
||||
dimensions?: ReviewDimensions
|
||||
sellingPointMatches?: SellingPointMatchResult[]
|
||||
briefMatchDetail?: BriefMatchDetail
|
||||
violations: Array<{ type: string; content: string; suggestion: string; dimension?: string }>
|
||||
}
|
||||
agencyReview: null | { result: 'approved' | 'rejected'; comment: string; reviewer: string; time: string }
|
||||
brandReview: null | { result: 'approved' | 'rejected'; comment: string; reviewer: string; time: string }
|
||||
@@ -39,7 +50,7 @@ type ScriptTaskUI = {
|
||||
|
||||
type BriefUI = {
|
||||
files: AgencyBriefFile[]
|
||||
sellingPoints: { id: string; content: string; required: boolean }[]
|
||||
sellingPoints: { id: string; content: string; priority: 'core' | 'recommended' | 'reference' }[]
|
||||
blacklistWords: { id: string; word: string; reason: string }[]
|
||||
}
|
||||
|
||||
@@ -47,8 +58,11 @@ type BriefUI = {
|
||||
function mapApiToScriptUI(task: TaskResponse): ScriptTaskUI {
|
||||
const stage = task.stage
|
||||
let status = 'pending_upload'
|
||||
const aiAutoRejected = task.script_ai_result?.ai_auto_rejected === true
|
||||
switch (stage) {
|
||||
case 'script_upload': status = 'pending_upload'; break
|
||||
case 'script_upload':
|
||||
status = aiAutoRejected ? 'ai_rejected' : 'pending_upload'
|
||||
break
|
||||
case 'script_ai_review': status = 'ai_reviewing'; break
|
||||
case 'script_agency_review': status = 'agent_reviewing'; break
|
||||
case 'script_brand_review': status = 'brand_reviewing'; break
|
||||
@@ -65,10 +79,10 @@ function mapApiToScriptUI(task: TaskResponse): ScriptTaskUI {
|
||||
|
||||
const aiResult = task.script_ai_result ? {
|
||||
score: task.script_ai_result.score,
|
||||
violations: task.script_ai_result.violations.map(v => ({ type: v.type, content: v.content, suggestion: v.suggestion })),
|
||||
complianceChecks: task.script_ai_result.violations.map(v => ({
|
||||
item: v.type, passed: v.severity !== 'error' && v.severity !== 'warning', note: v.suggestion,
|
||||
})),
|
||||
dimensions: task.script_ai_result.dimensions,
|
||||
sellingPointMatches: task.script_ai_result.selling_point_matches,
|
||||
briefMatchDetail: task.script_ai_result.brief_match_detail,
|
||||
violations: task.script_ai_result.violations.map(v => ({ type: v.type, content: v.content, suggestion: v.suggestion, dimension: v.dimension })),
|
||||
} : null
|
||||
|
||||
const agencyReview = task.script_agency_status && task.script_agency_status !== 'pending' ? {
|
||||
@@ -90,6 +104,8 @@ function mapApiToScriptUI(task: TaskResponse): ScriptTaskUI {
|
||||
brandName: task.project?.brand_name || '',
|
||||
scriptStatus: status,
|
||||
scriptFile: task.script_file_name || null,
|
||||
aiAutoRejected,
|
||||
aiRejectReason: task.script_ai_result?.ai_reject_reason,
|
||||
aiResult,
|
||||
agencyReview,
|
||||
brandReview,
|
||||
@@ -101,7 +117,7 @@ function mapBriefToUI(brief: BriefResponse): BriefUI {
|
||||
files: (brief.attachments || []).map((a, i) => ({
|
||||
id: a.id || `att-${i}`, name: a.name, size: a.size || '', uploadedAt: brief.updated_at || '',
|
||||
})),
|
||||
sellingPoints: (brief.selling_points || []).map((sp, i) => ({ id: `sp-${i}`, content: sp.content, required: sp.required })),
|
||||
sellingPoints: (brief.selling_points || []).map((sp, i) => ({ id: `sp-${i}`, content: sp.content, priority: getSellingPointPriority(sp) })),
|
||||
blacklistWords: (brief.blacklist_words || []).map((bw, i) => ({ id: `bw-${i}`, word: bw.word, reason: bw.reason })),
|
||||
}
|
||||
}
|
||||
@@ -113,9 +129,9 @@ const mockBrief: BriefUI = {
|
||||
{ id: 'af2', name: '产品卖点话术.docx', size: '800KB', uploadedAt: '2026-02-02' },
|
||||
],
|
||||
sellingPoints: [
|
||||
{ id: 'sp1', content: 'SPF50+ PA++++', required: true },
|
||||
{ id: 'sp2', content: '轻薄质地,不油腻', required: true },
|
||||
{ id: 'sp3', content: '延展性好,易推开', required: false },
|
||||
{ id: 'sp1', content: 'SPF50+ PA++++', priority: 'core' as const },
|
||||
{ id: 'sp2', content: '轻薄质地,不油腻', priority: 'core' as const },
|
||||
{ id: 'sp3', content: '延展性好,易推开', priority: 'recommended' as const },
|
||||
],
|
||||
blacklistWords: [
|
||||
{ id: 'bw1', word: '最好', reason: '绝对化用语' },
|
||||
@@ -134,8 +150,9 @@ function AgencyBriefSection({ toast, briefData }: { toast: ReturnType<typeof use
|
||||
const [isExpanded, setIsExpanded] = useState(true)
|
||||
const [previewFile, setPreviewFile] = useState<AgencyBriefFile | null>(null)
|
||||
const handleDownload = (file: AgencyBriefFile) => { toast.info(`下载文件: ${file.name}`) }
|
||||
const requiredPoints = briefData.sellingPoints.filter(sp => sp.required)
|
||||
const optionalPoints = briefData.sellingPoints.filter(sp => !sp.required)
|
||||
const corePoints = briefData.sellingPoints.filter(sp => sp.priority === 'core')
|
||||
const recommendedPoints = briefData.sellingPoints.filter(sp => sp.priority === 'recommended')
|
||||
const referencePoints = briefData.sellingPoints.filter(sp => sp.priority === 'reference')
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -173,18 +190,26 @@ function AgencyBriefSection({ toast, briefData }: { toast: ReturnType<typeof use
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2"><Target size={14} className="text-accent-green" />卖点要求</h4>
|
||||
<div className="space-y-2">
|
||||
{requiredPoints.length > 0 && (
|
||||
{corePoints.length > 0 && (
|
||||
<div className="p-3 bg-accent-coral/10 rounded-lg border border-accent-coral/30">
|
||||
<p className="text-xs text-accent-coral font-medium mb-2">必选卖点(必须提及)</p>
|
||||
<div className="flex flex-wrap gap-2">{requiredPoints.map((sp) => (
|
||||
<p className="text-xs text-accent-coral font-medium mb-2">核心卖点(建议优先提及)</p>
|
||||
<div className="flex flex-wrap gap-2">{corePoints.map((sp) => (
|
||||
<span key={sp.id} className="px-2 py-1 text-xs bg-accent-coral/20 text-accent-coral rounded">{sp.content}</span>
|
||||
))}</div>
|
||||
</div>
|
||||
)}
|
||||
{optionalPoints.length > 0 && (
|
||||
{recommendedPoints.length > 0 && (
|
||||
<div className="p-3 bg-accent-amber/10 rounded-lg border border-accent-amber/30">
|
||||
<p className="text-xs text-accent-amber font-medium mb-2">推荐卖点(建议提及)</p>
|
||||
<div className="flex flex-wrap gap-2">{recommendedPoints.map((sp) => (
|
||||
<span key={sp.id} className="px-2 py-1 text-xs bg-accent-amber/20 text-accent-amber rounded">{sp.content}</span>
|
||||
))}</div>
|
||||
</div>
|
||||
)}
|
||||
{referencePoints.length > 0 && (
|
||||
<div className="p-3 bg-bg-elevated rounded-lg">
|
||||
<p className="text-xs text-text-tertiary font-medium mb-2">可选卖点</p>
|
||||
<div className="flex flex-wrap gap-2">{optionalPoints.map((sp) => (
|
||||
<p className="text-xs text-text-tertiary font-medium mb-2">参考信息</p>
|
||||
<div className="flex flex-wrap gap-2">{referencePoints.map((sp) => (
|
||||
<span key={sp.id} className="px-2 py-1 text-xs bg-bg-page text-text-secondary rounded">{sp.content}</span>
|
||||
))}</div>
|
||||
</div>
|
||||
@@ -217,64 +242,109 @@ function AgencyBriefSection({ toast, briefData }: { toast: ReturnType<typeof use
|
||||
|
||||
function UploadSection({ taskId, onUploaded }: { taskId: string; onUploaded: () => void }) {
|
||||
const [file, setFile] = useState<File | null>(null)
|
||||
const { upload, isUploading, progress } = useOSSUpload('script')
|
||||
const [isUploading, setIsUploading] = useState(false)
|
||||
const [progress, setProgress] = useState(0)
|
||||
const [uploadError, setUploadError] = useState<string | null>(null)
|
||||
const toast = useToast()
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const selectedFile = e.target.files?.[0]
|
||||
if (selectedFile) setFile(selectedFile)
|
||||
if (selectedFile) {
|
||||
setFile(selectedFile)
|
||||
setUploadError(null)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!file) return
|
||||
setIsUploading(true)
|
||||
setProgress(0)
|
||||
setUploadError(null)
|
||||
try {
|
||||
const result = await upload(file)
|
||||
if (!USE_MOCK) {
|
||||
if (USE_MOCK) {
|
||||
for (let i = 0; i <= 100; i += 20) {
|
||||
await new Promise(r => setTimeout(r, 400))
|
||||
setProgress(i)
|
||||
}
|
||||
toast.success('脚本已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
} else {
|
||||
const result = await api.proxyUpload(file, 'script', (pct) => {
|
||||
setProgress(Math.min(90, Math.round(pct * 0.9)))
|
||||
})
|
||||
setProgress(95)
|
||||
await api.uploadTaskScript(taskId, { file_url: result.url, file_name: result.file_name })
|
||||
setProgress(100)
|
||||
toast.success('脚本已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
}
|
||||
toast.success('脚本已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : '上传失败')
|
||||
const msg = err instanceof Error ? err.message : '上传失败'
|
||||
setUploadError(msg)
|
||||
toast.error(msg)
|
||||
} finally {
|
||||
setIsUploading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const formatSize = (bytes: number) => {
|
||||
if (bytes < 1024) return bytes + 'B'
|
||||
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB'
|
||||
return (bytes / (1024 * 1024)).toFixed(1) + 'MB'
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader><CardTitle className="flex items-center gap-2"><Upload size={18} className="text-accent-indigo" />上传脚本</CardTitle></CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="border-2 border-dashed border-border-subtle rounded-lg p-8 text-center hover:border-accent-indigo/50 transition-colors">
|
||||
{file ? (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-center gap-3">
|
||||
<FileText size={24} className="text-accent-indigo" />
|
||||
<span className="text-text-primary">{file.name}</span>
|
||||
{!file ? (
|
||||
<label className="border-2 border-dashed border-border-subtle rounded-lg p-8 text-center hover:border-accent-indigo/50 transition-colors cursor-pointer block">
|
||||
<Upload size={32} className="mx-auto text-text-tertiary mb-3" />
|
||||
<p className="text-text-secondary mb-1">点击上传脚本文件</p>
|
||||
<p className="text-xs text-text-tertiary">支持 Word、PDF、TXT、Excel 格式</p>
|
||||
<input type="file" accept=".doc,.docx,.pdf,.txt,.xls,.xlsx" onChange={handleFileChange} className="hidden" />
|
||||
</label>
|
||||
) : (
|
||||
<div className="border border-border-subtle rounded-lg overflow-hidden">
|
||||
<div className="px-4 py-2.5 bg-bg-elevated border-b border-border-subtle">
|
||||
<span className="text-xs font-medium text-text-secondary">已选文件</span>
|
||||
</div>
|
||||
<div className="px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
{isUploading ? (
|
||||
<Loader2 size={16} className="animate-spin text-accent-indigo flex-shrink-0" />
|
||||
) : uploadError ? (
|
||||
<AlertTriangle size={16} className="text-accent-coral flex-shrink-0" />
|
||||
) : (
|
||||
<CheckCircle size={16} className="text-accent-green flex-shrink-0" />
|
||||
)}
|
||||
<FileText size={14} className="text-accent-indigo flex-shrink-0" />
|
||||
<span className="flex-1 text-sm text-text-primary truncate">{file.name}</span>
|
||||
<span className="text-xs text-text-tertiary">{formatSize(file.size)}</span>
|
||||
{!isUploading && (
|
||||
<button type="button" onClick={() => setFile(null)} className="p-1 hover:bg-bg-elevated rounded-full">
|
||||
<XCircle size={16} className="text-text-tertiary" />
|
||||
<button type="button" onClick={() => { setFile(null); setUploadError(null) }} className="p-1 hover:bg-bg-elevated rounded">
|
||||
<XCircle size={14} className="text-text-tertiary" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{isUploading && (
|
||||
<div className="w-full max-w-xs mx-auto">
|
||||
<div className="h-2 bg-bg-elevated rounded-full overflow-hidden mb-2">
|
||||
<div className="h-full bg-accent-indigo transition-all" style={{ width: `${progress}%` }} />
|
||||
</div>
|
||||
<p className="text-sm text-text-tertiary">上传中 {progress}%</p>
|
||||
<div className="mt-2 ml-[30px] h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div className="h-full bg-accent-indigo rounded-full transition-all duration-300" style={{ width: `${progress}%` }} />
|
||||
</div>
|
||||
)}
|
||||
{isUploading && (
|
||||
<p className="mt-1 ml-[30px] text-xs text-text-tertiary">上传中 {progress}%</p>
|
||||
)}
|
||||
{uploadError && (
|
||||
<p className="mt-1 ml-[30px] text-xs text-accent-coral">{uploadError}</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<label className="cursor-pointer">
|
||||
<Upload size={32} className="mx-auto text-text-tertiary mb-3" />
|
||||
<p className="text-text-secondary mb-1">点击或拖拽上传脚本文件</p>
|
||||
<p className="text-xs text-text-tertiary">支持 Word、PDF、TXT 格式</p>
|
||||
<input type="file" accept=".doc,.docx,.pdf,.txt" onChange={handleFileChange} className="hidden" />
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<Button onClick={handleSubmit} disabled={!file || isUploading} fullWidth>
|
||||
{isUploading ? '上传中...' : '提交脚本'}
|
||||
{isUploading ? (
|
||||
<><Loader2 size={16} className="animate-spin" />上传中 {progress}%</>
|
||||
) : '提交脚本'}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
@@ -311,8 +381,15 @@ function AIReviewingSection() {
|
||||
)
|
||||
}
|
||||
|
||||
function getDimensionLabel(key: string) {
|
||||
const labels: Record<string, string> = { legal: '法规合规', platform: '平台规则', brand_safety: '品牌安全', brief_match: 'Brief 匹配' }
|
||||
return labels[key] || key
|
||||
}
|
||||
|
||||
function AIResultSection({ task }: { task: ScriptTaskUI }) {
|
||||
if (!task.aiResult) return null
|
||||
const { dimensions, sellingPointMatches, briefMatchDetail, violations } = task.aiResult
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
@@ -322,32 +399,107 @@ function AIResultSection({ task }: { task: ScriptTaskUI }) {
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{task.aiResult.violations.length > 0 && (
|
||||
{dimensions && (
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
{(['legal', 'platform', 'brand_safety', 'brief_match'] as const).map(key => {
|
||||
const dim = dimensions[key]
|
||||
if (!dim) return null
|
||||
return (
|
||||
<div key={key} className={`p-3 rounded-lg border ${dim.passed ? 'bg-accent-green/5 border-accent-green/20' : 'bg-accent-coral/5 border-accent-coral/20'}`}>
|
||||
<div className="flex items-center justify-between mb-1">
|
||||
<span className="text-xs text-text-secondary">{getDimensionLabel(key)}</span>
|
||||
{dim.passed ? <CheckCircle size={14} className="text-accent-green" /> : <XCircle size={14} className="text-accent-coral" />}
|
||||
</div>
|
||||
<span className={`text-lg font-bold ${dim.passed ? (dim.score >= 85 ? 'text-accent-green' : 'text-yellow-400') : 'text-accent-coral'}`}>{dim.score}</span>
|
||||
{dim.issue_count > 0 && <span className="text-xs text-text-tertiary ml-1">({dim.issue_count} 项问题)</span>}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
{violations.length > 0 && (
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2"><AlertTriangle size={14} className="text-orange-500" />违规检测 ({task.aiResult.violations.length})</h4>
|
||||
{task.aiResult.violations.map((v, idx) => (
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2"><AlertTriangle size={14} className="text-orange-500" />违规检测 ({violations.length})</h4>
|
||||
{violations.map((v, idx) => (
|
||||
<div key={idx} className="p-3 bg-orange-500/10 rounded-lg border border-orange-500/30 mb-2">
|
||||
<div className="flex items-center gap-2 mb-1"><WarningTag>{v.type}</WarningTag></div>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<WarningTag>{v.type}</WarningTag>
|
||||
{v.dimension && <span className="text-xs text-text-tertiary">{getDimensionLabel(v.dimension)}</span>}
|
||||
</div>
|
||||
<p className="text-sm text-text-primary">「{v.content}」</p>
|
||||
<p className="text-xs text-accent-indigo mt-1">{v.suggestion}</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2">合规检查</h4>
|
||||
<div className="space-y-2">
|
||||
{task.aiResult.complianceChecks.map((check, idx) => (
|
||||
<div key={idx} className="flex items-start gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{check.passed ? <CheckCircle size={16} className="text-accent-green flex-shrink-0 mt-0.5" /> : <XCircle size={16} className="text-accent-coral flex-shrink-0 mt-0.5" />}
|
||||
<div className="flex-1">
|
||||
<span className="text-sm text-text-primary">{check.item}</span>
|
||||
{check.note && <p className="text-xs text-text-tertiary mt-0.5">{check.note}</p>}
|
||||
{briefMatchDetail && (
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2"><Target size={14} className="text-accent-indigo" />Brief 匹配度分析</h4>
|
||||
<div className="p-3 bg-bg-elevated rounded-lg space-y-3">
|
||||
<p className="text-sm text-text-secondary">{briefMatchDetail.explanation}</p>
|
||||
{briefMatchDetail.total_points > 0 && (
|
||||
<div>
|
||||
<div className="flex items-center justify-between text-xs mb-1">
|
||||
<span className="text-text-tertiary">卖点覆盖率</span>
|
||||
<span className="text-text-primary font-medium">{briefMatchDetail.matched_points}/{briefMatchDetail.required_points > 0 ? briefMatchDetail.required_points : briefMatchDetail.total_points} 条</span>
|
||||
</div>
|
||||
<div className="h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div className={`h-full rounded-full transition-all ${briefMatchDetail.coverage_score >= 80 ? 'bg-accent-green' : briefMatchDetail.coverage_score >= 50 ? 'bg-accent-amber' : 'bg-accent-coral'}`} style={{ width: `${briefMatchDetail.coverage_score}%` }} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
)}
|
||||
{briefMatchDetail.highlights.length > 0 && (
|
||||
<div>
|
||||
<p className="text-xs text-accent-green font-medium mb-1">亮点</p>
|
||||
<div className="space-y-1">
|
||||
{briefMatchDetail.highlights.map((h, i) => (
|
||||
<div key={i} className="flex items-start gap-2">
|
||||
<CheckCircle size={14} className="text-accent-green flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-text-secondary">{h}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{briefMatchDetail.issues.length > 0 && (
|
||||
<div>
|
||||
<p className="text-xs text-accent-coral font-medium mb-1">可改进</p>
|
||||
<div className="space-y-1">
|
||||
{briefMatchDetail.issues.map((issue, i) => (
|
||||
<div key={i} className="flex items-start gap-2">
|
||||
<AlertTriangle size={14} className="text-accent-coral flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-text-secondary">{issue}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{sellingPointMatches && sellingPointMatches.length > 0 && (
|
||||
<div>
|
||||
<h4 className="text-sm font-medium text-text-primary mb-2 flex items-center gap-2"><Target size={14} className="text-accent-green" />卖点匹配</h4>
|
||||
<div className="space-y-2">
|
||||
{sellingPointMatches.map((sp, idx) => (
|
||||
<div key={idx} className="flex items-start gap-2 p-2 rounded-lg bg-bg-elevated">
|
||||
{sp.matched ? <CheckCircle size={16} className="text-accent-green flex-shrink-0 mt-0.5" /> : <XCircle size={16} className="text-accent-coral flex-shrink-0 mt-0.5" />}
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm text-text-primary">{sp.content}</span>
|
||||
<span className={`px-1.5 py-0.5 text-xs rounded ${
|
||||
sp.priority === 'core' ? 'bg-accent-coral/20 text-accent-coral' :
|
||||
sp.priority === 'recommended' ? 'bg-accent-amber/20 text-accent-amber' :
|
||||
'bg-bg-page text-text-tertiary'
|
||||
}`}>{sp.priority === 'core' ? '核心' : sp.priority === 'recommended' ? '推荐' : '参考'}</span>
|
||||
</div>
|
||||
{sp.evidence && <p className="text-xs text-text-tertiary mt-0.5">{sp.evidence}</p>}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
@@ -440,11 +592,18 @@ export default function CreatorScriptPage() {
|
||||
return () => { unsub1(); unsub2() }
|
||||
}, [subscribe, taskId, loadTask])
|
||||
|
||||
// AI 审核中时轮询(SSE 的后备方案)
|
||||
useEffect(() => {
|
||||
if (task.scriptStatus !== 'ai_reviewing' || USE_MOCK) return
|
||||
const interval = setInterval(() => { loadTask() }, 5000)
|
||||
return () => clearInterval(interval)
|
||||
}, [task.scriptStatus, loadTask])
|
||||
|
||||
const handleContinueToVideo = () => { router.push(`/creator/task/${params.id}/video`) }
|
||||
|
||||
const getStatusDisplay = () => {
|
||||
const map: Record<string, string> = {
|
||||
pending_upload: '待上传脚本', ai_reviewing: 'AI 审核中', ai_result: 'AI 审核完成',
|
||||
pending_upload: '待上传脚本', ai_rejected: 'AI 审核未通过', ai_reviewing: 'AI 审核中', ai_result: 'AI 审核完成',
|
||||
agent_reviewing: '代理商审核中', agent_rejected: '代理商驳回',
|
||||
brand_reviewing: '品牌方终审中', brand_passed: '审核通过', brand_rejected: '品牌方驳回',
|
||||
}
|
||||
@@ -470,6 +629,23 @@ export default function CreatorScriptPage() {
|
||||
<AgencyBriefSection toast={toast} briefData={briefData} />
|
||||
|
||||
{task.scriptStatus === 'pending_upload' && <UploadSection taskId={taskId} onUploaded={loadTask} />}
|
||||
{task.scriptStatus === 'ai_rejected' && (
|
||||
<>
|
||||
<Card className="border-accent-coral/30 bg-accent-coral/5">
|
||||
<CardContent className="py-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<XCircle size={20} className="text-accent-coral mt-0.5 flex-shrink-0" />
|
||||
<div>
|
||||
<p className="text-text-primary font-medium">AI 审核未通过,请修改后重新上传</p>
|
||||
{task.aiRejectReason && <p className="text-sm text-text-secondary mt-1">{task.aiRejectReason}</p>}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<AIResultSection task={task} />
|
||||
<UploadSection taskId={taskId} onUploaded={loadTask} />
|
||||
</>
|
||||
)}
|
||||
{task.scriptStatus === 'ai_reviewing' && <AIReviewingSection />}
|
||||
{task.scriptStatus === 'ai_result' && <><AIResultSection task={task} /><WaitingSection message="等待代理商审核" /></>}
|
||||
{task.scriptStatus === 'agent_reviewing' && <><AIResultSection task={task} /><WaitingSection message="等待代理商审核" /></>}
|
||||
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
import { useSSE } from '@/contexts/SSEContext'
|
||||
import { useOSSUpload } from '@/hooks/useOSSUpload'
|
||||
import type { TaskResponse } from '@/types/task'
|
||||
|
||||
// ========== 类型 ==========
|
||||
@@ -23,6 +22,8 @@ type VideoTaskUI = {
|
||||
brandName: string
|
||||
videoStatus: string
|
||||
videoFile: string | null
|
||||
aiAutoRejected?: boolean
|
||||
aiRejectReason?: string
|
||||
aiResult: null | {
|
||||
score: number
|
||||
hardViolations: Array<{ type: string; content: string; timestamp: number; suggestion: string }>
|
||||
@@ -37,8 +38,9 @@ type VideoTaskUI = {
|
||||
function mapApiToVideoUI(task: TaskResponse): VideoTaskUI {
|
||||
const stage = task.stage
|
||||
let status = 'pending_upload'
|
||||
const aiAutoRejected = task.video_ai_result?.ai_auto_rejected === true
|
||||
switch (stage) {
|
||||
case 'video_upload': status = 'pending_upload'; break
|
||||
case 'video_upload': status = aiAutoRejected ? 'ai_rejected' : 'pending_upload'; break
|
||||
case 'video_ai_review': status = 'ai_reviewing'; break
|
||||
case 'video_agency_review': status = 'agent_reviewing'; break
|
||||
case 'video_brand_review': status = 'brand_reviewing'; break
|
||||
@@ -81,6 +83,8 @@ function mapApiToVideoUI(task: TaskResponse): VideoTaskUI {
|
||||
brandName: task.project?.brand_name || '',
|
||||
videoStatus: status,
|
||||
videoFile: task.video_file_name || null,
|
||||
aiAutoRejected,
|
||||
aiRejectReason: task.video_ai_result?.ai_reject_reason,
|
||||
aiResult,
|
||||
agencyReview,
|
||||
brandReview,
|
||||
@@ -102,64 +106,109 @@ function formatTimestamp(seconds: number): string {
|
||||
|
||||
function UploadSection({ taskId, onUploaded }: { taskId: string; onUploaded: () => void }) {
|
||||
const [file, setFile] = useState<File | null>(null)
|
||||
const { upload, isUploading, progress } = useOSSUpload('video')
|
||||
const [isUploading, setIsUploading] = useState(false)
|
||||
const [progress, setProgress] = useState(0)
|
||||
const [uploadError, setUploadError] = useState<string | null>(null)
|
||||
const toast = useToast()
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const selectedFile = e.target.files?.[0]
|
||||
if (selectedFile) setFile(selectedFile)
|
||||
if (selectedFile) {
|
||||
setFile(selectedFile)
|
||||
setUploadError(null)
|
||||
}
|
||||
}
|
||||
|
||||
const handleUpload = async () => {
|
||||
if (!file) return
|
||||
setIsUploading(true)
|
||||
setProgress(0)
|
||||
setUploadError(null)
|
||||
try {
|
||||
const result = await upload(file)
|
||||
if (!USE_MOCK) {
|
||||
if (USE_MOCK) {
|
||||
for (let i = 0; i <= 100; i += 10) {
|
||||
await new Promise(r => setTimeout(r, 300))
|
||||
setProgress(i)
|
||||
}
|
||||
toast.success('视频已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
} else {
|
||||
const result = await api.proxyUpload(file, 'video', (pct) => {
|
||||
setProgress(Math.min(90, Math.round(pct * 0.9)))
|
||||
})
|
||||
setProgress(95)
|
||||
await api.uploadTaskVideo(taskId, { file_url: result.url, file_name: result.file_name })
|
||||
setProgress(100)
|
||||
toast.success('视频已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
}
|
||||
toast.success('视频已提交,等待 AI 审核')
|
||||
onUploaded()
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : '上传失败')
|
||||
const msg = err instanceof Error ? err.message : '上传失败'
|
||||
setUploadError(msg)
|
||||
toast.error(msg)
|
||||
} finally {
|
||||
setIsUploading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const formatSize = (bytes: number) => {
|
||||
if (bytes < 1024) return bytes + 'B'
|
||||
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB'
|
||||
return (bytes / (1024 * 1024)).toFixed(1) + 'MB'
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader><CardTitle className="flex items-center gap-2"><Upload size={18} className="text-purple-400" />上传视频</CardTitle></CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="border-2 border-dashed border-border-subtle rounded-lg p-8 text-center hover:border-accent-indigo/50 transition-colors">
|
||||
{file ? (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-center gap-3">
|
||||
<Video size={24} className="text-purple-400" />
|
||||
<span className="text-text-primary">{file.name}</span>
|
||||
{!file ? (
|
||||
<label className="border-2 border-dashed border-border-subtle rounded-lg p-8 text-center hover:border-accent-indigo/50 transition-colors cursor-pointer block">
|
||||
<Upload size={32} className="mx-auto text-text-tertiary mb-3" />
|
||||
<p className="text-text-secondary mb-1">点击上传视频文件</p>
|
||||
<p className="text-xs text-text-tertiary">支持 MP4、MOV、AVI 格式,最大 500MB</p>
|
||||
<input type="file" accept="video/*" onChange={handleFileChange} className="hidden" />
|
||||
</label>
|
||||
) : (
|
||||
<div className="border border-border-subtle rounded-lg overflow-hidden">
|
||||
<div className="px-4 py-2.5 bg-bg-elevated border-b border-border-subtle">
|
||||
<span className="text-xs font-medium text-text-secondary">已选文件</span>
|
||||
</div>
|
||||
<div className="px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
{isUploading ? (
|
||||
<Loader2 size={16} className="animate-spin text-purple-400 flex-shrink-0" />
|
||||
) : uploadError ? (
|
||||
<AlertTriangle size={16} className="text-accent-coral flex-shrink-0" />
|
||||
) : (
|
||||
<CheckCircle size={16} className="text-accent-green flex-shrink-0" />
|
||||
)}
|
||||
<Video size={14} className="text-purple-400 flex-shrink-0" />
|
||||
<span className="flex-1 text-sm text-text-primary truncate">{file.name}</span>
|
||||
<span className="text-xs text-text-tertiary">{formatSize(file.size)}</span>
|
||||
{!isUploading && (
|
||||
<button type="button" onClick={() => setFile(null)} className="p-1 hover:bg-bg-elevated rounded-full">
|
||||
<XCircle size={16} className="text-text-tertiary" />
|
||||
<button type="button" onClick={() => { setFile(null); setUploadError(null) }} className="p-1 hover:bg-bg-elevated rounded">
|
||||
<XCircle size={14} className="text-text-tertiary" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{isUploading && (
|
||||
<div className="w-full max-w-xs mx-auto">
|
||||
<div className="h-2 bg-bg-elevated rounded-full overflow-hidden mb-2">
|
||||
<div className="h-full bg-accent-indigo transition-all" style={{ width: `${progress}%` }} />
|
||||
</div>
|
||||
<p className="text-sm text-text-tertiary">上传中 {progress}%</p>
|
||||
<div className="mt-2 ml-[30px] h-2 bg-bg-page rounded-full overflow-hidden">
|
||||
<div className="h-full bg-purple-400 rounded-full transition-all duration-300" style={{ width: `${progress}%` }} />
|
||||
</div>
|
||||
)}
|
||||
{isUploading && (
|
||||
<p className="mt-1 ml-[30px] text-xs text-text-tertiary">上传中 {progress}%</p>
|
||||
)}
|
||||
{uploadError && (
|
||||
<p className="mt-1 ml-[30px] text-xs text-accent-coral">{uploadError}</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<label className="cursor-pointer">
|
||||
<Upload size={32} className="mx-auto text-text-tertiary mb-3" />
|
||||
<p className="text-text-secondary mb-1">点击或拖拽上传视频文件</p>
|
||||
<p className="text-xs text-text-tertiary">支持 MP4、MOV、AVI 格式,最大 500MB</p>
|
||||
<input type="file" accept="video/*" onChange={handleFileChange} className="hidden" />
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<Button onClick={handleUpload} disabled={!file || isUploading} fullWidth>
|
||||
{isUploading ? '上传中...' : '提交视频'}
|
||||
{isUploading ? (
|
||||
<><Loader2 size={16} className="animate-spin" />上传中 {progress}%</>
|
||||
) : '提交视频'}
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
@@ -318,9 +367,16 @@ export default function CreatorVideoPage() {
|
||||
return () => { unsub1(); unsub2() }
|
||||
}, [subscribe, taskId, loadTask])
|
||||
|
||||
// AI 审核中时轮询(SSE 的后备方案)
|
||||
useEffect(() => {
|
||||
if (task.videoStatus !== 'ai_reviewing' || USE_MOCK) return
|
||||
const interval = setInterval(() => { loadTask() }, 5000)
|
||||
return () => clearInterval(interval)
|
||||
}, [task.videoStatus, loadTask])
|
||||
|
||||
const getStatusDisplay = () => {
|
||||
const map: Record<string, string> = {
|
||||
pending_upload: '待上传视频', ai_reviewing: 'AI 审核中', ai_result: 'AI 审核完成',
|
||||
pending_upload: '待上传视频', ai_rejected: 'AI 审核未通过', ai_reviewing: 'AI 审核中', ai_result: 'AI 审核完成',
|
||||
agent_reviewing: '代理商审核中', agent_rejected: '代理商驳回',
|
||||
brand_reviewing: '品牌方终审中', brand_passed: '审核通过', brand_rejected: '品牌方驳回',
|
||||
}
|
||||
@@ -344,6 +400,23 @@ export default function CreatorVideoPage() {
|
||||
<Card><CardContent className="py-4"><ReviewSteps steps={getReviewSteps(task.videoStatus)} /></CardContent></Card>
|
||||
|
||||
{task.videoStatus === 'pending_upload' && <UploadSection taskId={taskId} onUploaded={loadTask} />}
|
||||
{task.videoStatus === 'ai_rejected' && (
|
||||
<>
|
||||
<Card className="border-accent-coral/30 bg-accent-coral/5">
|
||||
<CardContent className="py-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<XCircle size={20} className="text-accent-coral mt-0.5 flex-shrink-0" />
|
||||
<div>
|
||||
<p className="text-text-primary font-medium">视频 AI 审核未通过,请修改后重新上传</p>
|
||||
{task.aiRejectReason && <p className="text-sm text-text-secondary mt-1">{task.aiRejectReason}</p>}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<AIResultSection task={task} />
|
||||
<UploadSection taskId={taskId} onUploaded={loadTask} />
|
||||
</>
|
||||
)}
|
||||
{task.videoStatus === 'ai_reviewing' && <AIReviewingSection />}
|
||||
{task.videoStatus === 'ai_result' && <><AIResultSection task={task} /><WaitingSection message="等待代理商审核" /></>}
|
||||
{task.videoStatus === 'agent_reviewing' && <><AIResultSection task={task} /><WaitingSection message="等待代理商审核" /></>}
|
||||
|
||||
@@ -59,7 +59,7 @@ describe('MobileLayout', () => {
|
||||
</MobileLayout>
|
||||
);
|
||||
const main = container.querySelector('main');
|
||||
expect(main).toHaveClass('pb-[95px]');
|
||||
expect(main).toHaveClass('pb-[80px]');
|
||||
});
|
||||
|
||||
it('showBottomNav=false 时内容区域无底部 padding', () => {
|
||||
|
||||
@@ -30,7 +30,7 @@ describe('Sidebar', () => {
|
||||
it('渲染 agency 导航项', () => {
|
||||
render(<Sidebar role="agency" />);
|
||||
expect(screen.getByText('工作台')).toBeInTheDocument();
|
||||
expect(screen.getByText('审核决策')).toBeInTheDocument();
|
||||
expect(screen.getByText('审核台')).toBeInTheDocument();
|
||||
expect(screen.getByText('Brief 配置')).toBeInTheDocument();
|
||||
expect(screen.getByText('达人管理')).toBeInTheDocument();
|
||||
expect(screen.getByText('数据报表')).toBeInTheDocument();
|
||||
@@ -38,7 +38,7 @@ describe('Sidebar', () => {
|
||||
|
||||
it('渲染 brand 导航项', () => {
|
||||
render(<Sidebar role="brand" />);
|
||||
expect(screen.getByText('数据看板')).toBeInTheDocument();
|
||||
expect(screen.getByText('项目看板')).toBeInTheDocument();
|
||||
expect(screen.getByText('AI 配置')).toBeInTheDocument();
|
||||
expect(screen.getByText('规则配置')).toBeInTheDocument();
|
||||
expect(screen.getByText('终审台')).toBeInTheDocument();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { usePathname } from 'next/navigation'
|
||||
import {
|
||||
@@ -20,6 +21,8 @@ import {
|
||||
MessageSquare
|
||||
} from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
|
||||
interface NavItem {
|
||||
icon: React.ElementType
|
||||
@@ -40,7 +43,7 @@ const agencyNavItems: NavItem[] = [
|
||||
{ icon: LayoutDashboard, label: '工作台', href: '/agency' },
|
||||
{ icon: Scan, label: '审核台', href: '/agency/review' },
|
||||
{ icon: MessageSquare, label: '申诉处理', href: '/agency/appeals' },
|
||||
{ icon: FileText, label: 'Brief 配置', href: '/agency/briefs' },
|
||||
{ icon: FileText, label: '任务配置', href: '/agency/briefs' },
|
||||
{ icon: Users, label: '达人管理', href: '/agency/creators' },
|
||||
{ icon: BarChart3, label: '数据报表', href: '/agency/reports' },
|
||||
{ icon: Bell, label: '消息中心', href: '/agency/messages' },
|
||||
@@ -66,22 +69,47 @@ interface SidebarProps {
|
||||
|
||||
export function Sidebar({ role = 'creator', aiServiceError = false }: SidebarProps) {
|
||||
const pathname = usePathname() || ''
|
||||
const [unreadCount, setUnreadCount] = useState(0)
|
||||
|
||||
// 根据 aiServiceError 动态设置 AI 配置的徽章
|
||||
const getBrandNavItems = (): NavItem[] => {
|
||||
return brandNavItems.map(item => {
|
||||
const fetchUnreadCount = useCallback(async () => {
|
||||
if (USE_MOCK) return
|
||||
try {
|
||||
const res = await api.getUnreadCount()
|
||||
setUnreadCount(res.count)
|
||||
} catch {
|
||||
// 忽略错误(未登录等)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
fetchUnreadCount()
|
||||
const timer = setInterval(fetchUnreadCount, 30000) // 每 30 秒轮询
|
||||
return () => clearInterval(timer)
|
||||
}, [fetchUnreadCount])
|
||||
|
||||
// 消息中心路径
|
||||
const messagesHref = `/${role}/messages`
|
||||
|
||||
// 根据 aiServiceError 和 unreadCount 动态设置徽章
|
||||
const applyBadges = (items: NavItem[]): NavItem[] => {
|
||||
return items.map(item => {
|
||||
if (item.href === '/brand/ai-config' && aiServiceError) {
|
||||
return { ...item, badge: 'warning' as const }
|
||||
}
|
||||
if (item.href === messagesHref && unreadCount > 0) {
|
||||
return { ...item, badge: 'dot' as const }
|
||||
}
|
||||
return item
|
||||
})
|
||||
}
|
||||
|
||||
const navItems = role === 'creator'
|
||||
const baseItems = role === 'creator'
|
||||
? creatorNavItems
|
||||
: role === 'agency'
|
||||
? agencyNavItems
|
||||
: getBrandNavItems()
|
||||
: brandNavItems
|
||||
|
||||
const navItems = applyBadges(baseItems)
|
||||
|
||||
const isActive = (href: string) => {
|
||||
if (href === `/${role}`) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
} from 'lucide-react'
|
||||
import { Button } from './Button'
|
||||
import { Modal } from './Modal'
|
||||
import { api } from '@/lib/api'
|
||||
|
||||
// 文件信息类型
|
||||
export interface FileInfo {
|
||||
@@ -98,20 +99,29 @@ export function FileInfoCard({
|
||||
}) {
|
||||
const category = getFileCategory(file)
|
||||
|
||||
const handleDownload = () => {
|
||||
const handleDownload = async () => {
|
||||
if (onDownload) {
|
||||
onDownload()
|
||||
} else {
|
||||
// 默认下载行为
|
||||
const link = document.createElement('a')
|
||||
link.href = file.fileUrl
|
||||
link.download = file.fileName
|
||||
link.click()
|
||||
try {
|
||||
await api.downloadFile(file.fileUrl, file.fileName)
|
||||
} catch {
|
||||
// 回退到直接链接下载
|
||||
const link = document.createElement('a')
|
||||
link.href = file.fileUrl
|
||||
link.download = file.fileName
|
||||
link.click()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleOpenInNewTab = () => {
|
||||
window.open(file.fileUrl, '_blank')
|
||||
const handleOpenInNewTab = async () => {
|
||||
try {
|
||||
const blobUrl = await api.getPreviewUrl(file.fileUrl)
|
||||
window.open(blobUrl, '_blank')
|
||||
} catch {
|
||||
window.open(file.fileUrl, '_blank')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -300,15 +310,26 @@ export function DocumentPlaceholder({
|
||||
该文件格式暂不支持在线预览
|
||||
</p>
|
||||
<div className="flex gap-2 justify-center">
|
||||
<Button variant="secondary" onClick={() => window.open(file.fileUrl, '_blank')}>
|
||||
<Button variant="secondary" onClick={async () => {
|
||||
try {
|
||||
const blobUrl = await api.getPreviewUrl(file.fileUrl)
|
||||
window.open(blobUrl, '_blank')
|
||||
} catch {
|
||||
window.open(file.fileUrl, '_blank')
|
||||
}
|
||||
}}>
|
||||
<ExternalLink size={16} />
|
||||
在新标签页打开
|
||||
</Button>
|
||||
<Button onClick={() => {
|
||||
const link = document.createElement('a')
|
||||
link.href = file.fileUrl
|
||||
link.download = file.fileName
|
||||
link.click()
|
||||
<Button onClick={async () => {
|
||||
try {
|
||||
await api.downloadFile(file.fileUrl, file.fileName)
|
||||
} catch {
|
||||
const link = document.createElement('a')
|
||||
link.href = file.fileUrl
|
||||
link.download = file.fileName
|
||||
link.click()
|
||||
}
|
||||
}}>
|
||||
<Download size={16} />
|
||||
下载文件
|
||||
@@ -380,15 +401,26 @@ export function FilePreviewModal({
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button variant="secondary" onClick={() => window.open(file.fileUrl, '_blank')}>
|
||||
<Button variant="secondary" onClick={async () => {
|
||||
try {
|
||||
const blobUrl = await api.getPreviewUrl(file.fileUrl)
|
||||
window.open(blobUrl, '_blank')
|
||||
} catch {
|
||||
window.open(file.fileUrl, '_blank')
|
||||
}
|
||||
}}>
|
||||
<ExternalLink size={16} />
|
||||
新标签页打开
|
||||
</Button>
|
||||
<Button onClick={() => {
|
||||
const link = document.createElement('a')
|
||||
link.href = file.fileUrl
|
||||
link.download = file.fileName
|
||||
link.click()
|
||||
<Button onClick={async () => {
|
||||
try {
|
||||
await api.downloadFile(file.fileUrl, file.fileName)
|
||||
} catch {
|
||||
const link = document.createElement('a')
|
||||
link.href = file.fileUrl
|
||||
link.download = file.fileName
|
||||
link.click()
|
||||
}
|
||||
}}>
|
||||
<Download size={16} />
|
||||
下载
|
||||
|
||||
@@ -0,0 +1,520 @@
|
||||
/**
|
||||
* AuthContext Unit Tests
|
||||
*
|
||||
* Tests the AuthProvider, useAuth hook, and the exported USE_MOCK flag.
|
||||
* Covers: initial state, login (mock + real), logout, register, and
|
||||
* restoration of user from localStorage on mount.
|
||||
*/
|
||||
import React from 'react'
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { renderHook, act, waitFor } from '@testing-library/react'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Use vi.hoisted to create mock functions before vi.mock hoisting
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const { mockApiLogin, mockApiRegister, mockClearTokens, mockGetAccessToken } = vi.hoisted(() => ({
|
||||
mockApiLogin: vi.fn(),
|
||||
mockApiRegister: vi.fn(),
|
||||
mockClearTokens: vi.fn(),
|
||||
mockGetAccessToken: vi.fn(() => null),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/api', () => ({
|
||||
api: {
|
||||
login: mockApiLogin,
|
||||
register: mockApiRegister,
|
||||
},
|
||||
clearTokens: mockClearTokens,
|
||||
getAccessToken: mockGetAccessToken,
|
||||
setTokens: vi.fn(),
|
||||
}))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Import AuthContext after mocks are set up
|
||||
// ---------------------------------------------------------------------------
|
||||
import { AuthProvider, useAuth, USE_MOCK } from './AuthContext'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function mockLocalStorage() {
|
||||
const store: Record<string, string> = {}
|
||||
return {
|
||||
getItem: vi.fn((key: string) => store[key] ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => {
|
||||
store[key] = value
|
||||
}),
|
||||
removeItem: vi.fn((key: string) => {
|
||||
delete store[key]
|
||||
}),
|
||||
clear: vi.fn(() => {
|
||||
Object.keys(store).forEach((k) => delete store[k])
|
||||
}),
|
||||
get length() {
|
||||
return Object.keys(store).length
|
||||
},
|
||||
key: vi.fn((index: number) => Object.keys(store)[index] ?? null),
|
||||
}
|
||||
}
|
||||
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<AuthProvider>{children}</AuthProvider>
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('AuthContext', () => {
|
||||
let storage: ReturnType<typeof mockLocalStorage>
|
||||
|
||||
beforeEach(() => {
|
||||
storage = mockLocalStorage()
|
||||
Object.defineProperty(window, 'localStorage', { value: storage, writable: true })
|
||||
vi.clearAllMocks()
|
||||
// Default: no stored token
|
||||
mockGetAccessToken.mockReturnValue(null)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
storage.clear()
|
||||
})
|
||||
|
||||
// ---- USE_MOCK flag ----
|
||||
|
||||
describe('USE_MOCK export', () => {
|
||||
it('is exported as a boolean', () => {
|
||||
expect(typeof USE_MOCK).toBe('boolean')
|
||||
})
|
||||
|
||||
it('is true in test/development environment or based on env var', () => {
|
||||
// vitest runs with NODE_ENV=test. The AuthContext checks for
|
||||
// NODE_ENV === 'development' or NEXT_PUBLIC_USE_MOCK === 'true'.
|
||||
// We verify the export exists and is a boolean.
|
||||
expect([true, false]).toContain(USE_MOCK)
|
||||
})
|
||||
})
|
||||
|
||||
// ---- Initial state ----
|
||||
|
||||
describe('initial state', () => {
|
||||
it('starts with user as null', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.user).toBeNull()
|
||||
})
|
||||
|
||||
it('starts with isAuthenticated as false', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.isAuthenticated).toBe(false)
|
||||
})
|
||||
|
||||
it('starts with isLoading true, then becomes false', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// ---- Restoring user from localStorage ----
|
||||
|
||||
describe('restoring user from localStorage', () => {
|
||||
it('restores user when token and stored user exist', async () => {
|
||||
const storedUser = {
|
||||
id: 'u1',
|
||||
name: 'Restored User',
|
||||
email: 'restored@test.com',
|
||||
role: 'brand',
|
||||
is_verified: true,
|
||||
}
|
||||
|
||||
mockGetAccessToken.mockReturnValue('valid-token')
|
||||
storage.setItem('miaosi_user', JSON.stringify(storedUser))
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.user).toEqual(storedUser)
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
})
|
||||
|
||||
it('does not restore user when token is missing', async () => {
|
||||
const storedUser = { id: 'u1', name: 'No Token', role: 'brand', is_verified: true }
|
||||
storage.setItem('miaosi_user', JSON.stringify(storedUser))
|
||||
mockGetAccessToken.mockReturnValue(null)
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.user).toBeNull()
|
||||
expect(result.current.isAuthenticated).toBe(false)
|
||||
})
|
||||
|
||||
it('clears invalid JSON from localStorage gracefully', async () => {
|
||||
mockGetAccessToken.mockReturnValue('some-token')
|
||||
storage.setItem('miaosi_user', 'not-valid-json{{{')
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(mockClearTokens).toHaveBeenCalled()
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_user')
|
||||
expect(result.current.user).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// ---- Login ----
|
||||
|
||||
describe('login', () => {
|
||||
// When USE_MOCK is true, the AuthProvider uses mock login logic.
|
||||
// When false, it calls api.login().
|
||||
|
||||
if (!USE_MOCK) {
|
||||
it('calls api.login and sets user on success (real API path)', async () => {
|
||||
const loginResponse = {
|
||||
access_token: 'at',
|
||||
refresh_token: 'rt',
|
||||
token_type: 'bearer',
|
||||
expires_in: 900,
|
||||
user: {
|
||||
id: 'u1',
|
||||
name: 'API User',
|
||||
email: 'api@test.com',
|
||||
role: 'brand',
|
||||
is_verified: true,
|
||||
},
|
||||
}
|
||||
mockApiLogin.mockResolvedValueOnce(loginResponse)
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let loginResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
loginResult = await result.current.login({
|
||||
email: 'api@test.com',
|
||||
password: 'pass123',
|
||||
})
|
||||
})
|
||||
|
||||
expect(loginResult!.success).toBe(true)
|
||||
expect(result.current.user?.name).toBe('API User')
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
expect(storage.setItem).toHaveBeenCalledWith(
|
||||
'miaosi_user',
|
||||
expect.stringContaining('API User')
|
||||
)
|
||||
})
|
||||
|
||||
it('returns error on api.login failure (real API path)', async () => {
|
||||
mockApiLogin.mockRejectedValueOnce(new Error('Invalid credentials'))
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let loginResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
loginResult = await result.current.login({
|
||||
email: 'bad@test.com',
|
||||
password: 'wrong',
|
||||
})
|
||||
})
|
||||
|
||||
expect(loginResult!.success).toBe(false)
|
||||
expect(loginResult!.error).toBe('Invalid credentials')
|
||||
expect(result.current.user).toBeNull()
|
||||
})
|
||||
}
|
||||
|
||||
if (USE_MOCK) {
|
||||
it('logs in with mock user for known email (mock path)', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let loginResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
loginResult = await result.current.login({
|
||||
email: 'creator@demo.com',
|
||||
password: 'demo123',
|
||||
})
|
||||
})
|
||||
|
||||
expect(loginResult!.success).toBe(true)
|
||||
expect(result.current.user?.email).toBe('creator@demo.com')
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
})
|
||||
|
||||
it('returns error for unknown email in mock mode', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let loginResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
loginResult = await result.current.login({
|
||||
email: 'unknown@demo.com',
|
||||
password: 'demo123',
|
||||
})
|
||||
})
|
||||
|
||||
expect(loginResult!.success).toBe(false)
|
||||
expect(loginResult!.error).toBeDefined()
|
||||
})
|
||||
|
||||
it('returns error for wrong password in mock mode', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let loginResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
loginResult = await result.current.login({
|
||||
email: 'creator@demo.com',
|
||||
password: 'wrongpassword',
|
||||
})
|
||||
})
|
||||
|
||||
expect(loginResult!.success).toBe(false)
|
||||
expect(loginResult!.error).toBe('密码错误')
|
||||
})
|
||||
|
||||
it('allows email code login for known user in mock mode', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let loginResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
loginResult = await result.current.login({
|
||||
email: 'brand@demo.com',
|
||||
email_code: '123456',
|
||||
})
|
||||
})
|
||||
|
||||
expect(loginResult!.success).toBe(true)
|
||||
expect(result.current.user?.email).toBe('brand@demo.com')
|
||||
expect(result.current.user?.role).toBe('brand')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// ---- Register ----
|
||||
|
||||
describe('register', () => {
|
||||
const registerData = {
|
||||
email: 'new@example.com',
|
||||
password: 'secure123',
|
||||
name: 'New User',
|
||||
role: 'creator' as const,
|
||||
email_code: '123456',
|
||||
}
|
||||
|
||||
if (!USE_MOCK) {
|
||||
it('calls api.register and sets user on success (real API path)', async () => {
|
||||
const registerResponse = {
|
||||
access_token: 'at',
|
||||
refresh_token: 'rt',
|
||||
token_type: 'bearer',
|
||||
expires_in: 900,
|
||||
user: {
|
||||
id: 'u2',
|
||||
name: 'New User',
|
||||
email: 'new@example.com',
|
||||
role: 'creator',
|
||||
is_verified: false,
|
||||
},
|
||||
}
|
||||
mockApiRegister.mockResolvedValueOnce(registerResponse)
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let registerResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
registerResult = await result.current.register(registerData)
|
||||
})
|
||||
|
||||
expect(registerResult!.success).toBe(true)
|
||||
expect(result.current.user?.name).toBe('New User')
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
})
|
||||
|
||||
it('returns error on api.register failure', async () => {
|
||||
mockApiRegister.mockRejectedValueOnce(new Error('Email already exists'))
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let registerResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
registerResult = await result.current.register(registerData)
|
||||
})
|
||||
|
||||
expect(registerResult!.success).toBe(false)
|
||||
expect(registerResult!.error).toBe('Email already exists')
|
||||
})
|
||||
}
|
||||
|
||||
if (USE_MOCK) {
|
||||
it('creates a mock user on register in mock mode', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
let registerResult: { success: boolean; error?: string }
|
||||
await act(async () => {
|
||||
registerResult = await result.current.register(registerData)
|
||||
})
|
||||
|
||||
expect(registerResult!.success).toBe(true)
|
||||
expect(result.current.user?.email).toBe('new@example.com')
|
||||
expect(result.current.user?.name).toBe('New User')
|
||||
expect(result.current.user?.role).toBe('creator')
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// ---- Logout ----
|
||||
|
||||
describe('logout', () => {
|
||||
it('clears user state, tokens, and localStorage', async () => {
|
||||
const storedUser = {
|
||||
id: 'u1',
|
||||
name: 'Logged In',
|
||||
email: 'in@test.com',
|
||||
role: 'brand',
|
||||
is_verified: true,
|
||||
}
|
||||
mockGetAccessToken.mockReturnValue('valid-token')
|
||||
storage.setItem('miaosi_user', JSON.stringify(storedUser))
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.user).not.toBeNull()
|
||||
expect(result.current.isAuthenticated).toBe(true)
|
||||
|
||||
act(() => {
|
||||
result.current.logout()
|
||||
})
|
||||
|
||||
expect(result.current.user).toBeNull()
|
||||
expect(result.current.isAuthenticated).toBe(false)
|
||||
expect(mockClearTokens).toHaveBeenCalled()
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_user')
|
||||
})
|
||||
})
|
||||
|
||||
// ---- switchRole ----
|
||||
|
||||
describe('switchRole', () => {
|
||||
it('updates the user role and persists to localStorage', async () => {
|
||||
const storedUser = {
|
||||
id: 'u1',
|
||||
name: 'Multi Role',
|
||||
email: 'multi@test.com',
|
||||
role: 'brand',
|
||||
is_verified: true,
|
||||
}
|
||||
mockGetAccessToken.mockReturnValue('valid-token')
|
||||
storage.setItem('miaosi_user', JSON.stringify(storedUser))
|
||||
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
expect(result.current.user?.role).toBe('brand')
|
||||
|
||||
act(() => {
|
||||
result.current.switchRole('agency')
|
||||
})
|
||||
|
||||
expect(result.current.user?.role).toBe('agency')
|
||||
// Check localStorage was updated with new role
|
||||
const storedCalls = storage.setItem.mock.calls.filter(
|
||||
([key]: [string, string]) => key === 'miaosi_user'
|
||||
)
|
||||
const lastStored = JSON.parse(storedCalls[storedCalls.length - 1][1])
|
||||
expect(lastStored.role).toBe('agency')
|
||||
})
|
||||
|
||||
it('does nothing when no user is logged in', async () => {
|
||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false)
|
||||
})
|
||||
|
||||
act(() => {
|
||||
result.current.switchRole('creator')
|
||||
})
|
||||
|
||||
expect(result.current.user).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// ---- useAuth outside provider ----
|
||||
|
||||
describe('useAuth outside AuthProvider', () => {
|
||||
it('throws an error when used outside AuthProvider', () => {
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
|
||||
expect(() => {
|
||||
renderHook(() => useAuth())
|
||||
}).toThrow('useAuth must be used within an AuthProvider')
|
||||
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -21,7 +21,8 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined)
|
||||
const USER_STORAGE_KEY = 'miaosi_user'
|
||||
|
||||
// 开发模式:使用 mock 数据
|
||||
export const USE_MOCK = process.env.NEXT_PUBLIC_USE_MOCK === 'true' || process.env.NODE_ENV === 'development'
|
||||
export const USE_MOCK = process.env.NEXT_PUBLIC_USE_MOCK === 'true' ||
|
||||
(process.env.NEXT_PUBLIC_USE_MOCK !== 'false' && process.env.NODE_ENV === 'development')
|
||||
|
||||
// Mock 用户数据
|
||||
const MOCK_USERS: Record<string, User & { password: string }> = {
|
||||
@@ -74,6 +75,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
if (token && storedUser) {
|
||||
try {
|
||||
const parsed = JSON.parse(storedUser)
|
||||
if (parsed.tenant_id) {
|
||||
api.setTenantId(parsed.tenant_id)
|
||||
}
|
||||
setUser(parsed)
|
||||
} catch {
|
||||
clearTokens()
|
||||
@@ -108,6 +112,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
// 真实 API 登录
|
||||
const response = await api.login(credentials)
|
||||
if (response.user.tenant_id) {
|
||||
api.setTenantId(response.user.tenant_id)
|
||||
}
|
||||
setUser(response.user)
|
||||
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(response.user))
|
||||
return { success: true }
|
||||
@@ -137,6 +144,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
// 真实 API 注册
|
||||
const response = await api.register(data)
|
||||
if (response.user.tenant_id) {
|
||||
api.setTenantId(response.user.tenant_id)
|
||||
}
|
||||
setUser(response.user)
|
||||
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(response.user))
|
||||
return { success: true }
|
||||
|
||||
@@ -53,47 +53,12 @@ export function useOSSUpload(fileType: string = 'general'): UseOSSUploadReturn {
|
||||
return result
|
||||
}
|
||||
|
||||
// 1. 获取上传凭证
|
||||
setProgress(10)
|
||||
const policy = await api.getUploadPolicy(fileType)
|
||||
|
||||
// 2. 构建 COS 直传 FormData
|
||||
const fileKey = `${policy.dir}${Date.now()}_${file.name}`
|
||||
const formData = new FormData()
|
||||
formData.append('key', fileKey)
|
||||
formData.append('q-sign-algorithm', policy.q_sign_algorithm)
|
||||
formData.append('q-ak', policy.q_ak)
|
||||
formData.append('q-key-time', policy.q_key_time)
|
||||
formData.append('q-signature', policy.q_signature)
|
||||
formData.append('policy', policy.policy)
|
||||
formData.append('success_action_status', '200')
|
||||
formData.append('file', file)
|
||||
|
||||
// 3. 上传到 COS
|
||||
setProgress(30)
|
||||
const xhr = new XMLHttpRequest()
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
xhr.upload.onprogress = (e) => {
|
||||
if (e.lengthComputable) {
|
||||
setProgress(30 + Math.round((e.loaded / e.total) * 50))
|
||||
}
|
||||
}
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve()
|
||||
} else {
|
||||
reject(new Error(`上传失败: ${xhr.status}`))
|
||||
}
|
||||
}
|
||||
xhr.onerror = () => reject(new Error('网络错误'))
|
||||
xhr.open('POST', policy.host)
|
||||
xhr.send(formData)
|
||||
// 后端代理上传:文件 → 后端 → TOS,避免浏览器 CORS/代理问题
|
||||
setProgress(5)
|
||||
const result = await api.proxyUpload(file, fileType, (pct) => {
|
||||
setProgress(5 + Math.round(pct * 0.9))
|
||||
})
|
||||
|
||||
// 4. 回调通知后端
|
||||
setProgress(90)
|
||||
const result = await api.fileUploaded(fileKey, file.name, file.size, fileType)
|
||||
|
||||
setProgress(100)
|
||||
setIsUploading(false)
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* 获取私有桶文件的预签名访问 URL
|
||||
*
|
||||
* 用于展示/下载 TOS 私有桶中的文件。
|
||||
* 自动缓存签名 URL,过期前 5 分钟刷新。
|
||||
*/
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
import { api } from '@/lib/api'
|
||||
import { USE_MOCK } from '@/contexts/AuthContext'
|
||||
|
||||
const urlCache = new Map<string, { signedUrl: string; expireAt: number }>()
|
||||
|
||||
export function useSignedUrl(originalUrl: string | undefined | null) {
|
||||
const [signedUrl, setSignedUrl] = useState<string | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const mountedRef = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
return () => { mountedRef.current = false }
|
||||
}, [])
|
||||
|
||||
const fetchSignedUrl = useCallback(async () => {
|
||||
if (!originalUrl) {
|
||||
setSignedUrl(null)
|
||||
return
|
||||
}
|
||||
|
||||
// Mock 模式直接返回原始 URL
|
||||
if (USE_MOCK) {
|
||||
setSignedUrl(originalUrl)
|
||||
return
|
||||
}
|
||||
|
||||
// 非 TOS URL(如外部链接)直接返回
|
||||
if (!originalUrl.includes('tos-cn-') && !originalUrl.includes('volces.com') && !originalUrl.startsWith('uploads/')) {
|
||||
setSignedUrl(originalUrl)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查缓存(提前 5 分钟过期)
|
||||
const cached = urlCache.get(originalUrl)
|
||||
if (cached && cached.expireAt > Date.now() + 5 * 60 * 1000) {
|
||||
setSignedUrl(cached.signedUrl)
|
||||
return
|
||||
}
|
||||
|
||||
setLoading(true)
|
||||
try {
|
||||
const expireSeconds = 3600
|
||||
const url = await api.getSignedUrl(originalUrl)
|
||||
if (mountedRef.current) {
|
||||
setSignedUrl(url)
|
||||
urlCache.set(originalUrl, {
|
||||
signedUrl: url,
|
||||
expireAt: Date.now() + expireSeconds * 1000,
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// 签名失败时回退到原始 URL
|
||||
if (mountedRef.current) {
|
||||
setSignedUrl(originalUrl)
|
||||
}
|
||||
} finally {
|
||||
if (mountedRef.current) {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
}, [originalUrl])
|
||||
|
||||
useEffect(() => {
|
||||
fetchSignedUrl()
|
||||
}, [fetchSignedUrl])
|
||||
|
||||
return { signedUrl, loading, refresh: fetchSignedUrl }
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量获取签名 URL 的工具函数
|
||||
*/
|
||||
export async function getSignedUrls(urls: string[]): Promise<Map<string, string>> {
|
||||
const result = new Map<string, string>()
|
||||
|
||||
if (USE_MOCK) {
|
||||
urls.forEach(u => result.set(u, u))
|
||||
return result
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
urls.map(async (url) => {
|
||||
try {
|
||||
const signed = await api.getSignedUrl(url)
|
||||
result.set(url, signed)
|
||||
} catch {
|
||||
result.set(url, url)
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,599 @@
|
||||
/**
|
||||
* API Client Unit Tests
|
||||
*
|
||||
* Tests the ApiClient singleton: axios configuration, token interceptors,
|
||||
* 401 refresh flow, and key API method endpoint mappings.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// vi.hoisted() runs BEFORE vi.mock hoisting, so these variables are available
|
||||
// inside the vi.mock factory.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const {
|
||||
mockAxiosInstance,
|
||||
requestInterceptorHolder,
|
||||
responseErrorInterceptorHolder,
|
||||
} = vi.hoisted(() => {
|
||||
const requestInterceptorHolder: { fn: ((config: any) => any) | null } = { fn: null }
|
||||
const responseErrorInterceptorHolder: { fn: ((error: any) => any) | null } = { fn: null }
|
||||
|
||||
const mockAxiosInstance = {
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
put: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
interceptors: {
|
||||
request: {
|
||||
use: vi.fn((onFulfilled: any) => {
|
||||
requestInterceptorHolder.fn = onFulfilled
|
||||
}),
|
||||
},
|
||||
response: {
|
||||
use: vi.fn((_onFulfilled: any, onRejected: any) => {
|
||||
responseErrorInterceptorHolder.fn = onRejected
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return { mockAxiosInstance, requestInterceptorHolder, responseErrorInterceptorHolder }
|
||||
})
|
||||
|
||||
vi.mock('axios', () => {
|
||||
return {
|
||||
default: {
|
||||
create: vi.fn(() => mockAxiosInstance),
|
||||
post: vi.fn(),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Import the module under test AFTER mocking axios
|
||||
// ---------------------------------------------------------------------------
|
||||
import axios from 'axios'
|
||||
import { api, getAccessToken, getRefreshToken, setTokens, clearTokens } from './api'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function mockLocalStorage() {
|
||||
const store: Record<string, string> = {}
|
||||
return {
|
||||
getItem: vi.fn((key: string) => store[key] ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => {
|
||||
store[key] = value
|
||||
}),
|
||||
removeItem: vi.fn((key: string) => {
|
||||
delete store[key]
|
||||
}),
|
||||
clear: vi.fn(() => {
|
||||
Object.keys(store).forEach((k) => delete store[k])
|
||||
}),
|
||||
get length() {
|
||||
return Object.keys(store).length
|
||||
},
|
||||
key: vi.fn((index: number) => Object.keys(store)[index] ?? null),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('API Client', () => {
|
||||
let storage: ReturnType<typeof mockLocalStorage>
|
||||
|
||||
beforeEach(() => {
|
||||
storage = mockLocalStorage()
|
||||
Object.defineProperty(window, 'localStorage', { value: storage, writable: true })
|
||||
|
||||
// Reset all mock call counts but preserve the interceptor captures
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
storage.clear()
|
||||
})
|
||||
|
||||
// ---- Axios instance configuration ----
|
||||
|
||||
describe('axios instance configuration', () => {
|
||||
it('creates axios instance with correct baseURL and timeout', () => {
|
||||
// axios.create is called at module load time before vi.clearAllMocks().
|
||||
// Instead of checking call count, verify the singleton `api` exists and
|
||||
// that axios.create was invoked (the mock instance is connected).
|
||||
// We confirm indirectly: if api.login calls mockAxiosInstance.post,
|
||||
// that proves axios.create returned our mock.
|
||||
expect(api).toBeDefined()
|
||||
// Also verify the interceptor callbacks were captured (proves setup ran)
|
||||
expect(requestInterceptorHolder.fn).not.toBeNull()
|
||||
expect(responseErrorInterceptorHolder.fn).not.toBeNull()
|
||||
})
|
||||
|
||||
it('captures request and response interceptors during setup', () => {
|
||||
// The interceptor functions were captured during module initialization.
|
||||
// Verify they are functions that we can invoke.
|
||||
expect(typeof requestInterceptorHolder.fn).toBe('function')
|
||||
expect(typeof responseErrorInterceptorHolder.fn).toBe('function')
|
||||
})
|
||||
})
|
||||
|
||||
// ---- Token management helpers ----
|
||||
|
||||
describe('token management helpers', () => {
|
||||
it('getAccessToken returns null when no token stored', () => {
|
||||
expect(getAccessToken()).toBeNull()
|
||||
})
|
||||
|
||||
it('getAccessToken returns the stored token', () => {
|
||||
storage.setItem('miaosi_access_token', 'test-access-token')
|
||||
expect(getAccessToken()).toBe('test-access-token')
|
||||
})
|
||||
|
||||
it('getRefreshToken returns null when no token stored', () => {
|
||||
expect(getRefreshToken()).toBeNull()
|
||||
})
|
||||
|
||||
it('getRefreshToken returns the stored refresh token', () => {
|
||||
storage.setItem('miaosi_refresh_token', 'test-refresh-token')
|
||||
expect(getRefreshToken()).toBe('test-refresh-token')
|
||||
})
|
||||
|
||||
it('setTokens stores both access and refresh tokens', () => {
|
||||
setTokens('access-123', 'refresh-456')
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_access_token', 'access-123')
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_refresh_token', 'refresh-456')
|
||||
})
|
||||
|
||||
it('clearTokens removes both tokens from storage', () => {
|
||||
storage.setItem('miaosi_access_token', 'a')
|
||||
storage.setItem('miaosi_refresh_token', 'b')
|
||||
clearTokens()
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_access_token')
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_refresh_token')
|
||||
})
|
||||
})
|
||||
|
||||
// ---- Request interceptor ----
|
||||
|
||||
describe('request interceptor', () => {
|
||||
it('adds Authorization header when access token exists', () => {
|
||||
storage.setItem('miaosi_access_token', 'my-token')
|
||||
const config = { headers: {} as Record<string, string> }
|
||||
const result = requestInterceptorHolder.fn!(config)
|
||||
expect(result.headers.Authorization).toBe('Bearer my-token')
|
||||
})
|
||||
|
||||
it('does not add Authorization header when no token', () => {
|
||||
const config = { headers: {} as Record<string, string> }
|
||||
const result = requestInterceptorHolder.fn!(config)
|
||||
expect(result.headers.Authorization).toBeUndefined()
|
||||
})
|
||||
|
||||
it('always adds X-Tenant-ID header', () => {
|
||||
const config = { headers: {} as Record<string, string> }
|
||||
const result = requestInterceptorHolder.fn!(config)
|
||||
expect(result.headers['X-Tenant-ID']).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
// ---- Response interceptor: 401 refresh ----
|
||||
|
||||
describe('response interceptor - 401 refresh', () => {
|
||||
it('attempts token refresh on 401 response', async () => {
|
||||
storage.setItem('miaosi_refresh_token', 'valid-refresh')
|
||||
|
||||
const newAccessToken = 'new-access-token-123';
|
||||
|
||||
(axios.post as ReturnType<typeof vi.fn>).mockResolvedValueOnce({
|
||||
data: { access_token: newAccessToken, token_type: 'bearer', expires_in: 900 },
|
||||
})
|
||||
|
||||
const error = {
|
||||
response: { status: 401 },
|
||||
config: { headers: {} as Record<string, string>, _retry: false },
|
||||
}
|
||||
|
||||
// The response interceptor will:
|
||||
// 1. Call axios.post to refresh the token
|
||||
// 2. Try to call this.client(originalRequest) to retry
|
||||
// Since mockAxiosInstance is an object (not callable), step 2 throws.
|
||||
// We catch that and verify step 1 happened correctly.
|
||||
try {
|
||||
await responseErrorInterceptorHolder.fn!(error)
|
||||
} catch {
|
||||
// Expected: this.client is not a function (mockAxiosInstance is not callable)
|
||||
}
|
||||
|
||||
expect(axios.post).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/auth/refresh'),
|
||||
{ refresh_token: 'valid-refresh' }
|
||||
)
|
||||
// Verify the new access token was stored
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_access_token', newAccessToken)
|
||||
})
|
||||
|
||||
it('clears tokens and redirects to /login when refresh fails', async () => {
|
||||
storage.setItem('miaosi_refresh_token', 'expired-refresh')
|
||||
|
||||
;(axios.post as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('Token expired'))
|
||||
|
||||
const originalHref = window.location.href
|
||||
Object.defineProperty(window, 'location', {
|
||||
value: { href: '' },
|
||||
writable: true,
|
||||
configurable: true,
|
||||
})
|
||||
|
||||
const error = {
|
||||
response: { status: 401 },
|
||||
config: { headers: {} as Record<string, string>, _retry: false },
|
||||
}
|
||||
|
||||
await expect(responseErrorInterceptorHolder.fn!(error)).rejects.toThrow()
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_access_token')
|
||||
expect(window.location.href).toBe('/login')
|
||||
|
||||
// Restore
|
||||
Object.defineProperty(window, 'location', {
|
||||
value: { href: originalHref },
|
||||
writable: true,
|
||||
configurable: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects with error message for non-401 errors', async () => {
|
||||
const error = {
|
||||
response: { status: 500, data: { detail: 'Internal Server Error' } },
|
||||
config: { headers: {} as Record<string, string> },
|
||||
message: 'Request failed',
|
||||
}
|
||||
|
||||
await expect(responseErrorInterceptorHolder.fn!(error)).rejects.toThrow('Internal Server Error')
|
||||
})
|
||||
|
||||
it('falls through to error handler when _retry is already true', async () => {
|
||||
const error = {
|
||||
response: { status: 401 },
|
||||
config: { headers: {} as Record<string, string>, _retry: true },
|
||||
message: 'Unauthorized',
|
||||
}
|
||||
|
||||
await expect(responseErrorInterceptorHolder.fn!(error)).rejects.toThrow()
|
||||
expect(axios.post).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects when no refresh token is available', async () => {
|
||||
// No refresh token in storage
|
||||
const error = {
|
||||
response: { status: 401 },
|
||||
config: { headers: {} as Record<string, string>, _retry: false },
|
||||
}
|
||||
|
||||
Object.defineProperty(window, 'location', {
|
||||
value: { href: '' },
|
||||
writable: true,
|
||||
configurable: true,
|
||||
})
|
||||
|
||||
await expect(responseErrorInterceptorHolder.fn!(error)).rejects.toThrow()
|
||||
// refresh should not have been called since there's no token
|
||||
expect(axios.post).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
// ---- API methods: endpoint mapping ----
|
||||
|
||||
describe('api.login()', () => {
|
||||
it('calls POST /auth/login with credentials and stores tokens', async () => {
|
||||
const mockResponse = {
|
||||
data: {
|
||||
access_token: 'at',
|
||||
refresh_token: 'rt',
|
||||
token_type: 'bearer',
|
||||
expires_in: 900,
|
||||
user: { id: 'u1', name: 'Test', role: 'brand', is_verified: true },
|
||||
},
|
||||
}
|
||||
mockAxiosInstance.post.mockResolvedValueOnce(mockResponse)
|
||||
|
||||
const result = await api.login({ email: 'test@example.com', password: 'pass123' })
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/auth/login', {
|
||||
email: 'test@example.com',
|
||||
password: 'pass123',
|
||||
})
|
||||
expect(result.access_token).toBe('at')
|
||||
expect(result.user.name).toBe('Test')
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_access_token', 'at')
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_refresh_token', 'rt')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.register()', () => {
|
||||
it('calls POST /auth/register with user data and stores tokens', async () => {
|
||||
const registerData = {
|
||||
email: 'new@example.com',
|
||||
password: 'secure123',
|
||||
name: 'New User',
|
||||
role: 'creator' as const,
|
||||
email_code: '123456',
|
||||
}
|
||||
const mockResponse = {
|
||||
data: {
|
||||
access_token: 'new-at',
|
||||
refresh_token: 'new-rt',
|
||||
token_type: 'bearer',
|
||||
expires_in: 900,
|
||||
user: { id: 'u2', name: 'New User', email: 'new@example.com', role: 'creator', is_verified: false },
|
||||
},
|
||||
}
|
||||
mockAxiosInstance.post.mockResolvedValueOnce(mockResponse)
|
||||
|
||||
const result = await api.register(registerData)
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/auth/register', registerData)
|
||||
expect(result.user.email).toBe('new@example.com')
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_access_token', 'new-at')
|
||||
expect(storage.setItem).toHaveBeenCalledWith('miaosi_refresh_token', 'new-rt')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.listTasks()', () => {
|
||||
it('calls GET /tasks with pagination params', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({
|
||||
data: { items: [], total: 0, page: 1, page_size: 20 },
|
||||
})
|
||||
|
||||
const result = await api.listTasks(1, 20)
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/tasks', {
|
||||
params: { page: 1, page_size: 20, stage: undefined },
|
||||
})
|
||||
expect(result.total).toBe(0)
|
||||
})
|
||||
|
||||
it('passes stage filter when provided', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({
|
||||
data: { items: [], total: 0, page: 1, page_size: 10 },
|
||||
})
|
||||
|
||||
await api.listTasks(1, 10, 'script_upload' as any)
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/tasks', {
|
||||
params: { page: 1, page_size: 10, stage: 'script_upload' },
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.getUploadPolicy()', () => {
|
||||
it('calls POST /upload/policy with file type', async () => {
|
||||
mockAxiosInstance.post.mockResolvedValueOnce({
|
||||
data: {
|
||||
x_tos_algorithm: 'algo',
|
||||
x_tos_credential: 'cred',
|
||||
x_tos_date: '2024-01-01',
|
||||
x_tos_signature: 'sig',
|
||||
policy: 'base64policy',
|
||||
host: 'https://oss.example.com',
|
||||
dir: 'uploads/',
|
||||
expire: 3600,
|
||||
max_size_mb: 100,
|
||||
},
|
||||
})
|
||||
|
||||
const result = await api.getUploadPolicy('video')
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/upload/policy', {
|
||||
file_type: 'video',
|
||||
})
|
||||
expect(result.host).toBe('https://oss.example.com')
|
||||
})
|
||||
|
||||
it('defaults to "general" file type when not specified', async () => {
|
||||
mockAxiosInstance.post.mockResolvedValueOnce({ data: {} })
|
||||
|
||||
await api.getUploadPolicy()
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/upload/policy', {
|
||||
file_type: 'general',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.getProfile()', () => {
|
||||
it('calls GET /profile', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({
|
||||
data: {
|
||||
id: 'u1',
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
role: 'brand',
|
||||
is_verified: true,
|
||||
},
|
||||
})
|
||||
|
||||
const result = await api.getProfile()
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/profile')
|
||||
expect(result.name).toBe('Test User')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.getMessages()', () => {
|
||||
it('calls GET /messages with params', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({
|
||||
data: { items: [], total: 0, page: 1, page_size: 20 },
|
||||
})
|
||||
|
||||
const params = { page: 1, page_size: 20, is_read: false }
|
||||
const result = await api.getMessages(params)
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/messages', { params })
|
||||
expect(result.total).toBe(0)
|
||||
})
|
||||
|
||||
it('calls GET /messages without params when none provided', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({
|
||||
data: { items: [], total: 0, page: 1, page_size: 20 },
|
||||
})
|
||||
|
||||
await api.getMessages()
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/messages', { params: undefined })
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.getUnreadCount()', () => {
|
||||
it('calls GET /messages/unread-count', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({ data: { count: 5 } })
|
||||
|
||||
const result = await api.getUnreadCount()
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/messages/unread-count')
|
||||
expect(result.count).toBe(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.logout()', () => {
|
||||
it('calls POST /auth/logout and clears tokens', async () => {
|
||||
storage.setItem('miaosi_access_token', 'at')
|
||||
storage.setItem('miaosi_refresh_token', 'rt')
|
||||
mockAxiosInstance.post.mockResolvedValueOnce({ data: undefined })
|
||||
|
||||
await api.logout()
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/auth/logout')
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_access_token')
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_refresh_token')
|
||||
})
|
||||
|
||||
it('clears tokens even when POST /auth/logout fails', async () => {
|
||||
mockAxiosInstance.post.mockRejectedValueOnce(new Error('Network error'))
|
||||
|
||||
await api.logout().catch(() => {})
|
||||
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_access_token')
|
||||
expect(storage.removeItem).toHaveBeenCalledWith('miaosi_refresh_token')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.sendEmailCode()', () => {
|
||||
it('calls POST /auth/send-code', async () => {
|
||||
mockAxiosInstance.post.mockResolvedValueOnce({
|
||||
data: { message: 'Code sent', expires_in: 300 },
|
||||
})
|
||||
|
||||
const result = await api.sendEmailCode({ email: 'a@b.com', purpose: 'login' })
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/auth/send-code', {
|
||||
email: 'a@b.com',
|
||||
purpose: 'login',
|
||||
})
|
||||
expect(result.expires_in).toBe(300)
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.resetPassword()', () => {
|
||||
it('calls POST /auth/reset-password', async () => {
|
||||
mockAxiosInstance.post.mockResolvedValueOnce({
|
||||
data: { message: 'Password reset' },
|
||||
})
|
||||
|
||||
const result = await api.resetPassword({
|
||||
email: 'a@b.com',
|
||||
email_code: '123456',
|
||||
new_password: 'newpass',
|
||||
})
|
||||
|
||||
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/auth/reset-password', {
|
||||
email: 'a@b.com',
|
||||
email_code: '123456',
|
||||
new_password: 'newpass',
|
||||
})
|
||||
expect(result.message).toBe('Password reset')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.updateProfile()', () => {
|
||||
it('calls PUT /profile with update data', async () => {
|
||||
const updateData = { name: 'Updated Name' }
|
||||
mockAxiosInstance.put.mockResolvedValueOnce({
|
||||
data: { id: 'u1', name: 'Updated Name', role: 'brand', is_verified: true },
|
||||
})
|
||||
|
||||
const result = await api.updateProfile(updateData)
|
||||
|
||||
expect(mockAxiosInstance.put).toHaveBeenCalledWith('/profile', updateData)
|
||||
expect(result.name).toBe('Updated Name')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.changePassword()', () => {
|
||||
it('calls PUT /profile/password', async () => {
|
||||
mockAxiosInstance.put.mockResolvedValueOnce({
|
||||
data: { message: 'Password changed' },
|
||||
})
|
||||
|
||||
const result = await api.changePassword({
|
||||
old_password: 'old',
|
||||
new_password: 'new',
|
||||
})
|
||||
|
||||
expect(mockAxiosInstance.put).toHaveBeenCalledWith('/profile/password', {
|
||||
old_password: 'old',
|
||||
new_password: 'new',
|
||||
})
|
||||
expect(result.message).toBe('Password changed')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.markMessageAsRead()', () => {
|
||||
it('calls PUT /messages/:id/read', async () => {
|
||||
mockAxiosInstance.put.mockResolvedValueOnce({ data: undefined })
|
||||
|
||||
await api.markMessageAsRead('msg-001')
|
||||
|
||||
expect(mockAxiosInstance.put).toHaveBeenCalledWith('/messages/msg-001/read')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.markAllMessagesAsRead()', () => {
|
||||
it('calls PUT /messages/read-all', async () => {
|
||||
mockAxiosInstance.put.mockResolvedValueOnce({ data: undefined })
|
||||
|
||||
await api.markAllMessagesAsRead()
|
||||
|
||||
expect(mockAxiosInstance.put).toHaveBeenCalledWith('/messages/read-all')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.setTenantId()', () => {
|
||||
it('updates the tenant ID used in subsequent requests', () => {
|
||||
api.setTenantId('BR002')
|
||||
const config = { headers: {} as Record<string, string> }
|
||||
const result = requestInterceptorHolder.fn!(config)
|
||||
expect(result.headers['X-Tenant-ID']).toBe('BR002')
|
||||
|
||||
// Reset for other tests
|
||||
api.setTenantId('default')
|
||||
})
|
||||
})
|
||||
|
||||
describe('api.healthCheck()', () => {
|
||||
it('calls GET /health', async () => {
|
||||
mockAxiosInstance.get.mockResolvedValueOnce({
|
||||
data: { status: 'ok', version: '1.0.0' },
|
||||
})
|
||||
|
||||
const result = await api.healthCheck()
|
||||
|
||||
expect(mockAxiosInstance.get).toHaveBeenCalledWith('/health')
|
||||
expect(result.status).toBe('ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
+141
-7
@@ -56,6 +56,11 @@ import type {
|
||||
PlatformListResponse,
|
||||
RuleValidateRequest,
|
||||
RuleValidateResponse,
|
||||
PlatformRuleParseRequest,
|
||||
PlatformRuleParseResponse,
|
||||
PlatformRuleConfirmRequest,
|
||||
BrandPlatformRuleResponse,
|
||||
BrandPlatformRuleListResponse,
|
||||
} from '@/types/rules'
|
||||
import type {
|
||||
AIConfigUpdate,
|
||||
@@ -136,10 +141,10 @@ export interface RefreshTokenResponse {
|
||||
}
|
||||
|
||||
export interface UploadPolicyResponse {
|
||||
q_sign_algorithm: string
|
||||
q_ak: string
|
||||
q_key_time: string
|
||||
q_signature: string
|
||||
x_tos_algorithm: string
|
||||
x_tos_credential: string
|
||||
x_tos_date: string
|
||||
x_tos_signature: string
|
||||
policy: string
|
||||
host: string
|
||||
dir: string
|
||||
@@ -426,7 +431,7 @@ class ApiClient {
|
||||
// ==================== 文件上传 ====================
|
||||
|
||||
/**
|
||||
* 获取 COS 上传凭证
|
||||
* 获取 TOS 上传凭证
|
||||
*/
|
||||
async getUploadPolicy(fileType: string = 'general'): Promise<UploadPolicyResponse> {
|
||||
const response = await this.client.post<UploadPolicyResponse>('/upload/policy', {
|
||||
@@ -448,6 +453,64 @@ class ApiClient {
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 后端代理上传(绕过浏览器直传 TOS 的 CORS/代理问题)
|
||||
*/
|
||||
async proxyUpload(file: File, fileType: string = 'general', onProgress?: (pct: number) => void): Promise<FileUploadedResponse> {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
formData.append('file_type', fileType)
|
||||
const response = await this.client.post<FileUploadedResponse>('/upload/proxy', formData, {
|
||||
timeout: 300000,
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
onUploadProgress: (e) => {
|
||||
if (e.total && onProgress) onProgress(Math.round((e.loaded / e.total) * 100))
|
||||
},
|
||||
})
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取私有桶文件的预签名访问 URL
|
||||
*/
|
||||
async getSignedUrl(url: string): Promise<string> {
|
||||
const response = await this.client.get<{ signed_url: string; expire_seconds: number }>(
|
||||
'/upload/sign-url',
|
||||
{ params: { url } }
|
||||
)
|
||||
return response.data.signed_url
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过后端代理获取文件预览 Blob URL(用于 iframe/img src)
|
||||
*/
|
||||
async getPreviewUrl(fileUrl: string): Promise<string> {
|
||||
const response = await this.client.get('/upload/preview', {
|
||||
params: { url: fileUrl },
|
||||
responseType: 'blob',
|
||||
})
|
||||
return URL.createObjectURL(response.data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过后端代理下载文件(避免 TOS CORS 问题)
|
||||
*/
|
||||
async downloadFile(fileUrl: string, filename: string): Promise<void> {
|
||||
const response = await this.client.get('/upload/download', {
|
||||
params: { url: fileUrl },
|
||||
responseType: 'blob',
|
||||
})
|
||||
const blob = new Blob([response.data])
|
||||
const blobUrl = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = blobUrl
|
||||
a.download = filename
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
URL.revokeObjectURL(blobUrl)
|
||||
}
|
||||
|
||||
// ==================== 视频审核 ====================
|
||||
|
||||
/**
|
||||
@@ -491,9 +554,9 @@ class ApiClient {
|
||||
/**
|
||||
* 查询任务列表
|
||||
*/
|
||||
async listTasks(page: number = 1, pageSize: number = 20, stage?: TaskStage): Promise<TaskListResponse> {
|
||||
async listTasks(page: number = 1, pageSize: number = 20, stage?: TaskStage, projectId?: string): Promise<TaskListResponse> {
|
||||
const response = await this.client.get<TaskListResponse>('/tasks', {
|
||||
params: { page, page_size: pageSize, stage },
|
||||
params: { page, page_size: pageSize, stage, project_id: projectId },
|
||||
})
|
||||
return response.data
|
||||
}
|
||||
@@ -644,6 +707,37 @@ class ApiClient {
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理商更新 Brief(agency_attachments + selling_points + blacklist_words)
|
||||
*/
|
||||
async updateBriefByAgency(projectId: string, data: {
|
||||
agency_attachments?: Array<{ id?: string; name: string; url: string; size?: string }>
|
||||
selling_points?: Array<{ content: string; required: boolean }>
|
||||
blacklist_words?: Array<{ word: string; reason: string }>
|
||||
brand_tone?: string
|
||||
other_requirements?: string
|
||||
min_selling_points?: number | null
|
||||
}): Promise<BriefResponse> {
|
||||
const response = await this.client.patch<BriefResponse>(`/projects/${projectId}/brief/agency-attachments`, data)
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* AI 解析 Brief 文档
|
||||
*/
|
||||
async parseBrief(projectId: string): Promise<{
|
||||
product_name: string
|
||||
target_audience: string
|
||||
content_requirements: string
|
||||
selling_points: Array<{ content: string; required: boolean }>
|
||||
blacklist_words: Array<{ word: string; reason: string }>
|
||||
}> {
|
||||
const response = await this.client.post(`/projects/${projectId}/brief/parse`, null, {
|
||||
timeout: 180000, // 3 分钟,文档下载 + AI 解析较慢
|
||||
})
|
||||
return response.data
|
||||
}
|
||||
|
||||
// ==================== 组织关系 ====================
|
||||
|
||||
/**
|
||||
@@ -808,6 +902,13 @@ class ApiClient {
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除白名单
|
||||
*/
|
||||
async deleteWhitelistItem(id: string): Promise<void> {
|
||||
await this.client.delete(`/rules/whitelist/${id}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询竞品列表
|
||||
*/
|
||||
@@ -857,6 +958,39 @@ class ApiClient {
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文档并 AI 解析平台规则
|
||||
*/
|
||||
async parsePlatformRule(data: PlatformRuleParseRequest): Promise<PlatformRuleParseResponse> {
|
||||
const response = await this.client.post<PlatformRuleParseResponse>('/rules/platform-rules/parse', data, {
|
||||
timeout: 180000, // 3 分钟,视觉模型解析图片 PDF 较慢
|
||||
})
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认/编辑平台规则
|
||||
*/
|
||||
async confirmPlatformRule(ruleId: string, data: PlatformRuleConfirmRequest): Promise<BrandPlatformRuleResponse> {
|
||||
const response = await this.client.put<BrandPlatformRuleResponse>(`/rules/platform-rules/${ruleId}/confirm`, data)
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询品牌方平台规则列表
|
||||
*/
|
||||
async listBrandPlatformRules(params?: { brand_id?: string; platform?: string; status?: string }): Promise<BrandPlatformRuleListResponse> {
|
||||
const response = await this.client.get<BrandPlatformRuleListResponse>('/rules/platform-rules', { params })
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除平台规则
|
||||
*/
|
||||
async deletePlatformRule(ruleId: string): Promise<void> {
|
||||
await this.client.delete(`/rules/platform-rules/${ruleId}`)
|
||||
}
|
||||
|
||||
// ==================== AI 配置 ====================
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,802 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mapTaskToUI } from './taskStageMapper'
|
||||
import type { TaskResponse, TaskStage, TaskStatus } from '@/types/task'
|
||||
|
||||
/**
|
||||
* Helper: create a minimal TaskResponse with sensible defaults.
|
||||
* Pass overrides for fields you care about.
|
||||
*/
|
||||
function mockTask(overrides: Partial<TaskResponse> = {}): TaskResponse {
|
||||
return {
|
||||
id: 'TK000001',
|
||||
name: '测试任务',
|
||||
sequence: 1,
|
||||
stage: 'script_upload' as TaskStage,
|
||||
project: { id: 'PJ000001', name: '测试项目' },
|
||||
agency: { id: 'AG000001', name: '测试代理商' },
|
||||
creator: { id: 'CR000001', name: '测试达人' },
|
||||
script_file_url: null,
|
||||
script_file_name: null,
|
||||
script_uploaded_at: null,
|
||||
script_ai_score: null,
|
||||
script_ai_result: null,
|
||||
script_agency_status: null,
|
||||
script_agency_comment: null,
|
||||
script_brand_status: null,
|
||||
script_brand_comment: null,
|
||||
video_file_url: null,
|
||||
video_file_name: null,
|
||||
video_duration: null,
|
||||
video_thumbnail_url: null,
|
||||
video_uploaded_at: null,
|
||||
video_ai_score: null,
|
||||
video_ai_result: null,
|
||||
video_agency_status: null,
|
||||
video_agency_comment: null,
|
||||
video_brand_status: null,
|
||||
video_brand_comment: null,
|
||||
appeal_count: 0,
|
||||
is_appeal: false,
|
||||
appeal_reason: null,
|
||||
created_at: '2026-01-01T00:00:00Z',
|
||||
updated_at: '2026-01-01T00:00:00Z',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 1. script_upload stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — script_upload', () => {
|
||||
const task = mockTask({ stage: 'script_upload' })
|
||||
const ui = mapTaskToUI(task)
|
||||
|
||||
it('scriptStage.submit should be "current"', () => {
|
||||
expect(ui.scriptStage.submit).toBe('current')
|
||||
})
|
||||
|
||||
it('remaining script steps should be "pending"', () => {
|
||||
expect(ui.scriptStage.ai).toBe('pending')
|
||||
expect(ui.scriptStage.agency).toBe('pending')
|
||||
expect(ui.scriptStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('all video steps should be "pending"', () => {
|
||||
expect(ui.videoStage.submit).toBe('pending')
|
||||
expect(ui.videoStage.ai).toBe('pending')
|
||||
expect(ui.videoStage.agency).toBe('pending')
|
||||
expect(ui.videoStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('currentPhase should be "script"', () => {
|
||||
expect(ui.currentPhase).toBe('script')
|
||||
})
|
||||
|
||||
it('buttonText should be "上传脚本"', () => {
|
||||
expect(ui.buttonText).toBe('上传脚本')
|
||||
})
|
||||
|
||||
it('buttonType should be "primary"', () => {
|
||||
expect(ui.buttonType).toBe('primary')
|
||||
})
|
||||
|
||||
it('statusLabel should be "待上传"', () => {
|
||||
expect(ui.statusLabel).toBe('待上传')
|
||||
})
|
||||
|
||||
it('filterCategory should be "pending"', () => {
|
||||
expect(ui.filterCategory).toBe('pending')
|
||||
})
|
||||
|
||||
it('scriptColor should be "blue" (no errors, brand not done)', () => {
|
||||
expect(ui.scriptColor).toBe('blue')
|
||||
})
|
||||
|
||||
it('videoColor should be "blue"', () => {
|
||||
expect(ui.videoColor).toBe('blue')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 2. script_ai_review stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — script_ai_review', () => {
|
||||
it('ai step should be "current" when no result yet', () => {
|
||||
const task = mockTask({ stage: 'script_ai_review', script_ai_result: null })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('current')
|
||||
expect(ui.scriptStage.agency).toBe('pending')
|
||||
expect(ui.scriptStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('ai step should be "done" when result is present', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_ai_review',
|
||||
script_ai_result: { score: 85, violations: [], soft_warnings: [] },
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
})
|
||||
|
||||
it('statusLabel should be "AI 审核中"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_ai_review' }))
|
||||
expect(ui.statusLabel).toBe('AI 审核中')
|
||||
})
|
||||
|
||||
it('buttonText should be "审核中" and buttonType "disabled"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_ai_review' }))
|
||||
expect(ui.buttonText).toBe('审核中')
|
||||
expect(ui.buttonType).toBe('disabled')
|
||||
})
|
||||
|
||||
it('filterCategory should be "reviewing"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_ai_review' }))
|
||||
expect(ui.filterCategory).toBe('reviewing')
|
||||
})
|
||||
|
||||
it('currentPhase should be "script"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_ai_review' }))
|
||||
expect(ui.currentPhase).toBe('script')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 3. script_agency_review stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — script_agency_review', () => {
|
||||
it('agency step defaults to "current" when status is null/pending', () => {
|
||||
const task = mockTask({ stage: 'script_agency_review' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
expect(ui.scriptStage.agency).toBe('current')
|
||||
expect(ui.scriptStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('agency step is "current" when status is "processing"', () => {
|
||||
const task = mockTask({ stage: 'script_agency_review', script_agency_status: 'processing' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.agency).toBe('current')
|
||||
})
|
||||
|
||||
it('agency step is "done" when status is "passed"', () => {
|
||||
const task = mockTask({ stage: 'script_agency_review', script_agency_status: 'passed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
})
|
||||
|
||||
it('agency step is "done" when status is "force_passed"', () => {
|
||||
const task = mockTask({ stage: 'script_agency_review', script_agency_status: 'force_passed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
})
|
||||
|
||||
it('statusLabel should be "代理商审核中"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_agency_review' }))
|
||||
expect(ui.statusLabel).toBe('代理商审核中')
|
||||
})
|
||||
|
||||
it('buttonText should be "审核中", buttonType "disabled"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_agency_review' }))
|
||||
expect(ui.buttonText).toBe('审核中')
|
||||
expect(ui.buttonType).toBe('disabled')
|
||||
})
|
||||
|
||||
it('filterCategory should be "reviewing"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_agency_review' }))
|
||||
expect(ui.filterCategory).toBe('reviewing')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 4. script_brand_review stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — script_brand_review', () => {
|
||||
it('brand step defaults to "current" when status is null/pending', () => {
|
||||
const task = mockTask({ stage: 'script_brand_review' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
expect(ui.scriptStage.brand).toBe('current')
|
||||
})
|
||||
|
||||
it('brand step is "current" when status is "processing"', () => {
|
||||
const task = mockTask({ stage: 'script_brand_review', script_brand_status: 'processing' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.brand).toBe('current')
|
||||
})
|
||||
|
||||
it('brand step is "done" when status is "passed"', () => {
|
||||
const task = mockTask({ stage: 'script_brand_review', script_brand_status: 'passed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('statusLabel should be "品牌方审核中"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_brand_review' }))
|
||||
expect(ui.statusLabel).toBe('品牌方审核中')
|
||||
})
|
||||
|
||||
it('buttonText should be "审核中", buttonType "disabled"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_brand_review' }))
|
||||
expect(ui.buttonText).toBe('审核中')
|
||||
expect(ui.buttonType).toBe('disabled')
|
||||
})
|
||||
|
||||
it('filterCategory should be "reviewing"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'script_brand_review' }))
|
||||
expect(ui.filterCategory).toBe('reviewing')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 5. video_upload stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — video_upload', () => {
|
||||
const task = mockTask({ stage: 'video_upload' })
|
||||
const ui = mapTaskToUI(task)
|
||||
|
||||
it('all script steps should be "done"', () => {
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
expect(ui.scriptStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('videoStage.submit should be "current"', () => {
|
||||
expect(ui.videoStage.submit).toBe('current')
|
||||
})
|
||||
|
||||
it('remaining video steps should be "pending"', () => {
|
||||
expect(ui.videoStage.ai).toBe('pending')
|
||||
expect(ui.videoStage.agency).toBe('pending')
|
||||
expect(ui.videoStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('currentPhase should be "video"', () => {
|
||||
expect(ui.currentPhase).toBe('video')
|
||||
})
|
||||
|
||||
it('buttonText should be "上传视频"', () => {
|
||||
expect(ui.buttonText).toBe('上传视频')
|
||||
})
|
||||
|
||||
it('buttonType should be "primary"', () => {
|
||||
expect(ui.buttonType).toBe('primary')
|
||||
})
|
||||
|
||||
it('statusLabel should be "待上传"', () => {
|
||||
expect(ui.statusLabel).toBe('待上传')
|
||||
})
|
||||
|
||||
it('filterCategory should be "pending"', () => {
|
||||
expect(ui.filterCategory).toBe('pending')
|
||||
})
|
||||
|
||||
it('scriptColor should be "green" (brand done)', () => {
|
||||
expect(ui.scriptColor).toBe('green')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 6. video_ai_review stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — video_ai_review', () => {
|
||||
it('video ai step should be "current" when no result', () => {
|
||||
const task = mockTask({ stage: 'video_ai_review', video_ai_result: null })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.submit).toBe('done')
|
||||
expect(ui.videoStage.ai).toBe('current')
|
||||
expect(ui.videoStage.agency).toBe('pending')
|
||||
expect(ui.videoStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('video ai step should be "done" when result is present', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_ai_review',
|
||||
video_ai_result: { score: 90, violations: [], soft_warnings: [] },
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.ai).toBe('done')
|
||||
})
|
||||
|
||||
it('currentPhase should be "video"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_ai_review' }))
|
||||
expect(ui.currentPhase).toBe('video')
|
||||
})
|
||||
|
||||
it('all script steps should be "done"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_ai_review' }))
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
expect(ui.scriptStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('statusLabel should be "AI 审核中"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_ai_review' }))
|
||||
expect(ui.statusLabel).toBe('AI 审核中')
|
||||
})
|
||||
|
||||
it('buttonText should be "审核中", buttonType "disabled"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_ai_review' }))
|
||||
expect(ui.buttonText).toBe('审核中')
|
||||
expect(ui.buttonType).toBe('disabled')
|
||||
})
|
||||
|
||||
it('filterCategory should be "reviewing"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_ai_review' }))
|
||||
expect(ui.filterCategory).toBe('reviewing')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 7. video_agency_review stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — video_agency_review', () => {
|
||||
it('video agency step defaults to "current" when status is null', () => {
|
||||
const task = mockTask({ stage: 'video_agency_review' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.submit).toBe('done')
|
||||
expect(ui.videoStage.ai).toBe('done')
|
||||
expect(ui.videoStage.agency).toBe('current')
|
||||
expect(ui.videoStage.brand).toBe('pending')
|
||||
})
|
||||
|
||||
it('video agency step is "current" when status is "processing"', () => {
|
||||
const task = mockTask({ stage: 'video_agency_review', video_agency_status: 'processing' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.agency).toBe('current')
|
||||
})
|
||||
|
||||
it('video agency step is "done" when status is "passed"', () => {
|
||||
const task = mockTask({ stage: 'video_agency_review', video_agency_status: 'passed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.agency).toBe('done')
|
||||
})
|
||||
|
||||
it('currentPhase should be "video"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_agency_review' }))
|
||||
expect(ui.currentPhase).toBe('video')
|
||||
})
|
||||
|
||||
it('statusLabel should be "代理商审核中"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_agency_review' }))
|
||||
expect(ui.statusLabel).toBe('代理商审核中')
|
||||
})
|
||||
|
||||
it('buttonText should be "审核中", buttonType "disabled"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_agency_review' }))
|
||||
expect(ui.buttonText).toBe('审核中')
|
||||
expect(ui.buttonType).toBe('disabled')
|
||||
})
|
||||
|
||||
it('filterCategory should be "reviewing"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_agency_review' }))
|
||||
expect(ui.filterCategory).toBe('reviewing')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 8. video_brand_review stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — video_brand_review', () => {
|
||||
it('video brand step defaults to "current" when status is null', () => {
|
||||
const task = mockTask({ stage: 'video_brand_review' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.submit).toBe('done')
|
||||
expect(ui.videoStage.ai).toBe('done')
|
||||
expect(ui.videoStage.agency).toBe('done')
|
||||
expect(ui.videoStage.brand).toBe('current')
|
||||
})
|
||||
|
||||
it('video brand step is "current" when status is "processing"', () => {
|
||||
const task = mockTask({ stage: 'video_brand_review', video_brand_status: 'processing' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.brand).toBe('current')
|
||||
})
|
||||
|
||||
it('video brand step is "done" when status is "passed"', () => {
|
||||
const task = mockTask({ stage: 'video_brand_review', video_brand_status: 'passed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('currentPhase should be "video"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_brand_review' }))
|
||||
expect(ui.currentPhase).toBe('video')
|
||||
})
|
||||
|
||||
it('statusLabel should be "品牌方审核中"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_brand_review' }))
|
||||
expect(ui.statusLabel).toBe('品牌方审核中')
|
||||
})
|
||||
|
||||
it('filterCategory should be "reviewing"', () => {
|
||||
const ui = mapTaskToUI(mockTask({ stage: 'video_brand_review' }))
|
||||
expect(ui.filterCategory).toBe('reviewing')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 9. completed stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — completed', () => {
|
||||
const task = mockTask({ stage: 'completed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
|
||||
it('all script steps should be "done"', () => {
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
expect(ui.scriptStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('all video steps should be "done"', () => {
|
||||
expect(ui.videoStage.submit).toBe('done')
|
||||
expect(ui.videoStage.ai).toBe('done')
|
||||
expect(ui.videoStage.agency).toBe('done')
|
||||
expect(ui.videoStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('currentPhase should be "completed"', () => {
|
||||
expect(ui.currentPhase).toBe('completed')
|
||||
})
|
||||
|
||||
it('buttonText should be "已完成"', () => {
|
||||
expect(ui.buttonText).toBe('已完成')
|
||||
})
|
||||
|
||||
it('buttonType should be "success"', () => {
|
||||
expect(ui.buttonType).toBe('success')
|
||||
})
|
||||
|
||||
it('statusLabel should be "已完成"', () => {
|
||||
expect(ui.statusLabel).toBe('已完成')
|
||||
})
|
||||
|
||||
it('filterCategory should be "completed"', () => {
|
||||
expect(ui.filterCategory).toBe('completed')
|
||||
})
|
||||
|
||||
it('scriptColor should be "green"', () => {
|
||||
expect(ui.scriptColor).toBe('green')
|
||||
})
|
||||
|
||||
it('videoColor should be "green"', () => {
|
||||
expect(ui.videoColor).toBe('green')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 10. rejected stage
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — rejected', () => {
|
||||
const task = mockTask({ stage: 'rejected' })
|
||||
const ui = mapTaskToUI(task)
|
||||
|
||||
it('buttonText should be "重新提交"', () => {
|
||||
expect(ui.buttonText).toBe('重新提交')
|
||||
})
|
||||
|
||||
it('buttonType should be "warning"', () => {
|
||||
expect(ui.buttonType).toBe('warning')
|
||||
})
|
||||
|
||||
it('statusLabel should be "已驳回"', () => {
|
||||
expect(ui.statusLabel).toBe('已驳回')
|
||||
})
|
||||
|
||||
it('filterCategory should be "rejected"', () => {
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
|
||||
it('all script steps should be "done" (stageIndex >= 4)', () => {
|
||||
expect(ui.scriptStage.submit).toBe('done')
|
||||
expect(ui.scriptStage.ai).toBe('done')
|
||||
expect(ui.scriptStage.agency).toBe('done')
|
||||
expect(ui.scriptStage.brand).toBe('done')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 11. Script agency rejected
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — script agency rejected', () => {
|
||||
it('scriptStage.agency should be "error" even during script_agency_review', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_agency_review',
|
||||
script_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.agency).toBe('error')
|
||||
})
|
||||
|
||||
it('scriptColor should be "red" when agency is error', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_agency_review',
|
||||
script_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptColor).toBe('red')
|
||||
})
|
||||
|
||||
it('filterCategory should be "rejected" when agency rejected', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_agency_review',
|
||||
script_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 12. Script brand rejected
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — script brand rejected', () => {
|
||||
it('scriptStage.brand should be "error"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_brand_review',
|
||||
script_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.brand).toBe('error')
|
||||
})
|
||||
|
||||
it('scriptColor should be "red" when brand is error', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_brand_review',
|
||||
script_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptColor).toBe('red')
|
||||
})
|
||||
|
||||
it('filterCategory should be "rejected"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_brand_review',
|
||||
script_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 13. Video agency rejected
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — video agency rejected', () => {
|
||||
it('videoStage.agency should be "error"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_agency_review',
|
||||
video_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.agency).toBe('error')
|
||||
})
|
||||
|
||||
it('videoColor should be "red" when video agency is error', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_agency_review',
|
||||
video_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoColor).toBe('red')
|
||||
})
|
||||
|
||||
it('filterCategory should be "rejected"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_agency_review',
|
||||
video_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 13b. Video brand rejected
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — video brand rejected', () => {
|
||||
it('videoStage.brand should be "error"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_brand_review',
|
||||
video_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.brand).toBe('error')
|
||||
})
|
||||
|
||||
it('videoColor should be "red"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_brand_review',
|
||||
video_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoColor).toBe('red')
|
||||
})
|
||||
|
||||
it('filterCategory should be "rejected"', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_brand_review',
|
||||
video_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 14. filterCategory across different stages
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — filterCategory for different stages', () => {
|
||||
const cases: Array<{ stage: TaskStage; expected: string; desc: string }> = [
|
||||
{ stage: 'script_upload', expected: 'pending', desc: 'script_upload => pending' },
|
||||
{ stage: 'video_upload', expected: 'pending', desc: 'video_upload => pending' },
|
||||
{ stage: 'script_ai_review', expected: 'reviewing', desc: 'script_ai_review => reviewing' },
|
||||
{ stage: 'script_agency_review', expected: 'reviewing', desc: 'script_agency_review => reviewing' },
|
||||
{ stage: 'script_brand_review', expected: 'reviewing', desc: 'script_brand_review => reviewing' },
|
||||
{ stage: 'video_ai_review', expected: 'reviewing', desc: 'video_ai_review => reviewing' },
|
||||
{ stage: 'video_agency_review', expected: 'reviewing', desc: 'video_agency_review => reviewing' },
|
||||
{ stage: 'video_brand_review', expected: 'reviewing', desc: 'video_brand_review => reviewing' },
|
||||
{ stage: 'completed', expected: 'completed', desc: 'completed => completed' },
|
||||
{ stage: 'rejected', expected: 'rejected', desc: 'rejected => rejected' },
|
||||
]
|
||||
|
||||
cases.forEach(({ stage, expected, desc }) => {
|
||||
it(desc, () => {
|
||||
const task = mockTask({ stage })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe(expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 15. Rejection override: any rejected status forces filterCategory to rejected
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — rejection status overrides filterCategory', () => {
|
||||
it('video_brand_review with script_agency_status=rejected => filterCategory=rejected', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_brand_review',
|
||||
script_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
|
||||
it('video_ai_review with video_agency_status=rejected => filterCategory=rejected', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_ai_review',
|
||||
video_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('rejected')
|
||||
})
|
||||
|
||||
it('completed stage ignores past rejections (filterCategory stays completed)', () => {
|
||||
// The source code: if (stage !== 'completed') filterCategory = 'rejected'
|
||||
const task = mockTask({
|
||||
stage: 'completed',
|
||||
script_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.filterCategory).toBe('completed')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 16. statusToStep — internal mapping via statusToStep through mapTaskToUI
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — statusToStep mapping via agency/brand statuses', () => {
|
||||
it('force_passed maps to done', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_brand_review',
|
||||
script_brand_status: 'force_passed',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.brand).toBe('done')
|
||||
})
|
||||
|
||||
it('processing maps to current', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_brand_review',
|
||||
video_brand_status: 'processing',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.brand).toBe('current')
|
||||
})
|
||||
|
||||
it('null status defaults to current (pending fallback to current)', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_agency_review',
|
||||
video_agency_status: null,
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.agency).toBe('current')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 17. Color logic
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — color logic', () => {
|
||||
it('scriptColor is "green" when script brand is done and no errors', () => {
|
||||
const task = mockTask({ stage: 'video_upload' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptColor).toBe('green')
|
||||
})
|
||||
|
||||
it('scriptColor is "blue" when script brand is not yet done and no errors', () => {
|
||||
const task = mockTask({ stage: 'script_ai_review' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptColor).toBe('blue')
|
||||
})
|
||||
|
||||
it('scriptColor is "red" when script agency has error', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_agency_review',
|
||||
script_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptColor).toBe('red')
|
||||
})
|
||||
|
||||
it('scriptColor is "red" when script brand has error', () => {
|
||||
const task = mockTask({
|
||||
stage: 'script_brand_review',
|
||||
script_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptColor).toBe('red')
|
||||
})
|
||||
|
||||
it('videoColor is "green" when completed', () => {
|
||||
const task = mockTask({ stage: 'completed' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoColor).toBe('green')
|
||||
})
|
||||
|
||||
it('videoColor is "blue" when video is in progress with no errors', () => {
|
||||
const task = mockTask({ stage: 'video_ai_review' })
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoColor).toBe('blue')
|
||||
})
|
||||
|
||||
it('videoColor is "red" when video brand has error', () => {
|
||||
const task = mockTask({
|
||||
stage: 'video_brand_review',
|
||||
video_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoColor).toBe('red')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 18. Edge case: rejected stage with various rejection sources
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('mapTaskToUI — rejected stage with rejection source details', () => {
|
||||
it('rejected stage with script_brand_status=rejected shows error on script brand', () => {
|
||||
const task = mockTask({
|
||||
stage: 'rejected',
|
||||
script_brand_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.scriptStage.brand).toBe('error')
|
||||
expect(ui.scriptColor).toBe('red')
|
||||
})
|
||||
|
||||
it('rejected stage with video_agency_status=rejected shows error on video agency', () => {
|
||||
const task = mockTask({
|
||||
stage: 'rejected',
|
||||
video_agency_status: 'rejected',
|
||||
})
|
||||
const ui = mapTaskToUI(task)
|
||||
expect(ui.videoStage.agency).toBe('error')
|
||||
expect(ui.videoColor).toBe('red')
|
||||
})
|
||||
})
|
||||
@@ -12,7 +12,8 @@ export interface BriefAttachment {
|
||||
|
||||
export interface SellingPoint {
|
||||
content: string
|
||||
required: boolean
|
||||
priority?: 'core' | 'recommended' | 'reference'
|
||||
required?: boolean // 向后兼容旧格式
|
||||
}
|
||||
|
||||
export interface BlacklistWord {
|
||||
@@ -27,6 +28,7 @@ export interface BriefResponse {
|
||||
file_url?: string | null
|
||||
file_name?: string | null
|
||||
selling_points?: SellingPoint[] | null
|
||||
min_selling_points?: number | null
|
||||
blacklist_words?: BlacklistWord[] | null
|
||||
competitors?: string[] | null
|
||||
brand_tone?: string | null
|
||||
@@ -34,6 +36,7 @@ export interface BriefResponse {
|
||||
max_duration?: number | null
|
||||
other_requirements?: string | null
|
||||
attachments?: BriefAttachment[] | null
|
||||
agency_attachments?: BriefAttachment[] | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
@@ -49,4 +52,5 @@ export interface BriefCreateRequest {
|
||||
max_duration?: number
|
||||
other_requirements?: string
|
||||
attachments?: BriefAttachment[]
|
||||
agency_attachments?: BriefAttachment[]
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ export interface ProjectResponse {
|
||||
id: string
|
||||
name: string
|
||||
description?: string | null
|
||||
platform?: string | null
|
||||
brand_id: string
|
||||
brand_name?: string | null
|
||||
status: string
|
||||
@@ -34,6 +35,7 @@ export interface ProjectListResponse {
|
||||
export interface ProjectCreateRequest {
|
||||
name: string
|
||||
description?: string
|
||||
platform?: string
|
||||
start_date?: string
|
||||
deadline?: string
|
||||
agency_ids?: string[]
|
||||
@@ -42,6 +44,7 @@ export interface ProjectCreateRequest {
|
||||
export interface ProjectUpdateRequest {
|
||||
name?: string
|
||||
description?: string
|
||||
platform?: string
|
||||
start_date?: string
|
||||
deadline?: string
|
||||
status?: 'active' | 'completed' | 'archived'
|
||||
|
||||
@@ -79,6 +79,54 @@ export interface PlatformListResponse {
|
||||
total: number
|
||||
}
|
||||
|
||||
// ===== 品牌方平台规则(文档上传 + AI 解析) =====
|
||||
|
||||
export interface ParsedRulesData {
|
||||
forbidden_words: string[]
|
||||
restricted_words: { word: string; condition: string; suggestion: string }[]
|
||||
duration: { min_seconds?: number; max_seconds?: number } | null
|
||||
content_requirements: string[]
|
||||
other_rules: { rule: string; description: string }[]
|
||||
}
|
||||
|
||||
export interface PlatformRuleParseRequest {
|
||||
document_url: string
|
||||
document_name: string
|
||||
platform: string
|
||||
brand_id: string
|
||||
}
|
||||
|
||||
export interface PlatformRuleParseResponse {
|
||||
id: string
|
||||
platform: string
|
||||
brand_id: string
|
||||
document_url: string
|
||||
document_name: string
|
||||
parsed_rules: ParsedRulesData
|
||||
status: string
|
||||
}
|
||||
|
||||
export interface PlatformRuleConfirmRequest {
|
||||
parsed_rules: ParsedRulesData
|
||||
}
|
||||
|
||||
export interface BrandPlatformRuleResponse {
|
||||
id: string
|
||||
platform: string
|
||||
brand_id: string
|
||||
document_url: string
|
||||
document_name: string
|
||||
parsed_rules: ParsedRulesData
|
||||
status: string // draft / active / inactive
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface BrandPlatformRuleListResponse {
|
||||
items: BrandPlatformRuleResponse[]
|
||||
total: number
|
||||
}
|
||||
|
||||
// ===== 规则冲突检测 =====
|
||||
|
||||
export interface RuleValidateRequest {
|
||||
|
||||
+44
-1
@@ -29,6 +29,7 @@ export interface ProjectInfo {
|
||||
id: string
|
||||
name: string
|
||||
brand_name?: string | null
|
||||
platform?: string | null
|
||||
}
|
||||
|
||||
export interface AgencyInfo {
|
||||
@@ -42,14 +43,57 @@ export interface CreatorInfo {
|
||||
avatar?: string | null
|
||||
}
|
||||
|
||||
// 审核维度评分
|
||||
export interface DimensionScore {
|
||||
score: number
|
||||
passed: boolean
|
||||
issue_count: number
|
||||
}
|
||||
|
||||
// 四维度审核结果
|
||||
export interface ReviewDimensions {
|
||||
legal: DimensionScore
|
||||
platform: DimensionScore
|
||||
brand_safety: DimensionScore
|
||||
brief_match: DimensionScore
|
||||
}
|
||||
|
||||
// 卖点匹配结果
|
||||
export interface SellingPointMatchResult {
|
||||
content: string
|
||||
priority: 'core' | 'recommended' | 'reference'
|
||||
matched: boolean
|
||||
evidence?: string
|
||||
}
|
||||
|
||||
// Brief 匹配度评分详情
|
||||
export interface BriefMatchDetail {
|
||||
total_points: number
|
||||
matched_points: number
|
||||
required_points: number
|
||||
coverage_score: number
|
||||
overall_score: number
|
||||
highlights: string[]
|
||||
issues: string[]
|
||||
explanation: string
|
||||
}
|
||||
|
||||
// AI 审核结果
|
||||
export interface AIReviewResult {
|
||||
score: number
|
||||
summary?: string
|
||||
dimensions?: ReviewDimensions
|
||||
selling_point_matches?: SellingPointMatchResult[]
|
||||
brief_match_detail?: BriefMatchDetail
|
||||
ai_auto_rejected?: boolean
|
||||
ai_reject_reason?: string
|
||||
ai_available?: boolean
|
||||
violations: Array<{
|
||||
type: string
|
||||
content: string
|
||||
severity: string
|
||||
suggestion: string
|
||||
dimension?: string
|
||||
timestamp?: number
|
||||
source?: string
|
||||
}>
|
||||
@@ -58,7 +102,6 @@ export interface AIReviewResult {
|
||||
content: string
|
||||
suggestion: string
|
||||
}>
|
||||
summary?: string
|
||||
}
|
||||
|
||||
// 任务响应(对应后端 TaskResponse)
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
"x": -271,
|
||||
"y": -494,
|
||||
"name": "达人端桌面 - 任务列表",
|
||||
"enabled": false,
|
||||
"clip": true,
|
||||
"width": 1440,
|
||||
"height": 4300,
|
||||
@@ -9615,7 +9614,6 @@
|
||||
"x": 3080,
|
||||
"y": 5772,
|
||||
"name": "达人端桌面 - 视频阶段/上传视频",
|
||||
"enabled": false,
|
||||
"clip": true,
|
||||
"width": 1440,
|
||||
"height": 900,
|
||||
@@ -10447,7 +10445,6 @@
|
||||
"x": -1477,
|
||||
"y": 4300,
|
||||
"name": "达人端桌面 - 消息中心",
|
||||
"enabled": false,
|
||||
"width": 1440,
|
||||
"height": 2400,
|
||||
"fill": "$--bg-page",
|
||||
@@ -14314,7 +14311,6 @@
|
||||
"x": 0,
|
||||
"y": 5400,
|
||||
"name": "达人端桌面 - 个人中心",
|
||||
"enabled": false,
|
||||
"clip": true,
|
||||
"width": 1440,
|
||||
"height": 900,
|
||||
@@ -28098,7 +28094,6 @@
|
||||
"x": 0,
|
||||
"y": 13100,
|
||||
"name": "代理商端 - 达人管理",
|
||||
"enabled": false,
|
||||
"clip": true,
|
||||
"width": 1440,
|
||||
"height": 900,
|
||||
|
||||
Reference in New Issue
Block a user