current-conversation.vue
7.82 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
<template>
<div class="current-conversation-wrapper">
<div class="current-conversation" @scroll="onScroll" v-if="showCurrentConversation">
<div class="header">
<div class="name">{{ name }}</div>
<div class="btn-more-info"
:class="showConversationProfile ? '' : 'left-arrow'"
@click="showMore"
v-show="!currentConversation.conversationID.includes('SYSTEM')"
title="查看详细信息">
</div>
</div>
<div class="content">
<div class="message-list" ref="message-list" @scroll="this.onScroll">
<div class="more" v-if="!isCompleted">
<el-button
type="text"
@click="$store.dispatch('getMessageList', currentConversation.conversationID)"
>查看更多</el-button>
</div>
<div class="no-more" v-else>没有更多了</div>
<message-item v-for="message in currentMessageList" :key="message.ID" :message="message"/>
</div>
<div v-show="isShowScrollButtomTips" class="newMessageTips" @click="scrollMessageListToButtom">回到最新位置</div>
</div>
<div class="footer" v-if="showMessageSendBox" >
<message-send-box/>
</div>
</div>
<div class="profile" v-if="showConversationProfile" >
<conversation-profile/>
</div>
<!-- 群成员资料组件 -->
<member-profile-card />
</div>
</template>
<script>
import { mapGetters, mapState } from 'vuex'
import MessageSendBox from '../message/message-send-box'
import MessageItem from '../message/message-item'
import ConversationProfile from './conversation-profile.vue'
import MemberProfileCard from '../group/member-profile-card'
export default {
name: 'CurrentConversation',
components: {
MessageSendBox,
MessageItem,
ConversationProfile,
MemberProfileCard
},
data() {
return {
isShowScrollButtomTips: false,
preScrollHeight: 0,
showConversationProfile: false,
timeout: ''
}
},
computed: {
...mapState({
currentConversation: state => state.conversation.currentConversation,
currentUnreadCount: state => state.conversation.currentConversation.unreadCount,
currentMessageList: state => state.conversation.currentMessageList,
isCompleted: state => state.conversation.isCompleted
}),
...mapGetters(['toAccount', 'hidden']),
// 是否显示当前会话组件
showCurrentConversation() {
return !!this.currentConversation.conversationID
},
name() {
if (this.currentConversation.type === 'C2C') {
return this.currentConversation.userProfile.nick || this.toAccount
} else if (this.currentConversation.type === 'GROUP') {
return this.currentConversation.groupProfile.name || this.toAccount
} else if (this.currentConversation.conversationID === '@TIM#SYSTEM') {
return '系统通知'
}
return this.toAccount
},
showMessageSendBox() {
return this.currentConversation.type !== this.TIM.TYPES.CONV_SYSTEM
}
},
mounted() {
this.$bus.$on('image-loaded', this.onImageLoaded)
this.$bus.$on('scroll-bottom', this.scrollMessageListToButtom)
if (this.currentConversation.conversationID === '@TIM#SYSTEM') {
return false
}
},
updated() {
this.keepMessageListOnButtom()
// 1. 系统会话隐藏右侧资料组件
// 2. 没有当前会话时,隐藏右侧资料组件。
// 背景:退出群组/删除会话时,会出现一处空白区域
if (this.currentConversation.conversationID === '@TIM#SYSTEM' ||
typeof this.currentConversation.conversationID === 'undefined') {
this.showConversationProfile = false
}
},
watch: {
currentUnreadCount(next) {
if (!this.hidden && next > 0) {
this.tim.setMessageRead({ conversationID: this.currentConversation.conversationID })
}
},
hidden(next) {
if (!next && this.currentUnreadCount > 0) {
this.tim.setMessageRead({ conversationID: this.currentConversation.conversationID })
}
}
},
methods: {
onScroll({ target: { scrollTop } }) {
let messageListNode = this.$refs['message-list']
if (!messageListNode) {
return
}
if (this.preScrollHeight - messageListNode.clientHeight - scrollTop < 20) {
this.isShowScrollButtomTips = false
}
},
// 如果滚到底部就保持在底部,否则提示是否要滚到底部
keepMessageListOnButtom() {
let messageListNode = this.$refs['message-list']
if (!messageListNode) {
return
}
// 距离底部20px内强制滚到底部,否则提示有新消息
if (this.preScrollHeight - messageListNode.clientHeight - messageListNode.scrollTop < 20) {
this.$nextTick(() => {
messageListNode.scrollTop = messageListNode.scrollHeight
})
this.isShowScrollButtomTips = false
} else {
this.isShowScrollButtomTips = true
}
this.preScrollHeight = messageListNode.scrollHeight
},
// 直接滚到底部
scrollMessageListToButtom() {
this.$nextTick(() => {
let messageListNode = this.$refs['message-list']
if (!messageListNode) {
return
}
messageListNode.scrollTop = messageListNode.scrollHeight
this.preScrollHeight = messageListNode.scrollHeight
this.isShowScrollButtomTips = false
})
},
showMore() {
this.showConversationProfile = !this.showConversationProfile
},
onImageLoaded() {
this.keepMessageListOnButtom()
}
}
}
</script>
<style lang="stylus" scoped>
/* 当前会话的骨架屏 */
.current-conversation-wrapper
height $height
background-color $background-light
color $base
display flex
.current-conversation
display: flex;
flex-direction: column;
width: 100%;
height: $height;
.profile
height: $height;
overflow-y: scroll;
width 220px
border-left 1px solid $border-base
flex-shrink 0
.more
display: flex;
justify-content: center;
font-size: 12px;
.no-more
display: flex;
justify-content: center;
color: $secondary;
font-size: 12px;
padding: 10px 10px;
.header
border-bottom 1px solid $border-base
height 50px
position relative
.name
padding 0 20px
color $base
font-size 18px
font-weight bold
line-height 50px
text-shadow $font-dark 0 0 0.1em
.btn-more-info
position absolute
top 10px
right -15px
border-radius 50%
width 30px
height 30px
cursor pointer
&::before
position absolute
right 0
z-index 0
content ""
width: 15px
height: 30px
border: 1px solid $border-base
border-radius: 0 100% 100% 0/50%
border-left: none
background-color $background-light
&::after
content ""
width: 8px;
height: 8px;
transition: transform 0.8s;
border-top: 2px solid $secondary;
border-right: 2px solid $secondary;
float:right;
position:relative;
top: 11px;
right: 8px;
transform:rotate(45deg)
&.left-arrow
transform rotate(180deg)
&::before
background-color $white
&:hover
&::after
border-color $light-primary
.content
display: flex;
flex 1
flex-direction: column;
height: 100%;
overflow: hidden;
position: relative;
.message-list
width: 100%;
box-sizing: border-box;
overflow-y: scroll;
padding: 0 20px;
.newMessageTips
position: absolute
cursor: pointer;
padding: 5px;
width: 120px;
margin: auto;
left: 0;
right: 0;
bottom: 5px;
font-size: 12px;
text-align: center;
border-radius: 10px;
border: $border-light 1px solid;
background-color: $white;
color: $primary;
.footer
border-top: 1px solid $border-base;
.show-more {
text-align: right;
padding: 10px 20px 0 0;
}
</style>