feat: 添加多站点支持,引入site_id字段

- 在Comment表中添加site_id字段,用于区分不同站点的评论
- 更新前后端API以支持site_id参数传递
- 修改自动迁移脚本,添加site_id字段迁移逻辑
- 更新前端组件配置,支持设置siteId参数
This commit is contained in:
anghunk
2026-02-09 13:56:35 +08:00
parent 5948949400
commit 32603d281a
8 changed files with 86 additions and 27 deletions

View File

@@ -35,6 +35,7 @@ onMounted(async () => {
const comments = new window.CWDComments({
el: commentsRoot.value,
apiBaseUrl,
siteId: 'cwd-doc',
theme: getTheme(),
});

View File

@@ -29,6 +29,9 @@ export class CWDComments {
*/
constructor(config) {
this.config = { ...config };
if (config.siteId) {
this.config.siteId = config.siteId;
}
if (typeof window !== 'undefined') {
this.config.postSlug = window.location.pathname;
}
@@ -571,6 +574,9 @@ export class CWDComments {
const prevConfig = { ...this.config };
Object.assign(this.config, newConfig);
if (newConfig.siteId !== undefined) {
this.config.siteId = newConfig.siteId;
}
if (typeof window !== 'undefined') {
this.config.postSlug = window.location.pathname;
}
@@ -589,7 +595,8 @@ export class CWDComments {
const shouldReload =
this.config.apiBaseUrl !== prevConfig.apiBaseUrl ||
this.config.pageSize !== prevConfig.pageSize ||
this.config.postSlug !== prevConfig.postSlug;
this.config.postSlug !== prevConfig.postSlug ||
this.config.siteId !== prevConfig.siteId;
if (shouldReload) {
const api = createApiClient(this.config);

View File

@@ -50,6 +50,10 @@ export function createApiClient(config) {
params.set('avatar_prefix', config.avatarPrefix);
}
if (config.siteId) {
params.set('site_id', config.siteId);
}
const response = await fetch(`${baseUrl}/api/comments?${params}`);
if (!response.ok) {
throw new Error(`获取评论失败:${response.status} ${response.statusText}`);
@@ -82,7 +86,8 @@ export function createApiClient(config) {
url: data.url || undefined,
content: data.content,
parent_id: data.parentId,
adminToken: data.adminToken
adminToken: data.adminToken,
site_id: config.siteId
}),
});