| 1 | import type { PublishStatus } from './publish.constants' |
| 2 | import type { PublishApiResponse, PublishRecordItem } from './publish.types' |
| 3 | import type { ChannelPublicPublishRecordItem, ChannelPublishRecordItem } from '@/api/channels/channel.types' |
| 4 | import type { PlatType } from '@/app/config/platConfig' |
| 5 | import type { IPlatOption } from '@/components/PublishDialog/publishDialog.type' |
| 6 | import { getChannelPublicPublishRecordApi, getChannelPublishFlowApi, getChannelPublishRecordApi } from '@/api/channels/channel.api' |
| 7 | |
| 8 | // Source: plat/publish.ts |
| 9 | function normalizeChannelPublishRecord(record: ChannelPublishRecordItem | ChannelPublicPublishRecordItem): PublishRecordItem { |
| 10 | return { |
| 11 | option: record.option || {}, |
| 12 | userId: record.userId || '', |
| 13 | flowId: record.flowId || '', |
| 14 | userTaskId: record.userTaskId || '', |
| 15 | taskId: record.taskId || record.id, |
| 16 | taskMaterialId: record.taskMaterialId || '', |
| 17 | type: record.type, |
| 18 | title: record.title || '', |
| 19 | desc: record.desc || '', |
| 20 | accountId: record.accountId || '', |
| 21 | topics: record.topics || [], |
| 22 | accountType: record.accountType, |
| 23 | uid: record.uid || '', |
| 24 | videoUrl: record.videoUrl || '', |
| 25 | coverUrl: record.coverUrl || '', |
| 26 | imgUrlList: record.imgUrlList || [], |
| 27 | publishTime: new Date(record.publishTime), |
| 28 | status: Number(record.status) as PublishStatus, |
| 29 | inQueue: record.inQueue || false, |
| 30 | dataId: record.dataId || record.platformWorkId || '', |
| 31 | workLink: record.workLink || '', |
| 32 | linkStatus: record.linkStatus, |
| 33 | linkError: record.linkError, |
| 34 | linkMeta: record.linkMeta, |
| 35 | platformWorkId: record.platformWorkId, |
| 36 | createdAt: record.createdAt || '', |
| 37 | updatedAt: record.updatedAt || '', |
| 38 | id: record.id, |
| 39 | errorMsg: record.errorMsg || '', |
| 40 | engagement: record.engagement, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // 根据平台类型过滤option参数 |
| 45 | function filterOptionByPlatform(option: IPlatOption, accountType: PlatType): IPlatOption { |
| 46 | if (!option) |
| 47 | return {} |
| 48 | |
| 49 | const key = accountType as keyof IPlatOption |
| 50 | return option[key] ? ({ [key]: option[key] } as IPlatOption) : {} |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * 获取发布记录详情 |
| 55 | */ |
| 56 | export function getPublishRecordDetail(flowId: string) { |
| 57 | return getChannelPublishFlowApi(flowId).then(async (flowRes): Promise<PublishApiResponse<PublishRecordItem>> => { |
| 58 | if (flowRes?.data?.tasks?.[0]?.id) { |
| 59 | const recordRes = await getChannelPublishRecordApi(flowRes.data.tasks[0].id) |
| 60 | if (recordRes?.data) { |
| 61 | return { |
| 62 | ...recordRes, |
| 63 | data: normalizeChannelPublishRecord(recordRes.data), |
| 64 | } |
| 65 | } |
| 66 | return recordRes as PublishApiResponse<PublishRecordItem> |
| 67 | } |
| 68 | |
| 69 | const recordRes = await getChannelPublishRecordApi(flowId) |
| 70 | if (recordRes?.data) { |
| 71 | return { |
| 72 | ...recordRes, |
| 73 | data: normalizeChannelPublishRecord(recordRes.data), |
| 74 | } |
| 75 | } |
| 76 | return recordRes as PublishApiResponse<PublishRecordItem> |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * 根据记录ID获取发布记录详情(公开接口,用于抖音 H5 发布轮询) |
| 82 | */ |
| 83 | export function getPublishRecordDetailById(id: string) { |
| 84 | return getChannelPublicPublishRecordApi(id).then((res) => { |
| 85 | if (!res?.data) |
| 86 | return res as PublishApiResponse<PublishRecordItem> |
| 87 | |
| 88 | return { |
| 89 | ...res, |
| 90 | data: normalizeChannelPublishRecord(res.data), |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 |