增加登陆功能,并修改正式环境用5002端口
This commit is contained in:
@@ -16,6 +16,199 @@ let entityPageSize = 20;
|
||||
let selectedEntityIds = new Set();
|
||||
let fieldCustomNames = {};
|
||||
|
||||
// ============== 登录相关 ==============
|
||||
const AUTH_TOKEN_KEY = 'daily_report_auth_token';
|
||||
const AUTH_USER_KEY = 'daily_report_auth_user';
|
||||
|
||||
// 获取token
|
||||
function getToken() {
|
||||
return localStorage.getItem(AUTH_TOKEN_KEY) || '';
|
||||
}
|
||||
|
||||
// 保存登录状态
|
||||
function setAuth(token, user) {
|
||||
localStorage.setItem(AUTH_TOKEN_KEY, token);
|
||||
localStorage.setItem(AUTH_USER_KEY, JSON.stringify(user));
|
||||
}
|
||||
|
||||
// 清除登录状态
|
||||
function clearAuth() {
|
||||
localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
localStorage.removeItem(AUTH_USER_KEY);
|
||||
}
|
||||
|
||||
// 检查是否已登录
|
||||
function isLoggedIn() {
|
||||
return !!getToken();
|
||||
}
|
||||
|
||||
// 获取当前用户
|
||||
function getCurrentUser() {
|
||||
const userStr = localStorage.getItem(AUTH_USER_KEY);
|
||||
return userStr ? JSON.parse(userStr) : null;
|
||||
}
|
||||
|
||||
// 全局拦截 fetch,自动加 token
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = function(url, options = {}) {
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
options.headers = options.headers || {};
|
||||
options.headers['Authorization'] = 'Bearer ' + token;
|
||||
}
|
||||
|
||||
return originalFetch(url, options).then(response => {
|
||||
if (response.status === 401) {
|
||||
clearAuth();
|
||||
showLoginPage();
|
||||
}
|
||||
return response;
|
||||
});
|
||||
};
|
||||
|
||||
// 显示登录页
|
||||
function showLoginPage() {
|
||||
document.getElementById('login-overlay').style.display = 'flex';
|
||||
document.getElementById('main-container').style.display = 'none';
|
||||
document.getElementById('login-password').value = '';
|
||||
document.getElementById('login-password').focus();
|
||||
}
|
||||
|
||||
// 隐藏登录页
|
||||
function hideLoginPage() {
|
||||
document.getElementById('login-overlay').style.display = 'none';
|
||||
document.getElementById('main-container').style.display = 'block';
|
||||
|
||||
const user = getCurrentUser();
|
||||
if (user) {
|
||||
document.getElementById('user-display').textContent = user.real_name || user.username;
|
||||
}
|
||||
}
|
||||
|
||||
// 登录
|
||||
async function handleLogin(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const username = document.getElementById('login-username').value.trim();
|
||||
const password = document.getElementById('login-password').value.trim();
|
||||
|
||||
if (!username || !password) {
|
||||
alert('请输入用户名和密码');
|
||||
return;
|
||||
}
|
||||
|
||||
const btn = document.getElementById('login-btn');
|
||||
btn.disabled = true;
|
||||
btn.textContent = '登录中...';
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password })
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
setAuth(result.data.token, {
|
||||
username: result.data.username,
|
||||
real_name: result.data.real_name
|
||||
});
|
||||
hideLoginPage();
|
||||
// 登录成功后加载数据
|
||||
loadConfigs();
|
||||
} else {
|
||||
alert(result.message || '登录失败');
|
||||
}
|
||||
} catch (error) {
|
||||
alert('登录失败: ' + error.message);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
btn.textContent = '登 录';
|
||||
}
|
||||
}
|
||||
|
||||
// 退出登录
|
||||
async function handleLogout() {
|
||||
if (!confirm('确定要退出登录吗?')) return;
|
||||
|
||||
try {
|
||||
await fetch('/api/auth/logout', { method: 'POST' });
|
||||
} catch (e) {
|
||||
// 忽略错误
|
||||
}
|
||||
|
||||
clearAuth();
|
||||
showLoginPage();
|
||||
}
|
||||
|
||||
// 修改密码弹窗
|
||||
function showChangePasswordModal() {
|
||||
document.getElementById('old-password').value = '';
|
||||
document.getElementById('new-password').value = '';
|
||||
document.getElementById('confirm-password').value = '';
|
||||
document.getElementById('change-password-modal').style.display = 'block';
|
||||
}
|
||||
|
||||
function closeChangePasswordModal() {
|
||||
document.getElementById('change-password-modal').style.display = 'none';
|
||||
}
|
||||
|
||||
async function saveChangePassword() {
|
||||
const oldPassword = document.getElementById('old-password').value.trim();
|
||||
const newPassword = document.getElementById('new-password').value.trim();
|
||||
const confirmPassword = document.getElementById('confirm-password').value.trim();
|
||||
|
||||
if (!oldPassword || !newPassword || !confirmPassword) {
|
||||
alert('请填写完整信息');
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPassword.length < 6) {
|
||||
alert('新密码长度不能少于6位');
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPassword !== confirmPassword) {
|
||||
alert('两次输入的新密码不一致');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/auth/change-password', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
old_password: oldPassword,
|
||||
new_password: newPassword
|
||||
})
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
alert('密码修改成功,请重新登录');
|
||||
closeChangePasswordModal();
|
||||
clearAuth();
|
||||
showLoginPage();
|
||||
} else {
|
||||
alert(result.message || '修改失败');
|
||||
}
|
||||
} catch (error) {
|
||||
alert('修改失败: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载时检查登录状态
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
if (isLoggedIn()) {
|
||||
hideLoginPage();
|
||||
} else {
|
||||
showLoginPage();
|
||||
}
|
||||
});
|
||||
|
||||
// 标签切换
|
||||
function switchTab(tabName) {
|
||||
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
|
||||
|
||||
@@ -209,7 +209,7 @@ function renderHistory(list) {
|
||||
<td><span class="${statusClass}">${statusText}</span></td>
|
||||
<td>
|
||||
${item.status === 1 && item.file_path ?
|
||||
`<a href="/api/download?path=${encodeURIComponent(item.file_path)}" class="btn btn-sm btn-success">下载</a>` :
|
||||
`<a href="/api/download?path=${encodeURIComponent(item.file_path)}&token=${encodeURIComponent(getToken())}" class="btn btn-sm btn-success">下载</a>` :
|
||||
'-'}
|
||||
<button class="btn btn-sm btn-danger" onclick="deleteHistory(${item.id})">删除</button>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user