返回 AiToEarn
index.ts
1 /*
2 * @Author: nevin
3 * @Date: 2025-02-06 15:57:02
4 * @LastEditTime: 2025-03-24 15:15:04
5 * @LastEditors: nevin
6 * @Description:
7 */
8 import { AccountModel } from '../../db/models/account';
9 import { PlatformBase } from './PlatformBase';
10 import kwai from './platforms/Kwai';
11 import xhs from './platforms/xhs';
12 import douyin from './platforms/douyin';
13 import wxSph from './platforms/wxSph';
14 import {
15 IAccountInfoParams,
16 IGetLocationDataParams,
17 IGetUsersParams,
18 WorkData,
19 } from './plat.type';
20 import { PublishVideoResult } from './module';
21 import { VideoModel } from '../../db/models/video';
22 import { PubItemVideo } from './pub/PubItemVideo';
23 import { PlatType } from '../../../commont/AccountEnum';
24 import { PubItemImgText } from './pub/PubItemImgText';
25 import { ImgTextModel } from '../../db/models/imgText';
26 import { EtEvent } from '../../global/event';
27
28 class PlatController {
29 // 所有平台
30 private readonly platforms = new Map<PlatType, PlatformBase>();
31
32 constructor() {
33 this.platforms.set(PlatType.KWAI, kwai);
34 this.platforms.set(PlatType.Xhs, xhs);
35 this.platforms.set(PlatType.Douyin, douyin);
36 this.platforms.set(PlatType.WxSph, wxSph);
37 }
38
39 // 获取平台类实例
40 private getPlatform(type: PlatType) {
41 const platform = this.platforms.get(type);
42 if (!platform) console.warn(`没有这个平台:${type}`);
43
44 return this.platforms.get(type);
45 }
46
47 /**
48 * 登录某个平台
49 * @param type 平台
50 * @param params 参数
51 */
52 public async platlogin(type: PlatType, params?: any) {
53 const platform = this.platforms.get(type)!;
54 const res = await platform.login(params);
55 if (!res || !res.loginCookie) return null;
56 // 获取账户信息
57 const info = await platform.getAccountInfo({
58 cookies: JSON.parse(res.loginCookie),
59 });
60 if (!!info) res.fansCount = info.fansCount || 0;
61 return res;
62 }
63
64 /**
65 * 平台登录检测
66 * @param type 平台
67 * @param account
68 */
69 public async platLoginCheck(type: PlatType, account: AccountModel) {
70 const platform = this.platforms.get(type)!;
71 return await platform.loginCheck(account);
72 }
73
74 /**
75 * 发布视频,支持发布到多个平台
76 * @param videoModels 视频记录数据
77 * @param accountModels 该用户的账号记录数据
78 */
79 public async videoPublish(
80 videoModels: VideoModel[],
81 accountModels: AccountModel[],
82 ) {
83 // 总发布记录状态更新
84 const tasks: Promise<PublishVideoResult>[] = [];
85 for (const videoModel of videoModels) {
86 const platform = this.getPlatform(videoModel.type);
87 if (platform) {
88 const pubItemVideo = new PubItemVideo(
89 accountModels.find((v) => v.id === videoModel.accountId)!,
90 videoModel,
91 platform,
92 );
93
94 EtEvent.emit('ET_TRACING_VIDEO_PUL', {
95 accountId: videoModel.accountId,
96 dataId: videoModel.dataId,
97 desc: '发布成功!',
98 });
99
100 tasks.push(pubItemVideo.publishVideo());
101 }
102 }
103 return await Promise.all(tasks);
104 }
105
106 /**
107 * 发布图文,支持发布到多个平台
108 * @param imgTextModels 图文记录数据
109 * @param accountModels 该用户的账号记录数据
110 */
111 public async imgTextPublish(
112 imgTextModels: ImgTextModel[],
113 accountModels: AccountModel[],
114 ) {
115 // 总发布记录状态更新
116 const tasks: Promise<PublishVideoResult>[] = [];
117 for (const videoModel of imgTextModels) {
118 const platform = this.getPlatform(videoModel.type);
119 if (platform) {
120 const pubItemVideo = new PubItemImgText(
121 accountModels.find((v) => v.id === videoModel.accountId)!,
122 videoModel,
123 platform,
124 );
125 tasks.push(pubItemVideo.publishImgText());
126 }
127 }
128 return await Promise.all(tasks);
129 }
130
131 /**
132 * 获取某个平台的话题数据
133 * @param account
134 * @param keyword
135 */
136 public async getTopic(account: AccountModel, keyword: string) {
137 const platform = this.platforms.get(account.type)!;
138 return await platform.getTopics({
139 keyword,
140 account,
141 });
142 }
143
144 /**
145 * 获取某个平台的账户信息
146 * @param type 平台
147 * @param params 参数
148 */
149 public async getAccountInfo(type: PlatType, params: IAccountInfoParams) {
150 const platform = this.platforms.get(type)!;
151 return await platform.getAccountInfo(params);
152 }
153
154 /**
155 * 获取某个平台的账户统计数据
156 * @param account 账户
157 */
158 public async getStatistics(account: AccountModel) {
159 const platform = this.platforms.get(account.type)!;
160 return await platform.getStatistics(account);
161 }
162
163 /**
164 * 获取某个平台的账户面板数据
165 * @param account 账户
166 * @param time
167 */
168 public async getDashboard(account: AccountModel, time?: [string, string]) {
169 const platform = this.platforms.get(account.type)!;
170 return await platform.getDashboard(account, time || []);
171 }
172
173 // 获取位置数据
174 public async getLocationData(params: IGetLocationDataParams) {
175 const platform = this.platforms.get(params.account!.type)!;
176 return await platform.getLocationData({
177 ...params,
178 cookie: JSON.parse(params.account!.loginCookie),
179 });
180 }
181
182 // 获取用户数据
183 public async getUsers(params: IGetUsersParams) {
184 const platform = this.platforms.get(params.account!.type)!;
185 return await platform.getUsers(params);
186 }
187
188 /**
189 * 点赞
190 * @param account
191 * @param dataId
192 * @param option
193 */
194 public async dianzanDyOther(
195 account: AccountModel,
196 dataId: string,
197 option?: any,
198 ) {
199 const platform = this.platforms.get(account.type)!;
200 return await platform.dianzanDyOther(account, dataId, option);
201 }
202
203 /**
204 * 获取作品列表
205 * @param account
206 * @param pcursor
207 */
208 public async shoucangDyOther(account: AccountModel, pcursor?: string) {
209 const platform = this.platforms.get(account.type)!;
210 return await platform.shoucangDyOther(account, pcursor);
211 }
212
213 /**
214 * 获取作品列表
215 * @param account
216 * @param pcursor
217 */
218 public async getWorkList(account: AccountModel, pcursor?: string) {
219 const platform = this.platforms.get(account.type)!;
220 return await platform.getWorkList(account, pcursor);
221 }
222
223 /**
224 * 搜索作品列表
225 * @param account
226 * @param qe
227 * @param pageInfo
228 */
229 public async getsearchNodeList(
230 account: AccountModel,
231 qe?: string,
232 pageInfo?: any,
233 ) {
234 const platform = this.platforms.get(account.type)!;
235 return await platform.getsearchNodeList(account, qe, pageInfo);
236 }
237
238 /**
239 * 获取评论列表
240 * @param account
241 * @param data
242 * @param pcursor
243 */
244 public async getCommentList(
245 account: AccountModel,
246 data: WorkData,
247 pcursor?: string,
248 ) {
249 const platform = this.platforms.get(account.type)!;
250 return await platform.getCommentList(account, data, pcursor);
251 }
252
253 /**
254 * 获取评论列表
255 * @param account
256 * @param data
257 * @param pcursor
258 */
259 public async getCreatorCommentListByOther(
260 account: AccountModel,
261 data: WorkData,
262 pcursor?: string,
263 ) {
264 const platform = this.platforms.get(account.type)!;
265 return await platform.getCreatorCommentListByOther(account, data, pcursor);
266 }
267
268 // 获取合集
269 public async getMixList(account: AccountModel) {
270 const platform = this.platforms.get(account.type)!;
271 return await platform.getMixList(JSON.parse(account.loginCookie));
272 }
273
274 /**
275 * 获取二级评论列表
276 * @param account
277 * @param data
278 * @param root_comment_id
279 * @param pcursor
280 */
281 public async getCreatorSecondCommentListByOther(
282 account: AccountModel,
283 data: WorkData,
284 root_comment_id: string,
285 pcursor?: string,
286 ) {
287 const platform = this.platforms.get(account.type)!;
288 return await platform.getCreatorSecondCommentListByOther(
289 account,
290 data,
291 root_comment_id,
292 pcursor,
293 );
294 }
295
296 /**
297 * 创建评论
298 * @param account
299 * @param dataId
300 * @param content
301 */
302 public async createComment(
303 account: AccountModel,
304 dataId: string,
305 content: string,
306 ) {
307 const platform = this.platforms.get(account.type)!;
308 return await platform.createComment(account, dataId, content);
309 }
310
311 /**
312 * 创建评论
313 * @param account
314 * @param dataId
315 * @param content
316 */
317 public async createCommentByOther(
318 account: AccountModel,
319 dataId: string,
320 content: string,
321 authorId?: string,
322 ) {
323 const platform = this.platforms.get(account.type)!;
324 return await platform.createCommentByOther(
325 account,
326 dataId,
327 content,
328 authorId,
329 );
330 }
331
332 /**
333 * 回复评论
334 * @param account
335 * @param commentId
336 * @param content
337 * @param option
338 */
339 public async replyCommentByOther(
340 account: AccountModel,
341 commentId: string,
342 content: string,
343 option: {
344 dataId?: string; // 作品ID
345 comment: any; // 辅助数据,原数据
346 videoAuthId?: string; // 视频作者ID
347 },
348 ) {
349 const platform = this.platforms.get(account.type)!;
350 return await platform.replyCommentByOther(
351 account,
352 commentId,
353 content,
354 option,
355 );
356 }
357
358 /**
359 * 回复评论
360 * @param account
361 * @param commentId
362 * @param content
363 * @param option
364 */
365 public async replyComment(
366 account: AccountModel,
367 commentId: string,
368 content: string,
369 option: {
370 dataId?: string; // 作品ID
371 comment: any; // 辅助数据,原数据
372 },
373 ) {
374 const platform = this.platforms.get(account.type)!;
375 return await platform.replyComment(account, commentId, content, option);
376 }
377 }
378
379 const platController = new PlatController();
380 export default platController;
381
381 lines TYPESCRIPT