-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathconversation.js
338 lines (334 loc) · 14.8 KB
/
conversation.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import _ from 'lodash'
import { useLocalStorage } from '@vueuse/core'
import {
createInform,
checkLastMsgIsHasMention
} from '@/utils/handleSomeData/index'
import { EMClient } from '@/IM'
import { INFORM_FROM } from '@/constant'
import { GROUP_OPERATION_TYPE, CHAT_TYPE } from '@/IM/constant'
const Conversation = {
state: {
informDetail: [],
conversationListFromLocal: []
},
mutations: {
//清空系统通知
CLEAR_INFORM_LIST: (state) => {
state.informDetail = []
},
//更新系统通知
UPDATE_INFORM_LIST: (state, informBody) => {
const toBeUpdateInform = _.assign([], state.informDetail)
toBeUpdateInform.unshift(informBody)
state.informDetail = toBeUpdateInform
},
//获取会话列表
GET_CONVERSATION_LIST_FROM_LOCAL: (state, payload) => {
state.conversationListFromLocal = payload
},
//更新本地缓存的会话列表数据
UPDATE_CONVERSATION_LIST_FROM_LOCAL: (state, conversationItem) => {
const _index = state.conversationListFromLocal.findIndex(
(c) => c.conversationId === conversationItem.conversationId
)
if (_index > -1) {
state.conversationListFromLocal.splice(_index, 1)
state.conversationListFromLocal.unshift(conversationItem)
} else {
state.conversationListFromLocal.unshift(conversationItem)
}
},
//删除某条会话
DELETE_CONVERSATION_ITEM_FROM_LOCAL: (state, conversationId) => {
const _index = state.conversationListFromLocal.findIndex(
(v) => v.conversationId === conversationId
)
if (_index > -1) {
state.conversationListFromLocal.splice(_index, 1)
}
},
//清除会话未读状态
CLEAR_CONVERSATION_ITEM_UNREAD_COUNT: (state, conversationId) => {
state.conversationListFromLocal.forEach((conversationItem) => {
if (conversationItem.conversationId === conversationId) {
conversationItem.unReadCount = 0
}
})
},
//清除会话@状态
CLEAR_CONVERSATION_ITEM_MENTION_STATUS: (state, conversationId) => {
state.conversationListFromLocal.map((conversationItem) => {
if (conversationItem.conversationId === conversationId) {
conversationItem.customField.mention = false
}
})
},
//清除信息卡片未读
CLEAR_UNTREATED_STATUS: (state, index) => {
state.informDetail[index].untreated = 0
},
//更改卡片消息的按钮状态
UPDATE_INFORM_BTNSTATUS: (state, { index: index, btnStatus }) => {
state.informDetail[index].operationStatus = btnStatus
}
},
actions: {
//添加新系统通知
createNewInform: ({ dispatch, commit }, params) => {
const { fromType, informContent } = params
const result = createInform(fromType, informContent)
commit('UPDATE_INFORM_LIST', result)
//部分事件需要调用接口更新本地信息或者增加消息内系统通知
if (fromType === INFORM_FROM.FRIEND) {
const informMsg = {
from: informContent.from,
to: informContent.to,
chatType: CHAT_TYPE.SINGLE
}
switch (informContent.type) {
case 'unsubscribed':
{
informMsg.msg = '你俩的友尽了,可重新发起好友申请'
dispatch('createInformMessage', informMsg)
}
break
case 'subscribed':
{
informMsg.msg = '你们已成为你的好友,开始聊天吧'
dispatch('createInformMessage', informMsg)
}
break
default:
break
}
}
if (fromType === INFORM_FROM.GROUP) {
const informMsg = {
from: informContent.from,
to: informContent.id,
chatType: CHAT_TYPE.GROUP
}
switch (informContent.operation) {
case GROUP_OPERATION_TYPE.MEMBER_PRESENCE: //入群通知
{
informMsg.msg = `${informContent.from}加入了群组`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.MEMBER_ABSENCE:
{
informMsg.msg = `${informContent.from}退出了群组`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.UPDATE_ANNOUNCEMENT:
{
//更新群公告
informMsg.msg = `${informContent.from}更新了群组公告,去看看更新的什么吧~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.SET_ADMIN:
{
informMsg.msg = `${informContent.from}设定${informContent.to}为管理员~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.REMOVE_ADMIN:
{
informMsg.msg = `${informContent.from}移除了${informContent.to}的管理员身份~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.MUTE_MEMBER:
{
informMsg.msg = `${informContent.from}禁言了${
informContent.to ? informContent.to : '你'
}~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.UNMUTE_MEMBER:
{
informMsg.msg = `${informContent.from}取消了${
informContent.to ? informContent.to : '你'
}的禁言~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.REMOVE_MEMBER:
{
informMsg.msg = `${informContent.from}将你移出了群组${informContent.id}~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.DESTROY:
{
informMsg.msg = `${informContent.from}解散了该群~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.UPDATE_INFO:
{
informMsg.msg = `${informContent.from}更新了群组详情~`
dispatch('createInformMessage', informMsg)
}
break
case GROUP_OPERATION_TYPE.MEMBER_ATTRIBUTES_UPDATE:
{
informMsg.msg = `${informContent.from}修改群内昵称为【${informContent?.attributes?.nickName}】`
dispatch('createInformMessage', informMsg)
}
break
default:
break
}
}
},
//从本地加载会话列表数据
getConversationListFromLocal: async ({ dispatch, commit }, params) => {
let conversationList = []
try {
const result = await EMClient.localCache.getLocalConversations()
if (result.data.length) {
conversationList = [...result.data]
} else {
//默认只取50条远端数据数据,实际可自行加载更多。
const result = await EMClient.getServerConversations({
pageSize: 50,
cursor: ''
})
if (result.data?.conversations?.length) {
conversationList = [...result.data.conversations]
}
}
commit('GET_CONVERSATION_LIST_FROM_LOCAL', conversationList)
//挑出为群组的会话id,用于获取群组详情
const groupConversationIds = _.chain(conversationList)
.filter({ conversationType: CHAT_TYPE.GROUP })
.map('conversationId')
.value()
console.log('groupConversationIds', groupConversationIds)
if (groupConversationIds.length > 0) {
//获取群组详情
dispatch('fetchGroupDetailFromServer', groupConversationIds)
}
//获取群组详情
} catch (error) {
console.error('获取会话列表失败', error)
}
},
//更新会话列表
updateLocalConversation: async ({ dispatch, commit }, params) => {
const { conversationId, chatType } = params
try {
const result = await EMClient.localCache.getLocalConversation({
conversationId,
conversationType: chatType
})
if (!result?.data) return
const toBeUpdateConversationItem = { ...result?.data }
//检查更新的lastmsg中是否包含提及
const isMention = toBeUpdateConversationItem?.customField
?.mention
? true
: checkLastMsgIsHasMention(
toBeUpdateConversationItem.lastMessage
)
const customField = (toBeUpdateConversationItem.customField && {
...toBeUpdateConversationItem.customField,
mention: isMention
}) || { mention: isMention }
//设置会话级别提及状态clear
await dispatch('setLocalConversationCustomAttributes', {
conversationId,
conversationType: chatType,
customField: customField
})
toBeUpdateConversationItem.customField = { ...customField }
commit('UPDATE_CONVERSATION_LIST_FROM_LOCAL', {
...toBeUpdateConversationItem
})
} catch (error) {
console.log('error', error)
}
},
//删除会话列表(本地以及远端)
removeLocalConversation: async ({ dispatch, commit }, params) => {
const { conversationId, conversationType } = params
const options = {
// 会话 ID:单聊为对方的用户 ID,群聊为群组 ID。
channel: conversationId,
// 会话类型:(默认) `singleChat`:单聊;`groupChat`:群聊。
chatType: conversationType,
// 删除会话时是否同时删除服务端漫游消息。
deleteRoam: false
}
try {
//会话列表删除时,需要先删除远端会话列表,再删除本地数据库,这样跨端获取会话列表才能同步。
await EMClient.deleteConversation(options)
//删除本地数据库数据
await EMClient.localCache.removeLocalConversation({
conversationId,
conversationType
})
commit('DELETE_CONVERSATION_ITEM_FROM_LOCAL', conversationId)
} catch (error) {
console.error(error)
}
},
//设置会话已读(发送会话已读回执。)
clearConversationUnreadCount: async ({ dispatch, commit }, params) => {
const { conversationId, chatType } = params
const option = {
chatType: chatType, // 会话类型,设置为单聊。
type: 'channel', // 消息类型。
to: conversationId // 接收消息对象的用户 ID。
}
try {
//只有发送了会话已读回执远端服务器的会话未读数才会清空。
const msg = EMClient.Message.create(option)
const res = await EMClient.send(msg)
//同步清空本地数据库未读数。
await EMClient.localCache.clearConversationUnreadCount({
conversationId,
conversationType: chatType
})
//通知更新缓存中的会话未读数。
commit('CLEAR_CONVERSATION_ITEM_UNREAD_COUNT', conversationId)
} catch (error) {}
},
//清除会话@提及状态
clearConversationMention: async ({ dispatch, commit }, params) => {
const { conversationId, conversationType, customField } = params
customField.mention = false
try {
await EMClient.localCache.setLocalConversationCustomField({
conversationId,
conversationType,
customField: { ...customField }
})
commit('CLEAR_CONVERSATION_ITEM_MENTION_STATUS', conversationId)
} catch (error) {}
},
//设置本地会话自定义属性
setLocalConversationCustomAttributes: async (
{ dispatch, commit },
params
) => {
const { conversationId, conversationType, customField } = params
try {
await EMClient.localCache.setLocalConversationCustomField({
conversationId,
conversationType,
customField: { ...customField }
})
} catch (error) {}
}
},
getters: {
conversationListFromLocal: (state) => state.conversationListFromLocal
}
}
export default Conversation