Files
tech-forum/frontend/topic.html
hubian e7ebc3a4d6 feat: 添加浏览器标签图标favicon
- 创建 favicon.svg(渐变背景 + </>代码符号)
- 所有页面添加 favicon 链接
2026-04-12 17:56:25 +08:00

399 lines
18 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<title>工具分享主题 - 技术论坛</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
<style>
.gradient-bg { background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%); }
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<!-- 导航栏 -->
<nav class="bg-white border-b border-gray-100 sticky top-0 z-50">
<div class="max-w-5xl mx-auto px-4">
<div class="flex items-center justify-between h-16">
<a href="/" class="text-gray-600 hover:text-gray-800 flex items-center gap-1">
<i class="ri-arrow-left-line"></i> 返回首页
</a>
<div id="userArea"></div>
</div>
</div>
</nav>
<main class="max-w-5xl mx-auto px-4 py-8">
<!-- 主题信息 -->
<div id="topicHeader" class="bg-white rounded-lg border border-gray-100 p-6 mb-6">
<div class="text-center py-8 text-gray-500">加载中...</div>
</div>
<!-- Tab切换 -->
<div class="bg-white rounded-lg border border-gray-100 mb-6">
<div class="flex border-b border-gray-100">
<button onclick="showTab('questions')" id="tab-questions" class="flex-1 py-3 text-blue-600 border-b-2 border-blue-600 font-medium">
问题讨论
</button>
<button onclick="showTab('subtopics')" id="tab-subtopics" class="flex-1 py-3 text-gray-500 hover:text-gray-700">
子主题
</button>
</div>
</div>
<!-- 问题列表 -->
<div id="questionsPanel">
<div class="flex justify-between items-center mb-4">
<h3 class="font-medium text-gray-800">问题列表</h3>
<button onclick="showAskModal()" class="px-4 py-2 gradient-bg text-white rounded-lg text-sm">
<i class="ri-question-line mr-1"></i> 提问
</button>
</div>
<div id="questionsList" class="space-y-4"></div>
</div>
<!-- 子主题列表 -->
<div id="subtopicsPanel" class="hidden">
<div class="flex justify-between items-center mb-4">
<h3 class="font-medium text-gray-800">子主题</h3>
<button onclick="showSubtopicModal()" class="px-4 py-2 gradient-bg text-white rounded-lg text-sm">
<i class="ri-add-line mr-1"></i> 添加子主题
</button>
</div>
<div id="subtopicsList" class="space-y-4"></div>
</div>
</main>
<!-- 提问弹窗 -->
<div id="askModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center p-4">
<div class="bg-white rounded-xl w-full max-w-lg">
<div class="p-4 border-b flex justify-between items-center">
<h3 class="font-bold">提出问题</h3>
<button onclick="closeAskModal()" class="text-gray-400"><i class="ri-close-line text-xl"></i></button>
</div>
<div class="p-4">
<input type="text" id="questionTitle" placeholder="问题标题"
class="w-full px-4 py-2 border border-gray-200 rounded-lg mb-3 focus:ring-2 focus:ring-blue-500">
<textarea id="questionContent" rows="4" placeholder="详细描述你的问题..."
class="w-full px-4 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
<button onclick="submitQuestion()" class="mt-3 w-full py-2 gradient-bg text-white rounded-lg">
提交问题
</button>
</div>
</div>
</div>
<!-- 子主题弹窗 -->
<div id="subtopicModal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center p-4">
<div class="bg-white rounded-xl w-full max-w-lg">
<div class="p-4 border-b flex justify-between items-center">
<h3 class="font-bold">添加子主题</h3>
<button onclick="closeSubtopicModal()" class="text-gray-400"><i class="ri-close-line text-xl"></i></button>
</div>
<div class="p-4">
<input type="text" id="subtopicTitle" placeholder="子主题标题"
class="w-full px-4 py-2 border border-gray-200 rounded-lg mb-3 focus:ring-2 focus:ring-blue-500">
<textarea id="subtopicContent" rows="4" placeholder="详细内容..."
class="w-full px-4 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500"></textarea>
<button onclick="submitSubtopic()" class="mt-3 w-full py-2 gradient-bg text-white rounded-lg">
提交
</button>
</div>
</div>
</div>
<script>
let currentUser = null;
let currentTopicId = null;
let topicData = null;
// 获取主题ID
const pathParts = window.location.pathname.split('/');
currentTopicId = pathParts[2];
document.addEventListener('DOMContentLoaded', () => {
checkLogin();
loadTopic();
});
async function checkLogin() {
const token = localStorage.getItem('token');
const user = localStorage.getItem('user');
if (token && user) {
currentUser = JSON.parse(user);
}
}
async function loadTopic() {
const res = await fetch(`/api/topics/${currentTopicId}`);
topicData = await res.json();
if (topicData.error) {
document.getElementById('topicHeader').innerHTML = `
<div class="p-6 text-center text-gray-500">${topicData.error}</div>
`;
return;
}
// 渲染主题头部
document.getElementById('topicHeader').innerHTML = `
<div class="flex items-start gap-4">
<span class="text-5xl">${topicData.icon}</span>
<div class="flex-1">
<h1 class="text-2xl font-bold text-gray-900 mb-2">${topicData.name}</h1>
<p class="text-gray-600 mb-4">${topicData.description || '暂无描述'}</p>
<div class="flex items-center gap-4 text-sm text-gray-500">
<span><i class="ri-user-line mr-1"></i> 创建者: ${topicData.author.username}</span>
<span><i class="ri-question-line mr-1"></i> ${topicData.questions.length} 问题</span>
<span><i class="ri-heart-line mr-1"></i> ${topicData.followers} 关注</span>
</div>
</div>
<button onclick="followTopic()" id="followBtn" class="px-4 py-2 border border-blue-500 text-blue-500 rounded-lg hover:bg-blue-50">
<i class="ri-heart-line mr-1"></i> 关注
</button>
</div>
`;
renderQuestions(topicData.questions);
renderSubtopics(topicData.sub_topics);
}
function renderQuestions(questions) {
const container = document.getElementById('questionsList');
if (!questions || questions.length === 0) {
container.innerHTML = '<p class="text-center text-gray-500 py-8">暂无问题</p>';
return;
}
container.innerHTML = questions.map(q => `
<div class="bg-white rounded-lg border border-gray-100 p-4">
<div class="flex items-start gap-3">
<div class="text-center min-w-[60px]">
<div class="text-lg font-bold text-gray-800">${q.answers.length}</div>
<div class="text-xs text-gray-500">回答</div>
</div>
<div class="flex-1">
<h4 class="font-medium text-gray-800 hover:text-blue-600 cursor-pointer" onclick="toggleAnswers('${q.id}')">
${q.title}
</h4>
${q.content ? `<p class="text-sm text-gray-500 mt-1">${q.content.substring(0, 100)}...</p>` : ''}
<div class="flex items-center gap-4 mt-2 text-xs text-gray-400">
<span>${q.author.username}</span>
<span>${new Date(q.created_at).toLocaleDateString()}</span>
<span><i class="ri-eye-line"></i> ${q.views}</span>
</div>
<!-- 回答列表 -->
<div id="answers-${q.id}" class="hidden mt-4 pt-4 border-t border-gray-100 space-y-3">
${q.answers.length > 0 ? q.answers.map(a => `
<div class="p-3 bg-gray-50 rounded">
<div class="flex items-center gap-2 mb-2">
<img src="${a.author.avatar}" class="w-6 h-6 rounded-full">
<span class="text-sm font-medium">${a.author.username}</span>
</div>
<p class="text-sm text-gray-700">${a.content}</p>
</div>
`).join('') : '<p class="text-sm text-gray-500">暂无回答</p>'}
<!-- 回答输入 -->
<div class="flex gap-2 mt-3">
<input type="text" id="answer-${q.id}" placeholder="写下你的回答..."
class="flex-1 px-3 py-2 border border-gray-200 rounded text-sm focus:ring-2 focus:ring-blue-500">
<button onclick="submitAnswer('${q.id}')" class="px-3 py-2 bg-blue-500 text-white rounded text-sm">
回答
</button>
</div>
</div>
</div>
</div>
</div>
`).join('');
}
function renderSubtopics(subtopics) {
const container = document.getElementById('subtopicsList');
if (!subtopics || subtopics.length === 0) {
container.innerHTML = '<p class="text-center text-gray-500 py-8">暂无子主题</p>';
return;
}
container.innerHTML = subtopics.map(st => `
<div class="bg-white rounded-lg border border-gray-100 p-4">
<h4 class="font-medium text-gray-800 mb-2">${st.title}</h4>
<p class="text-sm text-gray-600">${st.content || ''}</p>
<div class="flex items-center gap-4 mt-3 text-xs text-gray-400">
<span>${st.author.username}</span>
<span>${new Date(st.created_at).toLocaleDateString()}</span>
</div>
</div>
`).join('');
}
function showTab(tab) {
document.getElementById('questionsPanel').classList.toggle('hidden', tab !== 'questions');
document.getElementById('subtopicsPanel').classList.toggle('hidden', tab !== 'subtopics');
document.querySelectorAll('[id^="tab-"]').forEach(t => {
t.classList.remove('text-blue-600', 'border-b-2', 'border-blue-600', 'font-medium');
t.classList.add('text-gray-500');
});
document.getElementById(`tab-${tab}`).classList.remove('text-gray-500');
document.getElementById(`tab-${tab}`).classList.add('text-blue-600', 'border-b-2', 'border-blue-600', 'font-medium');
}
function toggleAnswers(questionId) {
document.getElementById(`answers-${questionId}`).classList.toggle('hidden');
}
function showAskModal() {
if (!currentUser) {
alert('请先登录');
return;
}
document.getElementById('askModal').classList.remove('hidden');
document.getElementById('askModal').classList.add('flex');
}
function closeAskModal() {
document.getElementById('askModal').classList.add('hidden');
document.getElementById('askModal').classList.remove('flex');
}
async function submitQuestion() {
const title = document.getElementById('questionTitle').value.trim();
const content = document.getElementById('questionContent').value.trim();
if (!title) {
alert('请输入问题标题');
return;
}
try {
const res = await fetch(`/api/topics/${currentTopicId}/question`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title, content })
});
const data = await res.json();
if (data.success) {
closeAskModal();
loadTopic();
}
} catch (err) {
alert('网络错误');
}
}
async function submitAnswer(questionId) {
const input = document.getElementById(`answer-${questionId}`);
const content = input.value.trim();
if (!content) {
alert('请输入回答内容');
return;
}
try {
const res = await fetch(`/api/topics/${currentTopicId}/question/${questionId}/answer`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ content })
});
const data = await res.json();
if (data.success) {
input.value = '';
loadTopic();
}
} catch (err) {
alert('网络错误');
}
}
function showSubtopicModal() {
if (!currentUser) {
alert('请先登录');
return;
}
document.getElementById('subtopicModal').classList.remove('hidden');
document.getElementById('subtopicModal').classList.add('flex');
}
function closeSubtopicModal() {
document.getElementById('subtopicModal').classList.add('hidden');
document.getElementById('subtopicModal').classList.remove('flex');
}
async function submitSubtopic() {
const title = document.getElementById('subtopicTitle').value.trim();
const content = document.getElementById('subtopicContent').value.trim();
if (!title) {
alert('请输入子主题标题');
return;
}
try {
const res = await fetch(`/api/topics/${currentTopicId}/subtopic`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title, content })
});
const data = await res.json();
if (data.success) {
closeSubtopicModal();
loadTopic();
}
} catch (err) {
alert('网络错误');
}
}
async function followTopic() {
if (!currentUser) {
alert('请先登录');
return;
}
try {
const res = await fetch(`/api/topics/${currentTopicId}/follow`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
});
const data = await res.json();
if (data.success) {
const btn = document.getElementById('followBtn');
if (data.followed) {
btn.innerHTML = '<i class="ri-heart-fill mr-1"></i> 已关注';
btn.classList.remove('border-blue-500', 'text-blue-500');
btn.classList.add('bg-blue-500', 'text-white');
} else {
btn.innerHTML = '<i class="ri-heart-line mr-1"></i> 关注';
btn.classList.add('border-blue-500', 'text-blue-500');
btn.classList.remove('bg-blue-500', 'text-white');
}
}
} catch (err) {
console.error(err);
}
}
</script>
</body>
</html>