fix: space关注按钮移除隐藏逻辑,自关注改为toast提示

- 自己的space也显示关注按钮,不再隐藏
- 点击关注自己时toast'不能关注自己',不发请求
- 自己的space下,发消息/更多按钮保持隐藏
This commit is contained in:
2026-06-03 01:13:01 +08:00
parent 58aabb9bf0
commit fb9724faed

View File

@ -35,10 +35,8 @@
<!-- 右:操作按钮 --> <!-- 右:操作按钮 -->
<div class="space-header-actions"> <div class="space-header-actions">
{{if .IsOwnSpace}} <button id="space-follow-btn" class="space-action-btn primary" data-uid="{{.SpaceUser.ID}}" data-current-uid="{{.CurrentUID}}">+ 关注</button>
<!-- 本人无关注按钮 --> {{if not .IsOwnSpace}}
{{else}}
<button id="space-follow-btn" class="space-action-btn primary" data-uid="{{.SpaceUser.ID}}">+ 关注</button>
<button class="space-action-btn">发消息</button> <button class="space-action-btn">发消息</button>
<button class="space-action-btn icon-only" title="更多"></button> <button class="space-action-btn icon-only" title="更多"></button>
{{end}} {{end}}
@ -576,10 +574,11 @@
{{template "layout/footer.html" .}} {{template "layout/footer.html" .}}
<!-- 访客空间:关注按钮交互 --> <!-- 关注按钮交互 -->
{{if and (not .IsOwnSpace) (or (eq .ActiveTab "following") (eq .ActiveTab "followers"))}} {{if or (eq .ActiveTab "following") (eq .ActiveTab "followers")}}
<script nonce="{{.CSPNonce}}"> <script nonce="{{.CSPNonce}}">
(function() { (function() {
var currentUID = {{.CurrentUID}} || 0;
var cards = document.querySelectorAll('.space-user-card'); var cards = document.querySelectorAll('.space-user-card');
if (!cards.length) return; if (!cards.length) return;
@ -605,7 +604,7 @@
if (state === 'following' || state === 'mutual') btn.classList.add('followed'); if (state === 'following' || state === 'mutual') btn.classList.add('followed');
} }
// 批量获取并更新每个卡片关注状态 // 批量获取并更新每个卡片关注状态(仅非自己)
cards.forEach(function(card) { cards.forEach(function(card) {
var btn = card.querySelector('.space-follow-btn'); var btn = card.querySelector('.space-follow-btn');
if (!btn || btn.disabled) return; if (!btn || btn.disabled) return;
@ -618,6 +617,10 @@
}); });
btn.addEventListener('click', function() { btn.addEventListener('click', function() {
if (currentUID && parseInt(targetUid) === currentUID) {
showToast('不能关注自己');
return;
}
btn.classList.add('waiting'); btn.classList.add('waiting');
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('POST', '/api/users/' + targetUid + '/follow', true); xhr.open('POST', '/api/users/' + targetUid + '/follow', true);
@ -637,38 +640,45 @@
var topFollowBtn = document.getElementById('space-follow-btn'); var topFollowBtn = document.getElementById('space-follow-btn');
if (topFollowBtn) { if (topFollowBtn) {
var topUid = topFollowBtn.getAttribute('data-uid'); var topUid = topFollowBtn.getAttribute('data-uid');
fetch('/api/users/' + topUid + '/follow-status') if (currentUID && parseInt(topUid) === currentUID) {
.then(function(res) { return res.json(); }) topFollowBtn.textContent = '+ 关注';
.then(function(data) { topFollowBtn.addEventListener('click', function() {
if (data && data.success && data.data) { showToast('不能关注自己');
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() { } else {
var xhr = new XMLHttpRequest(); fetch('/api/users/' + topUid + '/follow-status')
xhr.open('POST', '/api/users/' + topUid + '/follow', true); .then(function(res) { return res.json(); })
xhr.onreadystatechange = function() { .then(function(data) {
if (xhr.readyState === 4 && xhr.status === 200) { if (data && data.success && data.data) {
try { var s = data.data;
var resp = JSON.parse(xhr.responseText); topFollowBtn.textContent = (s.i_follow ? (s.they_follow ? '已互粉' : '已关注') : (s.they_follow ? '回关' : '+ 关注'));
if (resp.data && resp.data.status) { if (s.i_follow) topFollowBtn.classList.add('followed');
var st = resp.data.status; }
topFollowBtn.textContent = (st.i_follow ? (st.they_follow ? '已互粉' : '已关注') : (st.they_follow ? '回关' : '+ 关注')); });
topFollowBtn.classList.toggle('followed', st.i_follow); topFollowBtn.addEventListener('click', function() {
// 同时更新下方卡片中的对应按钮 var xhr = new XMLHttpRequest();
cards.forEach(function(c) { xhr.open('POST', '/api/users/' + topUid + '/follow', true);
if (c.getAttribute('data-uid') === topUid) { xhr.onreadystatechange = function() {
var cb = c.querySelector('.space-follow-btn'); if(cb) updateBtn(cb, st); if (xhr.readyState === 4 && xhr.status === 200) {
} try {
}); var resp = JSON.parse(xhr.responseText);
} if (resp.data && resp.data.status) {
} catch(e){} var st = resp.data.status;
} topFollowBtn.textContent = (st.i_follow ? (st.they_follow ? '已互粉' : '已关注') : (st.they_follow ? '回关' : '+ 关注'));
}; topFollowBtn.classList.toggle('followed', st.i_follow);
xhr.send(); // 同时更新下方卡片中的对应按钮
}); 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>