崔向南 | 464e19d | 2025-06-05 17:46:27 +0800 | [diff] [blame^] | 1 | import React from 'react'; |
| 2 | import { Form, Input, InputNumber, DatePicker, Button, message } from 'antd'; |
| 3 | import axios from 'axios'; |
| 4 | import { publishBounty } from '@/services/bounty/bounty'; // 新增 axios 导入 |
| 5 | |
| 6 | interface BountyPublishProps { |
| 7 | onSuccess?: () => void; // 提交成功回调 |
| 8 | onCancel?: () => void; // 取消操作回调 |
| 9 | } |
| 10 | |
| 11 | |
| 12 | |
| 13 | const BountyPublish: React.FC<BountyPublishProps> = ({ onSuccess, onCancel }) => { |
| 14 | const [form] = Form.useForm(); |
| 15 | |
| 16 | // ✅ 替换 axios 请求为服务方法调用 |
| 17 | const handleSubmit = async (values: { |
| 18 | title: string; |
| 19 | description: string; |
| 20 | reward: number; |
| 21 | deadline: string; |
| 22 | }) => { |
| 23 | try { |
| 24 | // 使用服务层方法 |
| 25 | const res = await publishBounty(values); |
| 26 | |
| 27 | if (res) { |
| 28 | //message.success('悬赏发布成功!'); |
| 29 | form.resetFields(); |
| 30 | onSuccess?.(); |
| 31 | } else { |
| 32 | message.error(res.msg || '发布失败,请重试'); |
| 33 | } |
| 34 | } catch (err) { |
| 35 | console.error('发布失败:', err); |
| 36 | message.error('网络请求失败,请检查网络'); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | return ( |
| 42 | <div className="page-container"> |
| 43 | <h2>发布新悬赏</h2> |
| 44 | <Form |
| 45 | form={form} |
| 46 | layout="vertical" |
| 47 | onFinish={handleSubmit} |
| 48 | requiredMark="optional" |
| 49 | style={{ maxWidth: 600 }} |
| 50 | > |
| 51 | {/* 标题 */} |
| 52 | <Form.Item |
| 53 | name="title" |
| 54 | label="悬赏标题" |
| 55 | rules={[{ required: true, message: '请输入悬赏标题' }]} |
| 56 | > |
| 57 | <Input placeholder="请输入悬赏标题" /> |
| 58 | </Form.Item> |
| 59 | |
| 60 | {/* 描述 */} |
| 61 | <Form.Item |
| 62 | name="description" |
| 63 | label="悬赏描述" |
| 64 | rules={[{ required: true, message: '请输入悬赏描述' }]} |
| 65 | > |
| 66 | <Input.TextArea rows={4} placeholder="请输入悬赏描述" /> |
| 67 | </Form.Item> |
| 68 | |
| 69 | {/* 奖励 */} |
| 70 | <Form.Item |
| 71 | name="reward" |
| 72 | label="悬赏奖励" |
| 73 | rules={[ |
| 74 | { required: true, message: '请输入奖励数值' }, |
| 75 | { type: 'number', min: 1, message: '奖励必须大于0' }, |
| 76 | ]} |
| 77 | > |
| 78 | <InputNumber min={1} placeholder="请输入奖励数值" style={{ width: '100%' }} /> |
| 79 | </Form.Item> |
| 80 | |
| 81 | {/* 截止时间 */} |
| 82 | <Form.Item |
| 83 | name="deadline" |
| 84 | label="截止时间" |
| 85 | rules={[{ required: true, message: '请选择截止时间' }]} |
| 86 | > |
| 87 | <DatePicker |
| 88 | showTime |
| 89 | format="YYYY-MM-DD HH:mm:ss" |
| 90 | placeholder="选择截止时间" |
| 91 | style={{ width: '100%' }} |
| 92 | /> |
| 93 | </Form.Item> |
| 94 | |
| 95 | {/* 提交按钮 */} |
| 96 | <Form.Item> |
| 97 | <Button type="primary" htmlType="submit"> |
| 98 | 发布悬赏 |
| 99 | </Button> |
| 100 | <Button |
| 101 | type="default" |
| 102 | onClick={() => { |
| 103 | form.resetFields(); |
| 104 | onCancel?.(); // 🔥 取消时触发回调 |
| 105 | }} |
| 106 | style={{ marginLeft: 16 }} |
| 107 | > |
| 108 | 重置 |
| 109 | </Button> |
| 110 | </Form.Item> |
| 111 | </Form> |
| 112 | </div> |
| 113 | ); |
| 114 | }; |
| 115 | |
| 116 | export default BountyPublish; |