Files
im/pages/conversation/chating/components/MessageItem/VideoMessageRender.vue
T
cansnow 6720c15e30 27
2026-02-09 07:29:02 +08:00

219 lines
5.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view>
<view class="video_message_container">
<u--image
:showLoading="true"
:src="src"
:width="imgWidth"
:height="maxHeight"
mode="widthFix"
@load="onLoaded" >
<template v-slot:loading>
<u-loading-icon color="red"></u-loading-icon>
</template>
</u--image>
<!-- 覆盖层显示封面下载状态/视频下载入口/播放按钮 -->
<view class="download-overlay" @click.stop="onOverlayClick">
<view v-if="videoDownloading" class="progress-box">
<view class="progress-bar">
<view class="progress-fill" :style="{ width: videoDownloadProgress + '%' }"></view>
</view>
<text class="progress-text">{{ videoDownloadProgress }}%</text>
</view>
<u-icon v-else-if="videoExists" class="play_icon" name="play-circle" size="48" color="#ccc"></u-icon>
<uni-icons v-else class="play_icon" type="cloud-download" size="48" color="#ccc"></uni-icons>
</view>
</view>
<!-- 视频播放器组件 -->
<videoPlayer :previewVideoFlag="previewVideoFlag" :previewVideoSrc="previewVideoSrc" @quitPlay="previewVideoFlag=false"></videoPlayer>
</view>
</template>
<script>
/*
{
"clientMsgID": "d9a596f3e665e84559821a8aa5fb9f16",
"serverMsgID": "76a56965a5fba40df213da2acb331517",
"createTime": 1765413732786,
"sendTime": 1765413733732,
"sessionType": 1,
"sendID": "100003",
"recvID": "100004",
"msgFrom": 100,
"contentType": 104,
"senderPlatformID": 2,
"senderNickname": "131****2222",
"senderFaceUrl": "/static/img/avatar.png",
"seq": 22,
"isRead": false,
"status": 2,
"videoElem": {
"videoPath": "/storage/emulated/0/Movies/顺网云电脑 2025-10-21 04-26-04-converted.mp4",
"videoUrl": "http://www.axzc.xyz/object/100003/msg_video_d9a596f3e665e84559821a8aa5fb9f16.mp4",
"videoType": "mp4",
"videoSize": 3751005,
"duration": 44,
"snapshotPath": "/storage/emulated/0/Android/data/hk.huisheng.im/apps/__UNI__E41111F/doc/video_cover_2ea1c8ec.jpg",
"snapshotSize": 36974,
"snapshotUrl": "http://www.axzc.xyz/object/100003/msg_videoSnapshot_d9a596f3e665e84559821a8aa5fb9f16.jpg",
"snapshotWidth": 1087,
"snapshotHeight": 726
},
"attachedInfoElem": {
"groupHasReadInfo": {
"hasReadCount": 0,
"groupMemberCount": 0
},
"isPrivateChat": false,
"burnDuration": 0,
"hasReadTime": 0,
"isEncryption": false,
"inEncryptStatus": false
}
}
*/
const imgWidth = 240;
import md5 from "md5";
import videoPlayer from "@/components/videoPlayer.vue";
import util from "@/util"
export default {
name: "VideoMessageContainer",
components: {
videoPlayer,
},
props: {
message: Object,
conversationID:String,
},
data() {
return {
loadingWidth: imgWidth+"px",
imgWidth:imgWidth,
isDownloading: false,
downloadProgress: 0,
coverDownloading: false,
coverDownloadProgress: 0,
videoDownloading: false,
videoDownloadProgress: 0,
videoExists: false,
videoCachePath:"",
previewVideoFlag: false,
previewVideoSrc: "",
src:"",
};
},
computed: {
maxHeight() {
const imageHeight = this.message.videoElem.snapshotHeight;
const imageWidth = this.message.videoElem.snapshotWidth;
const aspectRatio = imageHeight / imageWidth;
return imgWidth * aspectRatio;
}
},
async created(){
this.init();
},
methods: {
async init(){
const _this = this;
console.log(this.message?.videoElem,this.conversationID);
const snapshotUrl = this.message?.videoElem?.snapshotUrl;
_this.coverDownloading = true;
//console.log(snapshotUrl);
util.cacheFile(snapshotUrl,`${this.conversationID}`).then((fn)=>{
_this.coverDownloading = false;
_this.src = fn;
//console.log(fn);
});
_this.videoExists = util.fileExsit(util.getCachePath(_this.message?.videoElem?.videoUrl,`${_this.conversationID}`));
},
async onLoaded() {
this.loadingWidth = "auto";
},
onOverlayClick() {
const _this = this;
// 点击覆盖层:如果视频已缓存则直接播放,否则开始下载
const url = _this.message?.videoElem?.videoUrl;
if (!url) {
uni.showToast({ title: '无可下载的视频' });
return;
}
_this.videoDownloading = true;
_this.videoDownloadProgress = 0;
util.cacheFile(url, `${_this.conversationID}`, (prog) => {
//console.log(prog);
this.videoDownloadProgress = prog;
}).then((fn) => {
_this.videoDownloading = false;
_this.videoExists = true;
_this.playVideo(fn);
});
},
playVideo(localPath) {
if (!localPath) return;
this.previewVideoSrc = localPath;
this.previewVideoFlag = true;
},
},
};
</script>
<style lang="scss" scoped>
.video_message_container {
position: relative;
border-radius: 16rpx;
overflow: hidden;
.play_icon {
width: 48px;
height: 48px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.video_duration {
position: absolute;
bottom: 12rpx;
right: 24rpx;
color: #fff;
}
}
.download-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0,0,0,0.35);
}
.progress-box {
width: 70%;
max-width: 240px;
text-align: center;
}
.progress-bar {
width: 100%;
height: 8px;
background: rgba(255,255,255,0.25);
border-radius: 4px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: #4caf50;
width: 0%;
}
.progress-text {
display: block;
color: #fff;
margin-top: 8px;
font-size: 12px;
}
</style>