feat: ban list with dashboard widget, owner add/edit bans
This commit is contained in:
44
backend/routes/bans.js
Normal file
44
backend/routes/bans.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const express = require('express');
|
||||
const { query, getRow } = require('../db');
|
||||
const { authenticate, requireRole } = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authenticate, async (req, res) => {
|
||||
const rows = await query('SELECT * FROM bans ORDER BY created_at DESC LIMIT 200');
|
||||
res.json(rows);
|
||||
});
|
||||
|
||||
router.get('/active', authenticate, async (req, res) => {
|
||||
res.json(await query("SELECT * FROM bans WHERE status = 'active' ORDER BY created_at DESC"));
|
||||
});
|
||||
|
||||
router.post('/', authenticate, requireRole('owner'), async (req, res) => {
|
||||
const { player_name, player_uid, reason, type, duration, ticket_id, expires_at } = req.body;
|
||||
if (!player_name) return res.status(400).json({ error: '玩家名为必填' });
|
||||
const r = await query('INSERT INTO bans(player_name, player_uid, reason, type, duration, ticket_id, created_by, expires_at) VALUES (?,?,?,?,?,?,?,?)',
|
||||
[player_name, player_uid||'', reason||'', type||'ban', duration||'', ticket_id||null, req.user.id, expires_at||null]);
|
||||
if (ticket_id) {
|
||||
await query("INSERT INTO responses(ticket_id, user_id, content, is_staff) VALUES (?,?,?,1)",
|
||||
[ticket_id, req.user.id, `【处罚记录】${type==='ban'?'封禁':type==='mute'?'禁言':type==='warn'?'警告':'其他'}: ${player_name}${duration?' 时长:'+duration:''}${reason?' 原因:'+reason:''}`]);
|
||||
}
|
||||
res.status(201).json({ id: r.insertId });
|
||||
});
|
||||
|
||||
router.put('/:id', authenticate, requireRole('owner'), async (req, res) => {
|
||||
const fields = {};
|
||||
if (req.body.status) fields.status = req.body.status;
|
||||
if (req.body.reason) fields.reason = req.body.reason;
|
||||
if (req.body.duration) fields.duration = req.body.duration;
|
||||
if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
|
||||
const sets = Object.keys(fields).map(k => `${k} = ?`).join(', ');
|
||||
await query(`UPDATE bans SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]);
|
||||
res.json({ message: '更新成功' });
|
||||
});
|
||||
|
||||
router.delete('/:id', authenticate, requireRole('owner'), async (req, res) => {
|
||||
await query('DELETE FROM bans WHERE id = ?', [req.params.id]);
|
||||
res.json({ message: '已删除' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user