接悬赏连接后端
Change-Id: I5311f8482059adf3ea42816fd00b503f771ec645
diff --git a/react-ui/src/pages/Reward/index.tsx b/react-ui/src/pages/Reward/index.tsx
index c86f2c2..e55cfc1 100644
--- a/react-ui/src/pages/Reward/index.tsx
+++ b/react-ui/src/pages/Reward/index.tsx
@@ -1,5 +1,5 @@
import { ExclamationCircleOutlined, PlusOutlined, DeleteOutlined, UploadOutlined } from '@ant-design/icons';
-import { Button, message, Modal, Switch, Upload } from 'antd';
+import { Button, message, Modal, Switch, Upload, Form } from 'antd';
import React, { useRef, useState, useEffect } from 'react';
import { FormattedMessage, useIntl } from 'umi';
import { FooterToolbar, PageContainer } from '@ant-design/pro-layout';
@@ -13,6 +13,8 @@
import DictTag from '@/components/DictTag';
import { useAccess } from 'umi';
import { RewardItem, RewardListParams } from './data';
+import { BtTorrent } from '../Torrent/data';
+import { uploadTorrent } from '../Torrent/service';
/**
* 删除节点
@@ -75,40 +77,11 @@
* @param rewardId 悬赏ID
* @param fileList 上传的文件列表
*/
-const handleAcceptReward = async (rewardId: string, fileList: UploadFile[]) => {
- const hide = message.loading('正在提交悬赏...');
- try {
- // 这里需要根据实际后端API进行调整
- // 假设有一个接悬赏的API: acceptReward
- const formData = new FormData();
- formData.append('rewardId', rewardId);
- // 添加文件到FormData
- fileList.forEach((file, index) => {
- if (file.originFileObj) {
- formData.append(`files[${index}]`, file.originFileObj);
- }
- });
-
- // 这里需要替换为实际的API调用
- // const resp = await acceptReward(formData);
-
- // 模拟API调用
- await new Promise(resolve => setTimeout(resolve, 1000));
-
- hide();
- message.success('悬赏提交成功!');
- return true;
- } catch (error) {
- hide();
- message.error('提交失败,请重试!');
- return false;
- }
-};
const RewardTableList: React.FC = () => {
const formTableRef = useRef<FormInstance>();
-
+ const [uploadForm] = Form.useForm();
const [modalVisible, setModalVisible] = useState<boolean>(false);
const [readOnly, setReadOnly] = useState<boolean>(false);
@@ -134,19 +107,84 @@
});
}, []);
- // 处理文件上传变化
- const handleFileChange = ({ fileList: newFileList }: { fileList: UploadFile[] }) => {
- // 限制只能上传一个文件
- if (newFileList.length > 1) {
- message.warning('只能上传一个文件!');
- setFileList([newFileList[newFileList.length - 1]]); // 保留最后一个文件
- } else {
- setFileList(newFileList);
+ // 修复后的接悬赏相关代码
+
+ /**
+ * 处理接悬赏提交
+ * @param rewardId 悬赏ID
+ * @param file 上传的文件
+ */
+ const handleAcceptReward = async (rewardId: number, file: File) => {
+ const hide = message.loading('正在提交悬赏...');
+ try {
+ // 直接传递File对象给uploadTorrent函数
+ const resp = await uploadTorrent(file);
+
+ hide();
+ if (resp.code === 200) {
+ message.success('悬赏提交成功!');
+ return true;
+ } else {
+ message.error(resp.msg || '提交失败');
+ return false;
+ }
+ } catch (error) {
+ hide();
+ message.error('提交失败,请重试!');
+ console.error('上传错误:', error);
+ return false;
}
};
- // 文件上传前的检查
+ // 修复后的提交处理函数
+ const handleAcceptSubmit = async () => {
+ if (!currentAcceptReward) {
+ message.warning('无效的悬赏信息!');
+ return;
+ }
+
+ if (fileList.length === 0) {
+ message.warning('请选择一个种子文件!');
+ return;
+ }
+
+ // 获取原始File对象
+ const uploadFile = fileList[0];
+ let file: File;
+
+ if (uploadFile.originFileObj) {
+ // 如果有originFileObj,使用它(这是真正的File对象)
+ file = uploadFile.originFileObj;
+ } else {
+ message.error('文件信息异常,请重新选择文件!');
+ return;
+ }
+
+ const success = await handleAcceptReward(currentAcceptReward.rewardId, file);
+ if (success) {
+ setAcceptModalVisible(false);
+ setCurrentAcceptReward(undefined);
+ setFileList([]);
+ uploadForm.resetFields();
+ // 刷新表格数据
+ if (actionRef.current) {
+ actionRef.current.reload();
+ }
+ }
+ };
+
+ // 文件上传前的检查(保持不变,但添加更详细的验证)
const beforeUpload = (file: File) => {
+ console.log('上传前检查文件:', file.name, file.type, file.size);
+
+ // 检查文件类型
+ const isTorrent = file.name.toLowerCase().endsWith('.torrent');
+ if (!isTorrent) {
+ message.error('只能上传.torrent格式的文件!');
+ return false;
+ }
+
+ // 检查文件大小
const isValidSize = file.size / 1024 / 1024 < 50; // 限制50MB
if (!isValidSize) {
message.error('文件大小不能超过50MB!');
@@ -161,24 +199,14 @@
return false; // 阻止自动上传,我们手动处理
};
- // 处理接悬赏提交
- const handleAcceptSubmit = async () => {
- if (!currentAcceptReward) return;
+ // 处理文件列表变化(简化逻辑)
+ const handleFileChange = ({ fileList: newFileList }: { fileList: UploadFile[] }) => {
+ // 只保留最后一个文件
+ const latestFileList = newFileList.slice(-1);
+ setFileList(latestFileList);
- if (fileList.length === 0) {
- message.warning('请选择一个文件!');
- return;
- }
-
- const success = await handleAcceptReward(currentAcceptReward.rewardId, fileList);
- if (success) {
- setAcceptModalVisible(false);
- setCurrentAcceptReward(undefined);
- setFileList([]);
- // 刷新表格数据
- if (actionRef.current) {
- actionRef.current.reload();
- }
+ if (latestFileList.length > 0) {
+ console.log('已选择文件:', latestFileList[0].name);
}
};