- 更好的阅读体验
- 资料地址
- 1 - 构建工程篇
- 2 - 前后端交互篇
- 3 - 文件上传
- 4 - 课程管理
目录
- 一、课程章节增删改查
- 1.1、后端接口
- 1.2、前端实现
- 1.3、添加章节
- 1.4、修改章节
- 1.5、删除章节
- 二、课程章节小节功能
- 2.1、后端接口
- 2.2、前端实现
- 2.3、添加小节
- 2.4、删除小节
- 2.5、修改小节
- 三、课程发布信息预览
- 3.1、后端接口
- 3.3、前端实现
- 四、课程发布
- 4.1、后端接口
- 4.2、前端页面
- 五、课程列表
- 5.1、后端接口
- 5.2、前端实现
- 六、课程删除
- 6.1、后端接口
- 6.2、前端实现
- 七、编辑基本信息及大纲
一、课程章节增删改查 1.1、后端接口
@Api(tags = "章节模块")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
@Autowired
private EduChapterService eduChapterService;
@Autowired
EduCourseService eduCourseService;
// 添加章节
@PostMapping("addChapter")
public R addChapter(@RequestBody EduChapter eduChapter) {
eduChapterService.save(eduChapter);
return R.ok();
}
// 根据章节id查询
@GetMapping("getChapter/{chapterId}")
public R getChapter(@PathVariable String chapterId) {
EduChapter eduChapter = eduChapterService.getById(chapterId);
return R.ok().data("chapter", eduChapter);
}
// 修改章节
@PostMapping("updateChapter")
public R updateChapter(@RequestBody EduChapter eduChapter) {
eduChapterService.updateById(eduChapter);
return R.ok();
}
// 删除章节 若存在小节则不可删除
@DeleteMapping("deleteById/{chapterId}")
public R deleteById(@PathVariable String chapterId) {
boolean flag = eduChapterService.deleteChapter(chapterId);
if (flag) {
return R.ok();
} else {
return R.error();
}
}
}
@Override
public boolean deleteChapter(String chapterId) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("chapter_id", chapterId);
int count = eduVideoService.count(wrapper);
if (count > 0) {
throw new LaptoyException(20001, "还有小节数据,不能删除");
} else {
return this.removeById(chapterId);
}
}
1.2、前端实现
1、页面代码
发布新课程
添加小节 {{ video.title }}编辑 删除 添加章节 上一步 下 一步
data() {
return {
dialogChapterFormVisible: false,
chapter: {
title: "",
sort: 0
},
};
},
稍微优化了页面
2、创建api - chapter.js
//添加章节
addChapter(chapter) {
return request({
url: `/eduservice/chapter/addChapter`,
method: `post`,
data: chapter
})
},
//根据id查询章节
updateChapterById(chapterId) {
return request({
url: `/eduservice/chapter/getChapter/${chapterId}`,
method: `get`,
})
},
//修改章节
updateChapter(chapter) {
return request({
url: `/eduservice/chapter/updateChapter`,
method: `post`,
data: chapter
})
},
//删除章节
deleteById(chapterId) {
return request({
url: `/eduservice/chapter/deleteById/${chapterId}`,
method: `delete`,
})
}
1.3、添加章节
saveChapter() {
//设置课程id到chapter对象中,不指定课程id无法正常保存,因为数据库字段为非空
this.chapter.courseId = this.courseId
chapter.addChapter(this.chapter).then((resp) => {
this.dialogChapterFormVisible = false;
this.$message({
message: "添加章节成功",
type: "success",
});
this.chapter = {}
this.getChapterVideoByCourseId()
});
},
saveOrUpdate() {
this.saveChapter()
},
1.4、修改章节
1、点击修改章节按钮根据章节id回显数据
2、修改方法
// 修改章节弹窗回显数据
openEditChapter(id) {
this.dialogChapterFormVisible = true;
chapter.updateChapterById(id).then((resp) => {
this.chapter = resp.data.data
})
},
// 修改章节
updateChapter(id) {
//设置课程id到chapter对象中,不指定课程id无法正常保存,因为数据库字段为非空
this.chapter.courseId = this.courseId
chapter.updateChapter(this.chapter).then((resp) => {
this.dialogChapterFormVisible = false;
this.$message({
message: "修改章节成功",
type: "success",
});
this.getChapterVideoByCourseId();
});
},
1.5、删除章节
// 删除章节
removeById(chapterId) {
this.$confirm("此操作将永久删除章节信息, 是否继续?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}).then(() => {
chapter.deleteById(chapterId).then((resp) => {
this.$message({
type: "success",
message: "删除成功!",
});
this.getChapterVideoByCourseId();
});
});
},
二、课程章节小节功能 2.1、后端接口
@Api(tags = "小节模块")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin //解决跨域问题
public class EduVideoController {
@Autowired
private EduVideoService eduVideoService;
//添加小节
@PostMapping("/addVideo")
public R addVideo(@RequestBody EduVideo eduVideo) {
eduVideoService.save(eduVideo);
return R.ok();
}
//删除小节
// TODO 后面这个方法需要完善,删除小节的时候,同时也要把视频删除
@DeleteMapping("/deleteVideo/{id}")
public R deleteVideo(@PathVariable String id) {
eduVideoService.removeById(id);
return R.ok();
}
//修改小节
@PostMapping("/updateVideo")
public R updateVideo(@RequestBody EduVideo eduVideo) {
eduVideoService.updateById(eduVideo);
return R.ok();
}
//根据小节id查询
@GetMapping("/getVideoById/{videoId}")
public R getVideoById(@PathVariable String videoId) {
EduVideo eduVideo = eduVideoService.getById(videoId);
return R.ok().data("data", eduVideo);
}
}
2.2、前端实现
1、页面
添加小节 免费 默认
data() {
return {
saveVideoBtnDisabled: false,
dialogVideoFormVisible: false,
video: {
sort: 0,
title: "",
free: ""
}
};
},
2、API - srcapiteachervideo.js
import request from '@/utils/request' //引入已经封装好的axios 和 拦截器
export default {
//添加小节
addVideo(video) {
return request({
url: `/eduservice/video/addVideo`,
method: `post`,
data: video
})
},
//根据id查询小节
getVideoById(videoId) {
return request({
url: `/eduservice/video/getVideoById/${videoId}`,
method: `get`,
})
},
//修改小节
updateVideo(video) {
return request({
url: `/eduservice/video/updateVideo`,
method: `post`,
data: video
})
},
//删除小节
deleteById(videoId) {
return request({
url: `/eduservice/video/deleteVideo/${videoId}`,
method: `delete`,
})
},
}
2.3、添加小节
openSaveVideoForm(id) {
this.dialogVideoFormVisible = true
this.video.courseId = this.courseId
this.video.chapterId = id
},
saveOrUpdateVideo(){
if (this.video.id) {
//修改小节
} else {
//新增小节
this.saveVideo();
}
},
// 添加小节
saveVideo() {
video.addVideo(this.video).then((resp) => {
this.dialogVideoFormVisible = false;
this.$message({
message: "添加小节成功",
type: "success",
});
this.video = {}
this.getChapterVideoByCourseId();
});
},
2.4、删除小节
{{ video.title }}编辑 删除
// 删除小节
removeVideo(id) {
this.$confirm("此操作将永久删除小节信息, 是否继续?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}).then(() => {
video.deleteById(id).then((resp) => {
this.$message({
type: "success",
message: "删除成功!",
});
this.getChapterVideoByCourseId();
});
});
},
2.5、修改小节
{{ video.title }}编辑 删除
// 展开编辑表单
openEditVideoForm(id) {
this.dialogVideoFormVisible = true;
video.getVideoById(id).then((resp) => {
this.video = resp.data.data;
});
},
// 修改小节
updateVideo() {
video.updateVideo(this.video).then((resp) => {
this.dialogVideoFormVisible = false;
this.$message({
message: "修改小节成功",
type: "success",
});
this.getChapterVideoByCourseId();
});
},
// 添加或修改小节
saveOrUpdateVideo() {
if (this.video.id) {
//修改小节
this.updateVideo();
} else {
//新增小节
this.saveVideo();
}
},
三、课程发布信息预览 3.1、后端接口
1、创建 VO
@Data
public class CoursePublishVo implements Serializable {
private static final long serialVersionUID = 1L;
private String id;//课程id
private String title; //课程名称
private String cover; //封面
private Integer lessonNum;//课时数
private String subjectLevelOne;//一级分类
private String subjectLevelTwo;//二级分类
private String teacherName;//讲师名称
private String price;//价格 ,只用于显示
}
2、控制层
// 根据课程id查询课程确认信息
@GetMapping("/getPublishCourseInfo/{id}")
public R getPublishCourseInfo(@PathVariable String id) {
CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
return R.ok().data("data", publishCourseInfo);
}
3、业务层
@Override
public CoursePublishVo getPublishCourseInfo(String id) {
return eduCourseMapper.getPublishCourseInfo(id);
}
4、数据层
CoursePublishVo getPublishCourseInfo(String id);
5、将xml文件放到 resources/xml 目录下,并再YML文件指定
mybatis-plus: mapper-locations: /mapper/*.xml
3.3、前端实现
1、定义API - course.js
//课程确认信息显示
getPublishCourseInfo(courseId){
return request({
url:"/eduservice/course/getpublishCourseInfo/"+courseId,
method: 'get',
})
}
2、页面
![]()
{{ publishCourseInfo.title }}
共{{ publishCourseInfo.lessonNum }}课时
所属分类:{{ publishCourseInfo.subjectLevelOne }} : {{ publishCourseInfo.subjectLevelTwo }}
课程讲师:{{ publishCourseInfo.teacherName }}
¥{{ publishCourseInfo.price }}
3、方法
export default {
data() {
return {
saveBtnDisabled: false,
courseId: '',
publishCourseInfo: {},
};
},
methods: {
//根据课程id查询
getPublishCourseInfo() {
course.getPublishCourseInfo(this.courseId)
.then(resp => {
this.publishCourseInfo = resp.data.data
console.log(this.publishCourseInfo)
})
},
// 跳转到上一步
previous() {
this.$router.push("/course/chapter/" + this.courseId);
},
publish() {
this.$router.push("/course/list");
}
},
created() {
//获取路由中的id值
if (this.$route.params && this.$route.params.id) {
this.courseId = this.$route.params.id
//调用接口方法根据课程id查询课程信息
this.getPublishCourseInfo()
}
},
};
4、css
5、测试
四、课程发布 4.1、后端接口
1、控制层
//课程最终发布
//修改课程状态
@PostMapping("publishCourse/{id}")
public R publishCourse(@PathVariable String id){
EduCourse eduCourse = new EduCourse();
eduCourse.setStatus("Normal"); //设置课程发布状态
eduCourse.setId(id);
boolean flag = eduCourseService.updateById(eduCourse);
if (flag){
return R.ok();
}else {
return R.error();
}
}
4.2、前端页面
1、API
//课程最终发布
publishCourse(courseId) {
return request({
url: "/eduservice/course/publishCourse/" + courseId,
method: 'post',
})
}
2、方法
//发布课程
publish() {
this.$confirm("你确定要发布此课程, 是否继续?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}).then(() => {
course.publishCourse(this.courseId).then((resp) => {
this.$message({
message: "课程发布成功",
type: "success",
});
//跳转课程列表页面
this.$router.push({ path: "/course/list" });
});
});
}
五、课程列表 5.1、后端接口
1、实体类作为模糊查询条件
@ApiModel(value = "Course查询对象", description = "课程查询对象封装")
@Data
public class CourseQuery implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "课程名称,模糊查询")
private String title;
@ApiModelProperty(value = "发布状态 Normal已发布 Draft未发布")
private String status;
}
2、控制层
//多条件查询课程带分页
@ApiOperation(value = "多条件查询课程带分页")
@PostMapping("/pageCourseCondition/{page}/{limit}")
public R pageCourseCondition(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
@RequestBody(required = false) CourseQuery courseQuery) {//通过封装courseQuery对象来直接传递查询条件
//创建分页page对象
Page pageParam = new Page<>(page, limit);
//调用方法实现多条件分页查询
eduCourseService.pageQuery(pageParam, courseQuery);
//获取查询到的数据
List records = pageParam.getRecords();
//获取总记录数
long total = pageParam.getTotal();
return R.ok().data("total", total).data("data", records);
}
3、业务层
@Override public void pageQuery(PagepageParam, CourseQuery courseQuery) { QueryWrapper wrapper = new QueryWrapper<>(); String title = courseQuery.getTitle(); String status = courseQuery.getStatus(); if (!StringUtils.isEmpty(title)) { wrapper.like("title", title);//参数1:数据库字段名; 参数2:模糊查询的值 } if (!StringUtils.isEmpty(status)) { wrapper.eq("status", status); } wrapper.orderByDesc("gmt_create"); baseMapper.selectPage(pageParam, wrapper); }
5.2、前端实现
1、页面
查询 清空 {{ (page - 1) * limit + scope.$index + 1 }} {{ scope.row.status === "Normal" ? "已发布" : "未发布" }} 编辑基本信息 编辑课程大纲 点击删除课程
2、API - course.js
//课程列表多条件分页查询
//page:当前页,limit:每页记录数,teacherQuery:条件对象
getCourseListPage(page, limit, courseQuery) {
return request({
url: `/eduservice/course/pageCourseCondition/${page}/${limit}`,
method: 'post',
data: courseQuery
})
},
3、js
import course from "@/api/teacher/course.js";
export default {
data() {
return {
list: null, //查询之后给接口返回的数据装的集合
page: 1, //当前页
limit: 10, //每页显示记录数
courseQuery: {}, //条件封装对象
total: 0, //总记录数
};
},
created() {
this.getList();
},
methods: {
getList(page = 1) {
this.page = page;
course
.getCourseListPage(this.page, this.limit, this.courseQuery)
.then((resp) => {
this.list = resp.data.data;
this.total = resp.data.total;
}) //请求成功
.catch((err) => {
console.log(err);
}); //请求失败
},
//清空方法
resetData() {
//表单输入项数据清空
this.courseQuery = {};
//查询所有课程数据
this.getList();
},
},
};
六、课程删除 6.1、后端接口
1、控制层
//课程列表中删除课程方法
@DeleteMapping("/removeCourseById/{id}")
public R removeCourseById(@PathVariable String id) {
boolean flag = eduCourseService.removeCourse(id);
if (flag) {
return R.ok();
} else {
return R.error();
}
}
2、业务层
//删除课程
@Transactional
@Override
public boolean removeCourse(String id) {
//1、根据课程id删除小节
eduVideoService.removeVideoByCourseId(id);
//2、根据课程id删除章节部分
eduChapterService.removeChapterByCourseId(id);
//3、根据课程id删除课程描述
descriptionService.removeById(id);
//4、根据课程id删除课程本身
boolean flag = this.removeById(id);
if (flag) {
return true;
} else {
throw new LaptoyException(20001, "删除失败");
}
}
3、删除小节
//根据课程id删除小节
// TODO 删除小节,要删除对应的视频文件
@Override
public void removeVideoByCourseId(String id) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("course_id", id);
baseMapper.delete(wrapper);
}
4、删除章节
@Override
public void removeChapterByCourseId(String id) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("course_id",id);
baseMapper.delete(wrapper);
}
6.2、前端实现
1、API - course.js
//删除课程
removeCourseById(courseId) {
return request({
url: "/eduservice/course/removeCourseById/" + courseId,
method: 'delete',
})
}
2、方法
点击删除课程
七、编辑基本信息及大纲
1、页面
编辑基本信息 编辑课程大纲 点击删除课程
2、添加路由
{
path: 'info/:id',
name: 'EduCourseInfoEdit',
component: () => import('@/views/edu/course/info.vue'),
meta: { title: '编辑课程基本信息', noCache: true },
hidden: true
},
{
path: 'chapter/:id',
name: 'EduCourseChapterEdit',
component: () => import('@/views/edu/course/chapter.vue'),
meta: { title: '编辑课程大纲', noCache: true },
hidden: true
},



