| 1 | /** |
| 2 | * 平台交互管理器 |
| 3 | * 统一管理所有平台的交互操作 |
| 4 | */ |
| 5 | |
| 6 | import type { |
| 7 | CommentListParams, |
| 8 | CommentListResult, |
| 9 | CommentParams, |
| 10 | CommentResult, |
| 11 | DirectMessageParams, |
| 12 | DirectMessageResult, |
| 13 | FavoriteResult, |
| 14 | GetWorkDetailParams, |
| 15 | GetWorkDetailResult, |
| 16 | HomeFeedListParams, |
| 17 | HomeFeedListResult, |
| 18 | IPlatformInteraction, |
| 19 | LikeResult, |
| 20 | SubCommentListParams, |
| 21 | SupportedPlatformType, |
| 22 | } from './types' |
| 23 | import type { PlatType } from '@/app/config/platConfig' |
| 24 | import { douyinInteraction } from './douyin' |
| 25 | import { xhsInteraction } from './xhs' |
| 26 | |
| 27 | /** |
| 28 | * 平台交互管理器 |
| 29 | */ |
| 30 | class PlatformInteractionManager { |
| 31 | private platforms = new Map<SupportedPlatformType, IPlatformInteraction>() |
| 32 | |
| 33 | constructor() { |
| 34 | this.register(xhsInteraction) |
| 35 | this.register(douyinInteraction) |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * 注册平台 |
| 40 | */ |
| 41 | register(platform: IPlatformInteraction): void { |
| 42 | this.platforms.set(platform.platformType as SupportedPlatformType, platform) |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * 获取平台实例 |
| 47 | */ |
| 48 | get(platform: SupportedPlatformType): IPlatformInteraction { |
| 49 | const instance = this.platforms.get(platform) |
| 50 | if (!instance) { |
| 51 | throw new Error(`不支持的平台: ${platform}`) |
| 52 | } |
| 53 | return instance |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * 检查是否支持该平台 |
| 58 | */ |
| 59 | isSupported(platform: PlatType): platform is SupportedPlatformType { |
| 60 | return this.platforms.has(platform as SupportedPlatformType) |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * 获取所有支持的平台 |
| 65 | */ |
| 66 | getSupportedPlatforms(): SupportedPlatformType[] { |
| 67 | return Array.from(this.platforms.keys()) |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * 点赞/取消点赞 |
| 72 | */ |
| 73 | likeWork(platform: SupportedPlatformType, workId: string, isLike: boolean): Promise<LikeResult> { |
| 74 | return this.get(platform).likeWork(workId, isLike) |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * 评论作品 |
| 79 | */ |
| 80 | commentWork(platform: SupportedPlatformType, params: CommentParams): Promise<CommentResult> { |
| 81 | return this.get(platform).commentWork(params) |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * 收藏/取消收藏 |
| 86 | */ |
| 87 | favoriteWork( |
| 88 | platform: SupportedPlatformType, |
| 89 | workId: string, |
| 90 | isFavorite: boolean, |
| 91 | ): Promise<FavoriteResult> { |
| 92 | return this.get(platform).favoriteWork(workId, isFavorite) |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * 检查平台是否支持私信功能 |
| 97 | */ |
| 98 | supportsDirectMessage(platform: SupportedPlatformType): boolean { |
| 99 | const instance = this.get(platform) |
| 100 | return typeof instance.sendDirectMessage === 'function' |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * 发送私信 |
| 105 | * 注意:只有抖音支持私信,小红书不支持 |
| 106 | */ |
| 107 | async sendDirectMessage( |
| 108 | platform: SupportedPlatformType, |
| 109 | params: DirectMessageParams, |
| 110 | ): Promise<DirectMessageResult> { |
| 111 | const instance = this.get(platform) |
| 112 | if (!instance.sendDirectMessage) { |
| 113 | return { |
| 114 | success: false, |
| 115 | message: `${platform} 平台不支持私信功能`, |
| 116 | } |
| 117 | } |
| 118 | return instance.sendDirectMessage(params) |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * 获取首页作品列表 |
| 123 | * @param platform 平台类型 |
| 124 | * @param params 分页参数 |
| 125 | */ |
| 126 | getHomeFeedList( |
| 127 | platform: SupportedPlatformType, |
| 128 | params: HomeFeedListParams, |
| 129 | ): Promise<HomeFeedListResult> { |
| 130 | return this.get(platform).getHomeFeedList(params) |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * 获取作品详情 |
| 135 | * @param platform 平台类型 |
| 136 | * @param params 详情请求参数 |
| 137 | */ |
| 138 | getWorkDetail( |
| 139 | platform: SupportedPlatformType, |
| 140 | params: GetWorkDetailParams, |
| 141 | ): Promise<GetWorkDetailResult> { |
| 142 | return this.get(platform).getWorkDetail(params) |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * 获取评论列表 |
| 147 | * @param platform 平台类型 |
| 148 | * @param params 评论列表请求参数 |
| 149 | */ |
| 150 | getCommentList( |
| 151 | platform: SupportedPlatformType, |
| 152 | params: CommentListParams, |
| 153 | ): Promise<CommentListResult> { |
| 154 | return this.get(platform).getCommentList(params) |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * 获取子评论列表(查看更多回复) |
| 159 | * @param platform 平台类型 |
| 160 | * @param params 子评论列表请求参数 |
| 161 | */ |
| 162 | getSubCommentList( |
| 163 | platform: SupportedPlatformType, |
| 164 | params: SubCommentListParams, |
| 165 | ): Promise<CommentListResult> { |
| 166 | return this.get(platform).getSubCommentList(params) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * 管理器实例 |
| 172 | */ |
| 173 | export const platformManager = new PlatformInteractionManager() |
| 174 |