fix: no revote, blind result after voting, admin 1.5 vote weight

This commit is contained in:
2026-07-13 22:01:03 +08:00
parent 116fe89ac8
commit 74974864cd
2 changed files with 18 additions and 9 deletions

View File

@@ -13,13 +13,17 @@ router.get('/active', authenticate, async (req, res) => {
const polls = await query('SELECT id, title, description, active, created_at FROM polls WHERE active = 1 ORDER BY created_at DESC');
for (const p of polls) {
p.options = JSON.parse(p.options || '[]');
const rows = await query('SELECT option_index, COUNT(*) as count FROM poll_votes WHERE poll_id = ? GROUP BY option_index', [p.id]);
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] = r.count; total += r.count; }
p.total = total;
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;
}
res.json(polls);
});
@@ -54,11 +58,13 @@ router.delete('/:id', authenticate, requireRole('owner'), async (req, res) => {
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: '投票不存在或已关闭' });
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 (?,?,?) ON DUPLICATE KEY UPDATE option_index = VALUES(option_index)', [req.params.id, req.user.id, option_index]);
await query('INSERT INTO poll_votes(poll_id, user_id, option_index) VALUES (?,?,?)', [req.params.id, req.user.id, option_index]);
res.json({ message: '投票成功' });
});

View File

@@ -23,20 +23,22 @@ const PollsPage = {
renderPoll(p) {
const opts = p.options || [];
const max = Math.max(1, ...Object.values(p.votes || {}));
const showResult = Auth.isOwner() || !p.voted;
return `<div class="card" id="poll-${p.id}">
<div class="card-h"><span>${U.esc(p.title)}</span><span class="ts tm">${p.total||0} 票</span></div>
<div class="card-b">
${p.description?`<p class="tm mb-4">${U.esc(p.description)}</p>`:''}
${p.voted && !showResult ? `<div class="alert alert-s"><i class="fas fa-check-circle"></i> 已投票,结果仅在投票结束后向服主公开</div>` : ''}
<div style="display:flex;flex-direction:column;gap:8px">
${opts.map((o,i) => {
const v = p.votes?.[i] || 0;
const pct = p.total ? Math.round(v/p.total*100) : 0;
const v = showResult ? (p.votes?.[i] || 0) : 0;
const pct = showResult && p.total ? Math.round(v/p.total*100) : 0;
const my = p.my_vote === i;
return `<div class="poll-opt" style="position:relative;overflow:hidden;border:1px solid var(--g200);border-radius:var(--r);cursor:pointer" data-poll="${p.id}" data-idx="${i}">
<div style="position:absolute;top:0;left:0;bottom:0;width:${pct}%;background:${my?'var(--pl)':'var(--g2)'};transition:width .3s;z-index:0"></div>
return `<div class="poll-opt" style="position:relative;overflow:hidden;border:1px solid var(--g200);border-radius:var(--r);${p.voted?'cursor:default':'cursor:pointer'}" data-poll="${p.id}" data-idx="${i}">
${showResult ? `<div style="position:absolute;top:0;left:0;bottom:0;width:${pct}%;background:${my?'var(--pl)':'var(--g2)'};transition:width .3s;z-index:0"></div>` : ''}
<div style="position:relative;z-index:1;display:flex;justify-content:space-between;align-items:center;padding:10px 14px;font-size:13px">
<span>${my?'<i class="fas fa-check" style="color:var(--p)"></i> ':''}${U.esc(o)}</span>
<span class="fb">${v}${pct}%</span>
${showResult ? `<span class="fb">${v}${pct}%</span>` : ''}
</div>
</div>`;
}).join('')}
@@ -46,6 +48,7 @@ const PollsPage = {
},
bindVote(p) {
if (p.voted) return;
const card = document.getElementById('poll-'+p.id);
if (!card) return;
card.querySelectorAll('.poll-opt').forEach(el => {