-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
220 lines (187 loc) · 7.85 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import apiService from './api.js';
import { translations } from './translations.js';
class AuthManager {
constructor() {
this.loginModal = document.getElementById('loginModal');
this.loginButton = document.getElementById('loginButton');
this.registerButton = document.getElementById('registerButton');
this.loginToggle = document.getElementById('loginToggle');
this.logoutButton = document.getElementById('logoutButton');
this.welcomeText = document.getElementById('welcomeText');
this.usernameInput = document.getElementById('username');
this.passwordInput = document.getElementById('password');
// 确保初始状态为未登录
localStorage.removeItem('user');
this.setupEventListeners();
this.updateUIAfterLogout();
}
setupEventListeners() {
// 登录按钮点击事件
if (this.loginButton) {
this.loginButton.addEventListener('click', async (e) => {
e.preventDefault();
console.log('点击登录按钮'); // 添加调试日志
await this.login();
});
}
// 切换登录模态框显示
if (this.loginToggle) {
this.loginToggle.addEventListener('click', () => {
console.log('显示登录模态框'); // 添加调试日志
this.loginModal.style.display = 'block';
});
}
// 注册按钮点击事件
if (this.registerButton) {
this.registerButton.addEventListener('click', async (e) => {
e.preventDefault(); // 防止表单默认提交
await this.register();
});
}
// 退出登录
document.getElementById('logoutButton').addEventListener('click', () => {
this.logout();
});
// 点击模态框外部关闭
window.addEventListener('click', (e) => {
if (e.target === document.getElementById('loginModal')) {
document.getElementById('loginModal').style.display = 'none';
}
});
}
async login() {
try {
console.log('开始登录...'); // 调试日志
console.log('用户名输入框:', this.usernameInput); // 调试日志
console.log('密码输入框:', this.passwordInput); // 调试日志
// 确保输入框存在
if (!this.usernameInput || !this.passwordInput) {
console.error('找不到用户名或密码输入框');
return;
}
const username = this.usernameInput.value.trim();
const password = this.passwordInput.value.trim();
if (!username || !password) {
alert('请输入用户名和密码');
return;
}
console.log('发送登录请求...'); // 调试日志
const response = await apiService.login(username, password);
console.log('登录响应:', response); // 调试日志
if (response.success) {
localStorage.setItem('user', JSON.stringify({
username,
isLoggedIn: true
}));
this.isLoggedIn = true;
this.currentUsername = username;
this.updateUIAfterLogin(username);
this.loginModal.style.display = 'none';
alert('登录成功!');
} else {
alert(response.message || '登录失败,请检查用户名和密码');
}
} catch (error) {
console.error('登录错误:', error);
alert('登录过程中发生错误');
}
}
async register() {
try {
console.log('开始注册...'); // 调试日志
const username = this.username.value.trim();
const password = this.password.value.trim();
if (!username || !password) {
alert('请输入用户名和密码');
return;
}
console.log('发送注册请求...'); // 调试日志
// 使用 apiService 发送注册请求
const response = await apiService.register(username, password);
console.log('注册响应:', response); // 调试日志
if (response.success) {
alert('注册成功,请登录');
// 清空输入框
this.username.value = '';
this.password.value = '';
} else {
alert(response.message || '注册失败,请重试');
}
} catch (error) {
console.error('注册错误:', error);
alert('注册过程中发生错误');
}
}
logout() {
this.isLoggedIn = false;
this.username = '';
localStorage.removeItem('user');
this.updateUIAfterLogout();
}
checkLoginStatus() {
const user = JSON.parse(localStorage.getItem('user'));
if (user && user.isLoggedIn) {
this.isLoggedIn = true;
this.username = user.username;
this.updateUIAfterLogin();
this.loadChatHistory();
}
}
updateUIAfterLogin(username) {
this.welcomeText.textContent = translations.zh.welcome + username;
this.loginToggle.style.display = 'none';
this.logoutButton.style.display = 'block';
}
updateUIAfterLogout() {
this.welcomeText.textContent = translations.zh.notLogged;
this.loginToggle.style.display = 'block';
this.logoutButton.style.display = 'none';
}
createHistorySection() {
const chatSection = document.querySelector('.chat-section');
let historySection = document.querySelector('.chat-history');
if (!historySection) {
historySection = document.createElement('div');
historySection.className = 'chat-history';
historySection.innerHTML = `<h3>${translations[currentLang].chatHistory}</h3>`;
chatSection.insertBefore(historySection, chatSection.firstChild);
}
}
removeHistorySection() {
const historySection = document.querySelector('.chat-history');
if (historySection) {
historySection.remove();
}
}
loadChatHistory() {
// 从localStorage加载聊天历史
const history = JSON.parse(localStorage.getItem(`chat_history_${this.username}`)) || [];
this.displayChatHistory(history);
}
saveChatHistory(message) {
if (!this.isLoggedIn) return;
const history = JSON.parse(localStorage.getItem(`chat_history_${this.username}`)) || [];
history.push({
message,
timestamp: new Date().toISOString()
});
localStorage.setItem(`chat_history_${this.username}`, JSON.stringify(history));
this.displayChatHistory(history);
}
displayChatHistory(history) {
const historySection = document.querySelector('.chat-history');
if (!historySection) return;
historySection.innerHTML = `<h3>${translations[currentLang].chatHistory}</h3>`;
history.forEach(item => {
const historyItem = document.createElement('div');
historyItem.className = 'history-item';
historyItem.textContent = item.message;
historyItem.onclick = () => {
document.querySelector('.prompt-input').value = item.message;
};
historySection.appendChild(historyItem);
});
}
}
const authManager = new AuthManager();
export default authManager;