conversation-item.vue
8.67 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
<template>
<div
class="conversation-item-container"
:class="{
choose:
conversation.conversationID === currentConversation.conversationID,
}"
@click="selectConversation"
>
<div class="close-btn">
<span
class="tim-icon-close"
title="删除会话"
@click="deleteConversation"
></span>
</div>
<div class="warp">
<avatar :src="avatar" :type="conversation.type" />
<div class="content">
<div class="row-1">
<div class="name">
<div class="text-ellipsis">
<span
:title="
conversation.userProfile.nick ||
conversation.userProfile.userID
"
v-if="conversation.type === TIM.TYPES.CONV_C2C"
>{{
conversation.userProfile.nick ||
conversation.userProfile.userID
}}
</span>
<span
:title="
conversation.groupProfile.name ||
conversation.groupProfile.groupID
"
v-else-if="conversation.type === TIM.TYPES.CONV_GROUP"
>{{
conversation.groupProfile.name ||
conversation.groupProfile.groupID
}}
</span>
<span v-else-if="conversation.type === TIM.TYPES.CONV_SYSTEM"
>系统通知
</span>
</div>
</div>
<div class="unread-count">
<span class="badge" v-if="showUnreadCount">
{{
conversation.unreadCount > 99 ? "99+" : conversation.unreadCount
}}
</span>
</div>
</div>
<div class="row-2">
<div class="summary">
<div v-if="conversation.lastMessage" class="text-ellipsis">
<span class="remind" style="color: red" v-if="hasMessageAtMe"
>[有人提到我]</span
>
<span
class="text"
:title="conversation.lastMessage.messageForShow"
>
{{ messageForShow }}
</span>
</div>
</div>
<div class="date">
{{ date }}
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
import _ from "loadsh";
import { getDate, getTime, isToday } from "../../utils/date";
export default {
name: "conversation-item",
props: ["conversation"],
filters: {},
data() {
return {
popoverVisible: false,
hasMessageAtMe: false,
};
},
computed: {
showUnreadCount() {
if (this.$store.getters.hidden) {
return this.conversation.unreadCount > 0;
}
// 是否显示未读计数。当前会话和未读计数为0的会话,不显示。
return (
this.currentConversation.conversationID !==
this.conversation.conversationID && this.conversation.unreadCount > 0
);
},
date() {
if (
!this.conversation.lastMessage ||
!this.conversation.lastMessage.lastTime
) {
return "";
}
const date = new Date(this.conversation.lastMessage.lastTime * 1000);
if (isToday(date)) {
return getTime(date);
}
return getDate(date);
},
avatar: function () {
switch (this.conversation.type) {
case "GROUP":
return this.conversation.groupProfile.avatar;
case "C2C":
return this.conversation.userProfile.avatar;
default:
return "";
}
},
conversationName: function () {
if (this.conversation.type === this.TIM.TYPES.CONV_C2C) {
return (
this.conversation.userProfile.nick ||
this.conversation.userProfile.userID
);
}
if (this.conversation.type === this.TIM.TYPES.CONV_GROUP) {
return (
this.conversation.groupProfile.name ||
this.conversation.groupProfile.groupID
);
}
if (this.conversation.type === this.TIM.TYPES.CONV_SYSTEM) {
return "系统通知";
}
return "";
},
showGrayBadge() {
if (this.conversation.type !== this.TIM.TYPES.CONV_GROUP) {
return false;
}
return (
this.conversation.groupProfile.selfInfo.messageRemindType ===
"AcceptNotNotify"
);
},
messageForShow() {
if (this.conversation.lastMessage.isRevoked) {
if (
this.conversation.lastMessage.fromAccount ===
this.currentUserProfile.userID
) {
return "你撤回了一条消息";
}
if (this.conversation.type === this.TIM.TYPES.CONV_C2C) {
return "对方撤回了一条消息";
}
return `${
this.conversation.lastMessage.nick ||
this.conversation.lastMessage.fromAccount
}撤回了一条消息`;
} else {
switch (_.get(this.conversation.lastMessage, "payload.data", "other")) {
case "image":
return "[图片]";
case "video":
return "[视频]";
default:
return this.conversation.lastMessage.messageForShow;
}
}
},
...mapState({
currentConversation: (state) => state.conversation.currentConversation,
currentUserProfile: (state) => state.user.currentUserProfile,
}),
...mapGetters(["toAccount"]),
},
mounted() {
this.$bus.$on("new-messsage-at-me", (event) => {
if (
event.data.conversationID === this.conversation.conversationID &&
this.conversation.conversationID !==
this.currentConversation.conversationID
) {
this.hasMessageAtMe = true;
}
});
},
methods: {
selectConversation() {
if (
this.conversation.conversationID !==
this.currentConversation.conversationID
) {
this.$store.dispatch(
"checkoutConversation",
this.conversation.conversationID
);
}
},
deleteConversation(event) {
// 停止冒泡,避免和点击会话的事件冲突
event.stopPropagation();
this.tim
.deleteConversation(this.conversation.conversationID)
.then(() => {
this.$store.commit("showMessage", {
message: `会话【${this.conversationName}】删除成功!`,
type: "success",
});
this.popoverVisible = false;
this.$store.commit("resetCurrentConversation");
})
.catch((error) => {
this.$store.commit("showMessage", {
message: `会话【${this.conversationName}】删除失败!, error=${error.message}`,
type: "error",
});
this.popoverVisible = false;
});
},
showContextMenu() {
this.popoverVisible = true;
},
},
watch: {
currentConversation(next) {
if (next.conversationID === this.conversation.conversationID) {
this.hasMessageAtMe = false;
}
},
},
};
</script>
<style lang="stylus" scoped>
.conversation-item-container
padding 15px 20px
cursor pointer
position relative
overflow hidden
transition .2s
// &:first-child
// padding-top 30px
&:hover
background-color $background
.close-btn
right 3px
.close-btn
position absolute
right -20px
top 3px
color $font-dark
transition: all .2s ease;
&:hover
color $danger
.warp
display flex
.avatar
width 40px
height 40px
margin-right 10px
border-radius 50%
flex-shrink 0
.content
flex 1
height 40px
overflow hidden
.row-1
display flex
line-height 21px
.name
color $font-light
flex 1
min-width 0px
.unread-count
padding-left 10px
flex-shrink 0
color $font-dark
font-size 12px
.badge
vertical-align bottom
background-color $danger
border-radius 10px
color #FFF
display inline-block
font-size 12px
height 18px
max-width 40px
line-height 18px
padding 0 6px
text-align center
white-space nowrap
.row-2
display flex
font-size 12px
padding-top 3px
.summary
flex 1
overflow hidden
min-width 0px
color: $secondary
.remind
color $danger
.date
padding-left 10px
flex-shrink 0
text-align right
color $font-dark
.choose {
background-color: $background;
}
.context-menu-button {
padding: 10px
border: 2px solid $primary;
border-radius: 8px;
}
</style>