feat: feature voting (PCL-style), poll owner edit/delete, blind polls for all

This commit is contained in:
2026-07-14 01:17:56 +08:00
parent 74974864cd
commit ee2f33c61f
8 changed files with 195 additions and 8 deletions

View File

@@ -23,9 +23,11 @@ const PollsPage = {
renderPoll(p) {
const opts = p.options || [];
const max = Math.max(1, ...Object.values(p.votes || {}));
const showResult = Auth.isOwner() || !p.voted;
const showResult = !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-h"><span>${U.esc(p.title)}</span><span class="ts tm">${p.total||0} 票</span>
${Auth.isOwner() ? `<span><button class="btn btn-o btn-sm" onclick="PollsPage.showEdit(${p.id})"><i class="fas fa-edit"></i></button></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>` : ''}
@@ -74,11 +76,33 @@ const PollsPage = {
e.preventDefault();
const opts = document.getElementById('pl-opts').value.split('\n').map(s=>s.trim()).filter(s=>s);
if (opts.length < 2) return alert('至少需要2个选项');
try { await API.post('/polls', { title: document.getElementById('pl-title').value, description: document.getElementById('pl-desc').value, options: opts }); App.closeModal(); this.load(); } catch (ex) { alert(ex.message); }
};
},
async showEdit(id) {
U.modal('编辑投票', '<div class="loading"><i class="fas fa-spinner"></i></div>');
const p = await API.get('/polls/' + id);
document.getElementById('modal-body').innerHTML = `
<form id="pe-form">
<div class="form-group"><label>状态</label><select id="pe-active"><option value="1" ${p.active?'selected':''}>启用</option><option value="0" ${!p.active?'selected':''}>关闭</option></select></div>
<div class="form-group"><label>标题</label><input id="pe-title" value="${U.esc(p.title)}"></div>
<div class="form-group"><label>选项 (每行一个)</label><textarea id="pe-opts" rows="4">${U.esc((JSON.parse(p.options||'[]')).join('\n'))}</textarea></div>
<div class="modal-f"><button type="button" class="btn btn-o" onclick="App.closeModal()">取消</button><button type="button" class="btn btn-d" onclick="PollsPage.deletePoll(${id})"><i class="fas fa-trash"></i></button><button type="submit" class="btn btn-p">保存</button></div>
</form>
`;
document.getElementById('pe-form').onsubmit = async e => {
e.preventDefault();
const opts = document.getElementById('pe-opts').value.split('\n').map(s=>s.trim()).filter(s=>s);
try {
await API.post('/polls', { title: document.getElementById('pl-title').value, description: document.getElementById('pl-desc').value, options: opts });
App.closeModal();
this.load();
await API.put('/polls/'+id, { title: document.getElementById('pe-title').value, active: document.getElementById('pe-active').value==='1', options: opts.length>=2?opts:undefined });
App.closeModal(); this.load();
} catch (ex) { alert(ex.message); }
};
},
async deletePoll(id) {
if (!await U.confirm('确认删除此投票?')) return;
try { await API.req('DELETE', '/polls/'+id); App.closeModal(); this.load(); } catch(ex){alert(ex.message);}
}
};