163 lines
3.6 KiB
JavaScript
163 lines
3.6 KiB
JavaScript
// 消息组件逻辑
|
|
Component({
|
|
properties: {
|
|
// 消息内容
|
|
content: {
|
|
type: String,
|
|
value: ''
|
|
},
|
|
// 消息类型
|
|
type: {
|
|
type: String,
|
|
value: 'text'
|
|
},
|
|
// 发送者头像
|
|
avatar: {
|
|
type: String,
|
|
value: '/assets/images/default-avatar.png'
|
|
},
|
|
// 发送者昵称
|
|
nickname: {
|
|
type: String,
|
|
value: '未知用户'
|
|
},
|
|
// 发送时间
|
|
time: {
|
|
type: String,
|
|
value: ''
|
|
},
|
|
// 是否是自己发送的消息
|
|
isMe: {
|
|
type: Boolean,
|
|
value: false
|
|
},
|
|
// 文件名(文件消息)
|
|
fileName: {
|
|
type: String,
|
|
value: ''
|
|
},
|
|
// 文件大小(文件消息)
|
|
fileSize: {
|
|
type: String,
|
|
value: ''
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
// 预览图片
|
|
previewImage() {
|
|
if (this.properties.type === 'image') {
|
|
wx.previewImage({
|
|
urls: [this.properties.content],
|
|
current: this.properties.content
|
|
})
|
|
}
|
|
},
|
|
|
|
// 下载文件
|
|
downloadFile() {
|
|
if (this.properties.type === 'file') {
|
|
wx.showLoading({
|
|
title: '下载中...'
|
|
})
|
|
|
|
wx.downloadFile({
|
|
url: this.properties.content,
|
|
success: (res) => {
|
|
wx.hideLoading()
|
|
|
|
if (res.statusCode === 200) {
|
|
// 保存文件到本地
|
|
wx.saveFile({
|
|
tempFilePath: res.tempFilePath,
|
|
success: (saveRes) => {
|
|
wx.showToast({
|
|
title: '文件已保存',
|
|
icon: 'success'
|
|
})
|
|
|
|
// 打开文件
|
|
wx.openDocument({
|
|
filePath: saveRes.savedFilePath,
|
|
showMenu: true
|
|
})
|
|
},
|
|
fail: () => {
|
|
wx.showToast({
|
|
title: '保存失败',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
})
|
|
} else {
|
|
wx.showToast({
|
|
title: '下载失败',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
},
|
|
fail: () => {
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title: '下载失败',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
})
|
|
}
|
|
},
|
|
|
|
// 长按消息
|
|
onLongPress() {
|
|
wx.showActionSheet({
|
|
itemList: ['复制', '转发', '删除'],
|
|
success: (res) => {
|
|
switch (res.tapIndex) {
|
|
case 0: // 复制
|
|
this.copyMessage()
|
|
break
|
|
case 1: // 转发
|
|
this.forwardMessage()
|
|
break
|
|
case 2: // 删除
|
|
this.deleteMessage()
|
|
break
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
// 复制消息
|
|
copyMessage() {
|
|
if (this.properties.type === 'text') {
|
|
wx.setClipboardData({
|
|
data: this.properties.content,
|
|
success: () => {
|
|
wx.showToast({
|
|
title: '已复制',
|
|
icon: 'success'
|
|
})
|
|
}
|
|
})
|
|
}
|
|
},
|
|
|
|
// 转发消息
|
|
forwardMessage() {
|
|
this.triggerEvent('forward', {
|
|
content: this.properties.content,
|
|
type: this.properties.type,
|
|
fileName: this.properties.fileName,
|
|
fileSize: this.properties.fileSize
|
|
})
|
|
},
|
|
|
|
// 删除消息
|
|
deleteMessage() {
|
|
this.triggerEvent('delete', {
|
|
content: this.properties.content,
|
|
type: this.properties.type
|
|
})
|
|
}
|
|
}
|
|
}) |