blob: 37acf43858efa5fb0164b75410bbd4091580f25e [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()
131}