feat(admin): 添加域名列表获取功能用于筛选
新增获取域名列表的API端点,用于统一管理评论和访问统计中的域名筛选功能。修改了前端多个视图的域名获取逻辑,从原来的分散获取改为统一调用新接口,提高代码复用性和维护性。 docs: 添加反馈页面并更新侧边栏
This commit is contained in:
74
cwd-api/src/api/admin/getDomains.ts
Normal file
74
cwd-api/src/api/admin/getDomains.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import type { Context } from 'hono';
|
||||
import type { Bindings } from '../../bindings';
|
||||
|
||||
function extractDomain(source: string | null | undefined): string | null {
|
||||
if (!source) {
|
||||
return null;
|
||||
}
|
||||
const value = source.trim();
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
if (!/^https?:\/\//i.test(value)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const url = new URL(value);
|
||||
return url.hostname.toLowerCase();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const getDomains = async (c: Context<{ Bindings: Bindings }>) => {
|
||||
try {
|
||||
const domains = new Set<string>();
|
||||
|
||||
const { results: commentRows } = await c.env.CWD_DB.prepare(
|
||||
'SELECT post_slug, url FROM Comment'
|
||||
).all<{
|
||||
post_slug: string;
|
||||
url: string | null;
|
||||
}>();
|
||||
|
||||
for (const row of commentRows) {
|
||||
const domain =
|
||||
extractDomain(row.post_slug) || extractDomain(row.url);
|
||||
if (domain) {
|
||||
domains.add(domain);
|
||||
}
|
||||
}
|
||||
|
||||
await c.env.CWD_DB.prepare(
|
||||
'CREATE TABLE IF NOT EXISTS page_stats (id INTEGER PRIMARY KEY AUTOINCREMENT, post_slug TEXT UNIQUE NOT NULL, post_title TEXT, post_url TEXT, pv INTEGER NOT NULL DEFAULT 0, last_visit_at INTEGER, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL)'
|
||||
).run();
|
||||
|
||||
const { results: pageRows } = await c.env.CWD_DB.prepare(
|
||||
'SELECT post_slug, post_url FROM page_stats'
|
||||
).all<{
|
||||
post_slug: string;
|
||||
post_url: string | null;
|
||||
}>();
|
||||
|
||||
for (const row of pageRows) {
|
||||
const domain =
|
||||
extractDomain(row.post_url) || extractDomain(row.post_slug);
|
||||
if (domain) {
|
||||
domains.add(domain);
|
||||
}
|
||||
}
|
||||
|
||||
const list = Array.from(domains);
|
||||
list.sort();
|
||||
|
||||
return c.json({
|
||||
domains: list
|
||||
});
|
||||
} catch (e: any) {
|
||||
return c.json(
|
||||
{ message: e.message || '获取域名列表失败' },
|
||||
500
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user