update at 2025-10-16 16:10:58
This commit is contained in:
192
src/core/publisher-manager.ts
Normal file
192
src/core/publisher-manager.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* 文件:publisher-manager.ts
|
||||
* 作用:发布平台管理器,统一管理所有发布平台
|
||||
*/
|
||||
|
||||
import { IPlatformPublisher, PublishResult, PublishOptions } from './publisher-interface';
|
||||
import { ErrorHandler, PlatformError } from './error-handler';
|
||||
import { ProgressIndicator } from './progress-indicator';
|
||||
|
||||
export class PublisherManager {
|
||||
private publishers = new Map<string, IPlatformPublisher>();
|
||||
private static instance: PublisherManager;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
static getInstance(): PublisherManager {
|
||||
if (!this.instance) {
|
||||
this.instance = new PublisherManager();
|
||||
}
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册发布平台
|
||||
*/
|
||||
register(publisher: IPlatformPublisher): void {
|
||||
this.publishers.set(publisher.id, publisher);
|
||||
console.log(`[PublisherManager] 注册发布平台: ${publisher.displayName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销发布平台
|
||||
*/
|
||||
unregister(publisherId: string): void {
|
||||
if (this.publishers.delete(publisherId)) {
|
||||
console.log(`[PublisherManager] 注销发布平台: ${publisherId}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有已注册的发布平台
|
||||
*/
|
||||
getPublishers(): IPlatformPublisher[] {
|
||||
return Array.from(this.publishers.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的发布平台
|
||||
*/
|
||||
getPublisher(publisherId: string): IPlatformPublisher | undefined {
|
||||
return this.publishers.get(publisherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查平台是否可用
|
||||
*/
|
||||
async isPlatformAvailable(publisherId: string): Promise<boolean> {
|
||||
const publisher = this.getPublisher(publisherId);
|
||||
if (!publisher) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return await publisher.validateConfig();
|
||||
} catch (error) {
|
||||
console.warn(`[PublisherManager] 平台 ${publisherId} 验证失败:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布内容到指定平台
|
||||
*/
|
||||
async publish(
|
||||
platformId: string,
|
||||
content: string,
|
||||
options?: PublishOptions
|
||||
): Promise<PublishResult> {
|
||||
const progress = new ProgressIndicator();
|
||||
|
||||
try {
|
||||
progress.start(`准备发布到 ${platformId}`);
|
||||
|
||||
const publisher = this.getPublisher(platformId);
|
||||
if (!publisher) {
|
||||
throw new PlatformError(`发布平台 ${platformId} 未找到`, platformId);
|
||||
}
|
||||
|
||||
progress.update('验证平台配置');
|
||||
const isValid = await publisher.validateConfig();
|
||||
if (!isValid) {
|
||||
throw new PlatformError(`发布平台 ${platformId} 配置无效`, platformId);
|
||||
}
|
||||
|
||||
progress.update('发布内容');
|
||||
const result = await publisher.publish(content, options);
|
||||
|
||||
if (result.success) {
|
||||
progress.finish(`发布成功到 ${publisher.displayName}`);
|
||||
} else {
|
||||
progress.error(`发布失败到 ${publisher.displayName}: ${result.message}`);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error) {
|
||||
const errorMsg = `发布到 ${platformId} 失败`;
|
||||
progress.error(errorMsg);
|
||||
ErrorHandler.handle(error as Error, 'PublisherManager.publish');
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: errorMsg,
|
||||
error: error as Error
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量发布到多个平台
|
||||
*/
|
||||
async batchPublish(
|
||||
platformIds: string[],
|
||||
content: string,
|
||||
options?: PublishOptions
|
||||
): Promise<Map<string, PublishResult>> {
|
||||
const results = new Map<string, PublishResult>();
|
||||
|
||||
console.log(`[PublisherManager] 开始批量发布到 ${platformIds.length} 个平台`);
|
||||
|
||||
for (const platformId of platformIds) {
|
||||
try {
|
||||
const result = await this.publish(platformId, content, options);
|
||||
results.set(platformId, result);
|
||||
|
||||
// 添加延迟以避免频率限制
|
||||
if (platformIds.length > 1) {
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
}
|
||||
} catch (error) {
|
||||
results.set(platformId, {
|
||||
success: false,
|
||||
message: `批量发布失败: ${error}`,
|
||||
error: error as Error
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const successCount = Array.from(results.values()).filter(r => r.success).length;
|
||||
const totalCount = platformIds.length;
|
||||
|
||||
console.log(`[PublisherManager] 批量发布完成: ${successCount}/${totalCount} 成功`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成预览内容
|
||||
*/
|
||||
async generatePreview(
|
||||
platformId: string,
|
||||
content: string,
|
||||
options?: PublishOptions
|
||||
): Promise<string> {
|
||||
return await ErrorHandler.withErrorHandling(async () => {
|
||||
const publisher = this.getPublisher(platformId);
|
||||
if (!publisher) {
|
||||
throw new PlatformError(`发布平台 ${platformId} 未找到`, platformId);
|
||||
}
|
||||
|
||||
return await publisher.generatePreview(content, options);
|
||||
}, 'PublisherManager.generatePreview', '') || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取平台状态信息
|
||||
*/
|
||||
async getPlatformStatus(): Promise<Map<string, boolean>> {
|
||||
const status = new Map<string, boolean>();
|
||||
|
||||
for (const [id, publisher] of this.publishers) {
|
||||
try {
|
||||
const isAvailable = await publisher.validateConfig();
|
||||
status.set(id, isAvailable);
|
||||
} catch {
|
||||
status.set(id, false);
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user