22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from "react"; |
| 2 | import { API_BASE_URL } from "./config"; |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 3 | import "./AppealPage.css"; |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 4 | |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 5 | // State for appeals fetched from backend |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 6 | export default function AppealPage() { |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 7 | const [appeals, setAppeals] = useState([]); |
| 8 | const [selectedId, setSelectedId] = useState(null); |
| 9 | const [loading, setLoading] = useState(true); |
| 10 | const [error, setError] = useState(null); |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 11 | |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 12 | // Helper to load appeals |
| 13 | const fetchAppeals = async () => { |
| 14 | setLoading(true); |
| 15 | try { |
| 16 | const res = await fetch(`${API_BASE_URL}/api/appeals`); |
| 17 | if (!res.ok) throw new Error(`请求失败,状态码 ${res.status}`); |
| 18 | const data = await res.json(); |
| 19 | // console.log("Fetched appeals:", data); |
| 20 | setAppeals(data); |
| 21 | if (data.length > 0) setSelectedId(data[0].appealid); |
| 22 | setError(null); |
| 23 | } catch (err) { |
| 24 | setError(err.message); |
| 25 | } finally { |
| 26 | setLoading(false); |
| 27 | } |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 28 | }; |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 29 | |
| 30 | useEffect(() => { |
| 31 | fetchAppeals(); |
| 32 | }, []); |
| 33 | |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 34 | if (loading) return <div className="appeal-loading">加载中...</div>; |
| 35 | if (error) return <div className="appeal-error">加载失败:{error}</div>; |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 36 | const selectedAppeal = appeals.find(a => a.appealid === selectedId) || {}; |
| 37 | |
| 38 | // Approve selected appeal and refresh |
| 39 | const handleApprove = async () => { |
| 40 | try { |
| 41 | const res = await fetch(`${API_BASE_URL}/api/appeals-approve`, { |
| 42 | method: "POST", |
| 43 | headers: { "Content-Type": "application/json" }, |
| 44 | body: JSON.stringify({ appealid: selectedAppeal.appealid }) |
| 45 | }); |
| 46 | if (!res.ok) throw new Error(`请求失败,状态码 ${res.status}`); |
| 47 | alert("已通过申诉"); |
| 48 | await fetchAppeals(); |
| 49 | } catch (err) { |
| 50 | alert(`操作失败:${err.message}`); |
| 51 | } |
| 52 | }; |
| 53 | // Reject selected appeal and refresh |
| 54 | const handleReject = async () => { |
| 55 | try { |
| 56 | const res = await fetch(`${API_BASE_URL}/api/appeals-reject`, { |
| 57 | method: "POST", |
| 58 | headers: { "Content-Type": "application/json" }, |
| 59 | body: JSON.stringify({ appealid: selectedAppeal.appealid }) |
| 60 | }); |
| 61 | if (!res.ok) throw new Error(`请求失败,状态码 ${res.status}`); |
| 62 | alert("已拒绝申诉"); |
| 63 | await fetchAppeals(); |
| 64 | } catch (err) { |
| 65 | alert(`操作失败:${err.message}`); |
| 66 | } |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | return ( |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 70 | <div className="appeal-page-container"> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 71 | {/* 侧栏 */} |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 72 | <div className="appeal-sidebar"> |
| 73 | <h3 className="appeal-sidebar-title">申诉列表</h3> |
| 74 | <div className="appeal-list-container"> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 75 | {appeals.map(a => ( |
| 76 | <div |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 77 | key={a.appealid} |
| 78 | onClick={() => setSelectedId(a.appealid)} |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 79 | className={`appeal-list-item ${ |
| 80 | selectedId === a.appealid ? 'selected' : '' |
| 81 | } ${ |
| 82 | a.status === 1 || a.status === 2 ? 'approved' : 'pending' |
| 83 | }`} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 84 | > |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 85 | {a.user.username} |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 86 | <span className={`appeal-status-label ${ |
| 87 | a.status === 1 || a.status === 2 ? 'approved' : 'pending' |
| 88 | }`}> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 89 | {a.status === 1 || a.status === 2 ? "已审核" : "未审核"} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 90 | </span> |
| 91 | </div> |
| 92 | ))} |
| 93 | </div> |
| 94 | </div> |
| 95 | {/* 申诉详情 */} |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 96 | <div className="appeal-main-content"> |
| 97 | <h2 className="appeal-detail-title">申诉详情</h2> |
| 98 | <div className="appeal-detail-card"> |
| 99 | <div className="appeal-detail-item"> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 100 | <b>申诉ID:</b>{selectedAppeal.appealid} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 101 | </div> |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 102 | <div className="appeal-detail-item"> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 103 | <b>用户ID:</b>{selectedAppeal.user.userid} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 104 | </div> |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 105 | <div className="appeal-detail-item"> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 106 | <b>申诉内容:</b>{selectedAppeal.content} |
| 107 | </div> |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 108 | <div className="appeal-detail-item"> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 109 | <b>申诉文件:</b> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 110 | <FileViewer url={selectedAppeal.fileURL} /> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 111 | </div> |
| 112 | </div> |
| 113 | {/* 审核按钮 */} |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 114 | <div className="appeal-buttons-container"> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 115 | <button |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 116 | className={`appeal-btn appeal-btn-approve`} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 117 | disabled={selectedAppeal.status === 1} |
| 118 | onClick={handleApprove} |
| 119 | > |
| 120 | 通过 |
| 121 | </button> |
| 122 | <button |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 123 | className={`appeal-btn appeal-btn-reject`} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 124 | disabled={selectedAppeal.status === 1} |
| 125 | onClick={handleReject} |
| 126 | > |
| 127 | 不通过 |
| 128 | </button> |
| 129 | </div> |
| 130 | </div> |
| 131 | </div> |
| 132 | ); |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // 简单PDF预览组件 |
| 136 | function FileViewer({ url }) { |
| 137 | if (!url) return <div>无附件</div>; |
| 138 | if (url.endsWith(".pdf")) { |
| 139 | return ( |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 140 | <div className="file-viewer-container"> |
| 141 | <iframe |
| 142 | src={url} |
| 143 | title="PDF预览" |
| 144 | className="file-viewer-iframe" |
| 145 | /> |
| 146 | </div> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 147 | ); |
| 148 | } |
| 149 | // 这里只做PDF示例,实际可扩展为DOC等 |
wht | 3058782 | 2025-06-09 23:33:09 +0800 | [diff] [blame^] | 150 | return <a href={url} target="_blank" rel="noopener noreferrer" className="file-download-link">下载附件</a>; |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 151 | } |