增加登陆功能,并修改正式环境用5002端口
This commit is contained in:
@@ -867,6 +867,117 @@ color: #666;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
/* ========== 登录页面 ========== */
|
||||
.login-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 9999;
|
||||
}
|
||||
|
||||
.login-box {
|
||||
background: white;
|
||||
padding: 40px;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
||||
width: 400px;
|
||||
max-width: 90%;
|
||||
}
|
||||
|
||||
.login-box h2 {
|
||||
text-align: center;
|
||||
color: #333;
|
||||
margin-bottom: 8px;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.login-subtitle {
|
||||
text-align: center;
|
||||
color: #888;
|
||||
margin-bottom: 30px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.login-box .form-group {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.login-box label {
|
||||
display: block;
|
||||
margin-bottom: 8px;
|
||||
color: #555;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.login-box input {
|
||||
width: 100%;
|
||||
padding: 12px 15px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
box-sizing: border-box;
|
||||
transition: border-color 0.3s;
|
||||
}
|
||||
|
||||
.login-box input:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
|
||||
}
|
||||
|
||||
.btn-block {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
font-size: 16px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.login-tip {
|
||||
text-align: center;
|
||||
color: #aaa;
|
||||
font-size: 12px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
/* 顶部栏 */
|
||||
.header-bar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 15px;
|
||||
font-size: 14px;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.user-info span {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.btn-link {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--primary);
|
||||
cursor: pointer;
|
||||
padding: 4px 8px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.btn-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.page-btn.active {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
|
||||
@@ -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