封装el-upload,实现可上传视频、播放视频、编辑视频、移除视频,上传进度条展示

效果图

在这里插入图片描述

在这里插入图片描述

完整子组件代码
<template>
    <div>
        <el-upload
            :action="action"
            list-type="picture-card"
            :on-success="handleSuccess"
            :file-list="fileLists"
            :on-progress="handleProgress"
            :data="data"
            accept="video/mp4, video/ogg, video/flv,video/avi,video/wmv,video/rmvb"
            :headers="{username:username}"
            :disabled="fileLists.length >= limit || uploadBtn">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
                <video class="el-upload-list__item-thumbnail" :src="file.url" alt=""></video>
                <span class="el-upload-list__item-actions">
                    <span class="el-upload-list__item-preview" @click="handleShowVideo(file)">
                        <i class="el-icon-video-play"></i>
                    </span>
                    <span class="el-upload-list__item-edit" @click="handleEditVideo(file)">
                        <i class="el-icon-edit"></i>
                    </span>
                    <span class="el-upload-list__item-delete" @click="handleRemove(file)">
                        <i class="el-icon-delete"></i>
                    </span>
                </span>
                <el-progress type="circle" class="progressModule" :color="colors" :percentage="Number(uploadPercentage)" v-if="showProgress && file.url == uploadUrl"></el-progress>
            </div>
        </el-upload>
        
        <el-dialog :visible.sync="dialogVisible" append-to-body width="40%" style="text-align:center">
            <video :src="dialogImageUrl" alt="" autoplay class="video" controls="controls"></video>
        </el-dialog>
        <el-dialog :visible.sync="editView" width="40%" append-to-body>
            <el-input type="textarea" :rows="4" v-model="editForm.url" @input="editVideo"></el-input>
        </el-dialog>
    </div>
</template>

<script>
export default {
    props:{
        limit:{
            type:Number,
            default:12,
        },
        action:{
            type:String,
            default:'#'
        },
        fileList:{
            type:Array,
            default: ()=>{
                return []
            }
        },
        data:{
            type:Object,
            default:() =>{
                return {}
            }
        }
    },
    data(){
        return{
            dialogImageUrl: '',
            dialogVisible: false,
            fileLists:this.fileList,
            editForm:{
                url:'',
                uid:null
            },
            editView:false,
            username:'',
            uploadPercentage:0,
            showProgress:false,
            uploadUrl:'',
            colors: [
                {color: '#ADD8E6', percentage: 20},
                {color: '#87CEEB', percentage: 40},
                {color: '#87CEFA', percentage: 60},
                {color: '#00BFFF', percentage: 80},
                {color: '#1296DB', percentage: 100}
            ],
            uploadBtn:false,
        }
    },
    mounted(){
        this.username = Cookies.get('username')
    },
    methods:{
        // 移除视频
        handleRemove(file) {
            for(let i in this.fileLists){
                if(this.fileLists[i].uid == file.uid){
                    this.fileLists.splice(i,1)
                }
            }
            this.submitFile()
        },
        handleShowVideo(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        // 上传完成
        handleSuccess(response,file,fileList){
            this.showProgress = false
            this.uploadBtn = false
            if(response.code != '200'){
                for(var i =0;i<fileList.length;i++){
                    if(i+1 == fileList.length){
                        fileList.splice(i,1)
                    }
                }
                this.$message.error(response.sub_msg)
                return
            }else{
                this.$message.success(response.sub_msg)
                let obj = {
                    name: response.data.pictureName,
                    status: "success",
                    uid: file.uid,
                    url: response.data.url,
                }
                this.fileLists.push(obj)
                this.submitFile()
            }
        },
        // 播放视频
        handleEditVideo(file){
            this.editForm.url = file.url
            this.editForm.uid = file.uid
            this.editView = true
        },
        // 编辑视频
        editVideo(){
            for(let i in this.fileLists){
                if(this.fileLists[i].uid == this.editForm.uid){
                    this.fileLists[i].url = this.editForm.url
                }
            }
            this.submitFile()
        },
        // 将图片文件传回给父组件
        submitFile(){
            this.$emit('submitImg',this.fileLists)
        },
        // 上传进度
        handleProgress(response,file,fileList){
            this.uploadBtn = true
            this.uploadUrl = file.url
            this.showProgress = true
            this.uploadPercentage = file.percentage.toFixed(0)
        },
    }
}
</script>

<style scoped>
    .el-icon-plus{
        font-size: 30px!important;
    }
    .el-icon-edit{
        font-size: 18px !important;
    }
    .el-icon-video-play{
        font-size: 18px !important;
        
    }
    .el-icon-delete{
        font-size: 18px !important;
        color:rgb(243, 143, 130);
    }
    .el-input>>> .el-textarea__inner{
        font-size:18px!important;
    }
    .video{
        min-height: 200px;
        max-height: 600px;
        min-width: 200px;
        max-width: 100%;
    }
    .progressModule >>> .el-progress__text{
        color:#1296DB;
        font-size: 15px!important; 
    }
</style>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值