一、定义api
创建video.js
参考course.js
import request from '@/utils/request'
const api_name = '/admin/edu/video'
export default {
saveVideoInfo(videoInfo) {
return request({
url: `${api_name}/save-video-info`,
method: 'post',
data: videoInfo
})
},
getVideoInfoById(id) {
return request({
url: `${api_name}/video-info/${id}`,
method: 'get'
})
},
updateVideoInfoById(videoInfo) {
return request({
url: `${api_name}/update-video-info/${videoInfo.id}`,
method: 'put',
data: videoInfo
})
},
removeById(id) {
return request({
url: `${api_name}/${id}`,
method: 'delete'
})
}
}
二、新增课时页面功能
1、定义data数据
saveVideoBtnDisabled: false, // 课时按钮是否禁用
dialogVideoFormVisible: false, // 是否显示课时表单
chapterId: '', // 课时所在的章节id
video: {// 课时对象
title: '',
sort: 0,
free: 0,
videoSourceId: ''
},
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: '已取消删除'
})
}
})
}