uploadVideo.vue 8.84 KB
<template>
  <div class="broadcast-container" v-loading="loading">
    <div class="broadcast-content">
      <div class="edit-title">上传视频</div>
      <div class="broadcast-panel gl-form-page">
        <el-form ref="uploadForm" :model="uploadForm" label-width="120px" :rules="uploadRules">
          <el-form-item label="视频名称:" class="dis-in-block" prop="fileName">
            <el-input v-model="uploadForm.fileName" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="宣传文案:" class="dis-in-block">
            <el-input type="textarea" rows="2" placeholder="请输入内容" class="desc-input" v-model="uploadForm.introduction"></el-input>
          </el-form-item>
          <el-form-item label="视频:" class="dis-in-block" prop="mediaUrl">
            <div class="activity-img muti-imgs img-w-h">
              <div class="add-border" v-show="!uploadForm.mediaUrl">
                <div class="add-icon"><i class="el-icon-plus"></i></div>
                <div class="add-text">上传</div>
              </div>
              <div class="video-panel" v-if="uploadForm.mediaUrl">
                <video id="video" width="100%" height="100%" preload loop muted>
                  <source :src="uploadForm.mediaUrl" type="video/mp4" />
                </video>
                <i class="el-icon-video-play" @click="handleLook('video')"></i>
                <i class="el-icon-close" @click="uploadForm.mediaUrl = ''"></i>
              </div>
              <input v-if="!uploadForm.mediaUrl" type="file" class="activity-Input img-w-h" accept="video/*" @change="e => handleVideoChange(e)" style="opacity: 0" />
            </div>
          </el-form-item>
          <el-form-item label="封面:" class="dis-in-block" prop="coverUrl">
            <div class="activity-img muti-imgs img-w-h">
              <div class="add-border" v-show="!uploadForm.coverUrl">
                <div class="add-icon"><i class="el-icon-plus"></i></div>
                <div class="add-text">上传</div>
              </div>
              <img class="img-w-h" :src="uploadForm.coverUrl" v-show="uploadForm.coverUrl" />
              <span class="img-btns" v-show="uploadForm.coverUrl">
                <span class="btn-item">
                  <i @click="handleImgDelete" class="el-icon-delete" />
                  <i @click="handleLook('img')" class="el-icon-zoom-in" />
                </span>
              </span>
              <input type="file" class="activity-Input img-w-h" accept="image/png, image/jpeg, image/gif, image/jpg" @change="e => handleImgChange(e)" style="opacity: 0" />
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="broadcast-footer">
      <el-button type="primary" @click="handleUp" class="ok-btn">
        提交
      </el-button>
      <el-button class="cancel-btn" @click="handleClose">
        取消
      </el-button>
    </div>
    <el-dialog class="image-dialog" :visible.sync="dialogVisibleImg" width="30%">
      <big-view ref="viewForm" v-if="dialogVisibleImg" :dialogSrc="dialogSrc" :dialogType="dialogType"></big-view>
    </el-dialog>
  </div>
</template>

