init
This commit is contained in:
121
static/js/common.js
Normal file
121
static/js/common.js
Normal file
@@ -0,0 +1,121 @@
|
||||
// 通用工具函数
|
||||
// AJAX全局设置
|
||||
$.ajaxSetup({
|
||||
beforeSend: function(xhr, settings) {
|
||||
if (!settings.url.includes('/api/login')) {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) {
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// 检查登录状态
|
||||
function checkAuth() {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (!token) {
|
||||
window.location.href = '/';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 获取当前用户信息
|
||||
function getCurrentUser() {
|
||||
if (!checkAuth()) return;
|
||||
|
||||
$.ajax({
|
||||
url: '/api/me',
|
||||
method: 'GET',
|
||||
success: function(data) {
|
||||
$('#current-username').text(data.username);
|
||||
localStorage.setItem('current_user', JSON.stringify(data));
|
||||
|
||||
if (data.role === 'admin') {
|
||||
$('.admin-only').show();
|
||||
$('#nav-users').show();
|
||||
}
|
||||
},
|
||||
error: function(xhr) {
|
||||
if (xhr.status === 401 || xhr.status === 422) {
|
||||
logout();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 退出登录
|
||||
function logout() {
|
||||
localStorage.removeItem('access_token');
|
||||
localStorage.removeItem('current_user');
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
function formatDate(dateString) {
|
||||
if (!dateString) return '-';
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString('zh-CN');
|
||||
}
|
||||
|
||||
// 格式化日期时间
|
||||
function formatDateTime(dateString) {
|
||||
if (!dateString) return '-';
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleString('zh-CN');
|
||||
}
|
||||
|
||||
// 显示错误消息
|
||||
function showError(message) {
|
||||
alert(message);
|
||||
}
|
||||
|
||||
// 显示成功消息
|
||||
function showSuccess(message) {
|
||||
alert(message);
|
||||
}
|
||||
|
||||
// AJAX错误处理
|
||||
function handleAjaxError(xhr) {
|
||||
if (xhr.status === 401) {
|
||||
alert('登录已过期,请重新登录');
|
||||
logout();
|
||||
} else if (xhr.status === 403 && xhr.responseJSON?.code === 'USER_DISABLED') {
|
||||
alert('您的账号已被禁用,请联系管理员');
|
||||
logout();
|
||||
} else {
|
||||
const error = xhr.responseJSON?.error || '操作失败';
|
||||
showError(error);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取状态徽章HTML
|
||||
function getStatusBadge(status) {
|
||||
const statusMap = {
|
||||
'pending': { text: '待认领', class: 'status-pending' },
|
||||
'claimed': { text: '已认领', class: 'status-claimed' },
|
||||
'completed': { text: '已完成', class: 'status-completed' }
|
||||
};
|
||||
|
||||
const info = statusMap[status] || { text: status, class: '' };
|
||||
return `<span class="badge badge-status ${info.class}">${info.text}</span>`;
|
||||
}
|
||||
|
||||
// 获取优先级样式类
|
||||
function getPriorityClass(priority) {
|
||||
const map = {
|
||||
'高': 'priority-high',
|
||||
'中': 'priority-medium',
|
||||
'低': 'priority-low'
|
||||
};
|
||||
return map[priority] || '';
|
||||
}
|
||||
|
||||
// 页面加载时初始化
|
||||
$(document).ready(function() {
|
||||
// 如果不是登录页,检查认证
|
||||
if (!window.location.pathname.match(/^\/?$/)) {
|
||||
checkAuth();
|
||||
getCurrentUser();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user