feat: MC Report System - MySQL + Express + Vanilla JS SPA
This commit is contained in:
46
backend/routes/notifications.js
Normal file
46
backend/routes/notifications.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const express = require('express');
|
||||
const { query, getRow } = require('../db');
|
||||
const { authenticate, requireRole } = require('../middleware/auth');
|
||||
const { sendWebhook } = require('../webhook');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
res.json(await query('SELECT * FROM notification_configs ORDER BY id'));
|
||||
});
|
||||
|
||||
router.post('/', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
const { name, type, webhook_url, events } = req.body;
|
||||
if (!name||!type) return res.status(400).json({ error: '名称和类型为必填' });
|
||||
if (type==='webhook' && !webhook_url) return res.status(400).json({ error: 'Webhook地址必填' });
|
||||
const r = await query('INSERT INTO notification_configs(name, type, webhook_url, events) VALUES (?,?,?,?)', [name, type, webhook_url||'', events||'all']);
|
||||
await query("INSERT INTO audit_logs(user_id, username, action, entity_type, entity_id, details) VALUES (?,?,?,?,?,?)", [req.user.id, req.user.username, 'create_notification', 'notification_config', r.insertId, `创建通知: ${name}`]);
|
||||
res.status(201).json({ id: r.insertId, message: '创建成功' });
|
||||
});
|
||||
|
||||
router.put('/:id', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
const cfg = await getRow('SELECT * FROM notification_configs WHERE id = ?', [req.params.id]);
|
||||
if (!cfg) return res.status(404).json({ error: '配置不存在' });
|
||||
const fields = {};
|
||||
if (req.body.name) fields.name = req.body.name;
|
||||
if (req.body.webhook_url !== undefined) fields.webhook_url = req.body.webhook_url;
|
||||
if (req.body.events) fields.events = req.body.events;
|
||||
if (req.body.active !== undefined) fields.active = req.body.active?1:0;
|
||||
if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
|
||||
const sets = Object.keys(fields).map(k=>`${k}=?`).join(',');
|
||||
await query(`UPDATE notification_configs SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]);
|
||||
res.json({ message: '更新成功' });
|
||||
});
|
||||
|
||||
router.delete('/:id', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
await query('DELETE FROM notification_configs WHERE id = ?', [req.params.id]);
|
||||
res.json({ message: '已删除' });
|
||||
});
|
||||
|
||||
router.post('/:id/test', authenticate, requireRole('owner','admin'), async (req, res) => {
|
||||
const cfg = await getRow('SELECT * FROM notification_configs WHERE id = ?', [req.params.id]);
|
||||
if (!cfg||!cfg.webhook_url) return res.status(404).json({ error: '配置不存在' });
|
||||
sendWebhook('ticket_created', { ticket_id:'TEST', ticket_title:'测试通知', ticket_type:'测试', reporter_game_name:'TestPlayer', reporter_game_uid:'TEST001', ticket_reason:'这是一条测试消息' }).then(() => res.json({ message:'测试消息已发送' })).catch(e => res.status(500).json({ error:e.message }));
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user