blob: d6303a7e49953ed0f9e17408f35f0487d5fd17f6 [file] [log] [blame]
TRM-coding2a8fd602025-06-19 19:33:16 +08001import { getAuthToken } from '../utils/auth'
TRM-codingd1cbf672025-06-18 15:15:08 +08002const BASE = 'http://10.126.59.25:5713' // 后端地址
3
TRM-coding2a8fd602025-06-19 19:33:16 +08004
5
TRM-codingd1cbf672025-06-18 15:15:08 +08006/**
7 * 获取待审核的帖子列表
8 * POST /apostlist
9 * @param {number|string} userId 平台管理员的用户 ID
10 * @returns Promise<[ {id, title, status}, … ]>
11 */
TRM-coding2a8fd602025-06-19 19:33:16 +080012export async function fetchPosts() {
13 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +080014 const res = await fetch(`${BASE}/apostlist`, {
15 method: 'POST',
16 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +080017 body: JSON.stringify({ userid })
TRM-codingd1cbf672025-06-18 15:15:08 +080018 })
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-coding2a8fd602025-06-19 19:33:16 +080050export async function approvePost(postId) {
51 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +080052 const res = await fetch(`${BASE}/areview`, {
53 method: 'POST',
54 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +080055 body: JSON.stringify({ userid, postid: postId, status: 'published' })
TRM-codingd1cbf672025-06-18 15:15:08 +080056 })
57 if (!res.ok) throw new Error(`approvePost: ${res.status}`)
58 return res.json()
59}
60
61/**
62 * 驳回
63 * POST /areview
64 */
TRM-coding2a8fd602025-06-19 19:33:16 +080065export async function rejectPost(postId) {
66 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +080067 const res = await fetch(`${BASE}/areview`, {
68 method: 'POST',
69 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +080070 body: JSON.stringify({ userid, postid: postId, status: 'rejected' })
TRM-codingd1cbf672025-06-18 15:15:08 +080071 })
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-coding2a8fd602025-06-19 19:33:16 +080083export async function fetchPost(postId) {
84 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +080085 const res = await fetch(`${BASE}/agetpost`, {
86 method: 'POST',
87 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +080088 body: JSON.stringify({ userid, postid: postId })
TRM-codingd1cbf672025-06-18 15:15:08 +080089 })
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-coding2a8fd602025-06-19 19:33:16 +0800100export async function fetchUserList() {
101 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800102 const res = await fetch(`${BASE}/sgetuserlist`, {
103 method: 'POST',
104 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800105 body: JSON.stringify({ userid })
TRM-codingd1cbf672025-06-18 15:15:08 +0800106 })
107 if (!res.ok) throw new Error(`fetchUserList: ${res.status}`)
trm9984ee52025-06-20 15:16:56 +0000108
109 const json = await res.json()
110 console.log('fetchUserList response:', json)
111
112 // handle unauthorized
113 if (json.status === 'error' && json.message === 'Unauthorized') {
114 throw new Error('Unauthorized')
115 }
116
117 return json
TRM-codingd1cbf672025-06-18 15:15:08 +0800118}
119
TRM-coding2a8fd602025-06-19 19:33:16 +0800120export async function giveAdmin(targetId) {
121 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800122 const res = await fetch(`${BASE}/sgiveadmin`, {
123 method: 'POST',
124 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800125 body: JSON.stringify({ userid, targetid: targetId })
TRM-codingd1cbf672025-06-18 15:15:08 +0800126 })
127 if (!res.ok) throw new Error(`giveAdmin: ${res.status}`)
128 return res.json()
129}
130
TRM-coding2a8fd602025-06-19 19:33:16 +0800131export async function giveSuperAdmin(targetId) {
132 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800133 const res = await fetch(`${BASE}/sgivesuperadmin`, {
134 method: 'POST',
135 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800136 body: JSON.stringify({ userid, targetid: targetId })
TRM-codingd1cbf672025-06-18 15:15:08 +0800137 })
138 if (!res.ok) throw new Error(`giveSuperAdmin: ${res.status}`)
139 return res.json()
140}
141
TRM-coding2a8fd602025-06-19 19:33:16 +0800142export async function giveUser(targetId) {
143 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800144 const res = await fetch(`${BASE}/sgiveuser`, {
145 method: 'POST',
146 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800147 body: JSON.stringify({ userid, targetid: targetId })
TRM-codingd1cbf672025-06-18 15:15:08 +0800148 })
149 if (!res.ok) throw new Error(`giveUser: ${res.status}`)
150 return res.json()
TRM-coding85e5c322025-06-18 19:49:21 +0800151}
152
153/**
154 * 获取事务日志
155 * POST /getrecordlog
TRM-coding85e5c322025-06-18 19:49:21 +0800156 * @returns Promise<[ {id, user_id, type, content, ip, created_at}, … ]>
157 */
TRM-coding2a8fd602025-06-19 19:33:16 +0800158export async function fetchRecordLog() {
159 const userid = getAuthToken()
TRM-coding85e5c322025-06-18 19:49:21 +0800160 const res = await fetch(`${BASE}/getrecordlog`, {
161 method: 'POST',
162 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800163 body: JSON.stringify({ userid })
TRM-coding85e5c322025-06-18 19:49:21 +0800164 })
165 if (!res.ok) throw new Error(`fetchRecordLog: ${res.status}`)
166 const json = await res.json()
167 console.log('fetchRecordLog response:', json)
168 if (json.status === 'error' && json.message === 'Unauthorized') {
169 throw new Error('Unauthorized')
170 }
171 let list
172 if (Array.isArray(json)) {
173 list = json
174 } else if (Array.isArray(json.data)) {
175 list = json.data
176 } else {
177 list = []
178 }
179 console.log('Normalized record log list:', list)
180 return list
181}
182
183/**
184 * 获取系统性能消耗数据
185 * POST /getsyscost
186 * @param {number|string} userId 平台管理员的用户 ID
187 * @returns Promise<[ {id, record_time, endpoint, elapsed_time, cpu_user, cpu_system, memory_rss}, … ]>
188 */
TRM-coding2a8fd602025-06-19 19:33:16 +0800189export async function fetchSysCost() {
190 const userid = getAuthToken()
TRM-coding85e5c322025-06-18 19:49:21 +0800191 const res = await fetch(`${BASE}/getsyscost`, {
192 method: 'POST',
193 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800194 body: JSON.stringify({ userid })
TRM-coding85e5c322025-06-18 19:49:21 +0800195 })
196 if (!res.ok) throw new Error(`fetchSysCost: ${res.status}`)
197 const json = await res.json()
198 console.log('fetchSysCost response:', json)
199 if (json.status === 'error' && json.message === 'Unauthorized') {
200 throw new Error('Unauthorized')
201 }
202 let list
203 if (Array.isArray(json)) {
204 list = json
205 } else if (Array.isArray(json.data)) {
206 list = json.data
207 } else {
208 list = []
209 }
210 console.log('Normalized sys cost list:', list)
211 return list
TRM-codingd1cbf672025-06-18 15:15:08 +0800212}