fix: 修复 /space 关注按钮在默认标签页无法点击

- 将顶部关注按钮 JS 从 following/followers 标签页限定块中提取到全局脚本
- 按钮在所有标签页均可点击(之前仅在 following/followers 标签页有效)
- 本人点击关注时 toast 提示"不能关注自己",与赋能按钮行为一致
- 未登录用户点击关注跳转登录页
- 移除标签页限定块中的重复顶部按钮代码
This commit is contained in:
2026-06-03 13:00:12 +08:00
parent 6c9a4414cb
commit 584d7fd146

View File

@ -575,6 +575,55 @@
{{template "layout/footer.html" .}} {{template "layout/footer.html" .}}
<!-- 关注按钮交互 --> <!-- 关注按钮交互 -->
<!-- 顶部关注按钮(所有标签页通用) -->
<script nonce="{{.CSPNonce}}">
(function() {
var topFollowBtn = document.getElementById('space-follow-btn');
if (!topFollowBtn) return;
var topUid = topFollowBtn.getAttribute('data-uid');
var currentUID = parseInt(topFollowBtn.getAttribute('data-current-uid')) || 0;
if (currentUID && parseInt(topUid) !== currentUID) {
fetch('/api/users/' + topUid + '/follow-status')
.then(function(res) { return res.json(); })
.then(function(data) {
if (data && data.success && data.data) {
var s = data.data;
topFollowBtn.textContent = (s.i_follow ? (s.they_follow ? '已互粉' : '已关注') : (s.they_follow ? '回关' : '+ 关注'));
if (s.i_follow) topFollowBtn.classList.add('followed');
}
});
}
topFollowBtn.addEventListener('click', function() {
if (currentUID && parseInt(topUid) === currentUID) {
showToast('不能关注自己', 'warning');
return;
}
if (!currentUID) {
window.location.href = '/auth/login?redirect=/space/' + topUid;
return;
}
var xhr = new XMLHttpRequest();
xhr.open('POST', '/api/users/' + topUid + '/follow', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
try {
var resp = JSON.parse(xhr.responseText);
if (resp.data && resp.data.status) {
var st = resp.data.status;
topFollowBtn.textContent = (st.i_follow ? (st.they_follow ? '已互粉' : '已关注') : (st.they_follow ? '回关' : '+ 关注'));
topFollowBtn.classList.toggle('followed', st.i_follow);
}
} catch(e){}
}
};
xhr.send();
});
})();
</script>
{{if or (eq .ActiveTab "following") (eq .ActiveTab "followers")}} {{if or (eq .ActiveTab "following") (eq .ActiveTab "followers")}}
<script nonce="{{.CSPNonce}}"> <script nonce="{{.CSPNonce}}">
(function() { (function() {
@ -635,51 +684,6 @@
xhr.send(); xhr.send();
}); });
}); });
// 顶部关注按钮
var topFollowBtn = document.getElementById('space-follow-btn');
if (topFollowBtn) {
var topUid = topFollowBtn.getAttribute('data-uid');
if (currentUID && parseInt(topUid) === currentUID) {
topFollowBtn.textContent = '+ 关注';
topFollowBtn.addEventListener('click', function() {
showToast('不能关注自己');
});
} else {
fetch('/api/users/' + topUid + '/follow-status')
.then(function(res) { return res.json(); })
.then(function(data) {
if (data && data.success && data.data) {
var s = data.data;
topFollowBtn.textContent = (s.i_follow ? (s.they_follow ? '已互粉' : '已关注') : (s.they_follow ? '回关' : '+ 关注'));
if (s.i_follow) topFollowBtn.classList.add('followed');
}
});
topFollowBtn.addEventListener('click', function() {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/api/users/' + topUid + '/follow', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
try {
var resp = JSON.parse(xhr.responseText);
if (resp.data && resp.data.status) {
var st = resp.data.status;
topFollowBtn.textContent = (st.i_follow ? (st.they_follow ? '已互粉' : '已关注') : (st.they_follow ? '回关' : '+ 关注'));
topFollowBtn.classList.toggle('followed', st.i_follow);
// 同时更新下方卡片中的对应按钮
cards.forEach(function(c) {
if (c.getAttribute('data-uid') === topUid) {
var cb = c.querySelector('.space-follow-btn'); if(cb) updateBtn(cb, st);
}
});
}
} catch(e){}
}
};
xhr.send();
});
}
}
})(); })();
</script> </script>
{{end}} {{end}}