功能迭代

This commit is contained in:
张锐 committed 2026-12-02 16:10:54 +08:00
1 parent 5fc7d4564c
commit 84184a2dd1
41 files changed
+1054 -8

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
import { useEffect, useMemo, useState } from "react";
import { BarChart3, ExternalLink, Link2, SearchCheck, Unlink } from "lucide-react";
import { apiPost } from "./api";
import { getCurrentSiteCode } from "./siteContext";
type Connection = {
oauthConfigured: boolean;
connected: boolean;
accountEmail?: string;
propertyUri?: string;
status: string;
statusLabel: string;
operations: string[];
updateTime?: number;
};
type Metric = { clicks: number; impressions: number; ctr: number; position: number };
type Trend = Metric & { date: string };
type Keyword = Metric & { keyword: string };
type Page = Metric & { page: string };
type Statistics = {
submittedUrls: number;
indexedUrls: number;
keywordCount: number;
metric: Metric;
trends: Trend[];
keywords: Keyword[];
pages: Page[];
};
/** 格式化日期为接口使用的ISO日期。 */
function isoDate(date: Date): string { return date.toISOString().slice(0, 10); }
/** 格式化整数指标。 */
function integer(value: number): string { return new Intl.NumberFormat("zh-CN", { maximumFractionDigits: 0 }).format(value || 0); }
/** Google Search Console授权与效果统计页面。 */
export default function GscOperationsPage() {
const siteCode = getCurrentSiteCode();
const defaultEnd = useMemo(() => new Date(), []);
const defaultStart = useMemo(() => new Date(defaultEnd.getTime() - 27 * 86400000), [defaultEnd]);
const [connection, setConnection] = useState<Connection | null>(null);
const [statistics, setStatistics] = useState<Statistics | null>(null);
const [startDate, setStartDate] = useState(isoDate(defaultStart));
const [endDate, setEndDate] = useState(isoDate(defaultEnd));
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
/** 加载授权状态,并在已授权时加载统计。 */
const load = async () => {
try {
setLoading(true); setError("");
const value = await apiPost<Connection>("/gsc/connection", { siteCode });
setConnection(value);
if (value.connected) await loadStatistics(); else setStatistics(null);
} catch (reason) { setError(reason instanceof Error ? reason.message : "加载失败"); }
finally { setLoading(false); }
};
/** 查询所选日期范围的GSC统计。 */
const loadStatistics = async () => {
const value = await apiPost<Statistics>("/gsc/statistics", { siteCode, startDate, endDate });
setStatistics(value);
};
/** 跳转Google OAuth授权页面。 */
const authorize = async () => {
try {
setError("");
const result = await apiPost<{ authorizationUrl: string }>("/gsc/authorization", {
siteCode,
returnUrl: `${window.location.origin}/?section=gsc`,
});
window.location.assign(result.authorizationUrl);
} catch (reason) { setError(reason instanceof Error ? reason.message : "授权失败"); }
};
/** 解除当前站点GSC授权。 */
const disconnect = async () => {
if (!window.confirm("确认解除当前域名的 Google Search Console 授权吗?")) return;
try { await apiPost("/gsc/disconnect", { siteCode }); await load(); }
catch (reason) { setError(reason instanceof Error ? reason.message : "解绑失败"); }
};
useEffect(() => { void load(); }, [siteCode]);
return <div className="gsc-page">
<header className="gsc-title"><div><span>运营</span><h1>Google Search Console</h1><p>直接绑定店铺域名,查看收录、关键词和自然搜索效果。</p></div><SearchCheck size={36}/></header>
{error && <div className="gsc-error">{error}</div>}
<section className="gsc-panel gsc-connection">
<div><h2>域名授权</h2><p>{connection?.connected ? `${connection.accountEmail} · ${connection.propertyUri}` : "授权 Google 账户后自动匹配当前店铺域名对应的站点资源。"}</p></div>
{connection?.connected ? <div className="gsc-actions"><a href="https://search.google.com/search-console" target="_blank" rel="noreferrer"><ExternalLink size={16}/>打开 GSC</a><button onClick={disconnect}><Unlink size={16}/>解除授权</button></div>
: <button className="primary" disabled={loading || connection?.oauthConfigured === false} onClick={authorize}><Link2 size={16}/>授权当前域名</button>}
</section>
{connection?.oauthConfigured === false && <div className="gsc-notice">尚未配置 Google OAuth。请设置 SHOPLIX_GSC_CLIENT_ID、SHOPLIX_GSC_CLIENT_SECRET 和线上回调地址后重启后台服务。</div>}
<section className="gsc-panel gsc-toolbar"><div><h2>搜索效果</h2><p>Search Console 数据通常会延迟约 2–3 天。</p></div><div className="gsc-date"><input type="date" value={startDate} onChange={event => setStartDate(event.target.value)}/><span>至</span><input type="date" value={endDate} onChange={event => setEndDate(event.target.value)}/><button disabled={!connection?.connected || loading} onClick={() => { setLoading(true); loadStatistics().catch(reason => setError(reason.message)).finally(() => setLoading(false)); }}>查询</button></div></section>
<div className="gsc-metrics">
<article><span>已索引 URL</span><strong>{integer(statistics?.indexedUrls || 0)}</strong><small>站点地图提交 {integer(statistics?.submittedUrls || 0)}</small></article>
<article><span>关键词数</span><strong>{integer(statistics?.keywordCount || 0)}</strong><small>统计期内产生曝光</small></article>
<article><span>总点击次数</span><strong>{integer(statistics?.metric.clicks || 0)}</strong><small>自然搜索点击</small></article>
<article><span>总曝光次数</span><strong>{integer(statistics?.metric.impressions || 0)}</strong><small>Google 搜索曝光</small></article>
<article><span>平均点击率</span><strong>{((statistics?.metric.ctr || 0) * 100).toFixed(2)}%</strong><small>Clicks / Impressions</small></article>
<article><span>平均排名</span><strong>{(statistics?.metric.position || 0).toFixed(1)}</strong><small>数值越小排名越高</small></article>
</div>
<section className="gsc-panel"><div className="gsc-section-head"><div><h2>效果趋势</h2><p>每日点击与曝光变化</p></div><BarChart3 size={22}/></div><div className="gsc-trend">{statistics?.trends.length ? statistics.trends.map(item => { const max = Math.max(...statistics.trends.map(row => row.impressions), 1); return <div className="gsc-trend-item" key={item.date} title={`${item.date} · 曝光 ${integer(item.impressions)} · 点击 ${integer(item.clicks)}`}><i style={{height:`${Math.max(4, item.impressions / max * 100)}%`}}/><span>{item.date.slice(5)}</span></div>; }) : <div className="gsc-empty">{connection?.connected ? "所选日期暂无趋势数据" : "授权后展示搜索趋势"}</div>}</div></section>
<div className="gsc-tables">
<section className="gsc-panel"><h2>关键词效果</h2><div className="gsc-table-wrap"><table><thead><tr><th>关键词</th><th>点击</th><th>曝光</th><th>CTR</th><th>排名</th></tr></thead><tbody>{statistics?.keywords.slice(0, 100).map(item => <tr key={item.keyword}><td>{item.keyword}</td><td>{integer(item.clicks)}</td><td>{integer(item.impressions)}</td><td>{(item.ctr*100).toFixed(2)}%</td><td>{item.position.toFixed(1)}</td></tr>)}</tbody></table></div></section>
<section className="gsc-panel"><h2>页面收录效果</h2><div className="gsc-table-wrap"><table><thead><tr><th>页面</th><th>点击</th><th>曝光</th><th>CTR</th><th>排名</th></tr></thead><tbody>{statistics?.pages.slice(0, 100).map(item => <tr key={item.page}><td className="gsc-url" title={item.page}>{item.page}</td><td>{integer(item.clicks)}</td><td>{integer(item.impressions)}</td><td>{(item.ctr*100).toFixed(2)}%</td><td>{item.position.toFixed(1)}</td></tr>)}</tbody></table></div></section>
</div>
</div>;
}
+19 -5
View File
@@ -9,6 +9,7 @@ import {
FolderTree,
Globe2,
LayoutDashboard,
LineChart,
HelpCircle,
Image,
Mail,
@@ -44,6 +45,7 @@ import AgreementSettingsPage from "./AgreementSettingsPage";
import AboutUsSettingsPage from "./AboutUsSettingsPage";
import DashboardPage from "./DashboardPage";
import CrmCustomerPage from "./CrmCustomerPage";
import GscOperationsPage from "./GscOperationsPage";
import { apiPost, assetUrl } from "./api";
import { statusLabel } from "./statusLabels";
import {
@@ -82,7 +84,8 @@ type Section =
| "settings-store"
| "settings-account"
| "settings-social"
| "crm-customers";
| "crm-customers"
| "gsc";
type EditorialSection = "NEWS" | "CASE" | "HELP";
type ContentEditorState = {
contentType: EditorialSection;
@@ -183,6 +186,7 @@ const sectionLabels: Record<Section, string> = {
"settings-account": "账户与安全",
"settings-social": "社媒渠道",
"crm-customers": "客户列表",
gsc: "Google Search Console",
};
function App({
account,
@@ -212,10 +216,9 @@ function App({
};
};
const initialContentEditor = resolveContentRoute();
const [section, setSection] = useState<Section>(
initialContentEditor?.contentType ||
(window.location.pathname.startsWith("/pages/") ? "pages" : "dashboard"),
);
const [section, setSection] = useState<Section>(initialContentEditor?.contentType ||
(new URLSearchParams(window.location.search).get("section") === "gsc" ? "gsc" :
(window.location.pathname.startsWith("/pages/") ? "pages" : "dashboard")));
const [items, setItems] = useState<Item[]>([]);
const [error, setError] = useState("");
const [query, setQuery] = useState("");
@@ -289,6 +292,7 @@ function App({
const [frontSiteMenuOpen, setFrontSiteMenuOpen] = useState(false);
const [systemSettingsOpen, setSystemSettingsOpen] = useState(false);
const [crmMenuOpen, setCrmMenuOpen] = useState(false);
const [operationsMenuOpen, setOperationsMenuOpen] = useState(false);
const navigateSection = (next: Section) => {
setEditingProduct(null);
setEditingContent(null);
@@ -313,6 +317,8 @@ function App({
? { items: [] }
: section.startsWith("settings-")
? { items: [] }
: section === "gsc"
? { items: [] }
: section === "navigation"
? { items: [] }
: section === "agreements"
@@ -582,6 +588,12 @@ function App({
<LayoutDashboard size={18} />
数据概览
</button>
<div className="nav-group">
<button className={section === "gsc" ? "group-active" : ""} onClick={() => setOperationsMenuOpen(!operationsMenuOpen)}>
<LineChart size={18} />运营<ChevronDown className={operationsMenuOpen ? "expanded" : ""} size={16}/>
</button>
{operationsMenuOpen && <div className="nav-children"><button className={section === "gsc" ? "active" : ""} onClick={() => navigateSection("gsc")}><Search size={15}/>Google Search Console</button></div>}
</div>
<div className="nav-group">
<button
className={
@@ -789,6 +801,8 @@ function App({
<DashboardPage
onNavigate={(next) => navigateSection(next as Section)}
/>
) : section === "gsc" ? (
<GscOperationsPage />
) : section === "crm-customers" ? (
<CrmCustomerPage />
) : section === "settings-store" ? (
+1
View File
@@ -4016,3 +4016,4 @@ header p {
@media (max-width: 900px) {
.template-related-products { grid-template-columns: 1fr; }
}
.gsc-page{padding:32px;display:grid;gap:18px;max-width:1500px;margin:0 auto}.gsc-title{display:flex;align-items:center;justify-content:space-between}.gsc-title span{font-size:13px;color:#147363;font-weight:700}.gsc-title h1{margin:5px 0 6px;font-size:28px}.gsc-title p,.gsc-panel p{margin:0;color:#667085}.gsc-panel{background:#fff;border:1px solid #e2e8f0;border-radius:12px;padding:20px}.gsc-panel h2{margin:0 0 7px;font-size:17px}.gsc-connection,.gsc-toolbar,.gsc-section-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.gsc-actions,.gsc-date{display:flex;align-items:center;gap:10px}.gsc-actions a,.gsc-actions button,.gsc-connection>button,.gsc-date button{display:inline-flex;align-items:center;gap:7px;border:1px solid #d0d5dd;background:#fff;border-radius:7px;padding:9px 13px;text-decoration:none;color:#344054}.gsc-connection>button.primary{background:#126b5c;color:#fff;border-color:#126b5c}.gsc-date input{border:1px solid #d0d5dd;border-radius:6px;padding:8px}.gsc-error,.gsc-notice{padding:12px 15px;border-radius:8px}.gsc-error{background:#fff1f0;color:#b42318}.gsc-notice{background:#fffaeb;color:#854a0e}.gsc-metrics{display:grid;grid-template-columns:repeat(6,minmax(0,1fr));gap:12px}.gsc-metrics article{background:#fff;border:1px solid #e2e8f0;border-radius:10px;padding:18px}.gsc-metrics span{display:block;color:#667085;font-size:13px}.gsc-metrics strong{display:block;font-size:27px;margin:8px 0}.gsc-metrics small{color:#98a2b3}.gsc-trend{height:220px;display:flex;align-items:flex-end;gap:8px;padding-top:25px}.gsc-trend-item{height:100%;flex:1;min-width:12px;display:flex;flex-direction:column;justify-content:flex-end;align-items:center;gap:7px}.gsc-trend-item i{display:block;width:100%;max-width:26px;background:#168675;border-radius:4px 4px 0 0}.gsc-trend-item span{font-size:10px;color:#98a2b3;white-space:nowrap}.gsc-empty{margin:auto;color:#98a2b3}.gsc-tables{display:grid;grid-template-columns:1fr 1fr;gap:18px}.gsc-table-wrap{overflow:auto;max-height:520px}.gsc-table-wrap table{width:100%;border-collapse:collapse;margin-top:12px;font-size:13px}.gsc-table-wrap th,.gsc-table-wrap td{padding:10px 8px;border-bottom:1px solid #eef2f6;text-align:right}.gsc-table-wrap th:first-child,.gsc-table-wrap td:first-child{text-align:left}.gsc-url{max-width:310px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}@media(max-width:1100px){.gsc-metrics{grid-template-columns:repeat(3,1fr)}.gsc-tables{grid-template-columns:1fr}}@media(max-width:700px){.gsc-page{padding:18px}.gsc-connection,.gsc-toolbar{align-items:flex-start;flex-direction:column}.gsc-metrics{grid-template-columns:repeat(2,1fr)}.gsc-date{flex-wrap:wrap}}
+1 -1
View File
@@ -1 +1 @@
{"root":["./src/aboutussettingspage.tsx","./src/agreementsettingspage.tsx","./src/categorymanager.tsx","./src/contenteditor.tsx","./src/crmcustomerpage.tsx","./src/customcodemanager.tsx","./src/dashboardpage.tsx","./src/decorationeditor.tsx","./src/inquirysettingspanel.tsx","./src/loginpage.tsx","./src/mediacenter.tsx","./src/navigationmanager.tsx","./src/pageeditor.tsx","./src/producteditor.tsx","./src/producttemplatemanager.tsx","./src/seosettingspage.tsx","./src/settingspage.tsx","./src/api.ts","./src/main.tsx","./src/sitecontext.ts","./src/statuslabels.ts","./src/vite-env.d.ts"],"version":"5.7.3"}
{"root":["./src/aboutussettingspage.tsx","./src/agreementsettingspage.tsx","./src/categorymanager.tsx","./src/contenteditor.tsx","./src/crmcustomerpage.tsx","./src/customcodemanager.tsx","./src/dashboardpage.tsx","./src/decorationeditor.tsx","./src/gscoperationspage.tsx","./src/inquirysettingspanel.tsx","./src/loginpage.tsx","./src/mediacenter.tsx","./src/navigationmanager.tsx","./src/pageeditor.tsx","./src/producteditor.tsx","./src/producttemplatemanager.tsx","./src/seosettingspage.tsx","./src/settingspage.tsx","./src/api.ts","./src/main.tsx","./src/sitecontext.ts","./src/statuslabels.ts","./src/vite-env.d.ts"],"version":"5.7.3"}