图片.png图片.png图片.png

一、后端实现

1、定义vo

ChapterVo

  1. package com.guli.edu.vo;
  2. @ApiModel(value = "章节信息")
  3. @Data
  4. public class ChapterVo implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. private String id;
  7. private String title;
  8. private List<VideoVo> children = new ArrayList<>();
  9. }

VideoVo

  1. package com.guli.edu.vo;
  2. @ApiModel(value = "课时信息")
  3. @Data
  4. public class VideoVo implements Serializable {
  5. private static final long serialVersionUID = 1L;
  6. private String id;
  7. private String title;
  8. private Boolean free;
  9. }

2、服务层

接口

  1. package com.guli.edu.service;
  2. public interface ChapterService extends IService<Chapter> {
  3. List<ChapterVo> nestedList(String courseId);
  4. }

实现

  1. package com.guli.edu.service.impl;
  2. @Service
  3. public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {
  4. @Autowired
  5. private VideoService videoService;
  6. @Override
  7. public List<ChapterVo> nestedList(String courseId) {
  8. //最终要的到的数据列表
  9. ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>();
  10. //获取章节信息
  11. QueryWrapper<Chapter> queryWrapper1 = new QueryWrapper<>();
  12. queryWrapper1.eq("course_id", courseId);
  13. queryWrapper1.orderByAsc("sort", "id");
  14. List<Chapter> chapters = baseMapper.selectList(queryWrapper1);
  15. //获取课时信息
  16. QueryWrapper<Video> queryWrapper2 = new QueryWrapper<>();
  17. queryWrapper2.eq("course_id", courseId);
  18. queryWrapper2.orderByAsc("sort", "id");
  19. List<Video> videos = videoService.list(queryWrapper2);
  20. //填充章节vo数据
  21. int count1 = chapters.size();
  22. for (int i = 0; i < count1; i++) {
  23. Chapter chapter = chapters.get(i);
  24. //创建章节vo对象
  25. ChapterVo chapterVo = new ChapterVo();
  26. BeanUtils.copyProperties(chapter, chapterVo);
  27. chapterVoArrayList.add(chapterVo);
  28. //填充课时vo数据
  29. ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
  30. int count2 = videos.size();
  31. for (int j = 0; j < count2; j++) {
  32. Video video = videos.get(j);
  33. if(chapter.getId().equals(video.getChapterId())){
  34. //创建课时vo对象
  35. VideoVo videoVo = new VideoVo();
  36. BeanUtils.copyProperties(video, videoVo);
  37. videoVoArrayList.add(videoVo);
  38. }
  39. }
  40. chapterVo.setChildren(videoVoArrayList);
  41. }
  42. return chapterVoArrayList;
  43. }
  44. }

3、web层

  1. package com.guli.edu.controller.admin;
  2. @Api(description="课程章节管理")
  3. @CrossOrigin //跨域
  4. @RestController
  5. @RequestMapping("/admin/edu/chapter")
  6. public class ChapterAdminController {
  7. @Autowired
  8. private ChapterService chapterService;
  9. @ApiOperation(value = "嵌套章节数据列表")
  10. @GetMapping("nested-list/{courseId}")
  11. public R nestedListByCourseId(
  12. @ApiParam(name = "courseId", value = "课程ID", required = true)
  13. @PathVariable String courseId){
  14. List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
  15. return R.ok().data("items", chapterVoList);
  16. }
  17. }

4、Swagger测试

二、前端实现

1、定义api

chapter.js

  1. import request from '@/utils/request'
  2. const api_name = '/admin/edu/chapter'
  3. export default {
  4. getNestedTreeList(courseId) {
  5. return request({
  6. url: `${api_name}/nested-list/${courseId}`,
  7. method: 'get'
  8. })
  9. }
  10. }