diff --git a/backend/db.js b/backend/db.js
index 2bcc470..7c0498c 100644
--- a/backend/db.js
+++ b/backend/db.js
@@ -184,10 +184,15 @@ async function initSchema(connection) {
title VARCHAR(200) NOT NULL,
description TEXT,
options JSON NOT NULL,
+ group_name VARCHAR(100) NOT NULL DEFAULT '',
+ server_name VARCHAR(100) NOT NULL DEFAULT '',
+ start_time DATETIME,
+ end_time DATETIME,
active TINYINT(1) NOT NULL DEFAULT 1,
created_by INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
- INDEX idx_active (active)
+ INDEX idx_active (active),
+ INDEX idx_group_server (group_name, server_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
await createIfNotExists('poll_votes', `CREATE TABLE poll_votes (
@@ -204,6 +209,8 @@ async function initSchema(connection) {
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
description TEXT,
+ group_name VARCHAR(100) NOT NULL DEFAULT '',
+ server_name VARCHAR(100) NOT NULL DEFAULT '',
status ENUM('pending','planned','done') NOT NULL DEFAULT 'pending',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
@@ -217,6 +224,13 @@ async function initSchema(connection) {
INDEX idx_item (item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
+ await createIfNotExists('server_groups', `CREATE TABLE server_groups (
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ group_name VARCHAR(100) NOT NULL,
+ server_name VARCHAR(100) NOT NULL,
+ UNIQUE KEY uk_group_server (group_name, server_name)
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`);
+
await createIfNotExists('audit_logs', `CREATE TABLE audit_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
diff --git a/backend/routes/features.js b/backend/routes/features.js
new file mode 100644
index 0000000..192d87d
--- /dev/null
+++ b/backend/routes/features.js
@@ -0,0 +1,59 @@
+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 { group_name, server_name, sort } = req.query;
+ let orderClause = 'ORDER BY fi.created_at DESC';
+ if (sort === 'votes') orderClause = 'ORDER BY votes_count DESC, fi.created_at DESC';
+
+ const items = await query(`SELECT fi.*,
+ (SELECT COUNT(*) FROM feature_votes WHERE item_id = fi.id) as votes_count,
+ (SELECT COUNT(*) FROM feature_votes WHERE item_id = fi.id AND user_id = ?) as my_vote
+ FROM feature_items fi
+ WHERE (fi.group_name = ? OR ? = '') AND (fi.server_name = ? OR ? = '')
+ ${orderClause}`, [req.user.id, group_name||'', group_name||'', server_name||'', server_name||'']);
+ res.json(items);
+});
+
+router.post('/', authenticate, requireRole('owner'), async (req, res) => {
+ const { title, description, group_name, server_name } = req.body;
+ if (!title) return res.status(400).json({ error: '标题为必填' });
+ await query('INSERT INTO feature_items(title, description, group_name, server_name) VALUES (?,?,?,?)', [title, description||'', group_name||'', server_name||'']);
+ res.status(201).json({ id: 0 });
+});
+
+router.put('/:id', authenticate, requireRole('owner'), async (req, res) => {
+ const fields = {};
+ if (req.body.title) fields.title = req.body.title;
+ if (req.body.description !== undefined) fields.description = req.body.description;
+ if (req.body.status) fields.status = req.body.status;
+ if (req.body.group_name) fields.group_name = req.body.group_name;
+ if (req.body.server_name) fields.server_name = req.body.server_name;
+ if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
+ const sets = Object.keys(fields).map(k => `${k} = ?`).join(', ');
+ await query(`UPDATE feature_items 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 feature_votes WHERE item_id = ?', [req.params.id]);
+ await query('DELETE FROM feature_items WHERE id = ?', [req.params.id]);
+ res.json({ message: '已删除' });
+});
+
+router.post('/:id/vote', authenticate, async (req, res) => {
+ const item = await getRow('SELECT id FROM feature_items WHERE id = ?', [req.params.id]);
+ if (!item) return res.status(404).json({ error: '不存在' });
+ const voted = await getRow('SELECT id FROM feature_votes WHERE item_id = ? AND user_id = ?', [item.id, req.user.id]);
+ if (voted) {
+ await query('DELETE FROM feature_votes WHERE item_id = ? AND user_id = ?', [item.id, req.user.id]);
+ return res.json({ message: '已取消投票' });
+ }
+ await query('INSERT INTO feature_votes(item_id, user_id) VALUES (?,?)', [item.id, req.user.id]);
+ res.json({ message: '投票成功' });
+});
+
+module.exports = router;
diff --git a/backend/routes/polls.js b/backend/routes/polls.js
new file mode 100644
index 0000000..5a8ce4d
--- /dev/null
+++ b/backend/routes/polls.js
@@ -0,0 +1,107 @@
+const express = require('express');
+const { query, getRow } = require('../db');
+const { authenticate, requireRole } = require('../middleware/auth');
+
+const router = express.Router();
+
+router.get('/groups', authenticate, async (req, res) => {
+ res.json(await query('SELECT * FROM server_groups ORDER BY group_name, server_name'));
+});
+
+router.post('/groups', authenticate, requireRole('owner'), async (req, res) => {
+ const { group_name, server_name } = req.body;
+ if (!group_name || !server_name) return res.status(400).json({ error: '分组名和子服名为必填' });
+ await query('INSERT IGNORE INTO server_groups(group_name, server_name) VALUES (?,?)', [group_name, server_name]);
+ res.status(201).json({ message: '已添加' });
+});
+
+router.delete('/groups/:id', authenticate, requireRole('owner'), async (req, res) => {
+ await query('DELETE FROM server_groups WHERE id = ?', [req.params.id]);
+ res.json({ message: '已删除' });
+});
+
+router.get('/active', authenticate, async (req, res) => {
+ const groups = await query('SELECT DISTINCT group_name, server_name FROM server_groups ORDER BY group_name, server_name');
+ const result = {};
+ for (const g of groups) {
+ const polls = await query(`SELECT id, title, description, group_name, server_name, start_time, end_time, active, created_at FROM polls
+ WHERE active = 1 AND group_name = ? AND server_name = ? AND (end_time IS NULL OR end_time > NOW())
+ ORDER BY created_at DESC LIMIT 1`, [g.group_name, g.server_name]);
+ if (polls.length === 0) continue;
+ const p = polls[0];
+ p.options = JSON.parse(p.options || '[]');
+ const rows = await query(`SELECT pv.option_index,
+ SUM(CASE WHEN u.role IN ('owner','admin') THEN 1.5 ELSE 1 END) as total
+ FROM poll_votes pv JOIN users u ON pv.user_id = u.id
+ WHERE pv.poll_id = ? GROUP BY pv.option_index`, [p.id]);
+ p.votes = {};
+ let total = 0;
+ for (const r of rows) { p.votes[r.option_index] = Math.round(r.total * 10) / 10; total += r.total; }
+ p.total = Math.round(total * 10) / 10;
+ const myVote = await getRow('SELECT option_index FROM poll_votes WHERE poll_id = ? AND user_id = ?', [p.id, req.user.id]);
+ p.my_vote = myVote ? myVote.option_index : null;
+ p.voted = !!myVote;
+ if (!result[g.group_name]) result[g.group_name] = [];
+ result[g.group_name].push({ ...p, server_name: g.server_name });
+ }
+ res.json(result);
+});
+
+router.get('/:id', authenticate, requireRole('owner'), async (req, res) => {
+ const p = await getRow('SELECT * FROM polls WHERE id = ?', [req.params.id]);
+ if (!p) return res.status(404).json({ error: '不存在' });
+ res.json(p);
+});
+
+router.post('/', authenticate, requireRole('owner'), async (req, res) => {
+ const { title, description, options, group_name, server_name, start_time, end_time } = req.body;
+ if (!title || !options || !Array.isArray(options) || options.length < 2) return res.status(400).json({ error: '标题和至少2个选项为必填' });
+ if (!group_name || !server_name) return res.status(400).json({ error: '请选择分组和子服' });
+
+ const existing = await getRow(`SELECT id FROM polls WHERE active = 1 AND group_name = ? AND server_name = ? AND (end_time IS NULL OR end_time > NOW())`, [group_name, server_name]);
+ if (existing) return res.status(400).json({ error: '该子服已有进行中的投票' });
+
+ await query(`INSERT INTO polls(title, description, options, group_name, server_name, start_time, end_time, created_by) VALUES (?,?,?,?,?,?,?,?)`,
+ [title, description||'', JSON.stringify(options), group_name, server_name, start_time||null, end_time||null, req.user.id]);
+ res.status(201).json({ message: '创建成功' });
+});
+
+router.put('/:id', authenticate, requireRole('owner'), async (req, res) => {
+ const p = await getRow('SELECT * FROM polls WHERE id = ?', [req.params.id]);
+ if (!p) return res.status(404).json({ error: '投票不存在' });
+ const fields = {};
+ if (req.body.title) fields.title = req.body.title;
+ if (req.body.description !== undefined) fields.description = req.body.description;
+ if (req.body.active !== undefined) fields.active = req.body.active ? 1 : 0;
+ if (req.body.options) fields.options = JSON.stringify(req.body.options);
+ if (req.body.start_time !== undefined) fields.start_time = req.body.start_time || null;
+ if (req.body.end_time !== undefined) fields.end_time = req.body.end_time || null;
+ if (req.body.group_name) fields.group_name = req.body.group_name;
+ if (req.body.server_name) fields.server_name = req.body.server_name;
+ if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' });
+ const sets = Object.keys(fields).map(k => `${k} = ?`).join(', ');
+ await query(`UPDATE polls 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 poll_votes WHERE poll_id = ?', [req.params.id]);
+ await query('DELETE FROM polls WHERE id = ?', [req.params.id]);
+ res.json({ message: '已删除' });
+});
+
+router.post('/:id/vote', authenticate, async (req, res) => {
+ const p = await getRow('SELECT * FROM polls WHERE id = ? AND active = 1', [req.params.id]);
+ if (!p) return res.status(404).json({ error: '投票不存在或已关闭' });
+ if (p.end_time && new Date(p.end_time) < new Date()) return res.status(400).json({ error: '投票已结束' });
+ const voted = await getRow('SELECT id FROM poll_votes WHERE poll_id = ? AND user_id = ?', [p.id, req.user.id]);
+ if (voted) return res.status(400).json({ error: '您已投过票' });
+ const { option_index } = req.body;
+ if (option_index === undefined || option_index === null) return res.status(400).json({ error: '请选择选项' });
+ const opts = JSON.parse(p.options || '[]');
+ if (option_index < 0 || option_index >= opts.length) return res.status(400).json({ error: '无效选项' });
+ await query('INSERT INTO poll_votes(poll_id, user_id, option_index) VALUES (?,?,?)', [req.params.id, req.user.id, option_index]);
+ res.json({ message: '投票成功' });
+});
+
+module.exports = router;
diff --git a/backend/server.js b/backend/server.js
index 710e2a6..ddb6b41 100644
--- a/backend/server.js
+++ b/backend/server.js
@@ -104,6 +104,8 @@ if (isInstalled()) {
app.use('/api/export', methodGuard(['GET']), generalLimiter, require('./routes/export'));
app.use('/api/uploads', methodGuard(['GET']), generalLimiter, require('./routes/uploads'));
app.use('/api/notifications', methodGuard(['GET','POST','PUT','DELETE']), generalLimiter, require('./routes/notifications'));
+ app.use('/api/polls', methodGuard(['GET','POST','PUT','DELETE']), generalLimiter, require('./routes/polls'));
+ app.use('/api/features', methodGuard(['GET','POST','PUT','DELETE']), generalLimiter, require('./routes/features'));
app.use('/api/external', require('./routes/external'));
app.get('/api/verify', (req, res) => res.redirect(`/#/verify?token=${req.query.token}`));
diff --git a/public/index.html b/public/index.html
index 0bda9a2..c3401fd 100644
--- a/public/index.html
+++ b/public/index.html
@@ -69,6 +69,8 @@
+
+