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