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 | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 3 | |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 4 | // State for appeals fetched from backend |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 5 | export default function AppealPage() { |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 6 | const [appeals, setAppeals] = useState([]); |
| 7 | const [selectedId, setSelectedId] = useState(null); |
| 8 | const [loading, setLoading] = useState(true); |
| 9 | const [error, setError] = useState(null); |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 10 | |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 11 | // Helper to load appeals |
| 12 | const fetchAppeals = async () => { |
| 13 | setLoading(true); |
| 14 | try { |
| 15 | const res = await fetch(`${API_BASE_URL}/api/appeals`); |
| 16 | if (!res.ok) throw new Error(`请求失败,状态码 ${res.status}`); |
| 17 | const data = await res.json(); |
| 18 | // console.log("Fetched appeals:", data); |
| 19 | setAppeals(data); |
| 20 | if (data.length > 0) setSelectedId(data[0].appealid); |
| 21 | setError(null); |
| 22 | } catch (err) { |
| 23 | setError(err.message); |
| 24 | } finally { |
| 25 | setLoading(false); |
| 26 | } |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 27 | }; |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 28 | |
| 29 | useEffect(() => { |
| 30 | fetchAppeals(); |
| 31 | }, []); |
| 32 | |
| 33 | if (loading) return <div>加载中...</div>; |
| 34 | if (error) return <div>加载失败:{error}</div>; |
| 35 | const selectedAppeal = appeals.find(a => a.appealid === selectedId) || {}; |
| 36 | |
| 37 | // Approve selected appeal and refresh |
| 38 | const handleApprove = async () => { |
| 39 | try { |
| 40 | const res = await fetch(`${API_BASE_URL}/api/appeals-approve`, { |
| 41 | method: "POST", |
| 42 | headers: { "Content-Type": "application/json" }, |
| 43 | body: JSON.stringify({ appealid: selectedAppeal.appealid }) |
| 44 | }); |
| 45 | if (!res.ok) throw new Error(`请求失败,状态码 ${res.status}`); |
| 46 | alert("已通过申诉"); |
| 47 | await fetchAppeals(); |
| 48 | } catch (err) { |
| 49 | alert(`操作失败:${err.message}`); |
| 50 | } |
| 51 | }; |
| 52 | // Reject selected appeal and refresh |
| 53 | const handleReject = async () => { |
| 54 | try { |
| 55 | const res = await fetch(`${API_BASE_URL}/api/appeals-reject`, { |
| 56 | method: "POST", |
| 57 | headers: { "Content-Type": "application/json" }, |
| 58 | body: JSON.stringify({ appealid: selectedAppeal.appealid }) |
| 59 | }); |
| 60 | if (!res.ok) throw new Error(`请求失败,状态码 ${res.status}`); |
| 61 | alert("已拒绝申诉"); |
| 62 | await fetchAppeals(); |
| 63 | } catch (err) { |
| 64 | alert(`操作失败:${err.message}`); |
| 65 | } |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | return ( |
| 69 | <div style={{ display: "flex", minHeight: "100vh", background: "#f7faff" }}> |
| 70 | {/* 侧栏 */} |
| 71 | <div style={{ width: 180, background: "#fff", borderRight: "1px solid #e0e7ff", padding: 0 }}> |
| 72 | <h3 style={{ textAlign: "center", padding: "18px 0 0 0", color: "#1976d2" }}>申诉列表</h3> |
| 73 | <div style={{ display: "flex", flexDirection: "column", gap: 12, marginTop: 18 }}> |
| 74 | {appeals.map(a => ( |
| 75 | <div |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 76 | key={a.appealid} |
| 77 | onClick={() => setSelectedId(a.appealid)} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 78 | style={{ |
| 79 | margin: "0 12px", |
| 80 | padding: "16px 10px", |
| 81 | borderRadius: 8, |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 82 | background: selectedId === a.appealid ? "#e3f2fd" : "#fff", |
| 83 | border: `2px solid ${a.status === 1 || a.status === 2 ? "#43a047" : "#e53935"}`, |
| 84 | color: a.status === 1 || a.status === 2 ? "#43a047" : "#e53935", |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 85 | fontWeight: 600, |
| 86 | cursor: "pointer", |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 87 | boxShadow: selectedId === a.appealid ? "0 2px 8px #b2d8ea" : "none", |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 88 | transition: "all 0.2s" |
| 89 | }} |
| 90 | > |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 91 | {a.user.username} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 92 | <span style={{ |
| 93 | float: "right", |
| 94 | fontSize: 12, |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 95 | color: a.status === 1 || a.status === 2 ? "#43a047" : "#e53935" |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 96 | }}> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 97 | {a.status === 1 || a.status === 2 ? "已审核" : "未审核"} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 98 | </span> |
| 99 | </div> |
| 100 | ))} |
| 101 | </div> |
| 102 | </div> |
| 103 | {/* 申诉详情 */} |
| 104 | <div style={{ flex: 1, padding: "40px 48px" }}> |
| 105 | <h2 style={{ marginBottom: 24, color: "#1976d2" }}>申诉详情</h2> |
| 106 | <div style={{ background: "#fff", borderRadius: 12, padding: 32, boxShadow: "0 2px 8px #e0e7ff", marginBottom: 32 }}> |
| 107 | <div style={{ marginBottom: 18 }}> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 108 | <b>申诉ID:</b>{selectedAppeal.appealid} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 109 | </div> |
| 110 | <div style={{ marginBottom: 18 }}> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 111 | <b>用户ID:</b>{selectedAppeal.user.userid} |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 112 | </div> |
| 113 | <div style={{ marginBottom: 18 }}> |
| 114 | <b>申诉内容:</b>{selectedAppeal.content} |
| 115 | </div> |
| 116 | <div style={{ marginBottom: 18 }}> |
| 117 | <b>申诉文件:</b> |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 118 | <FileViewer url={selectedAppeal.fileURL} /> |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 119 | </div> |
| 120 | </div> |
| 121 | {/* 审核按钮 */} |
| 122 | <div style={{ display: "flex", gap: 32, justifyContent: "center" }}> |
| 123 | <button |
| 124 | style={{ |
| 125 | background: selectedAppeal.status === 1 ? "#bdbdbd" : "#43a047", |
| 126 | color: "#fff", |
| 127 | border: "none", |
| 128 | borderRadius: 8, |
| 129 | padding: "10px 38px", |
| 130 | fontWeight: 600, |
| 131 | fontSize: 18, |
| 132 | cursor: selectedAppeal.status === 1 ? "not-allowed" : "pointer" |
| 133 | }} |
| 134 | disabled={selectedAppeal.status === 1} |
| 135 | onClick={handleApprove} |
| 136 | > |
| 137 | 通过 |
| 138 | </button> |
| 139 | <button |
| 140 | style={{ |
| 141 | background: selectedAppeal.status === 1 ? "#bdbdbd" : "#e53935", |
| 142 | color: "#fff", |
| 143 | border: "none", |
| 144 | borderRadius: 8, |
| 145 | padding: "10px 38px", |
| 146 | fontWeight: 600, |
| 147 | fontSize: 18, |
| 148 | cursor: selectedAppeal.status === 1 ? "not-allowed" : "pointer" |
| 149 | }} |
| 150 | disabled={selectedAppeal.status === 1} |
| 151 | onClick={handleReject} |
| 152 | > |
| 153 | 不通过 |
| 154 | </button> |
| 155 | </div> |
| 156 | </div> |
| 157 | </div> |
| 158 | ); |
22301133 | 9e29215 | 2025-06-08 00:34:37 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // 简单PDF预览组件 |
| 162 | function FileViewer({ url }) { |
| 163 | if (!url) return <div>无附件</div>; |
| 164 | if (url.endsWith(".pdf")) { |
| 165 | return ( |
| 166 | <iframe |
| 167 | src={url} |
| 168 | title="PDF预览" |
| 169 | width="100%" |
| 170 | height="400px" |
| 171 | style={{ border: "1px solid #ccc", borderRadius: 8 }} |
| 172 | /> |
| 173 | ); |
| 174 | } |
| 175 | // 这里只做PDF示例,实际可扩展为DOC等 |
| 176 | return <a href={url} target="_blank" rel="noopener noreferrer">下载附件</a>; |
wht | b1e7959 | 2025-06-07 16:03:09 +0800 | [diff] [blame] | 177 | } |