Files
ylt_diy/public/static/js/common.js

258 lines
7.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 全局错误处理防止JavaScript错误中断执行
window.onerror = function(msg, url, line, col, error) {
console.error('JavaScript错误:', msg, 'at', url, 'line:', line);
return true; // 阻止错误传播
};
// 全局变量
let editingConfigId = null;
let allFields = [];
let currentPage = 1;
let pageSize = 10;
let currentEntityType = '';
let currentSearch = '';
let entityPage = 1;
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'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
document.querySelector(`.tab[onclick="switchTab('${tabName}')"]`).classList.add('active');
document.getElementById(`${tabName}-tab`).classList.add('active');
if (tabName === 'config') {
loadConfigs();
} else if (tabName === 'generate') {
loadGenerateConfigs();
} else if (tabName === 'history') {
loadHistory();
} else if (tabName === 'sum-data') {
loadSumData();
}
}
// 格式化日期时间
function formatDateTime(dateStr) {
if (!dateStr) return '';
const date = new Date(dateStr);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// 显示错误提示
function showError(message) {
alert('错误:' + message);
}
// 显示成功提示
function showSuccess(message) {
alert('成功:' + message);
}
// 页面加载时自动加载配置列表
document.addEventListener('DOMContentLoaded', function() {
loadConfigs();
});