Files
MC_Report/backend/routes/sources.js
canglan c5ceba7cf0 fix: data-action buttons, sources enabled, email notify, ticket detail single-page
- app.js: mount page objects via inline script (CSP blocks eval → data-action buttons dead)
- app.js: add tickets list entry to sidebar nav (admin could not find claimed tickets)
- sources: GET /sources?manage=1 returns enabled field for admin view
- mailer: sendNotifyEmail consumes email-type notification_configs (webhook_url = recipients)
- tickets: trigger notify on created/claimed/transferred/updated/status-change
- notifications: email type shows recipient field, validates email list
- ticket-detail: single-page chat flow (player/staff bubbles) + processing log timeline
- css: chat bubble styles
2026-08-22 20:00:12 +08:00

79 lines
4.0 KiB
JavaScript

/*
* MC Report System
* Copyright (C) 2026 Sea Network Technology Studio
* Author: CangLan <admin@sea-studio.top>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const express = require('express');
const { query, getRow } = require('../db');
const { authenticate, optionalAuth, requireRole } = require('../middleware/auth');
const { logSystem } = require('../logger');
const router = express.Router();
// ---- 来源列表 ----
// 公开(注册页/绑定身份页): 仅启用项, 不含 enabled
// ?manage=1(authenticate owner/admin): 全量含 enabled, 供来源管理页显示/切换状态
router.get('/', optionalAuth, async (req, res) => {
try {
if (req.query.manage === '1' && req.user && ['owner','admin'].includes(req.user.role)) {
return res.json(await query('SELECT code, label, sort_order, enabled FROM sources ORDER BY sort_order, id'));
}
const rows = await query('SELECT code, label, sort_order FROM sources WHERE enabled = 1 ORDER BY sort_order, id');
res.json(rows);
} catch (e) { res.status(500).json({ error: e.message }); }
});
// ---- 管理(owner) ----
router.post('/', authenticate, requireRole('owner'), async (req, res) => {
try {
const { code, label, sort_order } = req.body;
if (!code || !label) return res.status(400).json({ error: 'code 和 label 必填' });
if (!/^[a-z0-9_]{1,20}$/.test(code)) return res.status(400).json({ error: 'code 仅允许小写字母数字下划线(≤20)' });
const dup = await getRow('SELECT id FROM sources WHERE code = ?', [code]);
if (dup) return res.status(400).json({ error: '该来源已存在' });
const r = await query('INSERT INTO sources(code, label, sort_order) VALUES (?,?,?)', [code, label, parseInt(sort_order) || 0]);
await logSystem('info', 'source', `新增来源: ${code}(${label})`);
res.status(201).json({ id: r.insertId, message: '已添加' });
} catch (e) { res.status(500).json({ error: e.message }); }
});
router.put('/:code', authenticate, requireRole('owner'), async (req, res) => {
try {
const { label, enabled, sort_order } = req.body;
const row = await getRow('SELECT id FROM sources WHERE code = ?', [req.params.code]);
if (!row) return res.status(404).json({ error: '来源不存在' });
await query('UPDATE sources SET label = ?, enabled = ?, sort_order = ? WHERE code = ?',
[label || req.params.code, enabled ? 1 : 0, parseInt(sort_order) || 0, req.params.code]);
await logSystem('info', 'source', `更新来源: ${req.params.code}`, { label, enabled: !!enabled });
res.json({ message: '已更新' });
} catch (e) { res.status(500).json({ error: e.message }); }
});
router.delete('/:code', authenticate, requireRole('owner'), async (req, res) => {
try {
// 有关联数据时禁止删除(防误删)
const usedUsers = await getRow('SELECT COUNT(*) as c FROM users WHERE source = ?', [req.params.code]);
const usedIdent = await getRow('SELECT COUNT(*) as c FROM user_identities WHERE source = ?', [req.params.code]);
if (usedUsers.c > 0 || usedIdent.c > 0) return res.status(400).json({ error: `该来源已被 ${usedUsers.c + usedIdent.c} 条身份数据使用, 请先停用` });
await query('DELETE FROM sources WHERE code = ?', [req.params.code]);
await logSystem('info', 'source', `删除来源: ${req.params.code}`);
res.json({ message: '已删除' });
} catch (e) { res.status(500).json({ error: e.message }); }
});
module.exports = router;