Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: Browser plugins support scheduled publishing (#354)
Gao15a93c01b540
diff --git a/project/web/src/components/PublishDialog/index.tsx b/project/web/src/components/PublishDialog/index.tsxindex 23a231a3..8f87c396 100644--- a/project/web/src/components/PublishDialog/index.tsx+++ b/project/web/src/components/PublishDialog/index.tsx@@ -34,7 +34,7 @@ import { CSSTransition } from 'react-transition-group'import { useWindowSize } from 'react-use'import { useShallow } from 'zustand/react/shallow'import { getChatModels } from '@/api/ai'-import { apiCreatePublish, apiCreatePublishRecord } from '@/api/plat/publish'+import { apiCreatePublish } from '@/api/plat/publish'import { toolsApi } from '@/api/tools'import {getDays,@@ -76,12 +76,10 @@ import { useAccountStore } from '@/store/account'import {PlatformTaskStatus,PLUGIN_SUPPORTED_PLATFORMS,- PluginStatus,PublishDetailModal,usePluginStore,} from '@/store/plugin'import { generateUUID } from '@/utils'-import { getOssUrl } from '@/utils/oss'import styles from './publishDialog.module.scss'export interface IPublishDialogRef {@@ -598,167 +596,6 @@ const PublishDialog = memo(return PLUGIN_SUPPORTED_PLATFORMS.includes(platType as PluginPlatformType)}, [])- /**- * 执行插件发布- * @param pluginItems 需要通过插件发布的项目列表- * @param taskId 任务ID- * @param platformTaskIdMap 账号ID到平台任务ID的映射- */- const executePluginPublish = useCallback(async (- pluginItems: typeof pubListChoosed,- taskId: string,- platformTaskIdMap: Map<string, string>,- ) => {- const { status, publish, updatePlatformTask } = usePluginStore.getState()-- // 检查插件状态- if (status !== PluginStatus.READY) {- console.warn('插件未就绪,无法执行插件发布')- const { updatePlatformTaskByRequestId } = usePluginStore.getState()- // 将所有插件任务标记为失败- for (const item of pluginItems) {- const requestId = platformTaskIdMap.get(item.account.id)- if (requestId) {- updatePlatformTaskByRequestId(requestId, {- status: PlatformTaskStatus.ERROR,- error: '浏览器插件未就绪,请先安装并授权插件',- endTime: Date.now(),- })- }- }- return- }-- // 并行执行插件发布(不等待,同时发布多个平台)- const publishTasks = pluginItems.map(async (item) => {- const platform = item.account.type as PluginPlatformType- const accountId = item.account.id- // 获取该账号对应的 requestId(用于进度匹配)- const requestId = platformTaskIdMap.get(accountId)!- const { updatePlatformTaskByRequestId } = usePluginStore.getState()-- // 更新任务状态为发布中- updatePlatformTaskByRequestId(requestId, {- status: PlatformTaskStatus.PUBLISHING,- startTime: Date.now(),- })-- try {- // 构建插件发布参数- // 优先传递 File 对象,避免插件需要重新下载- const publishParams: PluginPublishParams = {- platform,- accountId, // 传入账号ID,用于区分同一平台的多个账号- requestId, // 传入 requestId,插件回调时带回用于匹配- type: item.params.video ? 'video' : 'image',- title: item.params.title || '',- desc: item.params.des || '',- topics: item.params.topics || [],- }-- // 视频发布 - 优先传 file,没有 file 才传 URL- if (item.params.video) {- // 视频:优先传 file(转换 Blob 为 File),没有则传 ossUrl- if (item.params.video.file) {- // 将 Blob 转换为 File- const videoFile = new File(- [item.params.video.file],- item.params.video.filename || 'video.mp4',- { type: item.params.video.file.type },- )- publishParams.video = videoFile- }- else if (item.params.video.ossUrl) {- publishParams.video = getOssUrl(item.params.video.ossUrl)- }-- // 封面:优先传 file,没有则传 ossUrl- if (item.params.video.cover?.file) {- publishParams.cover = item.params.video.cover.file- }- else if (item.params.video.cover?.ossUrl) {- publishParams.cover = getOssUrl(item.params.video.cover.ossUrl)- }- }- // 图文发布 - 优先传 file,没有 file 才传 URL- else if (item.params.images && item.params.images.length > 0) {- publishParams.images = item.params.images.map((img) => {- // 优先传 file,没有则传 ossUrl- if (img.file) {- return img.file- }- return img.ossUrl ? getOssUrl(img.ossUrl) : ''- }).filter(item => item !== '')- }-- // 执行发布,通过 requestId 匹配进度- const result = await publish(publishParams, (progress) => {- // 使用 requestId 精确更新进度- updatePlatformTaskByRequestId(requestId, {- progress,- })- })-- // 发布成功,更新任务状态- updatePlatformTaskByRequestId(requestId, {- status: PlatformTaskStatus.COMPLETED,- result: {- success: true,- workId: result.workId,- shareLink: result.shareLink,- },- endTime: Date.now(),- })-- // 发布成功后,创建发布记录- try {- await apiCreatePublishRecord({- flowId: generateUUID(),- type: item.params.video ? 'video' : 'article',- title: item.params.title || '',- desc: item.params.des || '',- accountId: item.account.id,- accountType: item.account.type,- videoUrl: item.params.video?.ossUrl,- coverUrl: item.params.video?.cover?.ossUrl- || (item.params.images && item.params.images.length > 0- ? item.params.images[0].ossUrl- : undefined),- imgUrlList: item.params.images- ?.map(v => v.ossUrl)- .filter((url): url is string => url !== undefined) || [],- topics: item.params.topics || [],- status: 1, // 已发布- dataId: `${result.workId}`,- workLink: result.shareLink,- uid: item.account.uid,- // @ts-ignore- publishTime: dayjs(Date.now()).utc().format(),- })- }- catch (recordError) {- console.error('创建发布记录失败:', recordError)- }- }- catch (error) {- // 发布失败- updatePlatformTaskByRequestId(requestId, {- status: PlatformTaskStatus.ERROR,- error: error instanceof Error ? error.message : '发布失败',- result: {- success: false,- failReason: error instanceof Error ? error.message : '发布失败',- },- endTime: Date.now(),- })- }- })-- // 并行执行所有发布任务- await Promise.all(publishTasks)-- useCalendarTiming.getState().getPubRecord()- }, [])/*** Publish content with scheduled time (from calendar picker)@@ -863,9 +700,18 @@ const PublishDialog = memo(}// 2. 再执行插件发布(插件支持平台)- if (pluginPublishItems.length > 0 && taskId) {+ if (pluginPublishItems.length > 0) {// 异步执行插件发布,不阻塞主流程- executePluginPublish(pluginPublishItems, taskId, platformTaskIdMap)+ // 使用 store 中封装的方法,支持定时发布+ usePluginStore.getState().executePluginPublish({+ items: pluginPublishItems,+ platformTaskIdMap,+ publishTime,+ onComplete: () => {+ // 发布完成后刷新发布记录+ useCalendarTiming.getState().getPubRecord()+ },+ })}// 关闭发布弹框@@ -876,7 +722,7 @@ const PublishDialog = memo(onPubSuccess()}usePublishDialogStorageStore.getState().clearPubData()- }, [pubListChoosed, pubTime, isPluginSupportedPlatform, executePluginPublish, t])+ }, [pubListChoosed, pubTime, isPluginSupportedPlatform, t])// 处理划词操作const handleTextSelection = useCallback((action: AIAction, selectedText: string) => {@@ -1295,6 +1141,7 @@ const PublishDialog = memo({pubListChoosed.map((v) => {return (<PlatParamsSetting+ key={v.account.id}pubItem={v}style={{ marginBottom: '12px' }}onImageToImage={handleImageToImage}diff --git a/project/web/src/store/account.ts b/project/web/src/store/account.tsindex 14cd5cbd..ccf7a0e4 100644--- a/project/web/src/store/account.ts+++ b/project/web/src/store/account.ts@@ -103,8 +103,13 @@ export const useAccountStore = create(})if (isBackground) {+ // 初始化时:执行完整的插件初始化(包括授权检查)await usePluginStore.getState().init()}+ else {+ // 刷新时:只同步账号状态,不重新授权+ await usePluginStore.getState().syncAccountStatus()+ }void useDataStatisticsStore.getState()diff --git a/project/web/src/store/plugin/index.ts b/project/web/src/store/plugin/index.tsindex 53481060..b1e98e06 100644--- a/project/web/src/store/plugin/index.ts+++ b/project/web/src/store/plugin/index.ts@@ -25,7 +25,12 @@ export {// Storeexport { usePluginStore } from './store'-export type { PlatformAccountsMap, PlatformProgressMap } from './store'+export type {+ ExecutePluginPublishParams,+ PlatformAccountsMap,+ PlatformProgressMap,+ PluginPublishItem,+} from './store'// Typesexport type {diff --git a/project/web/src/store/plugin/store.ts b/project/web/src/store/plugin/store.tsindex 7d904873..a16d5083 100644--- a/project/web/src/store/plugin/store.ts+++ b/project/web/src/store/plugin/store.ts@@ -13,17 +13,52 @@ import type {PublishTaskListConfig,} from './types/baseTypes'import type { SocialAccount } from '@/api/types/account.type'+import type { IImgFile, IPubParams, IVideoFile } from '@/components/PublishDialog/publishDialog.type'+import dayjs from 'dayjs'+import utc from 'dayjs/plugin/utc'import lodash from 'lodash'import { create } from 'zustand'import { combine } from 'zustand/middleware'import { createOrUpdateAccountApi } from '@/api/account'+import { apiCreatePublishRecord } from '@/api/plat/publish'import { ClientType } from '@/app/[lng]/accounts/accounts.enums'import { useDataStatisticsStore } from '@/app/[lng]/dataStatistics/useDataStatistics'import { AccountStatus } from '@/app/config/accountConfig'import { useAccountStore } from '@/store/account'+import { generateUUID } from '@/utils'+import { getOssUrl } from '@/utils/oss'import { DEFAULT_POLLING_INTERVAL } from './constants'import { calculateOverallStatus, createInitialPlatformAccounts, generateId } from './plugin.utils'-import { PLUGIN_SUPPORTED_PLATFORMS, PluginStatus as Status } from './types/baseTypes'+import { PlatformTaskStatus, PLUGIN_SUPPORTED_PLATFORMS, PluginStatus as Status } from './types/baseTypes'+import { parseTopicString } from '@/utils'++// 启用 dayjs utc 插件+dayjs.extend(utc)++/**+ * 插件发布项接口+ * 描述单个平台的发布内容+ */+export interface PluginPublishItem {+ /** 账号信息 */+ account: SocialAccount+ /** 发布参数 */+ params: IPubParams+}++/**+ * 执行插件发布的参数+ */+export interface ExecutePluginPublishParams {+ /** 需要发布的项目列表 */+ items: PluginPublishItem[]+ /** 账号ID 到 requestId 的映射(用于进度匹配) */+ platformTaskIdMap: Map<string, string>+ /** 发布时间(ISO 格式字符串,可选,不传则立即发布) */+ publishTime?: string+ /** 发布完成后的回调(可选) */+ onComplete?: () => void+}/** 平台账号信息映射 */export type PlatformAccountsMap = Record<PluginPlatformType, PlatAccountInfo | null>@@ -51,11 +86,11 @@ export type PlatformProgressMap = Map<string, ProgressEvent>export interface IPluginStore {status: StatuspollingTimer: NodeJS.Timeout | null- /** @deprecated 使用 publishingPlatforms 代替 */+ /** 是否正在发布(任意平台) */isPublishing: boolean/** 正在发布的集合,key 为 platform 或 platform-accountId,支持同一平台多账号同时发布 */publishingPlatforms: Set<string>- /** @deprecated 使用 platformProgress 代替 */+ /** 当前发布进度(最新一个) */publishProgress: ProgressEvent | null/** 各平台发布进度,key 为 platform 或 platform-accountId */platformProgress: PlatformProgressMap@@ -223,6 +258,17 @@ export const usePluginStore = create(}},+ /**+ * 同步账号状态(仅当插件已就绪时执行)+ * 用于刷新账号列表后,不重新授权,只同步在线/离线状态+ */+ async syncAccountStatus() {+ const { status } = get()+ if (status === Status.READY) {+ await methods.refreshAllPlatformAccounts()+ }+ },+/** 刷新所有平台账号信息,并同步更新 accountList 中的在线/离线状态 */async refreshAllPlatformAccounts() {const { status } = get()@@ -345,6 +391,12 @@ export const usePluginStore = create(/** 发布内容到指定平台 */async publish(params: PublishParams, onProgress?: ProgressCallback) {const { status, publishingPlatforms, platformProgress } = get()++ // 解析话题+ const { topics, cleanedString } = parseTopicString(params.desc || '')+ params.topics = [...new Set(params.topics?.concat(topics))]+ params.desc = cleanedString+const platform = params.platformconst accountId = params.accountId// 使用 platform + accountId 作为唯一标识,支持同一平台多账号同时发布@@ -370,7 +422,7 @@ export const usePluginStore = create(set({isPublishing: newPublishingPlatforms.size > 0,publishingPlatforms: newPublishingPlatforms,- publishProgress: initialProgress, // 兼容旧代码+ publishProgress: initialProgress,platformProgress: newPlatformProgress,})@@ -380,7 +432,7 @@ export const usePluginStore = create(const updatedProgress = new Map(get().platformProgress)updatedProgress.set(publishKey, progress)set({- publishProgress: progress, // 兼容旧代码+ publishProgress: progress,platformProgress: updatedProgress,})onProgress?.(progress)@@ -396,7 +448,7 @@ export const usePluginStore = create(set({isPublishing: updatedPlatforms.size > 0,publishingPlatforms: updatedPlatforms,- publishProgress: completedProgress, // 兼容旧代码+ publishProgress: completedProgress,platformProgress: updatedPlatformProgress,})@@ -418,7 +470,7 @@ export const usePluginStore = create(set({isPublishing: updatedPlatforms.size > 0,publishingPlatforms: updatedPlatforms,- publishProgress: errorProgress, // 兼容旧代码+ publishProgress: errorProgress,platformProgress: updatedPlatformProgress,})console.error('发布失败:', error)@@ -566,6 +618,156 @@ export const usePluginStore = create(taskListConfig: { ...state.taskListConfig, ...config },}))},++ /**+ * 执行插件发布(封装完整的发布流程)+ * 支持并行发布多个平台,支持定时发布+ * @param params 发布参数+ * @returns Promise<void>+ */+ async executePluginPublish(params: ExecutePluginPublishParams): Promise<void> {+ const { items, platformTaskIdMap, publishTime, onComplete } = params++ // 并行执行插件发布(不等待,同时发布多个平台)+ const publishTasks = items.map(async (item) => {+ const platform = item.account.type as PluginPlatformType+ const accountId = item.account.id+ // 获取该账号对应的 requestId(用于进度匹配)+ const requestId = platformTaskIdMap.get(accountId)++ if (!requestId) {+ console.error('未找到账号对应的 requestId:', accountId)+ return+ }++ // 更新任务状态为发布中+ methods.updatePlatformTaskByRequestId(requestId, {+ status: PlatformTaskStatus.PUBLISHING,+ startTime: Date.now(),+ })++ try {+ // 构建插件发布参数+ // 优先传递 File 对象,避免插件需要重新下载+ const publishParams: PublishParams = {+ platform,+ accountId, // 传入账号ID,用于区分同一平台的多个账号+ requestId, // 传入 requestId,插件回调时带回用于匹配+ type: item.params.video ? 'video' : 'image',+ title: item.params.title || '',+ desc: item.params.des || '',+ topics: item.params.topics || [],+ }++ // 如果有定时发布时间,则传入+ if (publishTime) {+ publishParams.scheduledTime = dayjs(publishTime).valueOf()+ }++ // 视频发布 - 优先传 file,没有 file 才传 URL+ if (item.params.video) {+ // 视频:优先传 file(转换 Blob 为 File),没有则传 ossUrl+ if (item.params.video.file) {+ // 将 Blob 转换为 File+ const videoFile = new File(+ [item.params.video.file],+ item.params.video.filename || 'video.mp4',+ { type: item.params.video.file.type },+ )+ publishParams.video = videoFile+ }+ else if (item.params.video.ossUrl) {+ publishParams.video = getOssUrl(item.params.video.ossUrl)+ }++ // 封面:优先传 file,没有则传 ossUrl+ if (item.params.video.cover?.file) {+ publishParams.cover = item.params.video.cover.file+ }+ else if (item.params.video.cover?.ossUrl) {+ publishParams.cover = getOssUrl(item.params.video.cover.ossUrl)+ }+ }+ // 图文发布 - 优先传 file,没有 file 才传 URL+ else if (item.params.images && item.params.images.length > 0) {+ publishParams.images = item.params.images.map((img) => {+ // 优先传 file,没有则传 ossUrl+ if (img.file) {+ return img.file+ }+ return img.ossUrl ? getOssUrl(img.ossUrl) : ''+ }).filter(v => v !== '')+ }++ // 执行发布,通过 requestId 匹配进度+ const result = await methods.publish(publishParams, (progress) => {+ // 使用 requestId 精确更新进度+ methods.updatePlatformTaskByRequestId(requestId, {+ progress,+ })+ })++ // 发布成功,更新任务状态+ methods.updatePlatformTaskByRequestId(requestId, {+ status: PlatformTaskStatus.COMPLETED,+ result: {+ success: true,+ workId: result.workId,+ shareLink: result.shareLink,+ },+ endTime: Date.now(),+ })++ // 发布成功后,创建发布记录+ try {+ await apiCreatePublishRecord({+ flowId: generateUUID(),+ type: item.params.video ? 'video' : 'article',+ title: item.params.title || '',+ desc: item.params.des || '',+ accountId: item.account.id,+ accountType: item.account.type,+ videoUrl: item.params.video?.ossUrl,+ coverUrl: item.params.video?.cover?.ossUrl+ || (item.params.images && item.params.images.length > 0+ ? item.params.images[0].ossUrl+ : undefined),+ imgUrlList: item.params.images+ ?.map(v => v.ossUrl)+ .filter((url): url is string => url !== undefined) || [],+ topics: item.params.topics || [],+ status: 1, // 已发布+ dataId: `${result.workId}`,+ workLink: result.shareLink,+ uid: item.account.uid,+ // @ts-ignore+ publishTime: publishTime || dayjs(Date.now()).utc().format(),+ })+ }+ catch (recordError) {+ console.error('创建发布记录失败:', recordError)+ }+ }+ catch (error) {+ // 发布失败+ methods.updatePlatformTaskByRequestId(requestId, {+ status: PlatformTaskStatus.ERROR,+ error: error instanceof Error ? error.message : '发布失败',+ result: {+ success: false,+ failReason: error instanceof Error ? error.message : '发布失败',+ },+ endTime: Date.now(),+ })+ }+ })++ // 并行执行所有发布任务+ await Promise.all(publishTasks)++ // 发布完成后的回调+ onComplete?.()+ },}return methodsdiff --git a/project/web/src/utils/storageIndexedDb.ts b/project/web/src/utils/storageIndexedDb.tsindex e54b5fe2..5262a87a 100644--- a/project/web/src/utils/storageIndexedDb.ts+++ b/project/web/src/utils/storageIndexedDb.ts@@ -1,51 +1,105 @@import type { StateStorage } from 'zustand/middleware'-import { clear, del, get, set } from 'idb-keyval'+/**+ * 检查是否在浏览器环境中+ * 用于避免在 SSR 环境下访问浏览器 API+ */+const isBrowser = typeof window !== 'undefined' && typeof indexedDB !== 'undefined'++/**+ * 动态导入 idb-keyval,仅在浏览器环境中使用+ * 避免 SSR 时报错:indexedDB is not defined+ */+const getIdbKeyval = async () => {+ if (!isBrowser) {+ return null+ }+ return import('idb-keyval')+}++/**+ * IndexedDB 存储类+ * 实现 zustand 的 StateStorage 接口+ * 在 SSR 环境下会 fallback 到 localStorage 或返回空值+ */class IndexedDBStorage implements StateStorage {public async getItem(name: string): Promise<string | null> {+ // SSR 环境下直接返回 null+ if (!isBrowser) {+ return null+ }+try {- const value = (await get(name)) || localStorage.getItem(name)- return value+ const idb = await getIdbKeyval()+ if (idb) {+ const value = (await idb.get(name)) || localStorage.getItem(name)+ return value+ }+ return localStorage.getItem(name)}catch (error) {- console.error(error)+ console.error('[IndexedDBStorage] getItem error:', error)return localStorage.getItem(name)}}public async setItem(name: string, value: string): Promise<void> {+ // SSR 环境下不执行存储操作+ if (!isBrowser) {+ return+ }+try {const _value = JSON.parse(value)if (!_value?.state?._hasHydrated) {- console.warn('skip setItem', name)+ console.warn('[IndexedDBStorage] skip setItem:', name)return}- await set(name, value)+ const idb = await getIdbKeyval()+ if (idb) {+ await idb.set(name, value)+ }}catch (error) {- console.error(error)- if (typeof window !== 'undefined') {+ console.error('[IndexedDBStorage] setItem error:', error)+ if (isBrowser) {localStorage.setItem(name, value)}}}public async removeItem(name: string): Promise<void> {+ // SSR 环境下不执行删除操作+ if (!isBrowser) {+ return+ }+try {- await del(name)+ const idb = await getIdbKeyval()+ if (idb) {+ await idb.del(name)+ }}catch (error) {- console.error(error)+ console.error('[IndexedDBStorage] removeItem error:', error)localStorage.removeItem(name)}}public async clear(): Promise<void> {+ // SSR 环境下不执行清除操作+ if (!isBrowser) {+ return+ }+try {- await clear()+ const idb = await getIdbKeyval()+ if (idb) {+ await idb.clear()+ }}catch (error) {- console.error(error)+ console.error('[IndexedDBStorage] clear error:', error)localStorage.clear()}}