<script>
import upload from '@/api/uploadApi'
import uploadMixin from '@/mixins/upload'
// const upload = new GlsxUpload({ baseURL: 'http://scrm.dev.glsx.com.cn' })
import bigView from '@/components/big-view'
export default {
  name: 'uploadVideo',
  mixins: [uploadMixin],
  components: {
    bigView
  },
  data() {
    return {
      limitSize: 20,
      size: 'small',
      dialogVisibleImg: false,
      loading: false,
      uploadForm: {
        id: ''
      },
      dialogSrc: '',
      dialogType: '',
      uploadRules: {
        fileName: [{ required: true, message: '请输入视频名称', trigger: 'blur' }],
        mediaUrl: [{ required: true, message: '请选择视频', trigger: 'blur' }],
        coverUrl: [{ required: true, message: '请选择封面', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleImgDelete() {
      this.uploadForm.coverUrl = ''
    },
    handleLook(type) {
      this.dialogType = type
      this.dialogVisibleImg = true
      this.dialogSrc = type == 'img' ? this.uploadForm.coverUrl : this.uploadForm.mediaUrl
    },
    handleClose() {
      this.$router.push('/shortVideo/index')
    },
    handleVideoChange(e) {
      const self = this
      let file = e.target.files[0]
      const isLt = file.size / 1024 / 1024 < this.limitSize
      if (!isLt) {
        this.$message.error('上传视频大小不能超过' + this.limitSize + 'MB!')
        return true
      }
      self.loading = true
      upload.init().then(res => {
        if (res.status == 0) {
          upload
            .uploaded(file)
            .then(res => {
              self.$refs['uploadForm'].clearValidate('mediaUrl')
              const videoUrl = res.video.url
              self.$set(self.uploadForm, 'mediaUrl', videoUrl)
              self.$set(self.uploadForm, 'fileId', res.fileId)
              self.$nextTick(() => {
                self.getImgUrl(videoUrl).then(res => {
                  self.$set(self.uploadForm, 'coverUrl', res)
                })
              })
              self.loading = false
            })
            .finally(end => {
              console.log(end)
            })
          upload.uploadEvent('', res => {
            console.log(res, '视频上传完成')
          })
          upload.uploadEvent('media_progress', res => {
            console.log(res)
          })
        }
      })
    },
    // // 图片
    handleImgChange(e) {
      var reader = new FileReader()
      var fileInfo = e.srcElement.files[0]
      reader.readAsDataURL(fileInfo)
      reader.onload = e => {
        const imgFile = e.target.result
        const base64 = fileInfo.name.split('.')[1] + '@' + imgFile.split(',')[1]
        this.$apis.common.pictureUpload({ file: base64 }).then(res => {
          this.$refs['uploadForm'].clearValidate('coverUrl')
          this.$set(this.uploadForm, 'coverUrl', res)
        })
      }
    },
    handleUp() {
      this.$refs['uploadForm'].validate(valid => {
        if (valid) {
          this.$apis.shortVideo.tencentcloudmediaUpdate(this.uploadForm).then(res => {
            if (res) {
              this.$router.push('/shortVideo/index')
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            }
          })
        }
      })
    },
    mediaDetail() {
      const { id } = this.uploadForm
      this.$apis.shortVideo.tencentcloudmediaDetail({ id }).then(res => {
        this.uploadForm = res
      })
    }
  },
  mounted() {
    const { id } = this.$route.query
    if (id) {
      this.uploadForm.id = id
      this.mediaDetail()
    }
  }
}
</script>
<style lang="scss" scoped>
.broadcast-content {
  background: #fff;
  height: calc(100vh - 136px);
  .edit-title {
    font-weight: 600;
    font-size: 16px;
    color: #333333;
    text-align: left;
    line-height: 58px;
    height: 58px;
    padding-left: 20px;
    border-bottom: 1px solid #e5e5e5;
  }
}
.broadcast-footer {
  margin-top: 16px;
  text-align: center;
}
.broadcast-panel {
  margin-top: 20px;
  .activity-img {
    width: 104px;
    height: 104px;
    background: rgba(0, 0, 0, 0.04);
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 4px;
    cursor: pointer;
    position: relative;
    input {
      cursor: pointer;
      font-size: 0;
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
    }
    .add-border {
      text-align: center;
      color: rgba(0, 0, 0, 0.45);
      .add-icon {
        margin-top: 20px;
        height: 20px;
        font-size: 17px;
      }
      .add-text {
        height: 30px;
        font-size: 12px;
      }
    }
  }
  .img-btns,
  img {
    width: 100%;
    height: 100%;
  }
  .video-panel {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    video {
      position: absolute;
      object-fit: fill;
    }
    .el-icon-video-play {
      z-index: 10;
      font-size: 20px;
      color: #fff;
      cursor: pointer;
    }
    .el-icon-close {
      position: absolute;
      top: 0px;
      right: 0px;
      background: #000000;
      color: #fff;
    }
  }
}
.video-dialog {
  z-index: 2001;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  background: rgba($color: #000000, $alpha: 0.5);
  &-panel {
    background: #000000;
    position: absolute;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    height: calc(100% - 200px);
    width: auto;
    box-sizing: border-box;
    padding: 10px 20px 20px;
    overflow: hidden;
    .video-close {
      display: flex;
      justify-content: flex-end;
      height: 40px;
      i {
        cursor: pointer;
        font-size: 25px;
        color: #5850ff;
      }
    }
  }
}
</style>