index.vue
15.2 KB
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
<template>
<div class="container">
<div class="loading" v-loading="showLoading" element-loading-text="正在拼命初始化..."
element-loading-background="rgba(0, 0, 0, 0.8)">
<div class="chat-wrapper">
<el-row>
<el-col :xs="10" :sm="10" :md="8" :lg="8" :xl="7">
<side-bar/>
</el-col>
<el-col :xs="14" :sm="14" :md="16" :lg="16" :xl="17">
<current-conversation/>
</el-col>
</el-row>
</div>
<calling ref="callLayer" class="chat-wrapper"/>
<image-previewer/>
<group-live/>
</div>
<div class="bg"></div>
</div>
</template>
<script>
import {Notification} from 'element-ui'
import {mapState} from 'vuex'
import CurrentConversation from './components/conversation/current-conversation'
import SideBar from './components/layout/side-bar'
import ImagePreviewer from './components/message/image-previewer.vue'
import {translateGroupSystemNotice} from './utils/common'
import GroupLive from './components/group-live/index'
import Calling from './components/message/trtc-calling/calling-index'
import {ACTION} from './utils/trtcCustomMessageMap'
import MTA from './utils/mta'
import Helper from './utils/helper'
export default {
title: '星斗推',
components: {
SideBar,
CurrentConversation,
ImagePreviewer,
GroupLive,
Calling,
},
computed: {
...mapState({
currentUserProfile: state => state.user.currentUserProfile,
currentConversation: state => state.conversation.currentConversation,
videoCall: state => state.conversation.videoCall,
audioCall: state => state.conversation.audioCall,
isLogin: state => state.user.isLogin,
isSDKReady: state => state.user.isSDKReady,
isBusy: state => state.video.isBusy,
userID: state => state.user.userID,
userSig: state => state.user.userSig,
sdkAppID: state => state.user.sdkAppID
}),
// 是否显示 Loading 状态
showLoading() {
return !this.isSDKReady
}
},
mounted() {
// 初始化监听器
this.initListener()
},
created() {
Helper.verifyToken().then(user => this.tim.login({userID: user.id, userSig: user.sign})
.then(() => {
this.loading = false
this.$store.commit('toggleIsLogin', true)
this.$store.commit('startComputeCurrent')
this.$store.commit({
type: 'GET_USER_INFO',
userID: user.id,
userSig: user.sign,
sdkAppID: window.genTestUserSig('').SDKAppID
})
Helper.contactList().then(res => this.$store.commit('updateFriendList', res.data))
// Helper.groupList({size: 20}).then(res => this.$store.commit('updateGroupList', res.data))
// this.$store.commit('showMessage', {type: 'success', message: '登录成功'})
})
.catch(error => {
this.loading = false
this.$store.commit('showMessage', {message: '登录失败:' + error.message, type: 'error'})
}))
},
watch: {
isLogin(next) {
if (next) {
MTA.clickStat('link_two', {show: 'true'})
}
},
},
methods: {
initListener() {
// 登录成功后会触发 SDK_READY 事件,该事件触发后,可正常使用 SDK 接口
this.tim.on(this.TIM.EVENT.SDK_READY, this.onReadyStateUpdate, this)
// SDK NOT READT
this.tim.on(this.TIM.EVENT.SDK_NOT_READY, this.onReadyStateUpdate, this)
// 被踢出
this.tim.on(this.TIM.EVENT.KICKED_OUT, this.onKickOut)
// SDK内部出错
this.tim.on(this.TIM.EVENT.ERROR, this.onError)
// 收到新消息
this.tim.on(this.TIM.EVENT.MESSAGE_RECEIVED, this.onReceiveMessage)
// 会话列表更新
this.tim.on(this.TIM.EVENT.CONVERSATION_LIST_UPDATED, this.onUpdateConversationList)
// 群组列表更新
// this.tim.on(this.TIM.EVENT.GROUP_LIST_UPDATED, this.onUpdateGroupList)
// 网络监测
this.tim.on(this.TIM.EVENT.NET_STATE_CHANGE, this.onNetStateChange)
// 已读回执
this.tim.on(this.TIM.EVENT.MESSAGE_READ_BY_PEER, this.onMessageReadByPeer)
},
onReceiveMessage({data: messageList}) {
this.handleVideoMessage(messageList)
this.handleAt(messageList)
this.handleQuitGroupTip(messageList)
this.handleCloseGroupLive(messageList)
this.$store.commit('pushCurrentMessageList', messageList)
this.$store.commit('pushAvChatRoomMessageList', messageList)
},
onError({data}) {
if (data.message !== 'Network Error') {
this.$store.commit('showMessage', {
message: data.message,
type: 'error'
})
}
},
onMessageReadByPeer() {
},
onReadyStateUpdate({name}) {
const isSDKReady = name === this.TIM.EVENT.SDK_READY
this.$store.commit('toggleIsSDKReady', isSDKReady)
if (isSDKReady) {
this.tim.getMyProfile()
.then(({data}) => this.$store.commit('updateCurrentUserProfile', data))
.catch(error => this.$store.commit('showMessage', {type: 'error', message: error.message}))
this.$store.dispatch('getBlacklist')
// 登录trtc calling
this.trtcCalling.login({sdkAppID: this.sdkAppID, userID: this.userID, userSig: this.userSig})
}
},
kickedOutReason(type) {
switch (type) {
case this.TIM.TYPES.KICKED_OUT_MULT_ACCOUNT:
return '由于多实例登录'
case this.TIM.TYPES.KICKED_OUT_MULT_DEVICE:
return '由于多设备登录'
case this.TIM.TYPES.KICKED_OUT_USERSIG_EXPIRED:
return '由于 userSig 过期'
default:
return ''
}
},
checkoutNetState(state) {
switch (state) {
case this.TIM.TYPES.NET_STATE_CONNECTED:
return {message: '已接入网络', type: 'success'}
case this.TIM.TYPES.NET_STATE_CONNECTING:
return {message: '当前网络不稳定', type: 'warning'}
case this.TIM.TYPES.NET_STATE_DISCONNECTED:
return {message: '当前网络不可用', type: 'error'}
default:
return ''
}
},
onNetStateChange(event) {
this.$store.commit('showMessage', this.checkoutNetState(event.data.state))
},
onKickOut(event) {
this.$store.commit('showMessage', {
message: `${this.kickedOutReason(event.data.type)}被踢出,请重新登录。`,
type: 'error'
})
this.$store.commit('toggleIsLogin', false)
this.$store.commit('reset')
},
onUpdateConversationList(event) {
this.$store.commit('updateConversationList', event.data)
},
// onUpdateGroupList(event) {
// this.$store.commit('updateGroupList', event.data)
// },
onReceiveGroupSystemNotice(event) {
const isKickedout = event.data.type === 4
const isCurrentConversation =
`GROUP${event.data.message.payload.groupProfile.groupID}` ===
this.currentConversation.conversationID
// 在当前会话被踢,需reset当前会话
if (isKickedout && isCurrentConversation) {
this.$store.commit('resetCurrentConversation')
}
Notification({
title: '新系统通知',
message: translateGroupSystemNotice(event.data.message),
duration: 3000,
onClick: () => {
const SystemConversationID = '@TIM#SYSTEM'
this.$store.dispatch('checkoutConversation', SystemConversationID)
}
})
},
/**
* 处理 @ 我的消息
* @param {Message[]} messageList
*/
handleAt(messageList) {
// 筛选有 @ 符号的文本消息
const atTextMessageList = messageList.filter(
message =>
message.type === this.TIM.TYPES.MSG_TEXT &&
message.payload.text.includes('@')
)
for (let i = 0; i < atTextMessageList.length; i++) {
const message = atTextMessageList[i]
const matched = message.payload.text.match(/@\w+/g)
if (!matched) {
continue
}
// @ 我的
if (matched.includes(`@${this.currentUserProfile.userID}`)) {
// 当前页面不可见时,调用window.Notification接口,系统级别通知。
if (this.$store.getters.hidden) {
this.notifyMe(message)
}
Notification({
title: `有人在群${message.conversationID.slice(5)}提到了你`,
message: message.payload.text,
duration: 3000
})
this.$bus.$emit('new-messsage-at-me', {
data: {conversationID: message.conversationID}
})
}
}
},
selectConversation(conversationID) {
if (conversationID !== this.currentConversation.conversationID) {
this.$store.dispatch('checkoutConversation', conversationID)
}
},
isJsonStr(str) {
try {
JSON.parse(str)
return true
} catch {
return false
}
},
handleVideoMessage(messageList) {
const videoMessageList = messageList.filter(
message => message.type === this.TIM.TYPES.MSG_CUSTOM && this.isJsonStr(message.payload.data)
)
if (videoMessageList.length === 0) return
const videoPayload = JSON.parse(videoMessageList[0].payload.data)
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_DIALING) {
if (this.isBusy) {
this.$bus.$emit('busy', videoPayload, videoMessageList[0])
return
}
this.$store.commit('GENERATE_VIDEO_ROOM', videoPayload.room_id)
this.selectConversation(videoMessageList[0].conversationID) // 切换当前会话页
if (videoMessageList[0].from !== this.userID) {
this.$bus.$emit('isCalled')
}
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_SPONSOR_CANCEL) {
this.$bus.$emit('missCall')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_REJECT) {
this.$bus.$emit('isRefused')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_SPONSOR_TIMEOUT) {
this.$bus.$emit('missCall')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_ACCEPTED) {
this.$bus.$emit('isAccept')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_HANGUP) {
this.$bus.$emit('isHungUp')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_LINE_BUSY) {
this.$bus.$emit('isRefused')
}
if (videoPayload.action === ACTION.VIDEO_CALL_ACTION_ERROR) {
this.$bus.$emit('isRefused')
}
},
/**
* 使用 window.Notification 进行全局的系统通知
* @param {Message} message
*/
notifyMe(message) {
// 需检测浏览器支持和用户授权
if (!('Notification' in window)) {
return
} else if (window.Notification.permission === 'granted') {
this.handleNotify(message)
} else if (window.Notification.permission !== 'denied') {
window.Notification.requestPermission().then(permission => {
// 如果用户同意,就可以向他们发送通知
if (permission === 'granted') {
this.handleNotify(message)
}
})
}
},
handleNotify(message) {
const notification = new window.Notification('有人提到了你', {
icon: 'https://webim-1252463788.file.myqcloud.com/demo/img/logo.dc3be0d4.png',
body: message.payload.text
})
notification.onclick = () => {
window.focus()
this.$store.dispatch('checkoutConversation', message.conversationID)
notification.close()
}
},
handleLinkClick() {
MTA.clickStat('link_two', {click: 'true'})
},
/**
* 收到有群成员退群/被踢出的groupTip时,需要将相关群成员从当前会话的群成员列表中移除
* @param {Message[]} messageList
*/
handleQuitGroupTip(messageList) {
// 筛选出当前会话的退群/被踢群的 groupTip
const groupTips = messageList.filter(message => {
return this.currentConversation.conversationID === message.conversationID &&
message.type === this.TIM.TYPES.MSG_GRP_TIP &&
(message.payload.operationType === this.TIM.TYPES.GRP_TIP_MBR_QUIT ||
message.payload.operationType === this.TIM.TYPES.GRP_TIP_MBR_KICKED_OUT)
})
// 清理当前会话的群成员列表
if (groupTips.length > 0) {
groupTips.forEach(groupTip => {
if (Array.isArray(groupTip.payload.userIDList) || groupTip.payload.userIDList.length > 0) {
this.$store.commit('deleteGroupMemberList', groupTip.payload.userIDList)
}
})
}
},
/**
* 收到结束直播自定义消息,派发事件关闭组件
* @param {Message[]} messageList
*/
handleCloseGroupLive(messageList) {
messageList.forEach(message => {
if (this.currentConversation.conversationID === message.conversationID && message.type === this.TIM.TYPES.MSG_CUSTOM) {
let data = {}
try {
data = JSON.parse(message.payload.data)
} catch (e) {
data = {}
}
if (data.roomId && Number(data.roomStatus) === 0) {
this.$bus.$emit('close-group-live')
}
}
})
},
}
}
</script>
<style lang="stylus">
body {
overflow: hidden;
margin: 0;
font-family: 'Microsoft YaHei', '微软雅黑', 'MicrosoftJhengHei', 'Lantinghei SC', 'Open Sans', Arial, 'Hiragino Sans GB', 'STHeiti', 'WenQuanYi Micro Hei', SimSun, sans-serif;
// font-family "Helvetica Neue", Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif
// text-shadow: $regular 0 0 0.05em
background-color: $bg;
-ms-scroll-chaining: chained;
-ms-overflow-style: none;
-ms-content-zooming: zoom;
-ms-scroll-rails: none;
-ms-content-zoom-limit-min: 100%;
-ms-content-zoom-limit-max: 500%;
-ms-scroll-snap-type: proximity;
-ms-scroll-snap-points-x: snapList(100%, 200%, 300%, 400%, 500%);
-ms-overflow-style: none;
overflow: auto;
div {
box-sizing: border-box;
&::before, &::after {
box-sizing: border-box;
}
}
}
#wrapper {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding-top: 60px;
}
.container
position relative
height 100vh
.container
position relative
height 100vh
// TODO filter mac chrome 会有问题,下次修改可以去掉
.bg {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
background: url('~@/./assets/image/bg.jpg') no-repeat 0 0;
background-size: cover;
// filter blur(67px)
}
.loading {
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
}
.text-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.chat-wrapper {
margin-top: 8vh;
width: $width;
height: $height;
max-width: 1280px;
box-shadow: 0 11px 20px 0 rgba(0, 0, 0, 0.3);
.official-link {
display: flex;
text-decoration: none;
color: #38c9ff;
width: fit-content;
float: right;
height: 45px;
align-items: center;
}
}
/* 设置滚动条的样式 */
::-webkit-scrollbar {
width: 3px;
height: 3px;
}
/* 滚动槽 */
::-webkit-scrollbar-track {
border-radius: 10px;
}
/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
border-radius: 10px;
background: rgba(0, 0, 0, 0.1);
}
</style>