feat(widget): 添加页面访问量显示功能
- 新增公开 API GET /api/analytics/pv 获取页面访问量 - 前端组件初始化时自动填充 #cwd-page-pv 容器 - 支持数字格式化(如 1.2k、1.5M) - 将版本号更新至 0.1.8
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cwd-api",
|
||||
"version": "0.1.7",
|
||||
"version": "0.1.8",
|
||||
"scripts": {
|
||||
"deploy": "node scripts/index.js && wrangler deploy",
|
||||
"dev": "wrangler dev",
|
||||
|
||||
27
cwd-api/src/api/public/getPagePv.ts
Normal file
27
cwd-api/src/api/public/getPagePv.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
@@ -28,6 +28,7 @@ import { testEmail } from './api/admin/testEmail';
|
||||
import { getStats } from './api/admin/getStats';
|
||||
import { getSites } from './api/admin/getDomains';
|
||||
import { trackVisit } from './api/public/trackVisit';
|
||||
import { getPagePv } from './api/public/getPagePv';
|
||||
import { getVisitOverview, getVisitPages } from './api/admin/visitAnalytics';
|
||||
import { getLikeStatus, likePage } from './api/public/like';
|
||||
import { likeComment } from './api/public/likeComment';
|
||||
@@ -268,6 +269,7 @@ app.get('/api/comments', getComments);
|
||||
app.post('/api/comments', postComment);
|
||||
app.post('/api/verify-admin', verifyAdminKey);
|
||||
app.post('/api/analytics/visit', trackVisit);
|
||||
app.get('/api/analytics/pv', getPagePv);
|
||||
app.get('/api/like', getLikeStatus);
|
||||
app.post('/api/like', likePage);
|
||||
app.post('/api/comments/like', likeComment);
|
||||
|
||||
Reference in New Issue
Block a user