刘嘉昕 | 9cdeca2 | 2025-06-03 16:54:30 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { |
| 3 | createRequest, |
| 4 | deleteRequest, |
| 5 | updateMoney, |
| 6 | findByUserid, |
| 7 | findByName, |
| 8 | getTotalMoneyByName, |
| 9 | updateLoaduserByName, |
| 10 | } from '../api/request'; |
| 11 | |
| 12 | const RequestBoard = ({ currentUserId }) => { |
| 13 | const [requests, setRequests] = useState([]); // 始终存储当前用户的求助帖 |
| 14 | const [searchedRequests, setSearchedRequests] = useState([]); // 存储搜索结果 |
| 15 | const [formData, setFormData] = useState({ |
| 16 | userid: currentUserId, |
| 17 | name: '', |
| 18 | plot: '', |
| 19 | money: '', |
| 20 | year: '', |
| 21 | country: '', |
| 22 | photo: null, |
| 23 | }); |
| 24 | const [searchName, setSearchName] = useState(''); |
| 25 | const [totalMoney, setTotalMoney] = useState(null); |
| 26 | |
| 27 | // 获取当前用户求助帖 |
| 28 | const loadUserRequests = async () => { |
| 29 | const data = await findByUserid(currentUserId); |
| 30 | setRequests(data); |
| 31 | }; |
| 32 | |
| 33 | useEffect(() => { |
| 34 | loadUserRequests(); |
| 35 | }, []); |
| 36 | |
| 37 | // 表单变更处理 |
| 38 | const handleChange = (e) => { |
| 39 | const { name, value, files } = e.target; |
| 40 | setFormData((prev) => ({ |
| 41 | ...prev, |
| 42 | [name]: files ? files[0] : value, |
| 43 | })); |
| 44 | }; |
| 45 | |
| 46 | // 提交创建 |
| 47 | const handleCreate = async (e) => { |
| 48 | e.preventDefault(); |
| 49 | const fd = new FormData(); |
| 50 | Object.entries(formData).forEach(([key, value]) => { |
| 51 | if (value !== '' && value !== null) fd.append(key, value); |
| 52 | }); |
| 53 | |
| 54 | const res = await createRequest(fd); |
| 55 | if (res.data === true) { |
| 56 | alert('创建成功'); |
| 57 | setFormData(prev => ({ |
| 58 | ...prev, |
| 59 | name: '', |
| 60 | plot: '', |
| 61 | money: '', |
| 62 | year: '', |
| 63 | country: '', |
| 64 | photo: null, |
| 65 | })); |
| 66 | loadUserRequests(); // 创建成功后刷新当前用户帖子 |
| 67 | } else { |
| 68 | alert('创建失败'); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | // 删除 |
| 73 | const handleDelete = async (id) => { |
| 74 | await deleteRequest(id); |
| 75 | loadUserRequests(); // 删除后刷新当前用户帖子 |
| 76 | }; |
| 77 | |
| 78 | // 更新金额 |
| 79 | const handleUpdateMoney = async (id, newMoney) => { |
| 80 | if (!newMoney) return; |
| 81 | await updateMoney(id, newMoney); |
| 82 | loadUserRequests(); // 更新金额后刷新当前用户帖子 |
| 83 | }; |
| 84 | |
| 85 | // 按名称搜索并计算总金额 |
| 86 | const handleSearch = async () => { |
| 87 | const data = await findByName(searchName); |
| 88 | const total = await getTotalMoneyByName(searchName); |
| 89 | setSearchedRequests(data); // 搜索结果存储到独立状态 |
| 90 | setTotalMoney(total); |
| 91 | }; |
| 92 | |
| 93 | // 上传更新被协助用户 ID(批量更新同名求助帖) |
| 94 | const handleUploadLoaduser = async (name) => { |
| 95 | try { |
| 96 | await updateLoaduserByName(name, currentUserId); |
| 97 | alert('更新成功'); |
| 98 | loadUserRequests(); // 更新后刷新当前用户帖子 |
| 99 | setSearchedRequests([]); // 同步清空搜索结果(可选) |
| 100 | } catch (error) { |
| 101 | alert('更新失败,请稍后重试'); |
| 102 | console.error(error); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | return ( |
| 107 | <div className="p-4 max-w-4xl mx-auto"> |
| 108 | <h2 className="text-2xl font-bold mb-4">发布求助帖</h2> |
| 109 | <form className="grid grid-cols-2 gap-4 mb-6" onSubmit={handleCreate}> |
| 110 | <input |
| 111 | name="name" |
| 112 | placeholder="标题/名称" |
| 113 | className="border p-2" |
| 114 | value={formData.name} |
| 115 | onChange={handleChange} |
| 116 | /> |
| 117 | <textarea |
| 118 | name="plot" |
| 119 | placeholder="内容/情节" |
| 120 | className="border p-2 col-span-2" |
| 121 | value={formData.plot} |
| 122 | onChange={handleChange} |
| 123 | /> |
| 124 | <input |
| 125 | name="money" |
| 126 | type="number" |
| 127 | placeholder="金额" |
| 128 | className="border p-2" |
| 129 | value={formData.money} |
| 130 | onChange={handleChange} |
| 131 | /> |
| 132 | <input |
| 133 | name="year" |
| 134 | type="number" |
| 135 | placeholder="年份" |
| 136 | className="border p-2" |
| 137 | value={formData.year} |
| 138 | onChange={handleChange} |
| 139 | /> |
| 140 | <input |
| 141 | name="country" |
| 142 | placeholder="国家" |
| 143 | className="border p-2" |
| 144 | value={formData.country} |
| 145 | onChange={handleChange} |
| 146 | /> |
| 147 | <input |
| 148 | name="photo" |
| 149 | type="file" |
| 150 | className="border p-2 col-span-2" |
| 151 | onChange={handleChange} |
| 152 | /> |
| 153 | <button |
| 154 | type="submit" |
| 155 | className="bg-blue-500 text-white p-2 col-span-2 rounded" |
| 156 | > |
| 157 | 发布 |
| 158 | </button> |
| 159 | </form> |
| 160 | |
| 161 | <div className="mb-6"> |
| 162 | <h3 className="text-xl font-semibold mb-2">查找求助帖</h3> |
| 163 | <div className="flex gap-2 mb-2"> |
| 164 | <input |
| 165 | type="text" |
| 166 | placeholder="输入标题" |
| 167 | value={searchName} |
| 168 | onChange={(e) => setSearchName(e.target.value)} |
| 169 | className="border p-2 flex-1" |
| 170 | /> |
| 171 | <button |
| 172 | onClick={handleSearch} |
| 173 | className="bg-green-500 text-white p-2 rounded" |
| 174 | > |
| 175 | 查找 |
| 176 | </button> |
| 177 | </div> |
| 178 | {totalMoney !== null && ( |
| 179 | <p className="text-gray-700"> |
| 180 | 该名称对应的总金额:<strong>{totalMoney}</strong> |
| 181 | </p> |
| 182 | )} |
| 183 | </div> |
| 184 | |
| 185 | {/* 搜索结果展示(有搜索结果时显示) */} |
| 186 | {searchedRequests.length > 0 && ( |
| 187 | <div className="mb-6"> |
| 188 | <h3 className="text-xl font-semibold mb-2">搜索结果</h3> |
| 189 | <button |
| 190 | onClick={() => { |
| 191 | setSearchedRequests([]); |
| 192 | setSearchName(''); |
| 193 | setTotalMoney(null); |
| 194 | }} |
| 195 | className="bg-gray-500 text-white p-1 px-2 rounded mb-2" |
| 196 | > |
| 197 | ← 返回我的求助帖 |
| 198 | </button> |
| 199 | {searchedRequests.length === 0 ? ( |
| 200 | <p className="text-gray-500">无匹配的求助帖</p> |
| 201 | ) : ( |
| 202 | <div className="grid grid-cols-1 gap-4"> |
| 203 | {searchedRequests.map((request) => ( |
| 204 | <div key={request.requestid} className="border p-3 rounded shadow"> |
| 205 | <h4 className="text-lg font-semibold">{request.name}</h4> |
| 206 | <p className="text-gray-600 mb-2">{request.plot}</p> |
| 207 | <div className="flex gap-2 mb-2"> |
| 208 | <span>金额:{request.money}</span> |
| 209 | <span>年份:{request.year || '未填写'}</span> |
| 210 | <span>国家:{request.country || '未填写'}</span> |
| 211 | </div> |
| 212 | {request.photo && ( |
| 213 | <img |
| 214 | src={`http://localhost:8080${request.photo}`} |
| 215 | alt="求助帖" |
| 216 | className="w-32 h-auto mb-2" |
| 217 | /> |
| 218 | )} |
| 219 | <div className="flex gap-2"> |
| 220 | <input |
| 221 | type="number" |
| 222 | placeholder="新金额" |
| 223 | onChange={(e) => handleUpdateMoney(request.requestid, e.target.value)} |
| 224 | className="border p-1 flex-1" |
| 225 | /> |
| 226 | <button |
| 227 | onClick={() => handleDelete(request.requestid)} |
| 228 | className="bg-red-500 text-white p-1 px-2 rounded" |
| 229 | > |
| 230 | 删除 |
| 231 | </button> |
| 232 | <button |
| 233 | onClick={() => handleUploadLoaduser(request.name)} |
| 234 | className="bg-blue-500 text-white p-1 px-2 rounded" |
| 235 | > |
| 236 | 上传更新loaduser |
| 237 | </button> |
| 238 | </div> |
| 239 | </div> |
| 240 | ))} |
| 241 | </div> |
| 242 | )} |
| 243 | </div> |
| 244 | )} |
| 245 | |
| 246 | {/* 我的求助帖展示(无搜索结果时显示) */} |
| 247 | {searchedRequests.length === 0 && ( |
| 248 | <div className="mb-6"> |
| 249 | <h3 className="text-xl font-semibold mb-2">我的求助帖</h3> |
| 250 | {requests.length === 0 ? ( |
| 251 | <p className="text-gray-500">暂无求助帖</p> |
| 252 | ) : ( |
| 253 | <div className="grid grid-cols-1 gap-4"> |
| 254 | {requests.map((request) => ( |
| 255 | <div key={request.requestid} className="border p-3 rounded shadow"> |
| 256 | <h4 className="text-lg font-semibold">{request.name}</h4> |
| 257 | <p className="text-gray-600 mb-2">{request.plot}</p> |
| 258 | <div className="flex gap-2 mb-2"> |
| 259 | <span>金额:{request.money}</span> |
| 260 | <span>年份:{request.year || '未填写'}</span> |
| 261 | <span>国家:{request.country || '未填写'}</span> |
| 262 | </div> |
| 263 | {request.photo && ( |
| 264 | <img |
| 265 | src={`http://localhost:8080${request.photo}`} |
| 266 | alt="求助帖" |
| 267 | className="w-32 h-auto mb-2" |
| 268 | /> |
| 269 | )} |
| 270 | <div className="flex gap-2"> |
| 271 | <input |
| 272 | type="number" |
| 273 | placeholder="新金额" |
| 274 | onChange={(e) => handleUpdateMoney(request.requestid, e.target.value)} |
| 275 | className="border p-1 flex-1" |
| 276 | /> |
| 277 | <button |
| 278 | onClick={() => handleDelete(request.requestid)} |
| 279 | className="bg-red-500 text-white p-1 px-2 rounded" |
| 280 | > |
| 281 | 删除 |
| 282 | </button> |
| 283 | <button |
| 284 | onClick={() => handleUploadLoaduser(request.name)} |
| 285 | className="bg-blue-500 text-white p-1 px-2 rounded" |
| 286 | > |
| 287 | 上传更新loaduser |
| 288 | </button> |
| 289 | </div> |
| 290 | </div> |
| 291 | ))} |
| 292 | </div> |
| 293 | )} |
| 294 | </div> |
| 295 | )} |
| 296 | </div> |
| 297 | ); |
| 298 | }; |
| 299 | |
| 300 | export default RequestBoard; |