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/dist/store.js
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* IM 状态管理 Store
|
|
4
|
+
* 使用 Vuex 管理会话、消息、用户、WebSocket 连接等状态
|
|
5
|
+
*/
|
|
6
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.install = install;
|
|
11
|
+
const vue_1 = __importDefault(require("vue"));
|
|
12
|
+
const vuex_1 = __importDefault(require("vuex"));
|
|
13
|
+
const websocket_1 = __importDefault(require("./websocket"));
|
|
14
|
+
vue_1.default.use(vuex_1.default);
|
|
15
|
+
// ==================== Persistence ====================
|
|
16
|
+
const STORAGE_KEY = 'ZSL_IM_STORE_STATE';
|
|
17
|
+
const loadState = () => {
|
|
18
|
+
try {
|
|
19
|
+
const stateStr = uni.getStorageSync(STORAGE_KEY);
|
|
20
|
+
if (stateStr) {
|
|
21
|
+
return JSON.parse(stateStr);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
console.error('Load state error:', e);
|
|
26
|
+
}
|
|
27
|
+
return {};
|
|
28
|
+
};
|
|
29
|
+
const saveState = (state) => {
|
|
30
|
+
const persistState = {
|
|
31
|
+
conversation: state.conversation,
|
|
32
|
+
message: state.message,
|
|
33
|
+
user: state.user
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
uni.setStorageSync(STORAGE_KEY, JSON.stringify(persistState));
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
console.error('Save state error:', e);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const persistencePlugin = (store) => {
|
|
43
|
+
store.subscribe((mutation, state) => {
|
|
44
|
+
if (mutation.type.startsWith('conversation/') ||
|
|
45
|
+
mutation.type.startsWith('message/') ||
|
|
46
|
+
mutation.type.startsWith('user/')) {
|
|
47
|
+
saveState(state);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
// ==================== Vuex Store ====================
|
|
52
|
+
const savedState = loadState();
|
|
53
|
+
const store = new vuex_1.default.Store({
|
|
54
|
+
state: {
|
|
55
|
+
conversation: {
|
|
56
|
+
conversations: [],
|
|
57
|
+
selectedConversationId: "",
|
|
58
|
+
...(savedState.conversation || {})
|
|
59
|
+
},
|
|
60
|
+
message: {
|
|
61
|
+
messages: {},
|
|
62
|
+
messageQueue: [],
|
|
63
|
+
...(savedState.message || {})
|
|
64
|
+
},
|
|
65
|
+
user: {
|
|
66
|
+
currentUser: null,
|
|
67
|
+
users: {},
|
|
68
|
+
...(savedState.user || {})
|
|
69
|
+
},
|
|
70
|
+
websocket: {
|
|
71
|
+
isConnected: false,
|
|
72
|
+
reconnectAttempts: 0,
|
|
73
|
+
imInstance: null,
|
|
74
|
+
},
|
|
75
|
+
global: {
|
|
76
|
+
loading: false,
|
|
77
|
+
error: null,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
plugins: [persistencePlugin],
|
|
81
|
+
// ==================== Mutations ====================
|
|
82
|
+
mutations: {
|
|
83
|
+
// ---------- 会话相关 ----------
|
|
84
|
+
/** 设置会话列表 */
|
|
85
|
+
"conversation/SET_CONVERSATIONS"(state, conversations) {
|
|
86
|
+
state.conversation.conversations = conversations;
|
|
87
|
+
},
|
|
88
|
+
/** 添加或更新会话 */
|
|
89
|
+
"conversation/ADD_CONVERSATION"(state, conversation) {
|
|
90
|
+
const index = state.conversation.conversations.findIndex((c) => c.conversationId === conversation.conversationId);
|
|
91
|
+
if (index === -1) {
|
|
92
|
+
state.conversation.conversations.unshift(conversation);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
state.conversation.conversations.splice(index, 1, conversation);
|
|
96
|
+
state.conversation.conversations.unshift(state.conversation.conversations.splice(index, 1)[0]);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
/** 更新会话信息 */
|
|
100
|
+
"conversation/UPDATE_CONVERSATION"(state, { conversationId, updates, }) {
|
|
101
|
+
const conversation = state.conversation.conversations.find((c) => c.conversationId === conversationId);
|
|
102
|
+
if (conversation)
|
|
103
|
+
Object.assign(conversation, updates);
|
|
104
|
+
},
|
|
105
|
+
/** 删除会话 */
|
|
106
|
+
"conversation/DELETE_CONVERSATION"(state, conversationId) {
|
|
107
|
+
state.conversation.conversations =
|
|
108
|
+
state.conversation.conversations.filter((c) => c.conversationId !== conversationId);
|
|
109
|
+
if (state.conversation.selectedConversationId === conversationId) {
|
|
110
|
+
state.conversation.selectedConversationId = "";
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
/** 设置当前选中的会话 */
|
|
114
|
+
"conversation/SET_SELECTED_CONVERSATION"(state, conversationId) {
|
|
115
|
+
state.conversation.selectedConversationId = conversationId;
|
|
116
|
+
},
|
|
117
|
+
/** 重置会话状态 */
|
|
118
|
+
"conversation/RESET_STATE"(state) {
|
|
119
|
+
state.conversation.conversations = [];
|
|
120
|
+
state.conversation.selectedConversationId = "";
|
|
121
|
+
},
|
|
122
|
+
// ---------- 消息相关 ----------
|
|
123
|
+
/** 设置指定会话的消息列表 */
|
|
124
|
+
"message/SET_MESSAGES"(state, { conversationId, messages, }) {
|
|
125
|
+
state.message.messages[conversationId] = messages;
|
|
126
|
+
},
|
|
127
|
+
/** 添加消息 */
|
|
128
|
+
"message/ADD_MESSAGE"(state, message) {
|
|
129
|
+
if (!state.message.messages[message.conversationId]) {
|
|
130
|
+
state.message.messages[message.conversationId] = [];
|
|
131
|
+
}
|
|
132
|
+
state.message.messages[message.conversationId].push(message);
|
|
133
|
+
},
|
|
134
|
+
/** 更新消息 */
|
|
135
|
+
"message/UPDATE_MESSAGE"(state, { messageId, conversationId, updates, }) {
|
|
136
|
+
const messages = state.message.messages[conversationId];
|
|
137
|
+
if (messages) {
|
|
138
|
+
const message = messages.find((m) => m.id === messageId);
|
|
139
|
+
if (message)
|
|
140
|
+
Object.assign(message, updates);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
/** 添加到消息队列 */
|
|
144
|
+
"message/ADD_TO_MESSAGE_QUEUE"(state, message) {
|
|
145
|
+
state.message.messageQueue.push(message);
|
|
146
|
+
},
|
|
147
|
+
/** 从消息队列移除 */
|
|
148
|
+
"message/REMOVE_FROM_MESSAGE_QUEUE"(state, messageId) {
|
|
149
|
+
state.message.messageQueue = state.message.messageQueue.filter((m) => m.id !== messageId);
|
|
150
|
+
},
|
|
151
|
+
/** 重置消息状态 */
|
|
152
|
+
"message/RESET_STATE"(state) {
|
|
153
|
+
state.message.messages = {};
|
|
154
|
+
state.message.messageQueue = [];
|
|
155
|
+
},
|
|
156
|
+
// ---------- 用户相关 ----------
|
|
157
|
+
/** 设置当前用户 */
|
|
158
|
+
"user/SET_CURRENT_USER"(state, user) {
|
|
159
|
+
state.user.currentUser = user;
|
|
160
|
+
},
|
|
161
|
+
/** 添加用户 */
|
|
162
|
+
"user/ADD_USER"(state, user) {
|
|
163
|
+
state.user.users[user.id] = user;
|
|
164
|
+
},
|
|
165
|
+
/** 更新用户信息 */
|
|
166
|
+
"user/UPDATE_USER"(state, { userId, updates }) {
|
|
167
|
+
if (state.user.users[userId]) {
|
|
168
|
+
Object.assign(state.user.users[userId], updates);
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
/** 重置用户状态 */
|
|
172
|
+
"user/RESET_STATE"(state) {
|
|
173
|
+
state.user.currentUser = null;
|
|
174
|
+
state.user.users = {};
|
|
175
|
+
},
|
|
176
|
+
// ---------- WebSocket 相关 ----------
|
|
177
|
+
/** 设置连接状态 */
|
|
178
|
+
"websocket/SET_CONNECTED"(state, isConnected) {
|
|
179
|
+
console.log("WebSocket 连接状态:", isConnected);
|
|
180
|
+
state.websocket.isConnected = isConnected;
|
|
181
|
+
if (isConnected)
|
|
182
|
+
state.websocket.reconnectAttempts = 0;
|
|
183
|
+
},
|
|
184
|
+
/** 增加重连次数 */
|
|
185
|
+
"websocket/INCREMENT_RECONNECT_ATTEMPTS"(state) {
|
|
186
|
+
state.websocket.reconnectAttempts++;
|
|
187
|
+
},
|
|
188
|
+
/** 设置 IM 实例 */
|
|
189
|
+
"websocket/SET_IM_INSTANCE"(state, imInstance) {
|
|
190
|
+
state.websocket.imInstance = imInstance;
|
|
191
|
+
},
|
|
192
|
+
/** 重置 WebSocket 状态 */
|
|
193
|
+
"websocket/RESET_STATE"(state) {
|
|
194
|
+
if (state.websocket.imInstance) {
|
|
195
|
+
state.websocket.imInstance.disconnect();
|
|
196
|
+
state.websocket.imInstance = null;
|
|
197
|
+
}
|
|
198
|
+
state.websocket.isConnected = false;
|
|
199
|
+
state.websocket.reconnectAttempts = 0;
|
|
200
|
+
},
|
|
201
|
+
// ---------- 全局状态 ----------
|
|
202
|
+
/** 设置加载状态 */
|
|
203
|
+
"global/SET_LOADING"(state, loading) {
|
|
204
|
+
state.global.loading = loading;
|
|
205
|
+
},
|
|
206
|
+
/** 设置错误信息 */
|
|
207
|
+
"global/SET_ERROR"(state, error) {
|
|
208
|
+
state.global.error = error;
|
|
209
|
+
},
|
|
210
|
+
/** 重置全局状态 */
|
|
211
|
+
"global/RESET_STATE"(state) {
|
|
212
|
+
state.global.loading = false;
|
|
213
|
+
state.global.error = null;
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
// ==================== Actions ====================
|
|
217
|
+
actions: {
|
|
218
|
+
/** 重置所有状态 */
|
|
219
|
+
resetState({ commit }) {
|
|
220
|
+
commit("conversation/RESET_STATE");
|
|
221
|
+
commit("message/RESET_STATE");
|
|
222
|
+
commit("user/RESET_STATE");
|
|
223
|
+
commit("websocket/RESET_STATE");
|
|
224
|
+
commit("global/RESET_STATE");
|
|
225
|
+
},
|
|
226
|
+
/** 连接 WebSocket */
|
|
227
|
+
async connectWebSocket({ commit, state, dispatch }, payload) {
|
|
228
|
+
if (!payload)
|
|
229
|
+
throw new Error("WebSocket 连接参数缺失");
|
|
230
|
+
const { wsUrl, token, userId } = payload;
|
|
231
|
+
// 如果已存在实例,先断开
|
|
232
|
+
if (state.websocket.imInstance) {
|
|
233
|
+
state.websocket.imInstance.disconnect();
|
|
234
|
+
commit("websocket/SET_IM_INSTANCE", null);
|
|
235
|
+
}
|
|
236
|
+
// 创建新实例
|
|
237
|
+
const imInstance = new websocket_1.default({ url: wsUrl, token, userId });
|
|
238
|
+
// 设置事件监听
|
|
239
|
+
imInstance.on("connect", () => commit("websocket/SET_CONNECTED", true));
|
|
240
|
+
imInstance.on("disconnect", () => {
|
|
241
|
+
commit("websocket/SET_CONNECTED", false);
|
|
242
|
+
commit("websocket/INCREMENT_RECONNECT_ATTEMPTS");
|
|
243
|
+
});
|
|
244
|
+
imInstance.on("error", (error) => console.error("WebSocket 错误:", error));
|
|
245
|
+
imInstance.on("message", (message) => dispatch("handleWebSocketMessage", message));
|
|
246
|
+
// 撤回消息
|
|
247
|
+
imInstance.on("recalled", (message) => dispatch("handleWebSocketRecalledMessage", message));
|
|
248
|
+
imInstance.on("reconnect", (attempt) => console.log(`WebSocket 重连中,第 ${attempt} 次尝试`));
|
|
249
|
+
commit("websocket/SET_IM_INSTANCE", imInstance);
|
|
250
|
+
try {
|
|
251
|
+
await imInstance.connect();
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
console.error("WebSocket 连接失败:", error);
|
|
255
|
+
commit("websocket/SET_CONNECTED", false);
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
/** 断开 WebSocket */
|
|
260
|
+
disconnectWebSocket({ commit, state }) {
|
|
261
|
+
if (state.websocket.imInstance) {
|
|
262
|
+
state.websocket.imInstance.disconnect();
|
|
263
|
+
commit("websocket/SET_IM_INSTANCE", null);
|
|
264
|
+
}
|
|
265
|
+
commit("websocket/SET_CONNECTED", false);
|
|
266
|
+
},
|
|
267
|
+
/** 处理 WebSocket 消息 */
|
|
268
|
+
async handleWebSocketMessage({ commit, state, dispatch }, message) {
|
|
269
|
+
try {
|
|
270
|
+
console.log("收到 WebSocket 消息:", message);
|
|
271
|
+
if (!message.conversationId) {
|
|
272
|
+
console.error("消息缺少 conversationId:", message);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
// 添加消息
|
|
276
|
+
commit("message/ADD_MESSAGE", message);
|
|
277
|
+
// 更新会话
|
|
278
|
+
const conversation = state.conversation.conversations.find((c) => c.conversationId === message.conversationId);
|
|
279
|
+
if (conversation) {
|
|
280
|
+
const updates = {
|
|
281
|
+
lastMessageId: message.id,
|
|
282
|
+
lastMessageContent: message.content,
|
|
283
|
+
lastMessageAt: message.createdAt,
|
|
284
|
+
lastMessageType: message.messageType,
|
|
285
|
+
};
|
|
286
|
+
// 判断是否是当前打开的会话
|
|
287
|
+
const isCurrentConversation = message.conversationId ===
|
|
288
|
+
state.conversation.selectedConversationId;
|
|
289
|
+
if (isCurrentConversation) {
|
|
290
|
+
// 如果是当前会话,自动发送已读回执
|
|
291
|
+
try {
|
|
292
|
+
await dispatch("sendReadReceipt", {
|
|
293
|
+
conversationId: message.conversationId,
|
|
294
|
+
lastReadMessageId: message.id,
|
|
295
|
+
});
|
|
296
|
+
console.log("已自动发送已读回执:", message.id);
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
console.error("发送已读回执失败:", error);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
// 如果不是当前会话,增加未读数
|
|
304
|
+
updates.unreadCount = (conversation.unreadCount || 0) + 1;
|
|
305
|
+
}
|
|
306
|
+
commit("conversation/UPDATE_CONVERSATION", {
|
|
307
|
+
conversationId: message.conversationId,
|
|
308
|
+
updates,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
console.error("处理 WebSocket 消息时出错:", error);
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
/** 处理 WebSocket 收到撤回消息 */
|
|
317
|
+
async handleWebSocketRecalledMessage({ commit, state, dispatch }, message) {
|
|
318
|
+
try {
|
|
319
|
+
console.log("收到 WebSocket 撤回消息:", message);
|
|
320
|
+
if (!message.conversationId) {
|
|
321
|
+
console.error("消息缺少 conversationId:", message);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
// 更新消息状态为撤回
|
|
325
|
+
const recalledMsgId = message.id; // 假设撤回消息的内容是原消息ID
|
|
326
|
+
commit("message/UPDATE_MESSAGE", {
|
|
327
|
+
messageId: recalledMsgId,
|
|
328
|
+
conversationId: message.conversationId,
|
|
329
|
+
updates: { status: "RECALLED" },
|
|
330
|
+
});
|
|
331
|
+
// 更新会话最后一条消息(如果是被撤回的那条)
|
|
332
|
+
const conversation = state.conversation.conversations.find((c) => c.conversationId === message.conversationId);
|
|
333
|
+
if (conversation && conversation.lastMessageId === recalledMsgId) {
|
|
334
|
+
const updates = {
|
|
335
|
+
lastMessageContent: "对方撤回了一条消息", // 或者 "你撤回了一条消息"
|
|
336
|
+
lastMessageType: "RECALLED",
|
|
337
|
+
};
|
|
338
|
+
commit("conversation/UPDATE_CONVERSATION", {
|
|
339
|
+
conversationId: message.conversationId,
|
|
340
|
+
updates,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
console.error("处理 WebSocket 撤回消息时出错:", error);
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
/** 发送消息 */
|
|
349
|
+
async sendMessage({ commit, state }, messageData) {
|
|
350
|
+
const newMessage = {
|
|
351
|
+
...messageData,
|
|
352
|
+
id: `temp-${Date.now()}`,
|
|
353
|
+
createdAt: new Date().toISOString(),
|
|
354
|
+
status: "SENDING",
|
|
355
|
+
};
|
|
356
|
+
// 添加到队列
|
|
357
|
+
commit("message/ADD_TO_MESSAGE_QUEUE", newMessage);
|
|
358
|
+
// 更新会话
|
|
359
|
+
const conversation = state.conversation.conversations.find((c) => c.conversationId === newMessage.conversationId);
|
|
360
|
+
if (conversation) {
|
|
361
|
+
commit("conversation/UPDATE_CONVERSATION", {
|
|
362
|
+
conversationId: newMessage.conversationId,
|
|
363
|
+
updates: {
|
|
364
|
+
lastMessageId: newMessage.id,
|
|
365
|
+
lastMessageContent: newMessage.content,
|
|
366
|
+
lastMessageAt: newMessage.createdAt,
|
|
367
|
+
lastMessageType: newMessage.messageType,
|
|
368
|
+
},
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
// 创建新会话
|
|
373
|
+
commit("conversation/ADD_CONVERSATION", {
|
|
374
|
+
conversationId: newMessage.conversationId,
|
|
375
|
+
conversationType: newMessage.conversationId.includes("p2p")
|
|
376
|
+
? "P2P"
|
|
377
|
+
: "GROUP",
|
|
378
|
+
isMuted: false,
|
|
379
|
+
isPinned: false,
|
|
380
|
+
lastMessageAt: newMessage.createdAt,
|
|
381
|
+
lastMessageContent: newMessage.content,
|
|
382
|
+
unreadCount: 1,
|
|
383
|
+
user: {
|
|
384
|
+
userId: newMessage.senderId,
|
|
385
|
+
displayName: "用户:" + newMessage.senderId,
|
|
386
|
+
account: newMessage.senderId,
|
|
387
|
+
avatarUrl: newMessage.icon || "/static/images/default-avatar.png",
|
|
388
|
+
},
|
|
389
|
+
lastMessageType: newMessage.messageType,
|
|
390
|
+
lastMessageSenderId: newMessage.senderId,
|
|
391
|
+
lastMessageSenderName: newMessage.senderId,
|
|
392
|
+
status: 0,
|
|
393
|
+
recalled: false,
|
|
394
|
+
recaller: { id: "", name: "" },
|
|
395
|
+
payload: { text: newMessage.content || "" },
|
|
396
|
+
lastMessageId: newMessage.id,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
try {
|
|
400
|
+
// 发送消息
|
|
401
|
+
if (!state.websocket.imInstance)
|
|
402
|
+
throw new Error("WebSocket 未连接");
|
|
403
|
+
await state.websocket.imInstance.sendText(newMessage.conversationId, newMessage.recipientId, newMessage.content, newMessage.messageType, newMessage.mediaUrl);
|
|
404
|
+
// 更新状态
|
|
405
|
+
commit("message/UPDATE_MESSAGE", {
|
|
406
|
+
messageId: newMessage.id,
|
|
407
|
+
conversationId: newMessage.conversationId,
|
|
408
|
+
updates: { status: "SENT" },
|
|
409
|
+
});
|
|
410
|
+
commit("message/REMOVE_FROM_MESSAGE_QUEUE", newMessage.id);
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
commit("message/UPDATE_MESSAGE", {
|
|
414
|
+
messageId: newMessage.id,
|
|
415
|
+
conversationId: newMessage.conversationId,
|
|
416
|
+
updates: { status: "FAILED" },
|
|
417
|
+
});
|
|
418
|
+
commit("global/SET_ERROR", "发送消息失败");
|
|
419
|
+
console.error("发送消息失败:", error);
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
},
|
|
423
|
+
/** 发送已读回执 */
|
|
424
|
+
async sendReadReceipt({ state }, payload) {
|
|
425
|
+
if (!state.websocket.imInstance)
|
|
426
|
+
throw new Error("WebSocket 未连接");
|
|
427
|
+
return state.websocket.imInstance.sendReadReceipt(payload.conversationId, payload.lastReadMessageId);
|
|
428
|
+
},
|
|
429
|
+
/** 撤回消息 */
|
|
430
|
+
async sendRecallMessage({ state }, payload) {
|
|
431
|
+
if (!state.websocket.imInstance)
|
|
432
|
+
throw new Error("WebSocket 未连接");
|
|
433
|
+
return state.websocket.imInstance.sendRecallMessage(payload.messageId);
|
|
434
|
+
},
|
|
435
|
+
/**
|
|
436
|
+
* 用户登录
|
|
437
|
+
* 注意:SDK 不包含登录 API,需要在应用层实现
|
|
438
|
+
* 登录成功后调用 connectWebSocket 连接 WebSocket
|
|
439
|
+
*/
|
|
440
|
+
async login({ commit, dispatch }, loginParams) {
|
|
441
|
+
commit("global/SET_LOADING", true);
|
|
442
|
+
console.log("=====login====", loginParams);
|
|
443
|
+
try {
|
|
444
|
+
const { user, wsUrl, token } = loginParams;
|
|
445
|
+
commit("user/SET_CURRENT_USER", user);
|
|
446
|
+
commit("user/ADD_USER", user);
|
|
447
|
+
await dispatch("connectWebSocket", { userId: user.id, wsUrl, token });
|
|
448
|
+
return user;
|
|
449
|
+
}
|
|
450
|
+
catch (error) {
|
|
451
|
+
commit("global/SET_ERROR", "登录失败");
|
|
452
|
+
console.error("登录失败:", error);
|
|
453
|
+
throw error;
|
|
454
|
+
}
|
|
455
|
+
finally {
|
|
456
|
+
commit("global/SET_LOADING", false);
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
/** 用户登出 */
|
|
460
|
+
logout({ dispatch }) {
|
|
461
|
+
dispatch("disconnectWebSocket");
|
|
462
|
+
dispatch("resetState");
|
|
463
|
+
},
|
|
464
|
+
},
|
|
465
|
+
// ==================== Getters ====================
|
|
466
|
+
getters: {
|
|
467
|
+
/** 排序后的会话列表 */
|
|
468
|
+
sortedConversations: (state) => [...state.conversation.conversations].sort((a, b) => new Date(b.lastMessageAt).getTime() -
|
|
469
|
+
new Date(a.lastMessageAt).getTime()),
|
|
470
|
+
/** 当前选中的会话 */
|
|
471
|
+
selectedConversation: (state) => state.conversation.conversations.find((c) => c.conversationId === state.conversation.selectedConversationId),
|
|
472
|
+
/** 获取指定会话的消息列表 */
|
|
473
|
+
conversationMessages: (state) => (conversationId) => state.message.messages[conversationId] || [],
|
|
474
|
+
/** 总未读消息数 */
|
|
475
|
+
unreadCount: (state) => state.conversation.conversations.reduce((total, c) => total + c.unreadCount, 0),
|
|
476
|
+
/** 获取用户信息 */
|
|
477
|
+
userInfo: (state) => (userId) => state.user.users[userId] || null,
|
|
478
|
+
/** WebSocket 连接状态 */
|
|
479
|
+
isConnected: (state) => state.websocket.isConnected,
|
|
480
|
+
/** 加载状态 */
|
|
481
|
+
loading: (state) => state.global.loading,
|
|
482
|
+
/** 错误信息 */
|
|
483
|
+
error: (state) => state.global.error,
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
// ==================== 插件安装 ====================
|
|
487
|
+
/** Vue 插件安装函数 */
|
|
488
|
+
function install(vue) {
|
|
489
|
+
if (vue.prototype) {
|
|
490
|
+
vue.prototype.$store = store;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
exports.default = store;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { MessageType } from "./message-type.type";
|
|
2
|
+
export declare enum ConversationType {
|
|
3
|
+
P2P = 1,// 单聊
|
|
4
|
+
GROUP = 2
|
|
5
|
+
}
|
|
6
|
+
interface IConversationUserInfo {
|
|
7
|
+
/** 用户ID (长整型) */
|
|
8
|
+
id?: string;
|
|
9
|
+
/** 业务系统标识 */
|
|
10
|
+
biz?: string;
|
|
11
|
+
/** 用户账号/唯一标识 */
|
|
12
|
+
account?: string;
|
|
13
|
+
/** 认证令牌 */
|
|
14
|
+
token?: string;
|
|
15
|
+
/** 显示名称 */
|
|
16
|
+
displayName?: string;
|
|
17
|
+
/** 头像URL */
|
|
18
|
+
avatarUrl?: string | null;
|
|
19
|
+
/** 用户状态 */
|
|
20
|
+
status?: string;
|
|
21
|
+
/** 删除标记: 0=正常, 1=已删除 */
|
|
22
|
+
isDel?: 0 | 1;
|
|
23
|
+
/** 创建时间 (ISO格式) */
|
|
24
|
+
createdAt?: string;
|
|
25
|
+
/** 更新时间 (ISO格式) */
|
|
26
|
+
updatedAt?: string | null;
|
|
27
|
+
}
|
|
28
|
+
export interface IConversation {
|
|
29
|
+
conversationId: string;
|
|
30
|
+
conversationType: ConversationType;
|
|
31
|
+
isMuted: boolean;
|
|
32
|
+
isPinned: boolean;
|
|
33
|
+
lastMessageAt: string;
|
|
34
|
+
lastMessageContent: string;
|
|
35
|
+
unreadCount: number;
|
|
36
|
+
user: IConversationUserInfo;
|
|
37
|
+
lastMessageType: MessageType;
|
|
38
|
+
lastMessageSenderId: string;
|
|
39
|
+
lastMessageSenderName: string;
|
|
40
|
+
lastMessageId: string;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConversationType = void 0;
|
|
4
|
+
var ConversationType;
|
|
5
|
+
(function (ConversationType) {
|
|
6
|
+
ConversationType[ConversationType["P2P"] = 1] = "P2P";
|
|
7
|
+
ConversationType[ConversationType["GROUP"] = 2] = "GROUP";
|
|
8
|
+
})(ConversationType || (exports.ConversationType = ConversationType = {}));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface IIMUserInfo {
|
|
2
|
+
/** 用户ID */
|
|
3
|
+
id?: string;
|
|
4
|
+
/** 业务标识 */
|
|
5
|
+
biz?: string;
|
|
6
|
+
/** 账号 */
|
|
7
|
+
account?: string;
|
|
8
|
+
/** 认证令牌 */
|
|
9
|
+
token?: string;
|
|
10
|
+
/** 显示名称 */
|
|
11
|
+
displayName?: string;
|
|
12
|
+
/** 头像URL */
|
|
13
|
+
avatarUrl?: string | null;
|
|
14
|
+
/** 状态 */
|
|
15
|
+
status?: string;
|
|
16
|
+
/** 是否删除 (0=未删除, 1=已删除) */
|
|
17
|
+
isDel?: number;
|
|
18
|
+
/** 创建时间 */
|
|
19
|
+
createdAt?: string;
|
|
20
|
+
/** 更新时间 */
|
|
21
|
+
updatedAt?: string | null;
|
|
22
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './socket-data.type';
|
|
1
|
+
export * from './conversation.type';
|
|
2
|
+
export * from './im-user.type';
|
|
3
|
+
export * from './message-type.type';
|
|
4
|
+
export * from './socket-data.type';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// 导出所有类型
|
|
18
|
+
__exportStar(require("./conversation.type"), exports);
|
|
19
|
+
__exportStar(require("./im-user.type"), exports);
|
|
20
|
+
__exportStar(require("./message-type.type"), exports);
|
|
21
|
+
__exportStar(require("./socket-data.type"), exports);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export declare enum MessageType {
|
|
2
|
+
TEXT = "TEXT",
|
|
3
|
+
IMAGE = "IMAGE",
|
|
4
|
+
VIDEO = "VIDEO",
|
|
5
|
+
CUSTOM = "CUSTOM",
|
|
6
|
+
TEXT_QUOTE = "TEXT_QUOTE",
|
|
7
|
+
AUDIO = "AUDIO",
|
|
8
|
+
IMAGE_TRANSMIT = "IMAGE_TRANSMIT",
|
|
9
|
+
EMOJI_PACK = "EMOJI_PACK",
|
|
10
|
+
RED_ENVELOPE = "RED_ENVELOPE",
|
|
11
|
+
MAP = "MAP",
|
|
12
|
+
GROUP_NOTICE = "GROUP_NOTICE",
|
|
13
|
+
UPDATE_GROUP_NAME = "UPDATE_GROUP_NAME",
|
|
14
|
+
ARTICLE = "ARTICLE",
|
|
15
|
+
SHARE_SBCF = "SHARE_SBCF",
|
|
16
|
+
SHARE_MALL = "SHARE_MALL"
|
|
17
|
+
}
|
|
18
|
+
export declare const MessageTypeData: {
|
|
19
|
+
TEXT: string;
|
|
20
|
+
IMAGE: string;
|
|
21
|
+
VIDEO: string;
|
|
22
|
+
CUSTOM: string;
|
|
23
|
+
TEXT_QUOTE: string;
|
|
24
|
+
AUDIO: string;
|
|
25
|
+
IMAGE_TRANSMIT: string;
|
|
26
|
+
EMOJI_PACK: string;
|
|
27
|
+
RED_ENVELOPE: string;
|
|
28
|
+
MAP: string;
|
|
29
|
+
GROUP_NOTICE: string;
|
|
30
|
+
UPDATE_GROUP_NAME: string;
|
|
31
|
+
ARTICLE: string;
|
|
32
|
+
SHARE_SBCF: string;
|
|
33
|
+
SHARE_MALL: string;
|
|
34
|
+
};
|
|
35
|
+
export declare enum MessageStatus {
|
|
36
|
+
SENT = "SENT",
|
|
37
|
+
DELIVERED = "DELIVERED",
|
|
38
|
+
READ = "READ",
|
|
39
|
+
RECALLED = "RECALLED"
|
|
40
|
+
}
|
|
41
|
+
export interface IMessage {
|
|
42
|
+
id: string | number;
|
|
43
|
+
conversationId: string;
|
|
44
|
+
senderId: string | number;
|
|
45
|
+
recipientId?: string | number;
|
|
46
|
+
messageType?: MessageType;
|
|
47
|
+
content?: string;
|
|
48
|
+
mediaUrl?: string;
|
|
49
|
+
metadata?: string;
|
|
50
|
+
createdAt: string;
|
|
51
|
+
status: MessageStatus;
|
|
52
|
+
}
|
|
53
|
+
export interface ISearchMessageRes {
|
|
54
|
+
/** 消息ID */
|
|
55
|
+
id?: string;
|
|
56
|
+
/** 会话ID */
|
|
57
|
+
conversationId?: string;
|
|
58
|
+
/** 发送者用户ID */
|
|
59
|
+
senderId?: string;
|
|
60
|
+
/** 接收者用户ID (单聊) 或群组ID (群聊) */
|
|
61
|
+
recipientId?: string;
|
|
62
|
+
/** 消息类型 */
|
|
63
|
+
messageType?: string;
|
|
64
|
+
/** 消息内容 */
|
|
65
|
+
content?: string;
|
|
66
|
+
/** 媒体文件URL */
|
|
67
|
+
mediaUrl?: string;
|
|
68
|
+
/** 自定义消息元数据 (JSON格式) */
|
|
69
|
+
metadata?: string;
|
|
70
|
+
/** 消息状态 */
|
|
71
|
+
status?: string;
|
|
72
|
+
/** 消息创建时间 */
|
|
73
|
+
createdAt?: string;
|
|
74
|
+
/** 消息更新时间 */
|
|
75
|
+
updatedAt?: string;
|
|
76
|
+
}
|