fix: 域能系统全量检查 — 修复 5 个逻辑/交互 Gap

- 修复 Admin 改名路径未扣域能(非审核路径补充 HasCompletedTask + DeductOnRename)
- 赋能按钮改为 Modal 弹窗(轻赋/重赋选择 + 每日上限提示)
- EnergyInfo API 增加 daily_energize_exp/daily_exp_cap_reached 返回值
- "我的域能" TAB 增加经验值和等级展示
- Energize 事务修复:导出 EnergyStore + WithTx 方法确保 DB 操作原子性
- Studio 写文章页新增 LV0 锁止横幅 + 禁用提交按钮
This commit is contained in:
2026-06-01 12:10:16 +08:00
parent e1d7c318ab
commit 6542d1e055
10 changed files with 325 additions and 44 deletions

View File

@ -43,6 +43,33 @@
<button class="btn btn-primary" id="energizeBtn" data-id="{{$.Post.ID}}">赋能</button>
{{end}}
</div>
<!-- 赋能弹窗 -->
<div class="energize-overlay" id="energizeOverlay" style="display:none">
<div class="energize-modal">
<div class="energize-modal-header">
<h3>为这篇文章注入能量</h3>
<button class="energize-close" id="energizeClose">&times;</button>
</div>
<div class="energize-modal-body">
<p class="energize-hint" id="energizeCapHint" style="display:none">
今日赋能经验奖励已达上限,继续赋能将消耗域能,对方仍会获得激励。
</p>
<div class="energize-options">
<button class="energize-option-btn" id="energizeLight" data-amount="10">
<span class="energize-option-title">轻赋</span>
<span class="energize-option-cost">消耗 1 域能</span>
<span class="energize-option-gain">获得 10 经验</span>
</button>
<button class="energize-option-btn" id="energizeHeavy" data-amount="20">
<span class="energize-option-title">重赋</span>
<span class="energize-option-cost">消耗 2 域能</span>
<span class="energize-option-gain">获得 20 经验</span>
</button>
</div>
</div>
</div>
</div>
{{end}}
</div>
@ -112,45 +139,83 @@
</script>
<script>
// 赋能按钮
// 赋能按钮 + 弹窗
(function() {
var btn = document.getElementById('energizeBtn');
if (!btn) return;
btn.addEventListener('click', function() {
var postId = btn.dataset.id;
var choice = confirm('为这篇文章注入能量\n\n选择「确定」轻赋 1 域能,选择「取消」则打开重赋选项');
var amount = 20; // 重赋 2 域能
if (choice) {
amount = 10; // 轻赋 1 域能
}
var overlay = document.getElementById('energizeOverlay');
var closeBtn = document.getElementById('energizeClose');
var capHint = document.getElementById('energizeCapHint');
var lightBtn = document.getElementById('energizeLight');
var heavyBtn = document.getElementById('energizeHeavy');
if (!btn || !overlay) return;
var csrf = document.querySelector('meta[name="csrf-token"]');
btn.disabled = true;
btn.textContent = '赋能中...';
var postId = btn.dataset.id;
var csrfMeta = document.querySelector('meta[name="csrf-token"]');
var csrfToken = csrfMeta ? csrfMeta.getAttribute('content') : '';
function showOverlay() {
overlay.style.display = 'flex';
}
function hideOverlay() {
overlay.style.display = 'none';
capHint.style.display = 'none';
lightBtn.disabled = false;
heavyBtn.disabled = false;
}
btn.addEventListener('click', function() {
// 先获取域能状态,判断是否需要显示上限提示
fetch('/api/energy/info')
.then(function(r) { return r.json(); })
.then(function(d) {
if (d.success && d.data && d.data.daily_exp_cap_reached) {
capHint.style.display = 'block';
}
showOverlay();
})
.catch(function() { showOverlay(); });
});
closeBtn.addEventListener('click', hideOverlay);
overlay.addEventListener('click', function(e) {
if (e.target === overlay) hideOverlay();
});
function doEnergize(amount) {
lightBtn.disabled = true;
heavyBtn.disabled = true;
fetch('/api/energy/energize', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': csrf ? csrf.getAttribute('content') : ''
'X-CSRF-Token': csrfToken
},
body: JSON.stringify({ post_id: parseInt(postId), amount: amount })
})
.then(function(r) { return r.json(); })
.then(function(d) {
btn.disabled = false;
btn.textContent = '赋能';
if (d.success) {
alert(d.data.message || '赋能成功!');
hideOverlay();
} else {
alert(d.message || '赋能失败');
lightBtn.disabled = false;
heavyBtn.disabled = false;
}
})
.catch(function() {
btn.disabled = false;
btn.textContent = '赋能';
alert('请求失败');
lightBtn.disabled = false;
heavyBtn.disabled = false;
});
}
lightBtn.addEventListener('click', function() {
doEnergize(parseInt(lightBtn.dataset.amount));
});
heavyBtn.addEventListener('click', function() {
doEnergize(parseInt(heavyBtn.dataset.amount));
});
})();
</script>

