a3eb5c7b2d
从百度网盘 /apps/bypy/dofile/ 下载的完整项目结构,包含: - dofile/: Flask 后端(端口 5000)+ 静态前端(端口 6002) - officefile/: 项目相关文档 - .gitignore: Python venv / 日志 / 缓存 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
209 lines
7.8 KiB
JavaScript
209 lines
7.8 KiB
JavaScript
// User Authentication Functions
|
|
|
|
let currentUser = null;
|
|
|
|
// Check if user is logged in
|
|
async function isLoggedIn() {
|
|
try {
|
|
const response = await window.DAELAPI.auth.checkAuth();
|
|
if (response.authenticated) {
|
|
currentUser = response.user;
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Get current user
|
|
function getCurrentUser() {
|
|
return currentUser;
|
|
}
|
|
|
|
// Login function
|
|
async function login(username, password, remember = false) {
|
|
try {
|
|
const response = await window.DAELAPI.auth.login(username, password, remember);
|
|
currentUser = response.user;
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Login failed:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Logout function
|
|
async function logout() {
|
|
try {
|
|
await window.DAELAPI.auth.logout();
|
|
currentUser = null;
|
|
window.location.href = 'website.html';
|
|
} catch (error) {
|
|
console.error('Logout failed:', error);
|
|
// 即使API失败也清除本地状态
|
|
currentUser = null;
|
|
window.location.href = 'website.html';
|
|
}
|
|
}
|
|
|
|
// Check protected content access
|
|
async function checkProtectedContent() {
|
|
const loggedIn = await isLoggedIn();
|
|
const protectedElements = document.querySelectorAll('.protected-content');
|
|
|
|
if (loggedIn) {
|
|
protectedElements.forEach(el => {
|
|
el.classList.remove('protected-content');
|
|
});
|
|
|
|
// 加载受保护内容
|
|
try {
|
|
const response = await window.DAELAPI.api.getProtectedContent();
|
|
displayProtectedContent(response);
|
|
} catch (error) {
|
|
console.error('Failed to load protected content:', error);
|
|
}
|
|
} else {
|
|
protectedElements.forEach(el => {
|
|
el.classList.add('protected-content');
|
|
});
|
|
}
|
|
}
|
|
|
|
// 显示受保护内容
|
|
async function displayProtectedContent(data) {
|
|
// 更新受保护的文章
|
|
const protectedArticlesContainer = document.getElementById('protected-articles');
|
|
if (protectedArticlesContainer && data.articles && data.articles.length > 0) {
|
|
protectedArticlesContainer.innerHTML = data.articles.map(article => `
|
|
<div class="border-b border-gray-200 pb-6 mb-6">
|
|
<h3 class="text-xl font-bold mb-2">${article.title}</h3>
|
|
<p class="text-gray-500 text-sm mb-2">${article.summary || article.content.substring(0, 100) + '...'}</p>
|
|
<div class="text-xs text-gray-400">${new Date(article.created_at).toLocaleDateString()}</div>
|
|
</div>
|
|
`).join('');
|
|
protectedArticlesContainer.classList.remove('hidden');
|
|
}
|
|
|
|
// 更新受保护的资源
|
|
const protectedResourcesContainer = document.getElementById('protected-resources');
|
|
if (protectedResourcesContainer && data.resources && data.resources.length > 0) {
|
|
protectedResourcesContainer.innerHTML = data.resources.map(resource => `
|
|
<div class="border border-gray-200 p-4 mb-4">
|
|
<h4 class="font-bold mb-2">${resource.name}</h4>
|
|
<p class="text-sm text-gray-500 mb-2">${resource.description || ''}</p>
|
|
<p class="text-xs text-gray-400 mb-2">大小: ${(resource.file_size / 1024 / 1024).toFixed(2)} MB</p>
|
|
<button onclick="window.DAELAPI.api.downloadResource(${resource.id})"
|
|
class="text-sm text-black border-b border-black hover:text-gray-600">
|
|
下载 ->
|
|
</button>
|
|
</div>
|
|
`).join('');
|
|
protectedResourcesContainer.classList.remove('hidden');
|
|
}
|
|
|
|
// 更新统计信息
|
|
try {
|
|
const statsResponse = await window.DAELAPI.api.getStats();
|
|
const stats = statsResponse.stats;
|
|
const statsContainer = document.getElementById('protected-stats');
|
|
if (statsContainer) {
|
|
const statElements = statsContainer.querySelectorAll('.text-3xl');
|
|
if (statElements.length >= 4) {
|
|
statElements[0].textContent = stats.articles || '0';
|
|
statElements[1].textContent = stats.resources || '0';
|
|
statElements[2].textContent = stats.team_members || '0';
|
|
statElements[3].textContent = stats.total_downloads || '0';
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to load stats:', error);
|
|
}
|
|
}
|
|
|
|
// Initialize auth on page load
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
// 等待API加载
|
|
if (typeof window.DAELAPI === 'undefined') {
|
|
console.warn('API not loaded, using fallback authentication');
|
|
// 使用本地存储作为后备
|
|
const localAuth = localStorage.getItem('dael_user_logged_in') === 'true';
|
|
updateUI(localAuth);
|
|
return;
|
|
}
|
|
|
|
await checkProtectedContent();
|
|
await updateAuthUI();
|
|
|
|
// Logout button handler
|
|
const logoutBtn = document.getElementById('logout-btn');
|
|
if (logoutBtn) {
|
|
logoutBtn.addEventListener('click', async (e) => {
|
|
e.preventDefault();
|
|
if (confirm('确定要退出登录吗?')) {
|
|
await logout();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
// 更新认证UI
|
|
async function updateAuthUI() {
|
|
const loggedIn = await isLoggedIn();
|
|
const loginBtn = document.getElementById('login-btn');
|
|
const logoutBtn = document.getElementById('logout-btn');
|
|
const adminBtn = document.getElementById('admin-btn');
|
|
const meetingsLink = document.getElementById('meetings-link');
|
|
const researchLink = document.getElementById('research-link');
|
|
const userInfo = document.getElementById('user-info');
|
|
|
|
if (loggedIn) {
|
|
const user = getCurrentUser();
|
|
if (loginBtn) loginBtn.classList.add('hidden');
|
|
if (logoutBtn) logoutBtn.classList.remove('hidden');
|
|
if (adminBtn && user && user.is_admin) {
|
|
adminBtn.classList.remove('hidden');
|
|
} else if (adminBtn) {
|
|
adminBtn.classList.add('hidden');
|
|
}
|
|
// 显示组会和研究管理链接(所有登录用户)
|
|
if (meetingsLink) meetingsLink.classList.remove('hidden');
|
|
if (researchLink) researchLink.classList.remove('hidden');
|
|
const mobileMeetingsLink = document.getElementById('mobile-meetings-link');
|
|
const mobileResearchLink = document.getElementById('mobile-research-link');
|
|
if (mobileMeetingsLink) mobileMeetingsLink.classList.remove('hidden');
|
|
if (mobileResearchLink) mobileResearchLink.classList.remove('hidden');
|
|
if (userInfo && user) {
|
|
userInfo.textContent = user.username;
|
|
userInfo.classList.remove('hidden');
|
|
}
|
|
} else {
|
|
if (loginBtn) loginBtn.classList.remove('hidden');
|
|
if (logoutBtn) logoutBtn.classList.add('hidden');
|
|
if (adminBtn) adminBtn.classList.add('hidden');
|
|
if (meetingsLink) meetingsLink.classList.add('hidden');
|
|
if (researchLink) researchLink.classList.add('hidden');
|
|
const mobileMeetingsLink = document.getElementById('mobile-meetings-link');
|
|
const mobileResearchLink = document.getElementById('mobile-research-link');
|
|
if (mobileMeetingsLink) mobileMeetingsLink.classList.add('hidden');
|
|
if (mobileResearchLink) mobileResearchLink.classList.add('hidden');
|
|
if (userInfo) userInfo.classList.add('hidden');
|
|
}
|
|
}
|
|
|
|
// 后备UI更新函数
|
|
function updateUI(loggedIn) {
|
|
const loginBtn = document.getElementById('login-btn');
|
|
const logoutBtn = document.getElementById('logout-btn');
|
|
|
|
if (loggedIn) {
|
|
if (loginBtn) loginBtn.classList.add('hidden');
|
|
if (logoutBtn) logoutBtn.classList.remove('hidden');
|
|
} else {
|
|
if (loginBtn) loginBtn.classList.remove('hidden');
|
|
if (logoutBtn) logoutBtn.classList.add('hidden');
|
|
}
|
|
}
|
|
|