TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { fetchRecordLog } from '../api/posts_trm'; |
| 3 | |
| 4 | function TransactionLogs({ userId }) { |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame^] | 5 | const [allRecords, setAllRecords] = useState([]); |
| 6 | const [currentPage, setCurrentPage] = useState(1); |
| 7 | const [pageSize] = useState(10); |
| 8 | const [loading, setLoading] = useState(false); |
| 9 | |
| 10 | const loadRecords = async () => { |
| 11 | setLoading(true); |
| 12 | try { |
| 13 | const data = await fetchRecordLog(); |
| 14 | setAllRecords(data); |
| 15 | } catch (err) { |
| 16 | console.error('fetchRecordLog error:', err); |
| 17 | } finally { |
| 18 | setLoading(false); |
| 19 | } |
| 20 | }; |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 21 | |
| 22 | useEffect(() => { |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame^] | 23 | loadRecords(); |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 24 | }, [userId]); |
| 25 | |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame^] | 26 | const total = allRecords.length; |
| 27 | const totalPages = Math.ceil(total / pageSize); |
| 28 | |
| 29 | // 计算当前页显示的数据 |
| 30 | const startIndex = (currentPage - 1) * pageSize; |
| 31 | const endIndex = startIndex + pageSize; |
| 32 | const currentRecords = allRecords.slice(startIndex, endIndex); |
| 33 | |
| 34 | const handlePrevPage = () => { |
| 35 | if (currentPage > 1) { |
| 36 | setCurrentPage(currentPage - 1); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | const handleNextPage = () => { |
| 41 | if (currentPage < totalPages) { |
| 42 | setCurrentPage(currentPage + 1); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | const handlePageClick = (page) => { |
| 47 | setCurrentPage(page); |
| 48 | }; |
| 49 | |
| 50 | const renderPageNumbers = () => { |
| 51 | const pages = []; |
| 52 | const maxVisiblePages = 5; |
| 53 | |
| 54 | let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2)); |
| 55 | let endPage = Math.min(totalPages, startPage + maxVisiblePages - 1); |
| 56 | |
| 57 | if (endPage - startPage < maxVisiblePages - 1) { |
| 58 | startPage = Math.max(1, endPage - maxVisiblePages + 1); |
| 59 | } |
| 60 | |
| 61 | for (let i = startPage; i <= endPage; i++) { |
| 62 | pages.push( |
| 63 | <button |
| 64 | key={i} |
| 65 | onClick={() => handlePageClick(i)} |
| 66 | className={`page-btn ${currentPage === i ? 'active' : ''}`} |
| 67 | > |
| 68 | {i} |
| 69 | </button> |
| 70 | ); |
| 71 | } |
| 72 | return pages; |
| 73 | }; |
| 74 | |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 75 | return ( |
| 76 | <section className="dashboard-logs"> |
| 77 | <table className="admin-table"> |
| 78 | <thead> |
| 79 | <tr> |
| 80 | <th>ID</th> |
| 81 | <th>用户ID</th> |
| 82 | <th>类型</th> |
| 83 | <th>内容</th> |
| 84 | <th>IP</th> |
| 85 | <th>创建时间</th> |
| 86 | </tr> |
| 87 | </thead> |
| 88 | <tbody> |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame^] | 89 | {loading ? ( |
| 90 | <tr> |
| 91 | <td colSpan="6" style={{ textAlign: 'center' }}>加载中...</td> |
| 92 | </tr> |
| 93 | ) : currentRecords.length > 0 ? ( |
| 94 | currentRecords.map((r, i) => ( |
| 95 | <tr key={r.id || i}> |
| 96 | <td>{r.id}</td> |
| 97 | <td>{r.user_id}</td> |
| 98 | <td>{r.type}</td> |
| 99 | <td>{r.content}</td> |
| 100 | <td>{r.ip}</td> |
| 101 | <td>{new Date(r.created_at).toLocaleString()}</td> |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 102 | </tr> |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame^] | 103 | )) |
| 104 | ) : ( |
| 105 | <tr> |
| 106 | <td colSpan="6" style={{ textAlign: 'center' }}>暂无数据</td> |
| 107 | </tr> |
| 108 | )} |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 109 | </tbody> |
| 110 | </table> |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame^] | 111 | |
| 112 | {totalPages > 1 && ( |
| 113 | <div className="pagination"> |
| 114 | <button |
| 115 | onClick={handlePrevPage} |
| 116 | disabled={currentPage === 1} |
| 117 | className="page-btn" |
| 118 | > |
| 119 | 上一页 |
| 120 | </button> |
| 121 | |
| 122 | {renderPageNumbers()} |
| 123 | |
| 124 | <button |
| 125 | onClick={handleNextPage} |
| 126 | disabled={currentPage === totalPages} |
| 127 | className="page-btn" |
| 128 | > |
| 129 | 下一页 |
| 130 | </button> |
| 131 | |
| 132 | <span className="page-info"> |
| 133 | 第 {currentPage} 页,共 {totalPages} 页,总计 {total} 条记录 |
| 134 | </span> |
| 135 | </div> |
| 136 | )} |
| 137 | |
| 138 | <style jsx>{` |
| 139 | .pagination { |
| 140 | display: flex; |
| 141 | align-items: center; |
| 142 | justify-content: center; |
| 143 | gap: 8px; |
| 144 | margin-top: 16px; |
| 145 | flex-wrap: wrap; |
| 146 | } |
| 147 | |
| 148 | .page-btn { |
| 149 | padding: 6px 12px; |
| 150 | border: 1px solid #ddd; |
| 151 | background: white; |
| 152 | cursor: pointer; |
| 153 | border-radius: 4px; |
| 154 | transition: all 0.2s; |
| 155 | } |
| 156 | |
| 157 | .page-btn:hover:not(:disabled) { |
| 158 | background: #f5f5f5; |
| 159 | border-color: #999; |
| 160 | } |
| 161 | |
| 162 | .page-btn:disabled { |
| 163 | opacity: 0.5; |
| 164 | cursor: not-allowed; |
| 165 | } |
| 166 | |
| 167 | .page-btn.active { |
| 168 | background: #007bff; |
| 169 | color: white; |
| 170 | border-color: #007bff; |
| 171 | } |
| 172 | |
| 173 | .page-info { |
| 174 | margin-left: 16px; |
| 175 | font-size: 14px; |
| 176 | color: #666; |
| 177 | } |
| 178 | `}</style> |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 179 | </section> |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | export default TransactionLogs; |