From 2ad9c90d1891cc98cd2072fd9f7b34af33d47084 Mon Sep 17 00:00:00 2001 From: anghunk Date: Mon, 16 Mar 2026 14:09:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(widget):=20=E4=BF=AE=E5=A4=8D=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=AE=BF=E9=97=AE=E9=87=8F=E8=8E=B7=E5=8F=96=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 前端 trackVisit 现在传递 siteId 参数 - 后端 getPagePv 查询时处理 site_id 为空或 NULL 的情况 --- cwd-api/src/api/public/getPagePv.ts | 22 +++++++++++++++++----- docs/widget/src/core/api.js | 14 +++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cwd-api/src/api/public/getPagePv.ts b/cwd-api/src/api/public/getPagePv.ts index 78351ac..cbf99e2 100644 --- a/cwd-api/src/api/public/getPagePv.ts +++ b/cwd-api/src/api/public/getPagePv.ts @@ -12,11 +12,23 @@ export const getPagePv = async (c: Context<{ Bindings: Bindings }>) => { return c.json({ message: 'post_slug is required' }, 400); } - const row = await c.env.CWD_DB.prepare( - 'SELECT pv FROM page_stats WHERE post_slug = ? AND site_id = ?' - ) - .bind(postSlug, siteId) - .first<{ pv: number }>(); + let row: { pv: number } | null = null; + + if (siteId) { + // 有 siteId 时精确匹配 + row = await c.env.CWD_DB.prepare( + 'SELECT pv FROM page_stats WHERE post_slug = ? AND site_id = ?' + ) + .bind(postSlug, siteId) + .first<{ pv: number }>(); + } else { + // 无 siteId 时,匹配空字符串或 NULL + row = await c.env.CWD_DB.prepare( + 'SELECT pv FROM page_stats WHERE post_slug = ? AND (site_id = ? OR site_id IS NULL)' + ) + .bind(postSlug, '') + .first<{ pv: number }>(); + } const pv = row?.pv || 0; diff --git a/docs/widget/src/core/api.js b/docs/widget/src/core/api.js index 14539ce..f3d0eba 100644 --- a/docs/widget/src/core/api.js +++ b/docs/widget/src/core/api.js @@ -122,16 +122,20 @@ export function createApiClient(config) { async function trackVisit() { try { + const body = { + postSlug: config.postUrl || config.postSlug, + postTitle: config.postTitle, + postUrl: config.postUrl + }; + if (config.siteId) { + body.siteId = config.siteId; + } await fetch(`${baseUrl}/api/analytics/visit`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - postSlug: config.postUrl || config.postSlug, - postTitle: config.postTitle, - postUrl: config.postUrl - }) + body: JSON.stringify(body) }); } catch (e) { }