diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a5918..89a2df9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,28 @@ --- +## 2026-11-16 · v3.6.7 + +### ✨ 新功能 + +- **AI 优化建议:点击即显示预计耗时**(按商品数量估算) + - 新增轻量预检接口 `POST /ai-optimize/preview`(校验口径与正式分析一致:模型 KEY → 商品同步状态), + 返回商品总数、送模型件数与预计秒区间 + - 点击按钮立即弹出提示:**「共 N 个商品,预计约 X~Y 秒」**(商品超送模型上限时注明"分析前 M 个") + - 分析期间按钮变两行进度:**「分析中 Xs」+「N 件 · 预计约 X~Ys」**(每秒刷新已耗时),悬停 tooltip 同步显示 + - 同步商品后再分析的场景也会在同步完成后补一次预检展示预计时间 + - 估算基准:实测 12 件约 28~55 秒,按商品数线性外推、宁宽勿窄,120s 在模型单次超时线内封顶; + 预检失败自动降级(不显示预计,分析照常) + +### ✅ 验证 + +- 端到端实测(14 个商品的真实账号):点击 → toast「共 14 个商品,预计约 29~74 秒」 + → 按钮「分析中 2s / 14 件 · 预计约 29~74s」→ 分析完成弹出结果报告(14 件、模型 big-pickle、逐件评分) +- 路由无 token 返回 401(已注册);`npm run build` 通过 +- 测试账号与临时属主变更已还原(用户表仅剩原有 admin/jinghui,账号属主回 admin) + +--- + ## 2026-11-16 · v3.6.6 ### ✏️ 调整 diff --git a/app/routers/ai_optimize.py b/app/routers/ai_optimize.py index 6aec9b3..26b89bc 100644 --- a/app/routers/ai_optimize.py +++ b/app/routers/ai_optimize.py @@ -105,6 +105,46 @@ def create_ai_optimize_router( raise HTTPException(status_code=403, detail="无权限操作该账号") return details + @router.post("/ai-optimize/preview") + def ai_optimize_preview( + req: AiOptimizeRequest, + current_user: dict[str, Any] = Depends(get_current_user), + ): + """轻量预检:返回该账号商品数与预计分析耗时,供前端点击后立刻展示给用户。 + + 校验顺序与 suggest 一致(模型 KEY → 商品同步状态),保证提示口径统一。 + """ + require_account(req.cookie_id, current_user) + + settings = db_manager.get_ai_reply_settings(req.cookie_id) + if not (settings.get("api_key") or "").strip(): + return { + "status": "need_api_key", + "message": "尚未配置模型 KEY,请先在 AI 设置中填写模型 KEY 再执行优化建议。", + } + + rows = db_manager.get_items_by_cookie(req.cookie_id) or [] + if not rows: + return { + "status": "need_sync", + "message": "该账号还没有同步商品,需要先同步商品才能执行优化分析。", + } + + total = len(rows) + analyzed = min(total, MAX_ITEMS) + # 实测基准:12 件约 28~55 秒(一次批量模型调用,单次 130s 超时)。 + # 输入商品越多 prompt 越长,线性外推;区间宁宽勿窄,120s 在超时线内封顶。 + est_min = int(min(8 + 1.5 * analyzed, 60)) + est_max = int(min(25 + 3.5 * analyzed, 120)) + return { + "status": "ok", + "total": total, + "analyzed_count": analyzed, + "truncated": total > MAX_ITEMS, + "est_min_seconds": est_min, + "est_max_seconds": est_max, + } + @router.post("/ai-optimize/suggest") def ai_optimize_suggest( req: AiOptimizeRequest, diff --git a/frontend/components/AccountList.tsx b/frontend/components/AccountList.tsx index f8d075f..4c75a22 100644 --- a/frontend/components/AccountList.tsx +++ b/frontend/components/AccountList.tsx @@ -20,6 +20,7 @@ import { startManualCaptchaSession, requestFreshCaptchaUrl, getAiOptimizeSuggest, + getAiOptimizePreview, syncItemsFromAccount, } from '../services/api'; import { confirmAction, notify } from '../services/feedback'; @@ -70,6 +71,19 @@ const AccountList: React.FC = () => { // AI 一键优化建议:哪个账号在跑分析 + 分析结果弹窗 const [aiOptimizingId, setAiOptimizingId] = useState(null); const [aiResult, setAiResult] = useState<{ account: AccountDetail; data: AiOptimizeResult } | null>(null); + // 分析中的预计耗时展示:商品数 + 预计秒区间 + 已耗时秒数 + const [aiProgress, setAiProgress] = useState<{ total: number; analyzed: number; estMin: number; estMax: number; truncated: boolean; startedAt: number } | null>(null); + const [aiElapsed, setAiElapsed] = useState(0); + + // 分析期间每秒刷新已耗时 + useEffect(() => { + if (!aiProgress) return; + setAiElapsed(0); + const timer = setInterval(() => { + setAiElapsed(Math.floor((Date.now() - aiProgress.startedAt) / 1000)); + }, 500); + return () => clearInterval(timer); + }, [aiProgress]); // 编辑表单状态 const [editForm, setEditForm] = useState({ @@ -404,7 +418,34 @@ const AccountList: React.FC = () => { if (aiOptimizingId) return; setAiOptimizingId(account.id); const errMsg = (e: any) => e?.response?.data?.detail || e?.message || 'AI 优化建议请求失败'; + + // 轻量预检:先拿商品数与预计耗时展示给用户(失败不阻断正式分析) + const applyPreview = async (id: string) => { + try { + const pv = await getAiOptimizePreview(id); + if (pv.status === 'ok' && pv.est_min_seconds != null && pv.est_max_seconds != null) { + setAiProgress({ + total: pv.total ?? 0, + analyzed: pv.analyzed_count ?? 0, + estMin: pv.est_min_seconds, + estMax: pv.est_max_seconds, + truncated: !!pv.truncated, + startedAt: Date.now(), + }); + notify( + pv.truncated + ? `共 ${pv.total} 个商品(分析前 ${pv.analyzed_count} 个),预计约 ${pv.est_min_seconds}~${pv.est_max_seconds} 秒` + : `共 ${pv.total} 个商品,预计约 ${pv.est_min_seconds}~${pv.est_max_seconds} 秒`, + 'info', + ); + } + } catch { + // 预检失败静默降级:不显示预计时间,分析照常进行 + } + }; + try { + await applyPreview(account.id); let res = await getAiOptimizeSuggest(account.id); if (res.status === 'need_api_key') { @@ -424,6 +465,7 @@ const AccountList: React.FC = () => { try { const syncRes = await syncItemsFromAccount(account.id); notify(syncRes?.message || '商品同步完成,开始执行优化分析', 'success'); + await applyPreview(account.id); } catch (syncErr) { notify(errMsg(syncErr), 'error'); return; @@ -448,6 +490,8 @@ const AccountList: React.FC = () => { notify(errMsg(e), 'error'); } finally { setAiOptimizingId(null); + setAiProgress(null); + setAiElapsed(0); } }; @@ -796,13 +840,27 @@ const AccountList: React.FC = () => { onClick={() => handleAiOptimize(account)} disabled={aiOptimizingId !== null} className="flex items-center gap-1.5 rounded-md bg-violet-50 px-2.5 py-2 text-xs font-bold text-violet-700 hover:bg-violet-100 disabled:cursor-not-allowed disabled:opacity-40" - title="AI 一键优化建议:检测该账号哪些商品需要优化(需已配置模型 KEY)" + title={aiOptimizingId === account.id && aiProgress ? `正在分析 ${aiProgress.total} 个商品,预计约 ${aiProgress.estMin}~${aiProgress.estMax} 秒` : "AI 一键优化建议:检测该账号哪些商品需要优化(需已配置模型 KEY)"} aria-label="AI一键优化建议" > - {aiOptimizingId === account.id - ? - : } - AI优化建议 + {aiOptimizingId === account.id ? ( + <> + + + 分析中 {aiElapsed}s + {aiProgress && ( + + {aiProgress.total} 件 · 预计约 {aiProgress.estMin}~{aiProgress.estMax}s + + )} + + + ) : ( + <> + + AI优化建议 + + )}