blob: 43d71052490af9dd91c51ce0b88ba54ac7842f0b [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}`)
108 return res.json()
109}
110
TRM-coding2a8fd602025-06-19 19:33:16 +0800111export async function giveAdmin(targetId) {
112 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800113 const res = await fetch(`${BASE}/sgiveadmin`, {
114 method: 'POST',
115 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800116 body: JSON.stringify({ userid, targetid: targetId })
TRM-codingd1cbf672025-06-18 15:15:08 +0800117 })
118 if (!res.ok) throw new Error(`giveAdmin: ${res.status}`)
119 return res.json()
120}
121
TRM-coding2a8fd602025-06-19 19:33:16 +0800122export async function giveSuperAdmin(targetId) {
123 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800124 const res = await fetch(`${BASE}/sgivesuperadmin`, {
125 method: 'POST',
126 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800127 body: JSON.stringify({ userid, targetid: targetId })
TRM-codingd1cbf672025-06-18 15:15:08 +0800128 })
129 if (!res.ok) throw new Error(`giveSuperAdmin: ${res.status}`)
130 return res.json()
131}
132
TRM-coding2a8fd602025-06-19 19:33:16 +0800133export async function giveUser(targetId) {
134 const userid = getAuthToken()
TRM-codingd1cbf672025-06-18 15:15:08 +0800135 const res = await fetch(`${BASE}/sgiveuser`, {
136 method: 'POST',
137 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800138 body: JSON.stringify({ userid, targetid: targetId })
TRM-codingd1cbf672025-06-18 15:15:08 +0800139 })
140 if (!res.ok) throw new Error(`giveUser: ${res.status}`)
141 return res.json()
TRM-coding85e5c322025-06-18 19:49:21 +0800142}
143
144/**
145 * 获取事务日志
146 * POST /getrecordlog
TRM-coding85e5c322025-06-18 19:49:21 +0800147 * @returns Promise<[ {id, user_id, type, content, ip, created_at}, … ]>
148 */
TRM-coding2a8fd602025-06-19 19:33:16 +0800149export async function fetchRecordLog() {
150 const userid = getAuthToken()
TRM-coding85e5c322025-06-18 19:49:21 +0800151 const res = await fetch(`${BASE}/getrecordlog`, {
152 method: 'POST',
153 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800154 body: JSON.stringify({ userid })
TRM-coding85e5c322025-06-18 19:49:21 +0800155 })
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-coding2a8fd602025-06-19 19:33:16 +0800180export async function fetchSysCost() {
181 const userid = getAuthToken()
TRM-coding85e5c322025-06-18 19:49:21 +0800182 const res = await fetch(`${BASE}/getsyscost`, {
183 method: 'POST',
184 headers: { 'Content-Type': 'application/json' },
TRM-coding2a8fd602025-06-19 19:33:16 +0800185 body: JSON.stringify({ userid })
TRM-coding85e5c322025-06-18 19:49:21 +0800186 })
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-codingd1cbf672025-06-18 15:15:08 +0800203}