View File

@ -159,7 +159,7 @@
<div class="settings-card">
<div class="settings-card-header">
<h2>我的域能</h2>
<p class="settings-card-desc">域能流水和余额</p>
<p class="settings-card-desc">域能余额和经验</p>
</div>
<div class="settings-card-body">
<div class="energy-summary">
@ -167,6 +167,14 @@
<span class="energy-label">域能余额</span>
<span class="energy-value" id="energyBalance">加载中...</span>
</div>
<div class="energy-stat">
<span class="energy-label">经验</span>
<span class="energy-value" id="energyExp">加载中...</span>
</div>
<div class="energy-stat">
<span class="energy-label">等级</span>
<span class="energy-value" id="energyLevel">加载中...</span>
</div>
</div>
<div class="energy-log-list" id="energyLogList">
<div class="energy-log-empty">加载中...</div>
@ -899,6 +907,8 @@
// ===== 域能 Tab =====
(function () {
var balanceEl = document.getElementById('energyBalance');
var expEl = document.getElementById('energyExp');
var levelEl = document.getElementById('energyLevel');
var logListEl = document.getElementById('energyLogList');
if (!balanceEl || !logListEl) return;
@ -914,6 +924,25 @@
})
.catch(function () { balanceEl.textContent = '获取失败'; });
// 获取经验和等级
if (expEl && levelEl) {
fetch('/api/level/info')
.then(function (r) { return r.json(); })
.then(function (d) {
if (d.success && d.data) {
expEl.textContent = d.data.exp;
levelEl.textContent = 'Lv' + d.data.level;
} else {
expEl.textContent = '获取失败';
levelEl.textContent = '获取失败';
}
})
.catch(function () {
expEl.textContent = '获取失败';
levelEl.textContent = '获取失败';
});
}
// 获取域能流水
fetch('/api/energy/logs?days=7&page=1&page_size=50')
.then(function (r) { return r.json(); })

View File

@ -20,6 +20,16 @@
</div>
{{end}}
{{if and (not .Post) (lt .Exp 1)}}
<div class="studio-locked-banner">
<div class="locked-banner-icon">🔒</div>
<div>
<strong>Lv1 解锁投稿</strong>
<p>完成首次任务(设置用户名、上传头像)或参与社区互动以获取经验。</p>
</div>
</div>
{{end}}
<form id="postForm" class="editor-form">
<input type="hidden" id="postId" value="{{if .Post}}{{.Post.ID}}{{end}}">
{{if .Post}}
@ -71,6 +81,8 @@
<span class="status-spacer"></span>
{{if and .Post .Post.IsLocked}}
<span class="locked-submit-hint">🔒 此文已锁定,无法修改</span>
{{else if and (not .Post) (lt .Exp 1)}}
<span class="locked-submit-hint">🔒 Lv1 解锁投稿</span>
{{else}}
<button type="submit" class="btn btn-primary btn-submit">{{if .Post}}保存修改{{else}}发布帖子{{end}}</button>
{{end}}

View File

@ -603,3 +603,113 @@
border-radius: 12px;
white-space: nowrap;
}
/* ========== 赋能弹窗 ========== */
.energize-overlay {
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0, 0, 0, 0.45);
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
}
.energize-modal {
background: #fff;
border-radius: 12px;
width: 360px;
max-width: 92vw;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
overflow: hidden;
}
.energize-modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 24px 16px;
}
.energize-modal-header h3 {
margin: 0;
font-size: 18px;
font-weight: 600;
color: #111827;
}
.energize-close {
background: none;
border: none;
font-size: 24px;
color: #9ca3af;
cursor: pointer;
padding: 0;
line-height: 1;
}
.energize-close:hover {
color: #374151;
}
.energize-modal-body {
padding: 0 24px 24px;
}
.energize-hint {
background: #fff7ed;
border: 1px solid #fed7aa;
border-radius: 8px;
padding: 10px 14px;
font-size: 13px;
color: #c2410c;
margin-bottom: 16px;
line-height: 1.5;
}
.energize-options {
display: flex;
gap: 12px;
}
.energize-option-btn {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
padding: 16px 12px;
border: 2px solid #e5e7eb;
border-radius: 10px;
background: #fff;
cursor: pointer;
transition: border-color 0.2s, background 0.2s;
}
.energize-option-btn:hover {
border-color: #6366f1;
background: #f5f3ff;
}
.energize-option-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.energize-option-title {
font-size: 16px;
font-weight: 600;
color: #111827;
margin-bottom: 4px;
}
.energize-option-cost {
font-size: 13px;
color: #6b7280;
}
.energize-option-gain {
font-size: 12px;
color: #059669;
margin-top: 2px;
font-weight: 500;
}