Update server.js
This commit is contained in:
1 parent
cbb15df923
commit
c563e2ed55
1 file changed
+21
-1
@@ -222,7 +222,7 @@ app.get('/api/auth/me', auth, async (req, res) => {
|
|||||||
// 用户管理(支持分页和搜索)
|
// 用户管理(支持分页和搜索)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
// 获取用户列表(支持分页和按用户名搜索)
|
// 获取用户列表(支持分页和按用户名搜索)- 管理员专用
|
||||||
app.get('/api/users', auth, admin, async (req, res) => {
|
app.get('/api/users', auth, admin, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const page = parseInt(req.query.page) || 1;
|
const page = parseInt(req.query.page) || 1;
|
||||||
@@ -270,6 +270,26 @@ app.get('/api/users', auth, admin, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 用户公开接口(无需登录)- 用于用户主页
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
// 获取单个用户公开信息(无需登录)
|
||||||
|
app.get('/api/users/profile/:username', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const r = await pool.query(
|
||||||
|
'SELECT id, username, avatar_url, bio, role, signature, created_at FROM users WHERE username = $1',
|
||||||
|
[req.params.username]
|
||||||
|
);
|
||||||
|
if (r.rows.length === 0) {
|
||||||
|
return res.status(404).json({ error: '用户不存在' });
|
||||||
|
}
|
||||||
|
res.json(r.rows[0]);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: '服务器错误' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 修改用户角色(管理员)
|
// 修改用户角色(管理员)
|
||||||
app.put('/api/users/:id/role', auth, admin, async (req, res) => {
|
app.put('/api/users/:id/role', auth, admin, async (req, res) => {
|
||||||
const { role } = req.body;
|
const { role } = req.body;
|
||||||
|
|||||||
Reference in new issue
Block a user