zsl-im-sdk 1.0.0 → 1.0.2
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/dist/api/common/common.api.d.ts +1 -0
- package/dist/api/common/common.api.js +13 -0
- package/dist/api/conversation/conversation.api.d.ts +54 -0
- package/dist/api/conversation/conversation.api.js +88 -0
- package/dist/api/im-login.d.ts +11 -0
- package/dist/api/im-login.js +16 -0
- package/dist/api/message/message.api.d.ts +23 -0
- package/dist/api/message/message.api.js +36 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +76 -0
- package/dist/store.d.ts +61 -0
- package/dist/store.js +493 -0
- package/dist/types/conversation.type.d.ts +42 -0
- package/dist/types/conversation.type.js +8 -0
- package/dist/types/im-user.type.d.ts +22 -0
- package/dist/types/im-user.type.js +2 -0
- package/{types/index.ts → dist/types/index.d.ts} +4 -5
- package/dist/types/index.js +21 -0
- package/dist/types/message-type.type.d.ts +76 -0
- package/dist/types/message-type.type.js +48 -0
- package/dist/types/socket-data.type.d.ts +38 -0
- package/dist/types/socket-data.type.js +19 -0
- package/dist/types.d.ts +62 -0
- package/dist/types.js +5 -0
- package/dist/utils/http.d.ts +41 -0
- package/dist/utils/http.js +159 -0
- package/dist/websocket.d.ts +46 -0
- package/dist/websocket.js +302 -0
- package/package.json +34 -5
- package/api/common/common.api.ts +0 -7
- package/api/conversation/conversation.api.ts +0 -93
- package/api/im-login.ts +0 -18
- package/api/message/message.api.ts +0 -51
- package/index.ts +0 -36
- package/jsconfig.json +0 -14
- package/store.ts +0 -692
- package/types/conversation.type.ts +0 -60
- package/types/im-user.type.ts +0 -31
- package/types/message-type.type.ts +0 -94
- package/types/socket-data.type.ts +0 -55
- package/types.ts +0 -66
- package/utils/http.ts +0 -195
- package/websocket.ts +0 -367
package/store.ts
DELETED
|
@@ -1,692 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IM 状态管理 Store
|
|
3
|
-
* 使用 Vuex 管理会话、消息、用户、WebSocket 连接等状态
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import Vue from "vue";
|
|
7
|
-
import Vuex, { Store } from "vuex";
|
|
8
|
-
import IM from "./websocket";
|
|
9
|
-
import { MessageType } from "./types/message-type.type";
|
|
10
|
-
import { IConversation } from "./types/conversation.type";
|
|
11
|
-
|
|
12
|
-
Vue.use(Vuex);
|
|
13
|
-
|
|
14
|
-
// ==================== 类型定义 ====================
|
|
15
|
-
|
|
16
|
-
/** 消息类型 */
|
|
17
|
-
export interface IMessage {
|
|
18
|
-
id: string;
|
|
19
|
-
conversationId: string;
|
|
20
|
-
content: string;
|
|
21
|
-
messageType: string;
|
|
22
|
-
senderId: string | number;
|
|
23
|
-
createdAt: string;
|
|
24
|
-
status: "SENDING" | "SENT" | "FAILED";
|
|
25
|
-
icon?: string;
|
|
26
|
-
recipientId?: string;
|
|
27
|
-
mediaUrl?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** 用户类型 */
|
|
31
|
-
export interface IUser {
|
|
32
|
-
account: string;
|
|
33
|
-
avatarUrl?: string;
|
|
34
|
-
biz: string;
|
|
35
|
-
createdAt: string;
|
|
36
|
-
displayName: string;
|
|
37
|
-
id: string | number;
|
|
38
|
-
isDel: number;
|
|
39
|
-
status: string;
|
|
40
|
-
token: string;
|
|
41
|
-
updatedAt: string;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** 根状态类型 */
|
|
45
|
-
export interface IState {
|
|
46
|
-
// 会话状态
|
|
47
|
-
conversation: {
|
|
48
|
-
conversations: IConversation[];
|
|
49
|
-
selectedConversationId: string;
|
|
50
|
-
};
|
|
51
|
-
// 消息状态
|
|
52
|
-
message: {
|
|
53
|
-
messages: Record<string, IMessage[]>;
|
|
54
|
-
messageQueue: IMessage[];
|
|
55
|
-
};
|
|
56
|
-
// 用户状态
|
|
57
|
-
user: {
|
|
58
|
-
currentUser: IUser | null;
|
|
59
|
-
users: Record<string | number, IUser>;
|
|
60
|
-
};
|
|
61
|
-
// WebSocket 状态
|
|
62
|
-
websocket: {
|
|
63
|
-
isConnected: boolean;
|
|
64
|
-
reconnectAttempts: number;
|
|
65
|
-
imInstance: IM | null;
|
|
66
|
-
};
|
|
67
|
-
// 全局状态
|
|
68
|
-
global: {
|
|
69
|
-
loading: boolean;
|
|
70
|
-
error: string | null;
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// ==================== Persistence ====================
|
|
75
|
-
const STORAGE_KEY = 'ZSL_IM_STORE_STATE';
|
|
76
|
-
|
|
77
|
-
const loadState = (): Partial<IState> => {
|
|
78
|
-
try {
|
|
79
|
-
const stateStr = uni.getStorageSync(STORAGE_KEY);
|
|
80
|
-
if (stateStr) {
|
|
81
|
-
return JSON.parse(stateStr);
|
|
82
|
-
}
|
|
83
|
-
} catch (e) {
|
|
84
|
-
console.error('Load state error:', e);
|
|
85
|
-
}
|
|
86
|
-
return {};
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const saveState = (state: IState) => {
|
|
90
|
-
const persistState = {
|
|
91
|
-
conversation: state.conversation,
|
|
92
|
-
message: state.message,
|
|
93
|
-
user: state.user
|
|
94
|
-
};
|
|
95
|
-
try {
|
|
96
|
-
uni.setStorageSync(STORAGE_KEY, JSON.stringify(persistState));
|
|
97
|
-
} catch (e) {
|
|
98
|
-
console.error('Save state error:', e);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const persistencePlugin = (store: Store<IState>) => {
|
|
103
|
-
store.subscribe((mutation, state) => {
|
|
104
|
-
if (mutation.type.startsWith('conversation/') ||
|
|
105
|
-
mutation.type.startsWith('message/') ||
|
|
106
|
-
mutation.type.startsWith('user/')) {
|
|
107
|
-
saveState(state);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// ==================== Vuex Store ====================
|
|
113
|
-
|
|
114
|
-
const savedState = loadState();
|
|
115
|
-
|
|
116
|
-
const store = new Vuex.Store<IState>({
|
|
117
|
-
state: {
|
|
118
|
-
conversation: {
|
|
119
|
-
conversations: [],
|
|
120
|
-
selectedConversationId: "",
|
|
121
|
-
...(savedState.conversation || {})
|
|
122
|
-
},
|
|
123
|
-
message: {
|
|
124
|
-
messages: {},
|
|
125
|
-
messageQueue: [],
|
|
126
|
-
...(savedState.message || {})
|
|
127
|
-
},
|
|
128
|
-
user: {
|
|
129
|
-
currentUser: null,
|
|
130
|
-
users: {},
|
|
131
|
-
...(savedState.user || {})
|
|
132
|
-
},
|
|
133
|
-
websocket: {
|
|
134
|
-
isConnected: false,
|
|
135
|
-
reconnectAttempts: 0,
|
|
136
|
-
imInstance: null,
|
|
137
|
-
},
|
|
138
|
-
global: {
|
|
139
|
-
loading: false,
|
|
140
|
-
error: null,
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
plugins: [persistencePlugin],
|
|
144
|
-
|
|
145
|
-
// ==================== Mutations ====================
|
|
146
|
-
mutations: {
|
|
147
|
-
// ---------- 会话相关 ----------
|
|
148
|
-
/** 设置会话列表 */
|
|
149
|
-
"conversation/SET_CONVERSATIONS"(state, conversations: IConversation[]) {
|
|
150
|
-
state.conversation.conversations = conversations;
|
|
151
|
-
},
|
|
152
|
-
|
|
153
|
-
/** 添加或更新会话 */
|
|
154
|
-
"conversation/ADD_CONVERSATION"(state, conversation: IConversation) {
|
|
155
|
-
const index = state.conversation.conversations.findIndex(
|
|
156
|
-
(c) => c.conversationId === conversation.conversationId,
|
|
157
|
-
);
|
|
158
|
-
if (index === -1) {
|
|
159
|
-
state.conversation.conversations.unshift(conversation);
|
|
160
|
-
} else {
|
|
161
|
-
state.conversation.conversations.splice(index, 1, conversation);
|
|
162
|
-
state.conversation.conversations.unshift(
|
|
163
|
-
state.conversation.conversations.splice(index, 1)[0],
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
},
|
|
167
|
-
|
|
168
|
-
/** 更新会话信息 */
|
|
169
|
-
"conversation/UPDATE_CONVERSATION"(
|
|
170
|
-
state,
|
|
171
|
-
{
|
|
172
|
-
conversationId,
|
|
173
|
-
updates,
|
|
174
|
-
}: { conversationId: string; updates: Partial<IConversation> },
|
|
175
|
-
) {
|
|
176
|
-
const conversation = state.conversation.conversations.find(
|
|
177
|
-
(c) => c.conversationId === conversationId,
|
|
178
|
-
);
|
|
179
|
-
if (conversation) Object.assign(conversation, updates);
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
/** 删除会话 */
|
|
183
|
-
"conversation/DELETE_CONVERSATION"(state, conversationId: string) {
|
|
184
|
-
state.conversation.conversations =
|
|
185
|
-
state.conversation.conversations.filter(
|
|
186
|
-
(c) => c.conversationId !== conversationId,
|
|
187
|
-
);
|
|
188
|
-
if (state.conversation.selectedConversationId === conversationId) {
|
|
189
|
-
state.conversation.selectedConversationId = "";
|
|
190
|
-
}
|
|
191
|
-
},
|
|
192
|
-
|
|
193
|
-
/** 设置当前选中的会话 */
|
|
194
|
-
"conversation/SET_SELECTED_CONVERSATION"(state, conversationId: string) {
|
|
195
|
-
state.conversation.selectedConversationId = conversationId;
|
|
196
|
-
},
|
|
197
|
-
|
|
198
|
-
/** 重置会话状态 */
|
|
199
|
-
"conversation/RESET_STATE"(state) {
|
|
200
|
-
state.conversation.conversations = [];
|
|
201
|
-
state.conversation.selectedConversationId = "";
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
// ---------- 消息相关 ----------
|
|
205
|
-
/** 设置指定会话的消息列表 */
|
|
206
|
-
"message/SET_MESSAGES"(
|
|
207
|
-
state,
|
|
208
|
-
{
|
|
209
|
-
conversationId,
|
|
210
|
-
messages,
|
|
211
|
-
}: { conversationId: string; messages: IMessage[] },
|
|
212
|
-
) {
|
|
213
|
-
state.message.messages[conversationId] = messages;
|
|
214
|
-
},
|
|
215
|
-
|
|
216
|
-
/** 添加消息 */
|
|
217
|
-
"message/ADD_MESSAGE"(state, message: IMessage) {
|
|
218
|
-
if (!state.message.messages[message.conversationId]) {
|
|
219
|
-
state.message.messages[message.conversationId] = [];
|
|
220
|
-
}
|
|
221
|
-
state.message.messages[message.conversationId].push(message);
|
|
222
|
-
},
|
|
223
|
-
|
|
224
|
-
/** 更新消息 */
|
|
225
|
-
"message/UPDATE_MESSAGE"(
|
|
226
|
-
state,
|
|
227
|
-
{
|
|
228
|
-
messageId,
|
|
229
|
-
conversationId,
|
|
230
|
-
updates,
|
|
231
|
-
}: {
|
|
232
|
-
messageId: string | number;
|
|
233
|
-
conversationId: string;
|
|
234
|
-
updates: Partial<IMessage>;
|
|
235
|
-
},
|
|
236
|
-
) {
|
|
237
|
-
const messages = state.message.messages[conversationId];
|
|
238
|
-
if (messages) {
|
|
239
|
-
const message = messages.find((m) => m.id === messageId);
|
|
240
|
-
if (message) Object.assign(message, updates);
|
|
241
|
-
}
|
|
242
|
-
},
|
|
243
|
-
|
|
244
|
-
/** 添加到消息队列 */
|
|
245
|
-
"message/ADD_TO_MESSAGE_QUEUE"(state, message: IMessage) {
|
|
246
|
-
state.message.messageQueue.push(message);
|
|
247
|
-
},
|
|
248
|
-
|
|
249
|
-
/** 从消息队列移除 */
|
|
250
|
-
"message/REMOVE_FROM_MESSAGE_QUEUE"(state, messageId: string | number) {
|
|
251
|
-
state.message.messageQueue = state.message.messageQueue.filter(
|
|
252
|
-
(m) => m.id !== messageId,
|
|
253
|
-
);
|
|
254
|
-
},
|
|
255
|
-
|
|
256
|
-
/** 重置消息状态 */
|
|
257
|
-
"message/RESET_STATE"(state) {
|
|
258
|
-
state.message.messages = {};
|
|
259
|
-
state.message.messageQueue = [];
|
|
260
|
-
},
|
|
261
|
-
|
|
262
|
-
// ---------- 用户相关 ----------
|
|
263
|
-
/** 设置当前用户 */
|
|
264
|
-
"user/SET_CURRENT_USER"(state, user: IUser) {
|
|
265
|
-
state.user.currentUser = user;
|
|
266
|
-
},
|
|
267
|
-
|
|
268
|
-
/** 添加用户 */
|
|
269
|
-
"user/ADD_USER"(state, user: IUser) {
|
|
270
|
-
state.user.users[user.id] = user;
|
|
271
|
-
},
|
|
272
|
-
|
|
273
|
-
/** 更新用户信息 */
|
|
274
|
-
"user/UPDATE_USER"(
|
|
275
|
-
state,
|
|
276
|
-
{ userId, updates }: { userId: string | number; updates: Partial<IUser> },
|
|
277
|
-
) {
|
|
278
|
-
if (state.user.users[userId]) {
|
|
279
|
-
Object.assign(state.user.users[userId], updates);
|
|
280
|
-
}
|
|
281
|
-
},
|
|
282
|
-
|
|
283
|
-
/** 重置用户状态 */
|
|
284
|
-
"user/RESET_STATE"(state) {
|
|
285
|
-
state.user.currentUser = null;
|
|
286
|
-
state.user.users = {};
|
|
287
|
-
},
|
|
288
|
-
|
|
289
|
-
// ---------- WebSocket 相关 ----------
|
|
290
|
-
/** 设置连接状态 */
|
|
291
|
-
"websocket/SET_CONNECTED"(state, isConnected: boolean) {
|
|
292
|
-
console.log("WebSocket 连接状态:", isConnected);
|
|
293
|
-
state.websocket.isConnected = isConnected;
|
|
294
|
-
if (isConnected) state.websocket.reconnectAttempts = 0;
|
|
295
|
-
},
|
|
296
|
-
|
|
297
|
-
/** 增加重连次数 */
|
|
298
|
-
"websocket/INCREMENT_RECONNECT_ATTEMPTS"(state) {
|
|
299
|
-
state.websocket.reconnectAttempts++;
|
|
300
|
-
},
|
|
301
|
-
|
|
302
|
-
/** 设置 IM 实例 */
|
|
303
|
-
"websocket/SET_IM_INSTANCE"(state, imInstance: IM) {
|
|
304
|
-
state.websocket.imInstance = imInstance;
|
|
305
|
-
},
|
|
306
|
-
|
|
307
|
-
/** 重置 WebSocket 状态 */
|
|
308
|
-
"websocket/RESET_STATE"(state) {
|
|
309
|
-
if (state.websocket.imInstance) {
|
|
310
|
-
state.websocket.imInstance.disconnect();
|
|
311
|
-
state.websocket.imInstance = null;
|
|
312
|
-
}
|
|
313
|
-
state.websocket.isConnected = false;
|
|
314
|
-
state.websocket.reconnectAttempts = 0;
|
|
315
|
-
},
|
|
316
|
-
|
|
317
|
-
// ---------- 全局状态 ----------
|
|
318
|
-
/** 设置加载状态 */
|
|
319
|
-
"global/SET_LOADING"(state, loading: boolean) {
|
|
320
|
-
state.global.loading = loading;
|
|
321
|
-
},
|
|
322
|
-
|
|
323
|
-
/** 设置错误信息 */
|
|
324
|
-
"global/SET_ERROR"(state, error: string | null) {
|
|
325
|
-
state.global.error = error;
|
|
326
|
-
},
|
|
327
|
-
|
|
328
|
-
/** 重置全局状态 */
|
|
329
|
-
"global/RESET_STATE"(state) {
|
|
330
|
-
state.global.loading = false;
|
|
331
|
-
state.global.error = null;
|
|
332
|
-
},
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
// ==================== Actions ====================
|
|
336
|
-
actions: {
|
|
337
|
-
/** 重置所有状态 */
|
|
338
|
-
resetState({ commit }) {
|
|
339
|
-
commit("conversation/RESET_STATE");
|
|
340
|
-
commit("message/RESET_STATE");
|
|
341
|
-
commit("user/RESET_STATE");
|
|
342
|
-
commit("websocket/RESET_STATE");
|
|
343
|
-
commit("global/RESET_STATE");
|
|
344
|
-
},
|
|
345
|
-
|
|
346
|
-
/** 连接 WebSocket */
|
|
347
|
-
async connectWebSocket(
|
|
348
|
-
{ commit, state, dispatch },
|
|
349
|
-
payload?: { wsUrl: string; token: string; userId: string | number },
|
|
350
|
-
) {
|
|
351
|
-
if (!payload) throw new Error("WebSocket 连接参数缺失");
|
|
352
|
-
|
|
353
|
-
const { wsUrl, token, userId } = payload;
|
|
354
|
-
|
|
355
|
-
// 如果已存在实例,先断开
|
|
356
|
-
if (state.websocket.imInstance) {
|
|
357
|
-
state.websocket.imInstance.disconnect();
|
|
358
|
-
commit("websocket/SET_IM_INSTANCE", null);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// 创建新实例
|
|
362
|
-
const imInstance = new IM({ url: wsUrl, token, userId });
|
|
363
|
-
|
|
364
|
-
// 设置事件监听
|
|
365
|
-
imInstance.on("connect", () => commit("websocket/SET_CONNECTED", true));
|
|
366
|
-
imInstance.on("disconnect", () => {
|
|
367
|
-
commit("websocket/SET_CONNECTED", false);
|
|
368
|
-
commit("websocket/INCREMENT_RECONNECT_ATTEMPTS");
|
|
369
|
-
});
|
|
370
|
-
imInstance.on("error", (error) =>
|
|
371
|
-
console.error("WebSocket 错误:", error),
|
|
372
|
-
);
|
|
373
|
-
imInstance.on("message", (message) =>
|
|
374
|
-
dispatch("handleWebSocketMessage", message),
|
|
375
|
-
);
|
|
376
|
-
// 撤回消息
|
|
377
|
-
imInstance.on("recalled", (message) =>
|
|
378
|
-
dispatch("handleWebSocketRecalledMessage", message),
|
|
379
|
-
);
|
|
380
|
-
imInstance.on("reconnect", (attempt) =>
|
|
381
|
-
console.log(`WebSocket 重连中,第 ${attempt} 次尝试`),
|
|
382
|
-
);
|
|
383
|
-
|
|
384
|
-
commit("websocket/SET_IM_INSTANCE", imInstance);
|
|
385
|
-
|
|
386
|
-
try {
|
|
387
|
-
await imInstance.connect();
|
|
388
|
-
} catch (error) {
|
|
389
|
-
console.error("WebSocket 连接失败:", error);
|
|
390
|
-
commit("websocket/SET_CONNECTED", false);
|
|
391
|
-
throw error;
|
|
392
|
-
}
|
|
393
|
-
},
|
|
394
|
-
|
|
395
|
-
/** 断开 WebSocket */
|
|
396
|
-
disconnectWebSocket({ commit, state }) {
|
|
397
|
-
if (state.websocket.imInstance) {
|
|
398
|
-
state.websocket.imInstance.disconnect();
|
|
399
|
-
commit("websocket/SET_IM_INSTANCE", null);
|
|
400
|
-
}
|
|
401
|
-
commit("websocket/SET_CONNECTED", false);
|
|
402
|
-
},
|
|
403
|
-
|
|
404
|
-
/** 处理 WebSocket 消息 */
|
|
405
|
-
async handleWebSocketMessage({ commit, state, dispatch }, message: any) {
|
|
406
|
-
try {
|
|
407
|
-
console.log("收到 WebSocket 消息:", message);
|
|
408
|
-
|
|
409
|
-
if (!message.conversationId) {
|
|
410
|
-
console.error("消息缺少 conversationId:", message);
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// 添加消息
|
|
415
|
-
commit("message/ADD_MESSAGE", message);
|
|
416
|
-
|
|
417
|
-
// 更新会话
|
|
418
|
-
const conversation = state.conversation.conversations.find(
|
|
419
|
-
(c) => c.conversationId === message.conversationId,
|
|
420
|
-
);
|
|
421
|
-
if (conversation) {
|
|
422
|
-
const updates: any = {
|
|
423
|
-
lastMessageId: message.id,
|
|
424
|
-
lastMessageContent: message.content,
|
|
425
|
-
lastMessageAt: message.createdAt,
|
|
426
|
-
lastMessageType: message.messageType,
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
// 判断是否是当前打开的会话
|
|
430
|
-
const isCurrentConversation =
|
|
431
|
-
message.conversationId ===
|
|
432
|
-
state.conversation.selectedConversationId;
|
|
433
|
-
|
|
434
|
-
if (isCurrentConversation) {
|
|
435
|
-
// 如果是当前会话,自动发送已读回执
|
|
436
|
-
try {
|
|
437
|
-
await dispatch("sendReadReceipt", {
|
|
438
|
-
conversationId: message.conversationId,
|
|
439
|
-
lastReadMessageId: message.id,
|
|
440
|
-
});
|
|
441
|
-
console.log("已自动发送已读回执:", message.id);
|
|
442
|
-
} catch (error) {
|
|
443
|
-
console.error("发送已读回执失败:", error);
|
|
444
|
-
}
|
|
445
|
-
} else {
|
|
446
|
-
// 如果不是当前会话,增加未读数
|
|
447
|
-
updates.unreadCount = (conversation.unreadCount || 0) + 1;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
commit("conversation/UPDATE_CONVERSATION", {
|
|
451
|
-
conversationId: message.conversationId,
|
|
452
|
-
updates,
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
} catch (error) {
|
|
456
|
-
console.error("处理 WebSocket 消息时出错:", error);
|
|
457
|
-
}
|
|
458
|
-
},
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
/** 处理 WebSocket 收到撤回消息 */
|
|
462
|
-
async handleWebSocketRecalledMessage({ commit, state, dispatch }, message: any) {
|
|
463
|
-
try {
|
|
464
|
-
console.log("收到 WebSocket 撤回消息:", message);
|
|
465
|
-
|
|
466
|
-
if (!message.conversationId) {
|
|
467
|
-
console.error("消息缺少 conversationId:", message);
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// 更新消息状态为撤回
|
|
472
|
-
const recalledMsgId = message.id; // 假设撤回消息的内容是原消息ID
|
|
473
|
-
commit("message/UPDATE_MESSAGE", {
|
|
474
|
-
messageId: recalledMsgId,
|
|
475
|
-
conversationId: message.conversationId,
|
|
476
|
-
updates: { status: "RECALLED" },
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
// 更新会话最后一条消息(如果是被撤回的那条)
|
|
480
|
-
const conversation = state.conversation.conversations.find(
|
|
481
|
-
(c) => c.conversationId === message.conversationId,
|
|
482
|
-
);
|
|
483
|
-
if (conversation && conversation.lastMessageId === recalledMsgId) {
|
|
484
|
-
const updates: any = {
|
|
485
|
-
lastMessageContent: "对方撤回了一条消息", // 或者 "你撤回了一条消息"
|
|
486
|
-
lastMessageType: "RECALLED",
|
|
487
|
-
};
|
|
488
|
-
commit("conversation/UPDATE_CONVERSATION", {
|
|
489
|
-
conversationId: message.conversationId,
|
|
490
|
-
updates,
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
} catch (error) {
|
|
494
|
-
console.error("处理 WebSocket 撤回消息时出错:", error);
|
|
495
|
-
}
|
|
496
|
-
},
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
/** 发送消息 */
|
|
500
|
-
async sendMessage(
|
|
501
|
-
{ commit, state },
|
|
502
|
-
messageData: Omit<IMessage, "id" | "createdAt" | "status">,
|
|
503
|
-
) {
|
|
504
|
-
const newMessage: IMessage = {
|
|
505
|
-
...messageData,
|
|
506
|
-
id: `temp-${Date.now()}`,
|
|
507
|
-
createdAt: new Date().toISOString(),
|
|
508
|
-
status: "SENDING",
|
|
509
|
-
};
|
|
510
|
-
|
|
511
|
-
// 添加到队列
|
|
512
|
-
commit("message/ADD_TO_MESSAGE_QUEUE", newMessage);
|
|
513
|
-
|
|
514
|
-
// 更新会话
|
|
515
|
-
const conversation = state.conversation.conversations.find(
|
|
516
|
-
(c) => c.conversationId === newMessage.conversationId,
|
|
517
|
-
);
|
|
518
|
-
if (conversation) {
|
|
519
|
-
commit("conversation/UPDATE_CONVERSATION", {
|
|
520
|
-
conversationId: newMessage.conversationId,
|
|
521
|
-
updates: {
|
|
522
|
-
lastMessageId: newMessage.id,
|
|
523
|
-
lastMessageContent: newMessage.content,
|
|
524
|
-
lastMessageAt: newMessage.createdAt,
|
|
525
|
-
lastMessageType: newMessage.messageType as MessageType,
|
|
526
|
-
},
|
|
527
|
-
});
|
|
528
|
-
} else {
|
|
529
|
-
// 创建新会话
|
|
530
|
-
commit("conversation/ADD_CONVERSATION", {
|
|
531
|
-
conversationId: newMessage.conversationId,
|
|
532
|
-
conversationType: newMessage.conversationId.includes("p2p")
|
|
533
|
-
? "P2P"
|
|
534
|
-
: "GROUP",
|
|
535
|
-
isMuted: false,
|
|
536
|
-
isPinned: false,
|
|
537
|
-
lastMessageAt: newMessage.createdAt,
|
|
538
|
-
lastMessageContent: newMessage.content,
|
|
539
|
-
unreadCount: 1,
|
|
540
|
-
user: {
|
|
541
|
-
userId: newMessage.senderId,
|
|
542
|
-
displayName: "用户:" + newMessage.senderId,
|
|
543
|
-
account: newMessage.senderId as string,
|
|
544
|
-
avatarUrl: newMessage.icon || "/static/images/default-avatar.png",
|
|
545
|
-
},
|
|
546
|
-
lastMessageType: newMessage.messageType as MessageType,
|
|
547
|
-
lastMessageSenderId: newMessage.senderId,
|
|
548
|
-
lastMessageSenderName: newMessage.senderId as string,
|
|
549
|
-
status: 0,
|
|
550
|
-
recalled: false,
|
|
551
|
-
recaller: { id: "", name: "" },
|
|
552
|
-
payload: { text: newMessage.content || "" },
|
|
553
|
-
lastMessageId: newMessage.id,
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
try {
|
|
558
|
-
// 发送消息
|
|
559
|
-
if (!state.websocket.imInstance) throw new Error("WebSocket 未连接");
|
|
560
|
-
|
|
561
|
-
await state.websocket.imInstance.sendText(
|
|
562
|
-
newMessage.conversationId,
|
|
563
|
-
newMessage.recipientId!,
|
|
564
|
-
newMessage.content,
|
|
565
|
-
newMessage.messageType as MessageType,
|
|
566
|
-
newMessage.mediaUrl,
|
|
567
|
-
);
|
|
568
|
-
|
|
569
|
-
// 更新状态
|
|
570
|
-
commit("message/UPDATE_MESSAGE", {
|
|
571
|
-
messageId: newMessage.id,
|
|
572
|
-
conversationId: newMessage.conversationId,
|
|
573
|
-
updates: { status: "SENT" },
|
|
574
|
-
});
|
|
575
|
-
commit("message/REMOVE_FROM_MESSAGE_QUEUE", newMessage.id);
|
|
576
|
-
} catch (error) {
|
|
577
|
-
commit("message/UPDATE_MESSAGE", {
|
|
578
|
-
messageId: newMessage.id,
|
|
579
|
-
conversationId: newMessage.conversationId,
|
|
580
|
-
updates: { status: "FAILED" },
|
|
581
|
-
});
|
|
582
|
-
commit("global/SET_ERROR", "发送消息失败");
|
|
583
|
-
console.error("发送消息失败:", error);
|
|
584
|
-
throw error;
|
|
585
|
-
}
|
|
586
|
-
},
|
|
587
|
-
|
|
588
|
-
/** 发送已读回执 */
|
|
589
|
-
async sendReadReceipt(
|
|
590
|
-
{ state },
|
|
591
|
-
payload: { conversationId: string; lastReadMessageId?: number },
|
|
592
|
-
) {
|
|
593
|
-
if (!state.websocket.imInstance) throw new Error("WebSocket 未连接");
|
|
594
|
-
return state.websocket.imInstance.sendReadReceipt(
|
|
595
|
-
payload.conversationId,
|
|
596
|
-
payload.lastReadMessageId,
|
|
597
|
-
);
|
|
598
|
-
},
|
|
599
|
-
|
|
600
|
-
/** 撤回消息 */
|
|
601
|
-
async sendRecallMessage({ state }, payload: { messageId: number }) {
|
|
602
|
-
if (!state.websocket.imInstance) throw new Error("WebSocket 未连接");
|
|
603
|
-
return state.websocket.imInstance.sendRecallMessage(payload.messageId);
|
|
604
|
-
},
|
|
605
|
-
|
|
606
|
-
/**
|
|
607
|
-
* 用户登录
|
|
608
|
-
* 注意:SDK 不包含登录 API,需要在应用层实现
|
|
609
|
-
* 登录成功后调用 connectWebSocket 连接 WebSocket
|
|
610
|
-
*/
|
|
611
|
-
async login(
|
|
612
|
-
{ commit, dispatch },
|
|
613
|
-
loginParams: { user: IUser; wsUrl: string; token: string },
|
|
614
|
-
) {
|
|
615
|
-
commit("global/SET_LOADING", true);
|
|
616
|
-
|
|
617
|
-
console.log("=====login====", loginParams);
|
|
618
|
-
|
|
619
|
-
try {
|
|
620
|
-
const { user, wsUrl, token } = loginParams;
|
|
621
|
-
commit("user/SET_CURRENT_USER", user);
|
|
622
|
-
commit("user/ADD_USER", user);
|
|
623
|
-
await dispatch("connectWebSocket", { userId: user.id, wsUrl, token });
|
|
624
|
-
return user;
|
|
625
|
-
} catch (error) {
|
|
626
|
-
commit("global/SET_ERROR", "登录失败");
|
|
627
|
-
console.error("登录失败:", error);
|
|
628
|
-
throw error;
|
|
629
|
-
} finally {
|
|
630
|
-
commit("global/SET_LOADING", false);
|
|
631
|
-
}
|
|
632
|
-
},
|
|
633
|
-
|
|
634
|
-
/** 用户登出 */
|
|
635
|
-
logout({ dispatch }) {
|
|
636
|
-
dispatch("disconnectWebSocket");
|
|
637
|
-
dispatch("resetState");
|
|
638
|
-
},
|
|
639
|
-
},
|
|
640
|
-
|
|
641
|
-
// ==================== Getters ====================
|
|
642
|
-
getters: {
|
|
643
|
-
/** 排序后的会话列表 */
|
|
644
|
-
sortedConversations: (state) =>
|
|
645
|
-
[...state.conversation.conversations].sort(
|
|
646
|
-
(a, b) =>
|
|
647
|
-
new Date(b.lastMessageAt).getTime() -
|
|
648
|
-
new Date(a.lastMessageAt).getTime(),
|
|
649
|
-
),
|
|
650
|
-
|
|
651
|
-
/** 当前选中的会话 */
|
|
652
|
-
selectedConversation: (state) =>
|
|
653
|
-
state.conversation.conversations.find(
|
|
654
|
-
(c) => c.conversationId === state.conversation.selectedConversationId,
|
|
655
|
-
),
|
|
656
|
-
|
|
657
|
-
/** 获取指定会话的消息列表 */
|
|
658
|
-
conversationMessages: (state) => (conversationId: string) =>
|
|
659
|
-
state.message.messages[conversationId] || [],
|
|
660
|
-
|
|
661
|
-
/** 总未读消息数 */
|
|
662
|
-
unreadCount: (state) =>
|
|
663
|
-
state.conversation.conversations.reduce(
|
|
664
|
-
(total, c) => total + c.unreadCount,
|
|
665
|
-
0,
|
|
666
|
-
),
|
|
667
|
-
|
|
668
|
-
/** 获取用户信息 */
|
|
669
|
-
userInfo: (state) => (userId: string | number) =>
|
|
670
|
-
state.user.users[userId] || null,
|
|
671
|
-
|
|
672
|
-
/** WebSocket 连接状态 */
|
|
673
|
-
isConnected: (state) => state.websocket.isConnected,
|
|
674
|
-
|
|
675
|
-
/** 加载状态 */
|
|
676
|
-
loading: (state) => state.global.loading,
|
|
677
|
-
|
|
678
|
-
/** 错误信息 */
|
|
679
|
-
error: (state) => state.global.error,
|
|
680
|
-
},
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
// ==================== 插件安装 ====================
|
|
684
|
-
|
|
685
|
-
/** Vue 插件安装函数 */
|
|
686
|
-
export function install(vue: any) {
|
|
687
|
-
if (vue.prototype) {
|
|
688
|
-
vue.prototype.$store = store;
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
export default store;
|