一、定义api

创建video.js
参考course.js

  1. import request from '@/utils/request'
  2. const api_name = '/admin/edu/video'
  3. export default {
  4. saveVideoInfo(videoInfo) {
  5. return request({
  6. url: `${api_name}/save-video-info`,
  7. method: 'post',
  8. data: videoInfo
  9. })
  10. },
  11. getVideoInfoById(id) {
  12. return request({
  13. url: `${api_name}/video-info/${id}`,
  14. method: 'get'
  15. })
  16. },
  17. updateVideoInfoById(videoInfo) {
  18. return request({
  19. url: `${api_name}/update-video-info/${videoInfo.id}`,
  20. method: 'put',
  21. data: videoInfo
  22. })
  23. },
  24. removeById(id) {
  25. return request({
  26. url: `${api_name}/${id}`,
  27. method: 'delete'
  28. })
  29. }
  30. }

二、新增课时页面功能

1、定义data数据

  1. saveVideoBtnDisabled: false, // 课时按钮是否禁用
  2. dialogVideoFormVisible: false, // 是否显示课时表单
  3. chapterId: '', // 课时所在的章节id
  4. video: {// 课时对象
  5. title: '',
  6. sort: 0,
  7. free: 0,
  8. videoSourceId: ''
  9. },

2、添加课时按钮

<el-button type="text" @click="dialogVideoFormVisible = true; chapterId = chapter.id">添加课时</el-button>

3、课时表单dialog

<!-- 添加和修改课时表单 -->
<el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
  <el-form :model="video" label-width="120px">
    <el-form-item label="课时标题">
      <el-input v-model="video.title"/>
    </el-form-item>
    <el-form-item label="课时排序">
      <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
    </el-form-item>
    <el-form-item label="是否免费">
      <el-radio-group v-model="video.free">
        <el-radio :label="true">免费</el-radio>
        <el-radio :label="false">默认</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="上传视频">
      <!-- TODO -->
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
    <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
  </div>

</el-dialog>

4、添加课时methods

引入video模块

import video from '@/api/edu/video'

方法的定义

saveOrUpdateVideo() {
   this.saveVideoBtnDisabled = true
   if (!this.video.id) {
     this.saveDataVideo()
   } else {
     this.updateDataVideo()

  }
},
saveDataVideo() {
  this.video.courseId = this.courseId
  this.video.chapterId = this.chapterId
  video.saveVideoInfo(this.video).then(response => {
    this.$message({
      type: 'success',
      message: '保存成功!'
    })
    this.helpSaveVideo()
  })

},
updateDataVideo() {

},
helpSaveVideo() {
  this.dialogVideoFormVisible = false// 如果保存成功则关闭对话框
  this.fetchChapterNestedListByCourseId()// 刷新列表
  this.video.title = ''// 重置章节标题
  this.video.sort = 0// 重置章节标题
  this.video.videoSourceId = ''// 重置视频资源id
  this.saveVideoBtnDisabled = false

},

三、修改课时信息

1、编辑课时按钮

<el-button type="text" @click="editVideo(video.id)">编辑</el-button>

2、定义编辑方法

editVideo(videoId) {
   this.dialogVideoFormVisible = true
   video.getVideoInfoById(videoId).then(response => {
     this.video = response.data.item
   })

},

3、定义更新方法

updateDataVideo() {
   video.updateVideoInfoById(this.video).then(response => {
     this.$message({
       type: 'success',
       message: '修改成功!'
     })
     this.helpSaveVideo()
   })

},

四、删除课时

1、按钮

<el-button type="text" @click="removeVideo(video.id)">删除</el-button>

2、定义删除方法

1
removeVideo(videoId) {
  this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    return video.removeById(videoId)
  }).then(() => {
    this.fetchChapterNestedListByCourseId()// 刷新列表
    this.$message({
      type: 'success',
      message: '删除成功!'
    })
  }).catch((response) => { // 失败
    if (response === 'cancel') {
      this.$message({
        type: 'info',
        message: '已取消删除'
      })
    }
  })

}