-
Notifications
You must be signed in to change notification settings - Fork 68
/
groups.js
616 lines (609 loc) · 24.3 KB
/
groups.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import { ElMessage } from 'element-plus'
import { GROUP_OPERATION_TYPE, GROUP_ROLE_TYPE } from '@/IM/constant'
import { EMClient } from '@/IM'
const Groups = {
state: {
groupsInfos: {}, //计划废弃
joinedGroup: {
pagingParams: {
pageNum: 0,
pageSize: 20
},
joinedGroupList: [],
joinedGroupListTotal: 0
},
groupDetails: new Map(), //key:groupId value:groupDetail
groupMembers: new Map() //key:groupId value:groupMemberList
},
mutations: {
SET_JOINED_GROUP: (state, payload) => {
const { total, entities: joinedGroupList } = payload
state.joinedGroup.pagingParams.pageNum++
state.joinedGroup.joinedGroupListTotal = total
state.joinedGroup.joinedGroupList = _.unionBy(
[...joinedGroupList],
[...state.joinedGroup.joinedGroupList],
(g) => g.groupId
)
},
SET_GROUP_DETAILS: (state, payload) => {
const { groupDetailsList } = payload
groupDetailsList.length > 0 &&
groupDetailsList.forEach((groupDetail) => {
state.groupDetails.set(groupDetail.id, groupDetail)
})
},
SET_GROUPS_MEMBERS: (state, payload) => {
const { groupId, members } = payload
state.groupMembers.set(groupId, [...members])
//同步更新群组列表里面的群人数
if (state.joinedGroup.joinedGroupList.length) {
state.joinedGroup.joinedGroupList.map((groupItem) => {
if (groupItem.groupId === groupId) {
groupItem.affiliationsCount = members.length
}
})
}
},
SET_GROUPS_BLIACK_LIST: (state, payload) => {
const { groupId, blacklist } = payload
if (!state.groupDetails.has(groupId)) {
state.groupDetails.set(groupId, { blacklist })
}
state.groupDetails.get(groupId).blacklist = blacklist
},
SET_GROUPS_MUTE_LIST: (state, payload) => {
const { groupId, mutelist } = payload
if (!state.groupDetails.has(groupId)) {
state.groupDetails.set(groupId, { mutelist })
}
state.groupDetails.get(groupId).mutelist = mutelist
},
SET_GROUPS_ANNOUN: (state, payload) => {
const { groupId, announcement } = payload
if (!state.groupDetails.has(groupId)) {
state.groupDetails.set(groupId, { announcement: announcement })
}
state.groupDetails.get(groupId).announcement = announcement
},
//设置用户在群组中的群组属性
SET_GROUP_MEMBERS_INFO: (state, payload) => {
const { groupId, inGroupInfo } = payload
let groupMemberInfo = {}
inGroupInfo.length > 0 &&
inGroupInfo.map(
(item) =>
(groupMemberInfo = Object.assign(groupMemberInfo, item))
)
if (!state.groupDetails.has(groupId)) {
state.groupDetails.set(groupId, { groupMemberInfo })
}
state.groupDetails.get(groupId).groupMemberInfo = groupMemberInfo
},
//更新本地缓存群组信息
UPDATE_CACHE_GROUP_INFO: (state, payload) => {
const { groupId, type, params } = payload
console.log('>>>>>执行更新', payload)
//更新群组列表内数据
if (type === 'groupName') {
state.joinedGroup.joinedGroupList.length > 0 &&
state.joinedGroup.joinedGroupList.map((groupItem) => {
if (groupItem.groupId === groupId) {
return (groupItem.groupName = params)
}
})
state.groupDetails.has(groupId) &&
(state.groupDetails.get(groupId).name = params)
}
//更新群组详情内的数据
if (type === 'groupDescription') {
state.joinedGroup.joinedGroupList.length > 0 &&
state.joinedGroup.joinedGroupList.map((groupItem) => {
if (groupItem.groupId === groupId) {
return (groupItem.description = params)
}
})
state.groupDetails.has(groupId) &&
(state.groupDetails.get(groupId).description = params)
}
//更新群成员数
if (type === 'groupMemberCount') {
state.joinedGroup.joinedGroupList.length > 0 &&
state.joinedGroup.joinedGroupList.map((groupItem) => {
return (groupItem.affiliationsCount = params)
})
state.groupDetails.has(groupId) &&
(state.groupDetails.get(groupId).affiliations_count =
params)
}
},
//更新本地缓存群组成员
UPDATE_GROUP_MEMBERS: (state, payload) => {
const { groupId, member, type } = payload
switch (type) {
case GROUP_OPERATION_TYPE.MEMBER_PRESENCE:
{
state.groupMembers.has(groupId) &&
state.groupMembers.get(groupId).push({ member })
}
break
case GROUP_OPERATION_TYPE.MEMBER_ABSENCE:
{
if (
state.groupMembers.has(groupId) &&
state.groupMembers.get(groupId).length > 0
) {
const _index = state.groupMembers
.get(groupId)
.findIndex((item) => item === member)
state.groupMembers.get(groupId).splice(_index, 1)
}
}
break
default:
break
}
},
//更新群组管理员
UPDATE_GORUPS_ADMIN: (state, payload) => {
const { type, groupId, userId } = payload
state.joinedGroup.joinedGroupList.length > 0 &&
state.joinedGroup.joinedGroupList.map((groupItem) => {
if (groupItem.groupId === groupId) {
if (type === GROUP_OPERATION_TYPE.SET_ADMIN) {
return (groupItem.role = GROUP_ROLE_TYPE.ADMIN)
} else if (type === GROUP_OPERATION_TYPE.REMOVE_ADMIN) {
return (groupItem.role = GROUP_ROLE_TYPE.MEMBER)
}
}
})
if (type === GROUP_OPERATION_TYPE.SET_ADMIN) {
state.groupDetails.has(groupId) &&
(state.groupDetails.get(groupId).adminlist = [userId])
} else if (type === GROUP_OPERATION_TYPE.REMOVE_ADMIN) {
if (
state.groupDetails.has(groupId) &&
state.groupDetails.get(groupId).adminlist?.length > 0
) {
const _index = state.groupDetails
.get(groupId)
.adminlist.findIndex((item) => item === userId)
state.groupDetails.get(groupId).adminlist.splice(_index, 1)
}
}
},
//删除缓存群组列表
DELETE_JOINED_GROUP_LIST: (state, payload) => {
const { groupId } = payload
if (state.joinedGroup.joinedGroupList.length > 0) {
const _index = state.joinedGroup.joinedGroupList.findIndex(
(item) => item.groupId === groupId
)
state.joinedGroup.joinedGroupList.splice(_index, 1)
state.joinedGroup.joinedGroupListTotal--
}
}
},
actions: {
//从服务端获取加入的群组列表
fetchJoinedGroupListFromServer: async (
{ state, commit },
params = {}
) => {
const {
pagingParams: { pageNum, pageSize }
} = state.joinedGroup
const { startPageNum } = params
try {
const { total, entities } = await EMClient.getJoinedGroups({
pageNum:
startPageNum !== undefined ? startPageNum : pageNum,
pageSize: pageSize,
needAffiliations: true,
needRole: true
})
if (entities?.length === 0) return
commit('SET_JOINED_GROUP', { total, entities })
} catch (error) {
console.error('加入的群组列表获取失败', error)
}
},
//从服务端获取群组详情
fetchGroupDetailFromServer: async ({ commit }, groupIds = []) => {
console.log('>>>>>>groupIds', groupIds)
let groupDetails = []
async function fetchDetailsForGroupIds(groupIdArray) {
try {
const result = await EMClient.getGroupInfo({
groupId: groupIdArray
})
groupDetails = groupDetails.concat(result.data)
commit('SET_GROUP_DETAILS', {
groupDetailsList: groupDetails
})
} catch (error) {
console.error('>>>群详情获取失败', error)
if (error?.data) {
const { error_description } = JSON.parse(error.data)
if (
error_description.includes(
'do not find this group:'
)
) {
// 使用正则表达式截取不存在的群组ID
const groupIdMatch =
error_description.match(/group:(\d+)/)
if (groupIdMatch) {
const nonExistentGroupId = groupIdMatch[1]
// 从groupIds数组中去除不存在的群组ID
_.pull(groupIdArray, nonExistentGroupId)
// 重新发起请求
await fetchDetailsForGroupIds(groupIdArray)
}
} else {
// 如果是其他类型的错误,可以在这里处理
console.error('发生未知错误:', error)
}
}
}
}
if (groupIds.length > 1) {
const groupIdsArr = _.chunk([...groupIds], 20)
for (const groupIdsChunk of groupIdsArr) {
await fetchDetailsForGroupIds(groupIdsChunk)
}
} else {
await fetchDetailsForGroupIds(groupIds)
}
},
//获取群组成员
fetchGroupsMemberFromServer: async ({ commit }, groupId) => {
//此接口支持分页,如果群组成员大于1000人,需要分页获取。
const options = {
pageNum: 1,
pageSize: 1000,
groupId: groupId
}
try {
const { data } = await EMClient.listGroupMembers(options)
commit('SET_GROUPS_MEMBERS', {
groupId: groupId,
members: data
})
} catch (error) {
console.error('>>>>>群组成员获取失败', error)
}
},
//获取群成员对应的群组属性
fetchGroupMemberAttributes: async ({ dispatch, commit }, params) => {
const { groupId, members } = params
const membersList = _.chunk(members, 10)
const requestTrack = []
membersList.forEach((list) => {
const groupMemberList = _.flatten(_.map(list, _.values))
requestTrack.push(
EMClient.getGroupMembersAttributes({
groupId: groupId,
userIds: groupMemberList
})
)
})
try {
const res = await Promise.all(requestTrack)
const groupUsersInfo = _.map(res, 'data')
commit('SET_GROUP_MEMBERS_INFO', {
groupId: groupId,
inGroupInfo: groupUsersInfo
})
} catch (error) {}
},
//获取登录用户在某群内的群组属性
fetchInTheGroupInfoFromServer: async (
{ dispatch, commit },
groupId
) => {
try {
let options = {
groupId: groupId,
userId: EMClient.user
}
const { data } = await EMClient.getGroupMemberAttributes(
options
)
commit('SET_GROUP_MEMBERS_INFO', {
groupId: groupId,
inGroupInfo: [
{ [EMClient.user]: { nickName: data.nickName } }
]
})
} catch (error) {
console.error('>>>>>群组属性获取失败', error)
}
},
//设置登录用户在某群的群组属性
setInTheGroupInfo: async ({ commit }, params) => {
const { groupId, nickName } = params
try {
await EMClient.setGroupMemberAttributes({
groupId: groupId,
userId: EMClient.user,
memberAttributes: {
nickName
}
})
commit('SET_GROUP_MEMBERS_INFO', {
groupId: groupId,
inGroupInfo: [{ [EMClient.user]: { nickName } }]
})
} catch (error) {
console.error(error)
}
},
//获取群公告
fetchAnnounmentFromServer: async ({ dispatch, commit }, groupId) => {
const option = {
groupId: groupId
}
try {
const { data } = await EMClient.fetchGroupAnnouncement(option)
commit('SET_GROUPS_ANNOUN', {
groupId: groupId,
announcement: data.announcement
})
} catch (error) {
console.error('>>>>>群组公告获取失败', error)
}
},
//群黑名单
fetchGroupsBlackListFromServer: async ({ commit }, groupId) => {
try {
const { data } = await EMClient.getGroupBlocklist({
groupId: groupId
})
commit('SET_GROUPS_BLIACK_LIST', {
groupId: groupId,
blacklist: data
})
} catch (error) {
console.error(error)
}
},
//群禁言列表
fetchGroupsMuteListFromServer: async ({ dispatch, commit }, params) => {
try {
const { data } = await EMClient.getGroupMuteList({
groupId: params
})
commit('SET_GROUPS_MUTE_LIST', {
groupId: params,
mutelist: data
})
} catch (error) {
console.error(error)
}
},
// 修改群名或者群描述
modifyGroupInfo: async ({ dispatch, commit }, params) => {
const { groupId, modifyType, content } = params
//0 是修改群名
if (modifyType === 0) {
const option = {
groupId: groupId,
groupName: content
}
await EMClient.modifyGroup(option)
//更新本地缓存数据
commit('UPDATE_CACHE_GROUP_INFO', {
groupId: groupId,
type: 'groupName',
params: content
})
}
//1 是修改群详情
if (modifyType === 1) {
const option = {
groupId: groupId,
description: content
}
await EMClient.modifyGroup(option)
//更新本地缓存数据
commit('UPDATE_CACHE_GROUP_INFO', {
groupId: groupId,
type: 'groupDescription',
params: content
})
}
},
// 设置/修改群组公告
modifyGroupAnnouncement: async ({ dispatch }, params) => {
//SDK入参属性名是确定的此示例直接将属性名改为了SDK所识别的参数如果修改,具体请看文档。
const { groupId, announcement } = params
try {
await EMClient.updateGroupAnnouncement({ ...params })
dispatch('fetchAnnounmentFromServer', groupId)
} catch (error) {
console.error('群公告修改失败', error)
}
},
//邀请群成员
inviteUserJoinTheGroup: async ({ dispatch }, params) => {
//SDK入参属性名是确定的此示例直接将属性名改为了SDK所识别的参数如果修改,具体请看文档。
const { users, groupId } = params
try {
await EMClient.inviteUsersToGroup({ users: [users], groupId })
ElMessage({
message: '群组邀请成功送出~',
type: 'success'
})
} catch (error) {
console.log('>>>>邀请失败', error)
ElMessage({
message: '群组邀请失败,请稍后重试~',
type: 'error'
})
}
},
//移出群成员
removeTheGroupMember: async ({ dispatch }, params) => {
//SDK入参属性名是确定的此示例直接将属性名改为了SDK所识别的参数如果修改,具体请看文档。
const { username, groupId } = params
try {
await EMClient.removeGroupMember({ username, groupId })
ElMessage({
message: `已将${username}移出群组!`,
type: 'success'
})
//更新群成员
dispatch('fetchGroupsMemberFromServer', groupId)
} catch (error) {
ElMessage({
message: '该群成员移出失败,请稍后重试!',
type: 'error'
})
}
},
//添加用户到黑名单
addMemberToBlackList: async ({ dispatch }, params) => {
const { groupId, usernames } = params
try {
//SDK入参属性名是确定的此示例直接将属性名改为了SDK所识别的参数如果修改,具体请看文档。
// let option = {
// groupId: "groupId",
// usernames: ["user1", "user2"]
// };
await EMClient.blockGroupMembers({ groupId, usernames })
ElMessage({
message: '黑名单添加成功~',
type: 'success'
})
//重新获取黑名单列表
dispatch('fetchGroupsBlackListFromServer', groupId)
//重新获取成员列表
dispatch('fetchGroupsMemberFromServer', groupId)
} catch (error) {
ElMessage({
message: '黑名单添加失败,请稍后重试~',
type: 'error'
})
}
},
//从黑名单中移出
removeTheMemberFromBlackList: async ({ dispatch }, params) => {
const { groupId, usernames } = params
try {
await EMClient.unblockGroupMembers({ groupId, usernames })
ElMessage({
message: '黑名单移除成功~',
type: 'success'
})
//重新获取黑名单列表
dispatch('fetchGroupsBlackListFromServer', groupId)
} catch (error) {
console.log('error', error)
ElMessage({
message: '黑名单移除失败,请稍后重试~',
type: 'error'
})
}
},
//添加用户到禁言列表
addMemberToMuteList: async ({ dispatch }, params) => {
const { groupId, username } = params
try {
await EMClient.muteGroupMember({
groupId,
username: username,
muteDuration: 886400000
})
ElMessage({
message: '禁言成功~',
type: 'success'
})
setTimeout(() => {
dispatch('fetchGroupsMuteListFromServer', groupId)
}, 800)
} catch (error) {
console.log('>>>>>error', error)
ElMessage({
message: '禁言失败,请稍后重试~',
type: 'error'
})
}
// let option = {
// groupId: 'groupId',
// username: 'user',
// muteDuration: 886400000, // 禁言时长,单位为毫秒。
// };
// await EMClient.muteGroupMember(option);
},
//从禁言列表中移出
removeTheMemberFromMuteList: async ({ dispatch }, params) => {
const { groupId, username } = params
try {
await EMClient.unmuteGroupMember({
groupId,
username: username
})
ElMessage({
message: '移除禁言成功~',
type: 'success'
})
setTimeout(() => {
dispatch('fetchGroupsMuteListFromServer', groupId)
}, 800)
} catch (error) {
console.log('>>>>>error', error)
ElMessage({
message: '移除禁言失败,请稍后重试~',
type: 'error'
})
}
},
//退出群组
leaveIntheGroup: async ({ commit }, params) => {
if (!params.groupId) return
const { groupId } = params
return new Promise((resolve, reject) => {
EMClient.leaveGroup({
groupId: groupId
})
.then((res) => {
commit('DELETE_JOINED_GROUP_LIST', {
groupId: groupId
})
resolve(res)
})
.catch((err) => {
reject(err)
})
})
},
//解散群组
destroyInTheGroup: async ({ commit }, params) => {
if (!params.groupId) return
const { groupId } = params
return new Promise((resolve, reject) => {
const option = {
groupId: groupId
}
EMClient.destroyGroup(option)
.then((res) => {
resolve(res)
commit('DELETE_JOINED_GROUP_LIST', {
groupId: groupId
})
})
.catch((err) => {
reject(err)
})
})
}
},
getters: {
getGroupDetailMap: (state) => state.groupDetails,
getGroupMembersMap: (state) => state.groupMembers,
getJoinedGroupList: (state) => state.joinedGroup.joinedGroupList,
getJoinedGroupTotal: (state) => state.joinedGroup.joinedGroupListTotal
}
}
export default Groups