TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 1 | import { getAuthToken } from '../utils/auth' |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 2 | const BASE = 'http://10.126.59.25:5713' // 后端地址 |
| 3 | |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 4 | |
| 5 | |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 6 | /** |
| 7 | * 获取待审核的帖子列表 |
| 8 | * POST /apostlist |
| 9 | * @param {number|string} userId 平台管理员的用户 ID |
| 10 | * @returns Promise<[ {id, title, status}, … ]> |
| 11 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 12 | export async function fetchPosts() { |
| 13 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 14 | const res = await fetch(`${BASE}/apostlist`, { |
| 15 | method: 'POST', |
| 16 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 17 | body: JSON.stringify({ userid }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 18 | }) |
| 19 | if (!res.ok) throw new Error(`fetchPosts: ${res.status}`) |
| 20 | |
| 21 | const json = await res.json() |
| 22 | console.log('fetchPosts response:', json) // debug: inspect shape |
| 23 | |
| 24 | // handle unauthorized |
| 25 | if (json.status === 'error' && json.message === 'Unauthorized') { |
| 26 | throw new Error('Unauthorized') |
| 27 | } |
| 28 | |
| 29 | // normalize response into an array |
| 30 | let list |
| 31 | if (Array.isArray(json)) { |
| 32 | list = json |
| 33 | } else if (Array.isArray(json.data)) { |
| 34 | list = json.data |
| 35 | } else if (Array.isArray(json.posts)) { |
| 36 | list = json.posts |
| 37 | } else if (Array.isArray(json.data?.posts)) { |
| 38 | list = json.data.posts |
| 39 | } else { |
| 40 | list = [] |
| 41 | } |
| 42 | console.log('Normalized post list:', list) // debug: check final shape |
| 43 | return list |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 审核通过 |
| 48 | * POST /areview |
| 49 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 50 | export async function approvePost(postId) { |
| 51 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 52 | const res = await fetch(`${BASE}/areview`, { |
| 53 | method: 'POST', |
| 54 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 55 | body: JSON.stringify({ userid, postid: postId, status: 'published' }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 56 | }) |
| 57 | if (!res.ok) throw new Error(`approvePost: ${res.status}`) |
| 58 | return res.json() |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * 驳回 |
| 63 | * POST /areview |
| 64 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 65 | export async function rejectPost(postId) { |
| 66 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 67 | const res = await fetch(`${BASE}/areview`, { |
| 68 | method: 'POST', |
| 69 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 70 | body: JSON.stringify({ userid, postid: postId, status: 'rejected' }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 71 | }) |
| 72 | if (!res.ok) throw new Error(`rejectPost: ${res.status}`) |
| 73 | return res.json() |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * 获取单个帖子详情 |
| 78 | * POST /agetpost |
| 79 | * @param {number|string} postId 帖子 ID |
| 80 | * @param {number|string} userId 平台管理员的用户 ID |
| 81 | * @returns Promise<{id, title, content, status}> |
| 82 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 83 | export async function fetchPost(postId) { |
| 84 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 85 | const res = await fetch(`${BASE}/agetpost`, { |
| 86 | method: 'POST', |
| 87 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 88 | body: JSON.stringify({ userid, postid: postId }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 89 | }) |
| 90 | if (!res.ok) throw new Error(`fetchPost: ${res.status}`) |
| 91 | return res.json() |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * 获取超级管理员用户列表 |
| 96 | * POST /sgetuserlist |
| 97 | * @param {number|string} userId 平台管理员的用户 ID |
| 98 | * @returns Promise<[ {id, name, role}, … ]> |
| 99 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 100 | export async function fetchUserList() { |
| 101 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 102 | const res = await fetch(`${BASE}/sgetuserlist`, { |
| 103 | method: 'POST', |
| 104 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 105 | body: JSON.stringify({ userid }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 106 | }) |
| 107 | if (!res.ok) throw new Error(`fetchUserList: ${res.status}`) |
| 108 | return res.json() |
| 109 | } |
| 110 | |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 111 | export async function giveAdmin(targetId) { |
| 112 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 113 | const res = await fetch(`${BASE}/sgiveadmin`, { |
| 114 | method: 'POST', |
| 115 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 116 | body: JSON.stringify({ userid, targetid: targetId }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 117 | }) |
| 118 | if (!res.ok) throw new Error(`giveAdmin: ${res.status}`) |
| 119 | return res.json() |
| 120 | } |
| 121 | |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 122 | export async function giveSuperAdmin(targetId) { |
| 123 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 124 | const res = await fetch(`${BASE}/sgivesuperadmin`, { |
| 125 | method: 'POST', |
| 126 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 127 | body: JSON.stringify({ userid, targetid: targetId }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 128 | }) |
| 129 | if (!res.ok) throw new Error(`giveSuperAdmin: ${res.status}`) |
| 130 | return res.json() |
| 131 | } |
| 132 | |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 133 | export async function giveUser(targetId) { |
| 134 | const userid = getAuthToken() |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 135 | const res = await fetch(`${BASE}/sgiveuser`, { |
| 136 | method: 'POST', |
| 137 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 138 | body: JSON.stringify({ userid, targetid: targetId }) |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 139 | }) |
| 140 | if (!res.ok) throw new Error(`giveUser: ${res.status}`) |
| 141 | return res.json() |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * 获取事务日志 |
| 146 | * POST /getrecordlog |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 147 | * @returns Promise<[ {id, user_id, type, content, ip, created_at}, … ]> |
| 148 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 149 | export async function fetchRecordLog() { |
| 150 | const userid = getAuthToken() |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 151 | const res = await fetch(`${BASE}/getrecordlog`, { |
| 152 | method: 'POST', |
| 153 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 154 | body: JSON.stringify({ userid }) |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 155 | }) |
| 156 | if (!res.ok) throw new Error(`fetchRecordLog: ${res.status}`) |
| 157 | const json = await res.json() |
| 158 | console.log('fetchRecordLog response:', json) |
| 159 | if (json.status === 'error' && json.message === 'Unauthorized') { |
| 160 | throw new Error('Unauthorized') |
| 161 | } |
| 162 | let list |
| 163 | if (Array.isArray(json)) { |
| 164 | list = json |
| 165 | } else if (Array.isArray(json.data)) { |
| 166 | list = json.data |
| 167 | } else { |
| 168 | list = [] |
| 169 | } |
| 170 | console.log('Normalized record log list:', list) |
| 171 | return list |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * 获取系统性能消耗数据 |
| 176 | * POST /getsyscost |
| 177 | * @param {number|string} userId 平台管理员的用户 ID |
| 178 | * @returns Promise<[ {id, record_time, endpoint, elapsed_time, cpu_user, cpu_system, memory_rss}, … ]> |
| 179 | */ |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 180 | export async function fetchSysCost() { |
| 181 | const userid = getAuthToken() |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 182 | const res = await fetch(`${BASE}/getsyscost`, { |
| 183 | method: 'POST', |
| 184 | headers: { 'Content-Type': 'application/json' }, |
TRM-coding | 2a8fd60 | 2025-06-19 19:33:16 +0800 | [diff] [blame] | 185 | body: JSON.stringify({ userid }) |
TRM-coding | 85e5c32 | 2025-06-18 19:49:21 +0800 | [diff] [blame] | 186 | }) |
| 187 | if (!res.ok) throw new Error(`fetchSysCost: ${res.status}`) |
| 188 | const json = await res.json() |
| 189 | console.log('fetchSysCost response:', json) |
| 190 | if (json.status === 'error' && json.message === 'Unauthorized') { |
| 191 | throw new Error('Unauthorized') |
| 192 | } |
| 193 | let list |
| 194 | if (Array.isArray(json)) { |
| 195 | list = json |
| 196 | } else if (Array.isArray(json.data)) { |
| 197 | list = json.data |
| 198 | } else { |
| 199 | list = [] |
| 200 | } |
| 201 | console.log('Normalized sys cost list:', list) |
| 202 | return list |
TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame] | 203 | } |