Skip to content

Let's use AI to Earn!

下载文件

DESKTOP-ZTS2NQJNA614bf4752fc4
diff --git a/src/utils/fileUtils.ts b/src/utils/fileUtils.ts
new file mode 100644
index 00000000..b2a0a528
--- /dev/null
+++ b/src/utils/fileUtils.ts
@@ -0,0 +1,106 @@
+/**
+ * 文件工具函数
+ */
+import { message } from 'antd';
+import { v4 as uuidv4 } from 'uuid';
+
+/**
+ * 从网络URL下载文件到本地临时目录
+ * @param url 文件URL
+ * @param fileType 文件类型 'video' | 'image'
+ * @returns 本地文件路径或Blob URL
+ */
+export const downloadFileToLocal = async (url: string, fileType: 'video' | 'image'): Promise<string> => {
+ try {
+ // 使用代理服务器解决跨域问题
+ const proxyUrl = `/api/proxy?url=${encodeURIComponent(url)}`;
+
+ // 下载文件
+ const response = await fetch(proxyUrl, {
+ mode: 'cors',
+ credentials: 'same-origin'
+ });
+
+ if (!response.ok) {
+ throw new Error(`下载失败: ${response.status} ${response.statusText}`);
+ }
+
+ // 获取文件内容
+ const blob = await response.blob();
+
+ // 创建Blob URL
+ const blobUrl = URL.createObjectURL(blob);
+
+ return blobUrl;
+ } catch (error) {
+ console.error('文件下载失败:', error);
+ message.error('文件下载失败,请重试');
+ throw error;
+ }
+};
+
+/**
+ * 使用视频元素加载视频URL
+ * @param videoUrl 视频URL
+ * @returns Promise<string> 视频URL
+ */
+export const loadVideoUrl = (videoUrl: string): Promise<string> => {
+ return new Promise((resolve) => {
+ // 直接返回原始URL,依赖视频元素自身的加载能力
+ resolve(videoUrl);
+ });
+};
+
+/**
+ * 使用图片元素加载图片URL
+ * @param imageUrl 图片URL
+ * @returns Promise<string> 图片URL
+ */
+export const loadImageUrl = (imageUrl: string): Promise<string> => {
+ return new Promise((resolve) => {
+ // 创建图片元素预加载
+ const img = new Image();
+ img.crossOrigin = 'anonymous'; // 尝试解决跨域问题
+ img.onload = () => resolve(imageUrl);
+ img.onerror = () => {
+ console.warn('图片加载失败,使用原始URL');
+ resolve(imageUrl);
+ };
+ img.src = imageUrl;
+ });
+};
+
+/**
+ * 从网络URL下载文件并保存到本地
+ * @param url 文件URL
+ * @param filename 保存的文件名
+ */
+export const downloadAndSaveFile = async (url: string, filename: string): Promise<void> => {
+ try {
+ // 创建一个a标签
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = filename;
+ a.target = '_blank'; // 在新窗口打开,避免跨域问题
+ a.style.display = 'none';
+
+ // 添加到DOM并触发点击
+ document.body.appendChild(a);
+ a.click();
+
+ // 清理
+ setTimeout(() => {
+ document.body.removeChild(a);
+ }, 100);
+
+ // 等待用户完成下载
+ return new Promise((resolve) => {
+ // 给用户足够的时间保存文件
+ setTimeout(resolve, 3000);
+ });
+ } catch (error) {
+ console.error('保存文件失败:', error);
+ message.error('保存文件失败,请重试');
+ throw error;
+ }
+};
\ No newline at end of file
diff --git a/src/views/task/components/videoInfo.tsx b/src/views/task/components/videoInfo.tsx
index e6b66ac7..6af2436f 100644
--- a/src/views/task/components/videoInfo.tsx
+++ b/src/views/task/components/videoInfo.tsx
@@ -7,6 +7,12 @@
*/
import { Button, Modal, Tag, Divider, Row, Col, message } from 'antd';
import { Task, TaskStatusName, TaskTypeName, TaskVideo } from '@@/types/task';
+import { PubType } from '@@/publish/PublishEnum';
+import {
+ icpCreatePubRecord,
+ icpCreateVideoPubRecord,
+ icpPubVideo,
+} from '@/icp/publish';
import { forwardRef, useImperativeHandle, useState } from 'react';
import { taskApi } from '@/api/task';
import styles from './videoInfo.module.scss';
@@ -16,8 +22,11 @@ import {
TagOutlined,
DollarOutlined,
PlayCircleOutlined,
- CopyOutlined
+ CopyOutlined,
+ LoadingOutlined
} from '@ant-design/icons';
+import ChooseAccountModule from "@/views/publish/components/ChooseAccountModule/ChooseAccountModule";
+import { downloadAndSaveFile } from '@/utils/fileUtils';
const FILE_BASE_URL = import.meta.env.VITE_APP_FILE_HOST;
@@ -28,6 +37,7 @@ export interface TaskInfoRef {
const Com = forwardRef<TaskInfoRef>((props: any, ref) => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [taskInfo, setTaskInfo] = useState<Task<TaskVideo> | null>();
+ const [downloading, setDownloading] = useState(false);
async function init(inTaskInfo: Task<TaskVideo>) {
setTaskInfo(inTaskInfo);
@@ -42,13 +52,15 @@ const Com = forwardRef<TaskInfoRef>((props: any, ref) => {
* 接受任务
*/
async function taskApply() {
+ console.log('接受任务', taskInfo);
if (!taskInfo) return;
try {
const res = await taskApi.taskApply<TaskVideo>(taskInfo?._id);
- setTaskInfo(res);
+ // setTaskInfo(res);
message.success('任务接受成功!');
setIsModalOpen(false);
+ setChooseAccountOpen(true);
} catch (error) {
message.error('接受任务失败,请稍后再试');
}
@@ -69,8 +81,126 @@ const Com = forwardRef<TaskInfoRef>((props: any, ref) => {
});
};
+ // 账户选择弹框显示隐藏状态
+ const [chooseAccountOpen, setChooseAccountOpen] = useState(false);
+ const [accountListChoose, setAccountListChoose] = useState<AccountModel[]>([]);
+
+ // 下载文件到本地并发布
+ const downloadAndPublish = async (aList: any) => {
+ console.log('下载文件到本地并发布', aList, taskInfo);
+ if (!taskInfo || !taskInfo.dataInfo) {
+ message.error('任务信息不完整,无法发布');
+ return;
+ }
+
+ setDownloading(true);
+ try {
+ console.log('开始下载文件...');
+ message.loading('正在下载视频和封面图片,请稍候...', 0);
+
+ // 构建完整的文件URL
+ const videoUrl = FILE_BASE_URL + taskInfo.dataInfo.videoUrl;
+ const coverUrl = FILE_BASE_URL + taskInfo.imageUrl;
+
+ console.log('下载视频:', videoUrl);
+ console.log('下载封面:', coverUrl);
+
+ // 使用下载链接触发浏览器下载
+ message.info('请在弹出的窗口中保存视频文件');
+ await downloadAndSaveFile(videoUrl, `视频任务_${taskInfo.id}.mp4`);
+
+ message.info('请在弹出的窗口中保存封面图片');
+ await downloadAndSaveFile(coverUrl, `封面_${taskInfo.id}.jpg`);
+
+ // 提示用户输入保存的文件路径
+ const localVideoPath = prompt('请输入视频文件保存的完整路径:', '');
+ if (!localVideoPath) {
+ throw new Error('未提供视频文件路径');
+ }
+
+ const localCoverPath = prompt('请输入封面图片保存的完整路径:', '');
+ if (!localCoverPath) {
+ throw new Error('未提供封面图片路径');
+ }
+
+ message.destroy();
+ message.success('文件下载完成,开始发布...');
+
+ // 创建一级记录
+ const recordRes = await icpCreatePubRecord({
+ title: taskInfo.dataInfo.title || '/',
+ desc: taskInfo.dataInfo.desc || '/',
+ type: PubType.VIDEO,
+ videoPath: localVideoPath,
+ coverPath: localCoverPath,
+ });
+
+ // 创建二级记录
+ for (const vData of aList) {
+ const account = vData.account!;
+ const video = vData.video!;
+
+ await icpCreateVideoPubRecord({
+ type: account.type,
+ accountId: account.id,
+ pubRecordId: recordRes.id,
+ publishTime: new Date(),
+ title: taskInfo.dataInfo.title,
+ // topics: vData.pubParams.topics,
+ desc: taskInfo.dataInfo.desc,
+ videoPath: localVideoPath,
+ coverPath: localCoverPath
+ });
+ }
+
+ // 发布视频
+ const okRes = await icpPubVideo(recordRes.id);
+
+ // 成功数据
+ const successList = okRes.filter((v) => v.code === 1);
+
+ console.log('发布成功', successList);
+ console.log('发布结果', okRes);
+
+ if (successList.length > 0) {
+ message.success(`成功发布到${successList.length}个平台`);
+ } else {
+ message.warning('发布完成,但没有成功发布的平台');
+ }
+
+ } catch (error) {
+ console.error('下载或发布过程中出错:', error);
+ message.error('下载或发布过程中出错,请重试');
+ } finally {
+ setDownloading(false);
+ setChooseAccountOpen(false);
+ }
+ };
+
return (
<>
+ <ChooseAccountModule
+ open={chooseAccountOpen}
+ onClose={() => !downloading && setChooseAccountOpen(false)}
+ choosedAccounts={accountListChoose}
+ onPlatConfirm={async (aList) => {
+ console.log('选择的账号:', aList);
+ setAccountListChoose(aList);
+
+ // 下载文件并发布
+ await downloadAndPublish(aList);
+ }}
+ pubType={PubType.VIDEO}
+ footer={
+ downloading ? (
+ <div style={{ textAlign: 'center', padding: '12px 0' }}>
+ <LoadingOutlined style={{ marginRight: 8 }} />
+ 正在处理文件,请勿关闭窗口...
+ </div>
+ ) : null
+ }
+ />
+
<Modal
title={null}
open={isModalOpen}