fix: no revote, blind result after voting, admin 1.5 vote weight
This commit is contained in:
@@ -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');
|
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) {
|
for (const p of polls) {
|
||||||
p.options = JSON.parse(p.options || '[]');
|
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 = {};
|
p.votes = {};
|
||||||
let total = 0;
|
let total = 0;
|
||||||
for (const r of rows) { p.votes[r.option_index] = r.count; total += r.count; }
|
for (const r of rows) { p.votes[r.option_index] = Math.round(r.total * 10) / 10; total += r.total; }
|
||||||
p.total = 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]);
|
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.my_vote = myVote ? myVote.option_index : null;
|
||||||
|
p.voted = !!myVote;
|
||||||
}
|
}
|
||||||
res.json(polls);
|
res.json(polls);
|
||||||
});
|
});
|
||||||
@@ -54,11 +58,13 @@ router.delete('/:id', authenticate, requireRole('owner'), async (req, res) => {
|
|||||||
router.post('/:id/vote', authenticate, 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]);
|
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) 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;
|
const { option_index } = req.body;
|
||||||
if (option_index === undefined || option_index === null) return res.status(400).json({ error: '请选择选项' });
|
if (option_index === undefined || option_index === null) return res.status(400).json({ error: '请选择选项' });
|
||||||
const opts = JSON.parse(p.options || '[]');
|
const opts = JSON.parse(p.options || '[]');
|
||||||
if (option_index < 0 || option_index >= opts.length) return res.status(400).json({ error: '无效选项' });
|
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: '投票成功' });
|
res.json({ message: '投票成功' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -23,20 +23,22 @@ const PollsPage = {
|
|||||||
renderPoll(p) {
|
renderPoll(p) {
|
||||||
const opts = p.options || [];
|
const opts = p.options || [];
|
||||||
const max = Math.max(1, ...Object.values(p.votes || {}));
|
const max = Math.max(1, ...Object.values(p.votes || {}));
|
||||||
|
const showResult = Auth.isOwner() || !p.voted;
|
||||||
return `<div class="card" id="poll-${p.id}">
|
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-h"><span>${U.esc(p.title)}</span><span class="ts tm">${p.total||0} 票</span></div>
|
||||||
<div class="card-b">
|
<div class="card-b">
|
||||||
${p.description?`<p class="tm mb-4">${U.esc(p.description)}</p>`:''}
|
${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">
|
<div style="display:flex;flex-direction:column;gap:8px">
|
||||||
${opts.map((o,i) => {
|
${opts.map((o,i) => {
|
||||||
const v = p.votes?.[i] || 0;
|
const v = showResult ? (p.votes?.[i] || 0) : 0;
|
||||||
const pct = p.total ? Math.round(v/p.total*100) : 0;
|
const pct = showResult && p.total ? Math.round(v/p.total*100) : 0;
|
||||||
const my = p.my_vote === i;
|
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}">
|
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}">
|
||||||
<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>
|
${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">
|
<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>${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>
|
||||||
</div>`;
|
</div>`;
|
||||||
}).join('')}
|
}).join('')}
|
||||||
@@ -46,6 +48,7 @@ const PollsPage = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
bindVote(p) {
|
bindVote(p) {
|
||||||
|
if (p.voted) return;
|
||||||
const card = document.getElementById('poll-'+p.id);
|
const card = document.getElementById('poll-'+p.id);
|
||||||
if (!card) return;
|
if (!card) return;
|
||||||
card.querySelectorAll('.poll-opt').forEach(el => {
|
card.querySelectorAll('.poll-opt').forEach(el => {
|
||||||
|
|||||||
Reference in New Issue
Block a user