/
renderMsg.vue
325 lines (297 loc) · 10.2 KB
/
renderMsg.vue
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
<template>
<div>
<!-- <div v-if="messageType===1"> -->
<div class="timeline" v-if="timeMessage != ''">{{ timeMessage }}</div>
<div :class="{ messageFrame: true, self: isSelf, roster: !isSelf }">
<div class="rosterInfo">
<img :src="userObj.avatar" />
</div>
<div class="contentFrame">
<p class="username" v-if="!isSelf">{{ userObj.username }}</p>
<div class="c_content">
<div v-if="message.type === 'text'">
{{ message.content }}
<div v-if="message.ext">ext:{{ message.ext }}</div>
</div>
<div v-if="message.type === 'image'">
<img :src="attachImage" @click="touchImage" v-if="attachImage !== ''" />
</div>
<div @click="playAudio" class="audio_frame" v-if="message.type === 'audio'">
<img class="audio" src="/image/audio.png" />
</div>
<div @click="playVideo" class="video_frame" v-if="message.type === 'video'">
<img :src="videoImage" class="preview" />
<img class="play" src="/image/play.png" />
</div>
<div class="loc_frame" v-if="message.type === 'file'">
<img class="loc" src="/image/file2.png" />
<span @click="downloadFile" class="loc_txt">{{ attachName }}</span>
</div>
<div @click="openLocation" class="loc_frame" v-if="message.type === 'location'">
<img class="loc" src="/image/loc.png" />
<span class="loc_txt">{{ attachLocation.addr }}</span>
</div>
<el-popover :placement="isSelf ? 'left' : 'right'" trigger="hover" width="70">
<div class="messageExt">
<div @click="deleteMessage" class="item delete" v-if="!message.h">删除</div>
<div @click="forwardMessage" class="recall item">转发</div>
<div @click="recallMessage" class="recall item" v-if="isSelf && !message.h">撤回</div>
<div class="msgStatus item item" v-if="isSelf && messageStatus === 'unread' && !message.h">未读</div>
<div class="msgStatus item" v-if="isSelf && messageStatus === 'delivered' && !message.h">送达</div>
<div class="msgStatus item" v-if="isSelf && messageStatus === 'read' && !message.h">已读</div>
<div class="unread item" v-if="messageStatus === 'unread' && !isSelf && !message.h">未读</div>
<div @click="unreadMessage" class="set_unread item" v-if="messageStatus !== 'unread' && !isSelf && !message.h">设置未读</div>
</div>
<div class="h_image" slot="reference">
<img src="/image/more.png" />
</div>
</el-popover>
</div>
</div>
</div>
<!-- </div> -->
<!-- <div v-if="messageType===3">
renderRosterNotice
</div>
<div v-if="messageType===4">
renderUserNotice
</div> -->
</div>
</template>
<script>
// import Chat from "./chat.vue";
// import Inputer from "./inputer.vue";
import moment from 'moment';
import { numToString, toNumber } from '../../../third/tools';
import { mapGetters } from 'vuex';
export default {
name: 'RosterChat',
data() {
return {
system_roster: {
name: '系统通知',
avatar: '/image/setting.png'
}
};
},
mounted() {
let { timestamp } = this.message;
timestamp = toNumber(timestamp);
const savedMessageTime = this.getMessageTime;
const last = (savedMessageTime.length && savedMessageTime[savedMessageTime.length - 1]) || 0;
if (timestamp - last > 5 * 60 * 1000) {
this.$store.dispatch('content/actionUpdateMessageTime', timestamp);
}
// Message displayed as read
const fromUid = toNumber(this.message.from);
const uid = this.$store.getters.im.userManage.getUid();
if (fromUid !== uid) {
//do not read message sent by oneself
const im = this.$store.getters.im;
if (im) im.rosterManage.readRosterMessage(this.getSid, this.message.id);
}
},
components: {
// Chat,
// Inputer
},
props: ['message'],
computed: {
...mapGetters('content', ['getSid', 'getMessageTime']),
timeMessage() {
let { timestamp } = this.message;
timestamp = toNumber(timestamp);
if (this.getMessageTime.indexOf(timestamp) >= 0) {
return moment(timestamp).calendar('', {
sameDay: 'HH:mm',
lastDay: 'HH:mm',
sameElse: 'YYYY-MM-DD HH:mm'
});
}
return '';
},
im() {
return this.$store.state.im;
},
token() {
return this.im.userManage.getToken();
},
isSelf() {
const uid = this.im.userManage.getUid();
const cid = numToString(this.message.from);
return cid + '' === uid + '';
},
userObj() {
const cuid = this.im.userManage.getUid();
const umaps = this.im.rosterManage.getAllRosterDetail();
const fromUid = toNumber(this.message.from);
const fromUserObj = umaps[fromUid] || {};
let username = fromUserObj.username || '';
let avatar = this.im.sysManage.getImage({ avatar: fromUserObj.avatar });
if (fromUid === cuid) {
username = '我';
} else if (0 == fromUid) {
username = this.system_roster.name;
avatar = this.system_roster.avatar;
}
return { username, avatar };
},
attachUrl() {
const attach = this.message.attach || {};
const { url = '' } = attach;
return this.im.sysManage.getChatFile({ url });
},
attachImage() {
return this.getImage({});
},
attachAudio() {
const attach = this.message.attach || {};
return this.im.sysManage.getAudio({ url: attach.url });
},
attachVideo() {
return this.getVideo();
},
attachFile() {
return this.attachUrl;
},
videoImage() {
const attachment = this.message.attach || '{}';
const { url, tUrl } = attachment;
if (tUrl && tUrl.length) {
return this.getImage({ url: tUrl, thumbnail: true });
} else if (url) {
return this.getVideo(true);
}
return url;
},
attachLocation() {
const attachObj = this.message.attach || {};
let loc = {};
if (attachObj.lat) {
loc.addr = attachObj.addr;
// attachObj.lat = 39.9087;
// attachObj.lon = 116.3975;
//"lat":39.90374,"lon":116.397827,"addr":"天安门广场
//title必须跟坐标对应,否则不出东西。。
//url = 'http://map.baidu.com/?latlng=' + attachObj.lat + ',' + attachObj.lon + '&title=' + attachObj.addr + '&content=' + attachObj.addr + '&autoOpen=true';
loc.url = 'http://map.baidu.com/?latlng=' + attachObj.lat + ',' + attachObj.lon;
}
return loc;
},
attachName() {
const attachment = this.message.attach || '{}';
let attachObj = {};
try {
attachObj = JSON.parse(attachment);
} catch (ex) {
//
}
if (attachObj.dName) {
return attachObj.dName;
}
return '文件附件';
},
messageStatus() {
const fromUid = toNumber(this.message.from);
const toUid = toNumber(this.message.to);
const uid = this.im.userManage.getUid();
const cid = fromUid === uid ? toUid : fromUid;
// status will be unread / delivered / read
return this.im.sysManage.getMessageStatus(cid, this.message.id);
}
},
methods: {
getImage({ url = '', thumbnail = true }) {
if (!url) {
const attach = this.message.attach || {};
url = attach.url;
}
return this.im.sysManage.getImage({ avatar: url, thumbnail });
},
touchImage() {
const image = this.getImage({ thumbnail: false });
if (image) {
this.openImage(image);
} else {
alert('附件错误..');
}
},
playAudio() {
const url = this.attachAudio;
if (!url) {
alert('url为空,不能播放');
return;
}
const au = document.querySelector('#audio_player');
au.src = url;
au.play();
},
getBlob(url) {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.response);
}
};
xhr.send();
});
},
downloadFile() {
if (this.attachUrl) {
//方法 1 可直接下载文件,如果你想重命名,可以使用方法2,但要注意在 Safari 浏览器里,有些扩展名可能因为安全原因被屏蔽
//1. 直接下载
window.open(this.attachUrl);
// //2. 下载并重命名
// this.getBlob(this.attachUrl).then(blob =>{
// var link = document.createElement("a");
// link.href = window.URL.createObjectURL(blob);;
// link.download = "Example.txt";
// link.click();
// });
} else {
alert('附件错误..');
}
},
openLocation() {
this.attachLocation.url && window.open(this.attachLocation.url);
},
//
deleteMessage() {
const idStr = numToString(this.message.id).toString();
this.im.rosterManage.deleteMessage(this.getSid, idStr);
},
forwardMessage() {
this.$store.dispatch('forward/actionRecordForwardMessage', this.message);
},
recallMessage() {
const idStr = numToString(this.message.id).toString();
this.im.rosterManage.recallMessage(this.getSid, idStr);
},
unreadMessage() {
const idStr = numToString(this.message.id).toString();
this.im.rosterManage.unreadMessage(this.getSid, idStr);
},
getVideo(cover = false) {
let url = this.attachUrl;
if (cover) {
url += '&imgage_type=3';
}
return url;
},
openImage(url) {
this.$store.dispatch('layer/actionSetShowing', 'image');
this.$store.dispatch('layer/actionSetShowmask', true);
this.$store.dispatch('layer/actionSetImageUrl', url);
},
playVideo() {
let attachUrl = this.attachUrl;
this.$store.dispatch('layer/actionSetShowing', 'video');
this.$store.dispatch('layer/actionSetShowmask', true);
this.$store.dispatch('layer/actionSetVideoUrl', attachUrl);
}
}
};
</script>