collabdocchat 2.0.8 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/main.js +0 -6
- package/src/pages/admin-dashboard.js +1409 -3591
- package/src/pages/login.js +100 -101
- package/src/pages/user-dashboard.js +905 -1508
- package/src/services/api.js +264 -326
- package/src/services/auth.js +53 -54
- package/src/services/websocket.js +79 -189
package/src/services/auth.js
CHANGED
|
@@ -1,55 +1,54 @@
|
|
|
1
|
-
const API_URL = 'http://localhost:3000/api';
|
|
2
|
-
|
|
3
|
-
export class AuthService {
|
|
4
|
-
async login(username, password) {
|
|
5
|
-
const response = await fetch(`${API_URL}/auth/login`, {
|
|
6
|
-
method: 'POST',
|
|
7
|
-
headers: { 'Content-Type': 'application/json' },
|
|
8
|
-
body: JSON.stringify({ username, password })
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
if (!response.ok) {
|
|
12
|
-
const error = await response.json();
|
|
13
|
-
throw new Error(error.message);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return await response.json();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async register(username, password) {
|
|
20
|
-
const response = await fetch(`${API_URL}/auth/register`, {
|
|
21
|
-
method: 'POST',
|
|
22
|
-
headers: { 'Content-Type': 'application/json' },
|
|
23
|
-
body: JSON.stringify({ username, password })
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (!response.ok) {
|
|
27
|
-
const error = await response.json();
|
|
28
|
-
throw new Error(error.message);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return await response.json();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async getCurrentUser() {
|
|
35
|
-
const token = localStorage.getItem('token');
|
|
36
|
-
const response = await fetch(`${API_URL}/auth/me`, {
|
|
37
|
-
headers: { 'Authorization': `Bearer ${token}` }
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (!response.ok) {
|
|
41
|
-
throw new Error('获取用户信息失败');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const data = await response.json();
|
|
45
|
-
return data.user;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
logout() {
|
|
49
|
-
localStorage.removeItem('token');
|
|
50
|
-
window.location.reload();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
const API_URL = 'http://localhost:3000/api';
|
|
2
|
+
|
|
3
|
+
export class AuthService {
|
|
4
|
+
async login(username, password) {
|
|
5
|
+
const response = await fetch(`${API_URL}/auth/login`, {
|
|
6
|
+
method: 'POST',
|
|
7
|
+
headers: { 'Content-Type': 'application/json' },
|
|
8
|
+
body: JSON.stringify({ username, password })
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
if (!response.ok) {
|
|
12
|
+
const error = await response.json();
|
|
13
|
+
throw new Error(error.message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return await response.json();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async register(username, password) {
|
|
20
|
+
const response = await fetch(`${API_URL}/auth/register`, {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: { 'Content-Type': 'application/json' },
|
|
23
|
+
body: JSON.stringify({ username, password })
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
const error = await response.json();
|
|
28
|
+
throw new Error(error.message);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return await response.json();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async getCurrentUser() {
|
|
35
|
+
const token = localStorage.getItem('token');
|
|
36
|
+
const response = await fetch(`${API_URL}/auth/me`, {
|
|
37
|
+
headers: { 'Authorization': `Bearer ${token}` }
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
throw new Error('获取用户信息失败');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const data = await response.json();
|
|
45
|
+
return data.user;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
logout() {
|
|
49
|
+
localStorage.removeItem('token');
|
|
50
|
+
window.location.reload();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
55
54
|
|
|
@@ -1,190 +1,80 @@
|
|
|
1
|
-
export class WebSocketService {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.ws = null;
|
|
4
|
-
this.listeners = new Map();
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
this.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.
|
|
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
|
-
message: '无法连接到服务器,请刷新页面重试'
|
|
81
|
-
});
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.reconnectAttempts++;
|
|
86
|
-
const delay = Math.min(this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1), 30000);
|
|
87
|
-
|
|
88
|
-
console.log(`🔄 ${delay / 1000}秒后尝试�?${this.reconnectAttempts} 次重�?..`);
|
|
89
|
-
|
|
90
|
-
setTimeout(() => {
|
|
91
|
-
if (this.token) {
|
|
92
|
-
this.connect(this.token);
|
|
93
|
-
}
|
|
94
|
-
}, delay);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
startHeartbeat() {
|
|
98
|
-
this.stopHeartbeat();
|
|
99
|
-
|
|
100
|
-
this.heartbeatInterval = setInterval(() => {
|
|
101
|
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
102
|
-
this.send({ type: 'ping', timestamp: Date.now() });
|
|
103
|
-
}
|
|
104
|
-
}, 30000); // �?0秒发送一次心�? }
|
|
105
|
-
|
|
106
|
-
stopHeartbeat() {
|
|
107
|
-
if (this.heartbeatInterval) {
|
|
108
|
-
clearInterval(this.heartbeatInterval);
|
|
109
|
-
this.heartbeatInterval = null;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
disconnect() {
|
|
114
|
-
this.stopHeartbeat();
|
|
115
|
-
this.reconnectAttempts = this.maxReconnectAttempts; // 阻止自动重连
|
|
116
|
-
|
|
117
|
-
if (this.ws) {
|
|
118
|
-
this.ws.close(1000, '用户主动断开');
|
|
119
|
-
this.ws = null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this.isAuthenticated = false;
|
|
123
|
-
console.log('👋 WebSocket 已断开');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
send(data) {
|
|
127
|
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
128
|
-
this.ws.send(JSON.stringify(data));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
on(event, callback) {
|
|
133
|
-
if (!this.listeners.has(event)) {
|
|
134
|
-
this.listeners.set(event, []);
|
|
135
|
-
}
|
|
136
|
-
this.listeners.get(event).push(callback);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
off(event, callback) {
|
|
140
|
-
if (this.listeners.has(event)) {
|
|
141
|
-
const callbacks = this.listeners.get(event);
|
|
142
|
-
const index = callbacks.indexOf(callback);
|
|
143
|
-
if (index > -1) {
|
|
144
|
-
callbacks.splice(index, 1);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
notifyListeners(event, data) {
|
|
150
|
-
if (this.listeners.has(event)) {
|
|
151
|
-
this.listeners.get(event).forEach(callback => callback(data));
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
joinGroup(groupId) {
|
|
156
|
-
this.send({ type: 'join_group', groupId });
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
sendChatMessage(groupId, username, content) {
|
|
160
|
-
this.send({ type: 'chat_message', groupId, username, content });
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
syncDocument(documentId, content, cursorPosition) {
|
|
164
|
-
this.send({ type: 'document_sync', documentId, content, cursorPosition });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
respondToCall(groupId, username) {
|
|
168
|
-
this.send({ type: 'call_response', groupId, username });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
sendTyping(documentId, username, isTyping) {
|
|
172
|
-
this.send({ type: 'typing', documentId, username, isTyping });
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// 撤回消息
|
|
176
|
-
recallMessage(messageId) {
|
|
177
|
-
this.send({ type: 'recall_message', messageId });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// 标记消息为已�? markMessageAsRead(messageId) {
|
|
181
|
-
this.send({ type: 'message_read', messageId });
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// 发送用户在线状�? sendUserOnline(username) {
|
|
185
|
-
this.send({ type: 'user_online', username });
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
1
|
+
export class WebSocketService {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.ws = null;
|
|
4
|
+
this.listeners = new Map();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
connect(token) {
|
|
8
|
+
this.ws = new WebSocket('ws://localhost:3000');
|
|
9
|
+
|
|
10
|
+
this.ws.onopen = () => {
|
|
11
|
+
console.log('✅ WebSocket 连接成功');
|
|
12
|
+
this.send({ type: 'auth', token });
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
this.ws.onmessage = (event) => {
|
|
16
|
+
const data = JSON.parse(event.data);
|
|
17
|
+
this.notifyListeners(data.type, data);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
this.ws.onerror = (error) => {
|
|
21
|
+
console.error('❌ WebSocket 错误:', error);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
this.ws.onclose = () => {
|
|
25
|
+
console.log('🔌 WebSocket 连接关闭');
|
|
26
|
+
setTimeout(() => this.connect(token), 3000);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
send(data) {
|
|
31
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
32
|
+
this.ws.send(JSON.stringify(data));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
on(event, callback) {
|
|
37
|
+
if (!this.listeners.has(event)) {
|
|
38
|
+
this.listeners.set(event, []);
|
|
39
|
+
}
|
|
40
|
+
this.listeners.get(event).push(callback);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
off(event, callback) {
|
|
44
|
+
if (this.listeners.has(event)) {
|
|
45
|
+
const callbacks = this.listeners.get(event);
|
|
46
|
+
const index = callbacks.indexOf(callback);
|
|
47
|
+
if (index > -1) {
|
|
48
|
+
callbacks.splice(index, 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
notifyListeners(event, data) {
|
|
54
|
+
if (this.listeners.has(event)) {
|
|
55
|
+
this.listeners.get(event).forEach(callback => callback(data));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
joinGroup(groupId) {
|
|
60
|
+
this.send({ type: 'join_group', groupId });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
sendChatMessage(groupId, username, content) {
|
|
64
|
+
this.send({ type: 'chat_message', groupId, username, content });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
syncDocument(documentId, content, cursorPosition) {
|
|
68
|
+
this.send({ type: 'document_sync', documentId, content, cursorPosition });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
respondToCall(groupId, username) {
|
|
72
|
+
this.send({ type: 'call_response', groupId, username });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
sendTyping(documentId, username, isTyping) {
|
|
76
|
+
this.send({ type: 'typing', documentId, username, isTyping });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
190
80
|
|