2025-12-2genxin

This commit is contained in:
2025-12-02 14:58:52 +08:00
parent 4fef65bd93
commit be0954828c
36 changed files with 3352 additions and 1638 deletions

View File

@@ -568,7 +568,7 @@
};
// 调用后端登录API
fetch('http://localhost:8000/api/user/login', {
fetch('http://localhost:8080/api/user/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -576,17 +576,18 @@
body: JSON.stringify(loginData)
})
.then(response => {
console.log('✅ 收到响应,状态码:', response.status);
if (!response.ok) {
// 处理HTTP错误状态码
if (response.status === 500) {
throw new Error('服务器内部错误');
} else if (response.status === 401) {
return response.json().then(data => {
throw new Error(data.msg || '用户名或密码错误');
throw new Error(data.message || '用户名或密码错误');
});
} else if (response.status === 400) {
return response.json().then(data => {
throw new Error(data.msg || '请求参数错误');
throw new Error(data.message || '请求参数错误');
});
} else {
throw new Error('网络响应异常');
@@ -595,13 +596,14 @@
return response.json();
})
.then(data => {
console.log('✅ 收到数据:', data);
// 检查响应状态
if (data.code === 200) {
if (data.success || data.code === 200) {
// 登录成功,存储认证信息
const authData = {
token: data.token,
user_id: data.user_id,
user_info: data.user_info,
token: data.data.token,
user_id: data.data.user_id,
user_info: data.data.user_info,
loginTime: new Date().toISOString(),
remember: remember
};
@@ -635,19 +637,19 @@
// 根据错误码显示不同的错误信息
switch(data.code) {
case 401:
errorMessage = data.msg || '用户名或密码错误';
errorMessage = data.message || '用户名或密码错误';
break;
case 400:
errorMessage = data.msg || '请求参数错误,请检查输入';
errorMessage = data.message || '请求参数错误,请检查输入';
break;
case 404:
errorMessage = data.msg || '账号不存在';
errorMessage = data.message || '账号不存在';
break;
case 500:
errorMessage = data.msg || '服务器内部错误,请稍后再试';
errorMessage = data.message || '服务器内部错误,请稍后再试';
break;
default:
errorMessage = data.msg || '登录失败,请检查用户名和密码';
errorMessage = data.message || '登录失败,请检查用户名和密码';
}
showError(errorMessage);
@@ -657,12 +659,17 @@
})
.catch(error => {
// 捕获网络或其他错误
console.error('登录请求失败:', error);
console.error('登录请求失败:', error);
console.error('错误类型:', error.name);
console.error('错误消息:', error.message);
// 根据错误信息显示具体的错误提示
let errorMessage = '登录失败,请稍后再试';
if (error.message.includes('账号不存在')) {
// 检查是否是网络错误
if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {
errorMessage = '无法连接到服务器,请确保:\n1. API服务器正在运行 (http://localhost:8080)\n2. 检查网络连接';
} else if (error.message.includes('账号不存在')) {
errorMessage = '账号不存在,请先注册';
} else if (error.message.includes('密码错误')) {
errorMessage = '密码错误,请重新输入';
@@ -701,15 +708,25 @@
window.addEventListener('load', function() {
const authData = localStorage.getItem('authData') || sessionStorage.getItem('authData');
if (authData) {
const auth = JSON.parse(authData);
showSuccess(`欢迎回来,${auth.user_info.username}`);
// 如果已经登录,可以选择直接跳转
setTimeout(() => {
if (confirm('检测到您已登录,是否跳转到首页?')) {
window.location.href = 'frontend.html';
try {
const auth = JSON.parse(authData);
// 检查user_info是否存在且有username
if (auth.user_info && auth.user_info.username) {
showSuccess(`欢迎回来,${auth.user_info.username}`);
// 如果已经登录,可以选择直接跳转
setTimeout(() => {
if (confirm('检测到您已登录,是否跳转到首页?')) {
window.location.href = 'frontend.html';
}
}, 1000);
}
}, 1000);
} catch (e) {
console.error('解析登录数据失败:', e);
// 清除无效的登录数据
localStorage.removeItem('authData');
sessionStorage.removeItem('authData');
}
}
});