| 1 | import { Controller, Et, Icp, Inject } from '../../core/decorators'; |
| 2 | import { PublishService } from '../service'; |
| 3 | import { AccountService } from '../../account/service'; |
| 4 | import { ImgTextPubService } from './service'; |
| 5 | import { ImgTextModel } from '../../../db/models/imgText'; |
| 6 | import platController from '../../plat'; |
| 7 | import { PubStatus } from '../../../../commont/publish/PublishEnum'; |
| 8 | |
| 9 | @Controller() |
| 10 | export class ImgTextPubController { |
| 11 | @Inject(ImgTextPubService) |
| 12 | private readonly imgTextService!: ImgTextPubService; |
| 13 | |
| 14 | @Inject(PublishService) |
| 15 | private readonly publishService!: PublishService; |
| 16 | |
| 17 | @Inject(AccountService) |
| 18 | private readonly accountService!: AccountService; |
| 19 | |
| 20 | // 创建图文发布记录 |
| 21 | @Icp('ICP_PUBLISH_CREATE_IMG_TEXT_PUL') |
| 22 | async createVideoPub( |
| 23 | event: Electron.IpcMainInvokeEvent, |
| 24 | imgText: ImgTextModel, |
| 25 | ): Promise<any> { |
| 26 | return await this.imgTextService.createImgTextPul(imgText); |
| 27 | } |
| 28 | |
| 29 | // 更新视频发布数据 |
| 30 | @Et('ET_PUBLISH_UPDATE_IMG_TEXT_PUL') |
| 31 | async updateImgTextPul(imgTextModel: ImgTextModel): Promise<any> { |
| 32 | await this.imgTextService.updateImgTextPul(imgTextModel); |
| 33 | } |
| 34 | |
| 35 | // 根据发布记录ID获取图文发布列表 |
| 36 | @Icp('ICP_PUBLISH_GET_IMG_TEXT_LIST') |
| 37 | async getImgTextList( |
| 38 | event: Electron.IpcMainInvokeEvent, |
| 39 | pubRecordId: number, |
| 40 | ) { |
| 41 | return await this.imgTextService.getImgTextPulListByPubRecordId( |
| 42 | pubRecordId, |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | // 发布图文 |
| 47 | @Icp('ICP_PUBLISH_IMG_TEXT') |
| 48 | async pubImgText(event: Electron.IpcMainInvokeEvent, pubRecordId: number) { |
| 49 | const pubRecordInfo = |
| 50 | await this.publishService.getPubRecordInfo(pubRecordId); |
| 51 | |
| 52 | if (pubRecordInfo?.status === PubStatus.RELEASED) { |
| 53 | console.error('发布记录已发布'); |
| 54 | } |
| 55 | |
| 56 | // 获取图文发布记录列表 |
| 57 | const imgTextModels = |
| 58 | await this.imgTextService.getImgTextPulListByPubRecordId(pubRecordId); |
| 59 | // 获取用到的账户信息 |
| 60 | const accountList = await this.accountService.getAccountsByIds( |
| 61 | imgTextModels.map((v) => v.accountId), |
| 62 | ); |
| 63 | |
| 64 | // 获取代理IP |
| 65 | const groupModels = await this.accountService.getAccountGroup(); |
| 66 | for (let i = 0; i < accountList.length; i++) { |
| 67 | const account = accountList[i]; |
| 68 | const group = groupModels.find((v) => v.id === account.groupId); |
| 69 | if (group && group.proxyIp && group.proxyOpen) { |
| 70 | imgTextModels[i].proxyIp = group.proxyIp; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // 发布 |
| 75 | const pubRes = await platController.imgTextPublish( |
| 76 | imgTextModels, |
| 77 | accountList, |
| 78 | ); |
| 79 | |
| 80 | let successCount = 0; |
| 81 | pubRes.map((v) => { |
| 82 | if (v.code === 1) successCount++; |
| 83 | }); |
| 84 | // 更改记录状态 |
| 85 | await this.publishService.updatePubRecordStatus( |
| 86 | pubRecordId, |
| 87 | successCount === 0 |
| 88 | ? PubStatus.FAIL |
| 89 | : successCount === pubRes.length |
| 90 | ? PubStatus.RELEASED |
| 91 | : PubStatus.PartSuccess, |
| 92 | ); |
| 93 | return pubRes; |
| 94 | } |
| 95 | } |
| 96 |