feat(widget): 添加页面访问量显示功能

- 新增公开 API GET /api/analytics/pv 获取页面访问量
- 前端组件初始化时自动填充 #cwd-page-pv 容器
- 支持数字格式化(如 1.2k、1.5M)
- 将版本号更新至 0.1.8
This commit is contained in:
anghunk
2026-03-16 13:57:57 +08:00
parent 607d36ead1
commit e2db5daecb
9 changed files with 107 additions and 5 deletions

View File

@@ -0,0 +1,27 @@
import type { Context } from 'hono';
import type { Bindings } from '../../bindings';
export const getPagePv = async (c: Context<{ Bindings: Bindings }>) => {
try {
const rawPostSlug = c.req.query('post_slug') || '';
const postSlug = rawPostSlug.trim();
const rawSiteId = c.req.query('siteId') || '';
const siteId = rawSiteId && rawSiteId !== 'default' ? rawSiteId : '';
if (!postSlug) {
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 }>();
const pv = row?.pv || 0;
return c.json({ pv, postSlug });
} catch (e: any) {
return c.json({ message: e.message || '获取访问量失败' }, 500);
}
};