| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-06 17:09:35 |
| 4 | * @LastEditTime: 2025-02-14 18:31:12 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | |
| 9 | import { dialog } from 'electron'; |
| 10 | import { Controller, Et, Icp, Inject } from '../../core/decorators'; |
| 11 | import { VideoPubService } from './service'; |
| 12 | import { getUserInfo } from '../../user/comment'; |
| 13 | import { VideoModel } from '../../../db/models/video'; |
| 14 | import { Between, FindOptionsWhere } from 'typeorm'; |
| 15 | import type { CorrectQuery } from '../../../global/table'; |
| 16 | import { PublishService } from '../service'; |
| 17 | import platController from '../../plat'; |
| 18 | import { AccountService } from '../../account/service'; |
| 19 | import { PlatType } from '../../../../commont/AccountEnum'; |
| 20 | import { PubStatus } from '../../../../commont/publish/PublishEnum'; |
| 21 | |
| 22 | @Controller() |
| 23 | export class VideoPubController { |
| 24 | @Inject(VideoPubService) |
| 25 | private readonly videoPubService!: VideoPubService; |
| 26 | |
| 27 | @Inject(PublishService) |
| 28 | private readonly publishService!: PublishService; |
| 29 | |
| 30 | @Inject(AccountService) |
| 31 | private readonly accountService!: AccountService; |
| 32 | |
| 33 | // 上传视频 |
| 34 | @Icp('ICP_ACCOUNT_UPLOAD_VIDEO') |
| 35 | async uploadVideo(event: Electron.IpcMainInvokeEvent): Promise<any> { |
| 36 | // 打开文件选择对话框 |
| 37 | const result = await dialog.showOpenDialog({ |
| 38 | properties: ['openFile'], |
| 39 | filters: [{ name: '所有文件', extensions: ['*'] }], |
| 40 | }); |
| 41 | |
| 42 | if (result.filePaths.length === 0) return; |
| 43 | return result.filePaths[0]; |
| 44 | } |
| 45 | |
| 46 | // 发布视频记录获取 |
| 47 | @Icp('ICP_PUB_GET_VIDEO_RECORD') |
| 48 | async getVideoRecord( |
| 49 | event: Electron.IpcMainInvokeEvent, |
| 50 | pubRecordId: number, |
| 51 | ) { |
| 52 | return this.videoPubService.getVideoPulListByPubRecordId(pubRecordId); |
| 53 | } |
| 54 | |
| 55 | // 发布视频 |
| 56 | @Icp('ICP_PUB_VIDEO') |
| 57 | async pubVideo(event: Electron.IpcMainInvokeEvent, pubRecordId: number) { |
| 58 | try { |
| 59 | const pubRecordInfo = |
| 60 | await this.publishService.getPubRecordInfo(pubRecordId); |
| 61 | |
| 62 | if (pubRecordInfo?.status === PubStatus.RELEASED) { |
| 63 | console.error('发布记录已发布'); |
| 64 | } |
| 65 | |
| 66 | // 获取视频发布记录列表 |
| 67 | const videoList = |
| 68 | await this.videoPubService.getVideoPulListByPubRecordId(pubRecordId); |
| 69 | // 获取用到的账户信息 |
| 70 | const accountList = await this.accountService.getAccountsByIds( |
| 71 | videoList.map((v) => v.accountId), |
| 72 | ); |
| 73 | |
| 74 | // 获取代理IP |
| 75 | const groupModels = await this.accountService.getAccountGroup(); |
| 76 | for (let i = 0; i < accountList.length; i++) { |
| 77 | const account = accountList[i]; |
| 78 | const group = groupModels.find((v) => v.id === account.groupId); |
| 79 | if (group && group.proxyIp && group.proxyOpen) { |
| 80 | videoList[i].proxyIp = group.proxyIp; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // 发布 |
| 85 | const pubRes = await platController.videoPublish(videoList, accountList); |
| 86 | |
| 87 | let successCount = 0; |
| 88 | pubRes.map((v) => { |
| 89 | if (v.code === 1) { |
| 90 | successCount++; |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | // 更改记录状态 |
| 95 | const theStatus = |
| 96 | successCount === 0 |
| 97 | ? PubStatus.FAIL |
| 98 | : successCount === pubRes.length |
| 99 | ? PubStatus.RELEASED |
| 100 | : PubStatus.PartSuccess; |
| 101 | |
| 102 | await this.publishService.updatePubRecordStatus(pubRecordId, theStatus); |
| 103 | |
| 104 | return pubRes; |
| 105 | } catch (e) { |
| 106 | console.error(e); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * 获取视频发布列表 |
| 112 | */ |
| 113 | @Icp('ICP_PUBLISH_GET_VIDEO_PUL_LIST') |
| 114 | async getVideoPulList( |
| 115 | event: Electron.IpcMainInvokeEvent, |
| 116 | page: CorrectQuery, |
| 117 | query: { |
| 118 | pubRecardId?: number; |
| 119 | type?: PlatType; |
| 120 | time?: [string, string]; |
| 121 | title?: string; |
| 122 | }, |
| 123 | ): Promise<any> { |
| 124 | const userInfo = getUserInfo(); |
| 125 | const where: FindOptionsWhere<VideoModel> = { |
| 126 | userId: userInfo.id, |
| 127 | ...(query.pubRecardId && { pubRecordId: query.pubRecardId }), |
| 128 | ...(query.title && { title: query.title }), |
| 129 | ...(query.time && |
| 130 | query.time.length === 2 && |
| 131 | Between(new Date(query.time[0]), new Date(query.time[1]))), |
| 132 | }; |
| 133 | |
| 134 | return await this.videoPubService.getVideoPulList(userInfo.id, page, where); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * 获取视频发布信息 |
| 139 | */ |
| 140 | @Icp('ICP_PUBLISH_GET_VIDEO_PUL_INFO') |
| 141 | async getVideoPulInfo( |
| 142 | event: Electron.IpcMainInvokeEvent, |
| 143 | id: number, |
| 144 | ): Promise<any> { |
| 145 | return await this.videoPubService.getVideoPulInfo(id); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * 创建视频发布记录 |
| 150 | */ |
| 151 | @Icp('ICP_PUBLISH_CREATE_VIDEO_PUL') |
| 152 | async createVideoPub( |
| 153 | event: Electron.IpcMainInvokeEvent, |
| 154 | video: VideoModel, |
| 155 | ): Promise<any> { |
| 156 | return await this.videoPubService.newVideoPul(video); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * 获取不同类型的视频发布的总数 |
| 161 | */ |
| 162 | @Icp('ICP_PUBLISH_GET_VIDEO_PUL_TYPE_COUNT') |
| 163 | async getVideoPulTypeCount( |
| 164 | event: Electron.IpcMainInvokeEvent, |
| 165 | type?: PlatType, |
| 166 | ): Promise<any> { |
| 167 | const userInfo = getUserInfo(); |
| 168 | return await this.videoPubService.getVideoPulTypeCount(userInfo.id, type); |
| 169 | } |
| 170 | |
| 171 | // 获取状态为审核中的视频列表 |
| 172 | @Et('ET_PUBLISH_VIDEO_AUDIT_LIST') |
| 173 | async getAuditVideoList(callback: (_: VideoModel[]) => void): Promise<any> { |
| 174 | console.log(callback); |
| 175 | const userInfo = getUserInfo(); |
| 176 | if (!userInfo?.id) return []; |
| 177 | const res = await this.videoPubService.getVideoPulList( |
| 178 | userInfo.id, |
| 179 | { |
| 180 | page_size: 20, |
| 181 | page_no: 1, |
| 182 | }, |
| 183 | { |
| 184 | status: PubStatus.Audit, |
| 185 | }, |
| 186 | ); |
| 187 | callback(res.list); |
| 188 | } |
| 189 | |
| 190 | // 根据dataid更新数据 |
| 191 | @Et('ET_PUBLISH_UPDATE_VIDEO_PUL_BY_DATAID') |
| 192 | async updateVideoPubByDataid(videoModel: Partial<VideoModel>): Promise<any> { |
| 193 | return await this.videoPubService.updateVideoPulByDataId(videoModel); |
| 194 | } |
| 195 | |
| 196 | // 更新视频发布数据 |
| 197 | @Et('ET_PUBLISH_UPDATE_VIDEO_PUL') |
| 198 | async updateVideoPul(videoModel: VideoModel): Promise<any> { |
| 199 | await this.videoPubService.updateVideoPul(videoModel); |
| 200 | } |
| 201 | |
| 202 | @Et('ET_DEL_PUB_RECORD_ITEM') |
| 203 | async delVideoPul(pulRecordId: number) { |
| 204 | await this.videoPubService.deleteVideoPulByPubRecordId(pulRecordId); |
| 205 | } |
| 206 | |
| 207 | // 删除发布记录 |
| 208 | @Icp('ICP_PUBLISH_DEL_PUB_RECORD_ITEM_VIDEO') |
| 209 | async delPubRecordItem( |
| 210 | event: Electron.IpcMainInvokeEvent, |
| 211 | id: number, |
| 212 | accountId: number, |
| 213 | ) { |
| 214 | return await this.videoPubService.deleteByPubRecordAndAccount( |
| 215 | id, |
| 216 | accountId, |
| 217 | ); |
| 218 | } |
| 219 | } |
| 220 |