v0.3测试
This commit is contained in:
@@ -9,19 +9,12 @@
|
||||
* 版权所有 © Sea Network Technology Studio
|
||||
*/
|
||||
|
||||
// API 使用相对路径,由 Nginx 反向代理 /api/ 到后端
|
||||
const API_BASE_URL = '';
|
||||
const JWT_STORAGE_KEY = 'class_system_token';
|
||||
const USER_STORAGE_KEY = 'class_system_user';
|
||||
|
||||
// 获取Token
|
||||
function getToken() {
|
||||
return localStorage.getItem(JWT_STORAGE_KEY);
|
||||
return localStorage.getItem(window.JWT_STORAGE_KEY || 'class_system_token');
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
function getUserInfo() {
|
||||
const userStr = localStorage.getItem(USER_STORAGE_KEY);
|
||||
const userStr = localStorage.getItem(window.USER_STORAGE_KEY || 'class_system_user');
|
||||
if (!userStr) return null;
|
||||
try {
|
||||
return JSON.parse(userStr);
|
||||
@@ -30,28 +23,15 @@ function getUserInfo() {
|
||||
}
|
||||
}
|
||||
|
||||
// 保存用户信息
|
||||
function setUserInfo(user) {
|
||||
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(user));
|
||||
localStorage.setItem(window.USER_STORAGE_KEY || 'class_system_user', JSON.stringify(user));
|
||||
}
|
||||
|
||||
// 清除登录信息
|
||||
function clearAuth() {
|
||||
localStorage.removeItem(JWT_STORAGE_KEY);
|
||||
localStorage.removeItem(USER_STORAGE_KEY);
|
||||
localStorage.removeItem(window.JWT_STORAGE_KEY || 'class_system_token');
|
||||
localStorage.removeItem(window.USER_STORAGE_KEY || 'class_system_user');
|
||||
}
|
||||
|
||||
// 检查登录状态
|
||||
function checkAuth() {
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
window.location.href = '/index.php';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// API请求封装
|
||||
async function apiRequest(url, options = {}) {
|
||||
const token = getToken();
|
||||
const headers = {
|
||||
@@ -62,16 +42,11 @@ async function apiRequest(url, options = {}) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// 确保 url 以 /api/ 开头
|
||||
const fullUrl = url.startsWith('/api/') ? url : `/api${url}`;
|
||||
|
||||
const config = {
|
||||
...options,
|
||||
headers
|
||||
};
|
||||
const baseUrl = window.API_BASE_URL;
|
||||
const fullUrl = `${baseUrl}${url}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(fullUrl, config);
|
||||
const response = await fetch(fullUrl, { ...options, headers });
|
||||
const data = await response.json();
|
||||
|
||||
if (response.status === 401) {
|
||||
@@ -87,60 +62,50 @@ async function apiRequest(url, options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// GET请求
|
||||
async function apiGet(url, params = {}) {
|
||||
function apiGet(url, params = {}) {
|
||||
const queryString = new URLSearchParams(params).toString();
|
||||
const fullUrl = queryString ? `${url}?${queryString}` : url;
|
||||
return apiRequest(fullUrl, { method: 'GET' });
|
||||
}
|
||||
|
||||
// POST请求
|
||||
async function apiPost(url, data = {}) {
|
||||
function apiPost(url, data = {}) {
|
||||
return apiRequest(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
async function apiPut(url, data = {}) {
|
||||
function apiPut(url, data = {}) {
|
||||
return apiRequest(url, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
}
|
||||
|
||||
// DELETE请求
|
||||
async function apiDelete(url) {
|
||||
function apiDelete(url) {
|
||||
return apiRequest(url, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
// 显示提示消息
|
||||
function showToast(message, type = 'success') {
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast toast-${type}`;
|
||||
toast.textContent = message;
|
||||
document.body.appendChild(toast);
|
||||
setTimeout(() => {
|
||||
toast.remove();
|
||||
}, 3000);
|
||||
setTimeout(() => toast.remove(), 3000);
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
function formatDate(dateStr) {
|
||||
if (!dateStr) return '-';
|
||||
const date = new Date(dateStr);
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// 格式化日期时间
|
||||
function formatDateTime(dateStr) {
|
||||
if (!dateStr) return '-';
|
||||
const date = new Date(dateStr);
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// 获取状态标签HTML
|
||||
function getStatusBadge(status, type = 'homework') {
|
||||
const statusMap = {
|
||||
homework: {
|
||||
@@ -179,49 +144,12 @@ function getStatusBadge(status, type = 'homework') {
|
||||
return `<span class="${className}">${text}</span>`;
|
||||
}
|
||||
|
||||
// 退出登录
|
||||
async function logout() {
|
||||
await apiPost('/api/auth/logout');
|
||||
clearAuth();
|
||||
window.location.href = '/index.php';
|
||||
}
|
||||
|
||||
// 加载用户信息
|
||||
function loadUserInfo() {
|
||||
const user = getUserInfo();
|
||||
const userNameSpan = document.getElementById('userName');
|
||||
if (userNameSpan && user) {
|
||||
userNameSpan.textContent = user.real_name || user.username;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否需要修改密码
|
||||
function checkNeedChangePassword() {
|
||||
const user = getUserInfo();
|
||||
if (user && user.need_change_password) {
|
||||
const newPassword = prompt('首次登录,请设置新密码(6-20位,需包含字母和数字):');
|
||||
if (newPassword) {
|
||||
changePassword(newPassword);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
async function changePassword(newPassword) {
|
||||
const res = await apiPost('/api/auth/change-password', {
|
||||
old_password: newPassword,
|
||||
new_password: newPassword
|
||||
});
|
||||
if (res && res.success) {
|
||||
showToast('密码修改成功,请重新登录');
|
||||
setTimeout(() => logout(), 1500);
|
||||
} else {
|
||||
showToast(res?.message || '密码修改失败', 'error');
|
||||
checkNeedChangePassword();
|
||||
}
|
||||
}
|
||||
|
||||
// HTML转义
|
||||
function escapeHtml(str) {
|
||||
if (!str) return '';
|
||||
return str.replace(/[&<>]/g, function(m) {
|
||||
@@ -232,14 +160,14 @@ function escapeHtml(str) {
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载时初始化
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
loadUserInfo();
|
||||
const user = getUserInfo();
|
||||
const userNameSpan = document.getElementById('userName');
|
||||
if (userNameSpan && user) {
|
||||
userNameSpan.textContent = user.real_name || user.username;
|
||||
}
|
||||
const logoutBtn = document.getElementById('logoutBtn');
|
||||
if (logoutBtn) {
|
||||
logoutBtn.addEventListener('click', logout);
|
||||
}
|
||||
if (window.location.pathname.includes('/student/') || window.location.pathname.includes('/parent/')) {
|
||||
checkNeedChangePassword();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user