// ============================================================
// 👤 用户主页
// ============================================================
async function renderUserProfile(username) {
const container = document.getElementById('userProfileContent');
container.innerHTML = Skeleton.userProfile();
try {
// 使用公开接口 /users/profile/:username
const user = await apiFetch('/users/profile/' + encodeURIComponent(username));
if (user.error || !user.id) {
container.innerHTML = '
${escapeHTML(user.username)}
${escapeHTML(user.bio || '这个人很懒,什么都没写')}
📅 加入于 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}
📝 发了 ${posts.length} 个帖子
${user.role === 'admin' ? '🛡️ 管理员' : ''}
${currentUser && currentUser.id !== user.id ? `
${blockStatus.blocked_by_me ? '' : `
`}
${blockStatus.blocked_by_me ? `
已屏蔽该用户:你不再看到 TA 的帖子与回复,TA 也无法给你发私信
` : ''}
` : ''}
${escapeHTML(p.title || '无标题')}
${escapeHTML((p.content || '').substring(0, 100))}${(p.content || '').length > 100 ? '...' : ''}
${time}
`;
});
}
container.innerHTML = sanitizeHTML(html);
container.querySelectorAll('.post-card[data-postid]').forEach(card => {
card.addEventListener('click', function() {
switchToPost(this.dataset.postid);
});
});
const dmBtn = document.getElementById('dmBtn');
if (dmBtn) {
dmBtn.addEventListener('click', function() {
openPrivateChat(user.id, user.username);
});
}
// 屏蔽 / 解除屏蔽。两者都需要确认——屏蔽会影响双方的内容可见性,
// 属于不容易察觉后果的操作。
const blockBtn = document.getElementById('blockBtn');
if (blockBtn) {
blockBtn.addEventListener('click', async function() {
const targetId = this.dataset.userid;
const targetName = this.dataset.username;
const isBlocked = this.dataset.blocked === '1';
const ok = await showConfirm(
isBlocked
? `解除对「${targetName}」的屏蔽?解除后你将继续看到 TA 的帖子和回复,TA 也能再次给你发私信。`
: `确定屏蔽「${targetName}」?屏蔽后你不再看到 TA 的帖子和回复,TA 也无法给你发私信。`,
{
title: isBlocked ? '解除屏蔽' : '屏蔽用户',
confirmText: isBlocked ? '解除屏蔽' : '屏蔽',
danger: !isBlocked,
}
);
if (!ok) return;
try {
if (isBlocked) {
await API.unblockUser(targetId);
showToast('已解除屏蔽', 'success');
} else {
await API.blockUser(targetId);
showToast('已屏蔽该用户', 'success');
}
// 重新渲染以更新按钮状态与提示
renderUserProfile(user.username);
} catch (err) {
showToast(err.message, 'error');
}
});
}
} catch (err) {
container.innerHTML = '