feat(core): 完成 Phase 2 核心功能开发

- 实现查询API (query.py): 支持star_id/unique_id/nickname三种查询方式
- 实现计算模块 (calculator.py): CPM/自然搜索UV/搜索成本计算
- 实现品牌API集成 (brand_api.py): 批量并发调用,10并发限制
- 实现导出服务 (export_service.py): Excel/CSV导出
- 前端组件: QueryForm/ResultTable/ExportButton
- 主页面集成: 支持6种页面状态
- 测试: 44个测试全部通过,覆盖率88%

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
zfc
2026-01-28 14:38:38 +08:00
co-authored by Claude Opus 4.5
parent ac0f086821
commit 8fbcb72a3f
21 changed files with 1677 additions and 100 deletions
+105 -95
View File
@@ -1,101 +1,111 @@
import Image from "next/image";
'use client';
import { useState } from 'react';
import { QueryForm, ResultTable, ExportButton } from '@/components';
import { QueryType, VideoData, PageState } from '@/types';
import { queryVideos } from '@/lib/api';
export default function Home() {
return (
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
<main className="flex flex-col gap-8 row-start-2 items-center sm:items-start">
<Image
className="dark:invert"
src="https://nextjs.org/icons/next.svg"
alt="Next.js logo"
width={180}
height={38}
priority
/>
<ol className="list-inside list-decimal text-sm text-center sm:text-left font-[family-name:var(--font-geist-mono)]">
<li className="mb-2">
Get started by editing{" "}
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold">
src/app/page.tsx
</code>
.
</li>
<li>Save and see your changes instantly.</li>
</ol>
const [pageState, setPageState] = useState<PageState>('default');
const [data, setData] = useState<VideoData[]>([]);
const [total, setTotal] = useState(0);
const [error, setError] = useState<string | null>(null);
<div className="flex gap-4 items-center flex-col sm:flex-row">
<a
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className="dark:invert"
src="https://nextjs.org/icons/vercel.svg"
alt="Vercel logomark"
width={20}
height={20}
/>
Deploy now
</a>
<a
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:min-w-44"
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Read our docs
</a>
</div>
</main>
<footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center">
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="https://nextjs.org/icons/file.svg"
alt="File icon"
width={16}
height={16}
/>
Learn
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="https://nextjs.org/icons/window.svg"
alt="Window icon"
width={16}
height={16}
/>
Examples
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="https://nextjs.org/icons/globe.svg"
alt="Globe icon"
width={16}
height={16}
/>
Go to nextjs.org
</a>
</footer>
const handleQuery = async (type: QueryType, values: string[]) => {
setPageState('loading');
setError(null);
try {
const response = await queryVideos({ type, values });
if (response.success) {
setData(response.data);
setTotal(response.total);
setPageState(response.total > 0 ? 'result' : 'empty');
} else {
setError(response.error || '查询失败');
setPageState('error');
}
} catch (err) {
console.error('Query error:', err);
setError(err instanceof Error ? err.message : '网络错误,请检查后端服务是否正常');
setPageState('error');
}
};
const handleRetry = () => {
setPageState('default');
setError(null);
setData([]);
setTotal(0);
};
return (
<div className="max-w-7xl mx-auto px-4 py-8">
{/* 查询区域 */}
<section className="mb-8">
<QueryForm onSubmit={handleQuery} isLoading={pageState === 'loading'} />
</section>
{/* 结果区域 */}
<section>
{/* 默认态 */}
{pageState === 'default' && (
<div className="bg-white rounded-lg shadow-sm p-12 text-center">
<div className="text-gray-400 text-6xl mb-4">🔍</div>
<p className="text-gray-500"></p>
</div>
)}
{/* 加载态 */}
{pageState === 'loading' && (
<div className="bg-white rounded-lg shadow-sm p-12 text-center">
<div className="animate-spin text-primary text-4xl mb-4"></div>
<p className="text-gray-500">...</p>
</div>
)}
{/* 结果态 */}
{pageState === 'result' && (
<div>
<div className="flex justify-between items-center mb-4">
<h2 className="text-lg font-medium text-gray-900"></h2>
<ExportButton hasData={total > 0} />
</div>
<ResultTable data={data} total={total} />
</div>
)}
{/* 空结果态 */}
{pageState === 'empty' && (
<div className="bg-white rounded-lg shadow-sm p-12 text-center">
<div className="text-gray-400 text-6xl mb-4">📦</div>
<p className="text-gray-700 mb-2"></p>
<p className="text-gray-500 text-sm mb-4"></p>
<button
onClick={handleRetry}
className="px-4 py-2 text-sm font-medium text-primary border border-primary rounded hover:bg-primary hover:text-white"
>
</button>
</div>
)}
{/* 错误态 */}
{pageState === 'error' && (
<div className="bg-white rounded-lg shadow-sm p-12 text-center">
<div className="text-error text-6xl mb-4"></div>
<p className="text-gray-700 mb-2"></p>
<p className="text-gray-500 text-sm mb-4">{error || '可能原因:网络异常或数据库连接失败'}</p>
<button
onClick={handleRetry}
className="px-4 py-2 text-sm font-medium text-white bg-primary rounded hover:bg-primary-dark"
>
</button>
</div>
)}
</section>
</div>
);
}