| 1 | /** |
| 2 | * 插件相关工具函数 |
| 3 | */ |
| 4 | |
| 5 | import type { PlatformConfigOptions } from './types' |
| 6 | import type { PlatAccountInfo, PlatformPublishTask, PluginPlatformType } from './types/baseTypes' |
| 7 | import type { IPubParams } from '@/components/PublishDialog/publishDialog.type' |
| 8 | import { PlatType } from '@/app/config/platConfig' |
| 9 | import { PlatformTaskStatus, PLUGIN_SUPPORTED_PLATFORMS } from './types/baseTypes' |
| 10 | |
| 11 | /** 生成唯一ID */ |
| 12 | export function generateId() { |
| 13 | return `task_${Date.now()}_${Math.random().toString(36).substr(2, 9)}` |
| 14 | } |
| 15 | |
| 16 | /** 计算整体任务状态 */ |
| 17 | export function calculateOverallStatus(platformTasks: PlatformPublishTask[]) { |
| 18 | if (platformTasks.every(task => task.status === PlatformTaskStatus.COMPLETED)) { |
| 19 | return PlatformTaskStatus.COMPLETED |
| 20 | } |
| 21 | if (platformTasks.every(task => task.status === PlatformTaskStatus.CANCELED)) { |
| 22 | return PlatformTaskStatus.CANCELED |
| 23 | } |
| 24 | if (platformTasks.some(task => task.status === PlatformTaskStatus.PUBLISHING)) { |
| 25 | return PlatformTaskStatus.PUBLISHING |
| 26 | } |
| 27 | if (platformTasks.some(task => task.status === PlatformTaskStatus.ERROR)) { |
| 28 | return PlatformTaskStatus.ERROR |
| 29 | } |
| 30 | return PlatformTaskStatus.PENDING |
| 31 | } |
| 32 | |
| 33 | /** 创建初始平台账号映射 */ |
| 34 | export function createInitialPlatformAccounts() { |
| 35 | return Object.fromEntries( |
| 36 | PLUGIN_SUPPORTED_PLATFORMS.map(platform => [platform, null]), |
| 37 | ) as Record<PluginPlatformType, PlatAccountInfo | null> |
| 38 | } |
| 39 | |
| 40 | /** 构造插件发布平台特定配置 */ |
| 41 | export function buildPluginPlatformConfig( |
| 42 | platform: PluginPlatformType, |
| 43 | params: IPubParams, |
| 44 | ): PlatformConfigOptions | undefined { |
| 45 | if (platform === PlatType.Xhs) { |
| 46 | const userDeclarationBind = params.option.xhs?.userDeclarationBind |
| 47 | |
| 48 | return userDeclarationBind === null || userDeclarationBind === undefined |
| 49 | ? undefined |
| 50 | : { userDeclarationBind } |
| 51 | } |
| 52 | |
| 53 | if (platform === PlatType.WxSph && params.video) { |
| 54 | return { |
| 55 | wxSph: { |
| 56 | videoMetadata: { |
| 57 | width: params.video.width, |
| 58 | height: params.video.height, |
| 59 | duration: params.video.duration, |
| 60 | size: params.video.size, |
| 61 | }, |
| 62 | poiInfo: params.option.wxSph?.poiInfo, |
| 63 | event: params.option.wxSph?.activity, |
| 64 | extLink: params.option.wxSph?.extLink, |
| 65 | postFlag: params.option.wxSph?.isOriginal ? 1 : 0, |
| 66 | }, |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return undefined |
| 71 | } |
| 72 |