一、后端实现

1、业务层

接口:CourseService.java

  1. CourseInfoForm getCourseInfoFormById(String id);

实现:CourseServiceImpl.java

  1. @Override
  2. public CourseInfoForm getCourseInfoFormById(String id) {
  3. Course course = this.getById(id);
  4. if(course == null){
  5. throw new GuliException(20001, "数据不存在");
  6. }
  7. CourseInfoForm courseInfoForm = new CourseInfoForm();
  8. BeanUtils.copyProperties(course, courseInfoForm);
  9. CourseDescription courseDescription = courseDescriptionService.getById(id);
  10. if(course != null){
  11. courseInfoForm.setDescription(courseDescription.getDescription());
  12. }
  13. return courseInfoForm;
  14. }

2、web层

  1. @ApiOperation(value = "根据ID查询课程")
  2. @GetMapping("course-info/{id}")
  3. public R getById(
  4. @ApiParam(name = "id", value = "课程ID", required = true)
  5. @PathVariable String id){
  6. CourseInfoForm courseInfoForm = courseService.getCourseInfoFormById(id);
  7. return R.ok().data("item", courseInfoForm);
  8. }

3、Swagger中测试

二、前端实现

1、定义api

api/edu/course.js

  1. getCourseInfoById(id) {
  2. return request({
  3. url: `${api_name}/course-info/${id}`,
  4. method: 'get'
  5. })
  6. }

2、组件js

  1. init() {
  2. if (this.$route.params && this.$route.params.id) {
  3. const id = this.$route.params.id
  4. //根据id获取课程基本信息
  5. this.fetchCourseInfoById(id)
  6. }
  7. ......
  8. },
  9. fetchCourseInfoById(id) {
  10. course.getCourseInfoById(id).then(response => {
  11. this.courseInfo = response.data.item
  12. }).catch((response) => {
  13. this.$message({
  14. type: 'error',
  15. message: response.message
  16. })
  17. })
  18. },

三、解决级联下拉菜单回显问题

1、数据库中增加冗余列

  1. subject_parent_id 课程专业父级ID

2、pojo中增加属性

entity.Course.java
form.CourseInfo.java

  1. @ApiModelProperty(value = "课程专业父级ID")
  2. private String subjectParentId;

3、vue组件中绑定数据

edu/course/infoinfo.vue

  1. <el-select v-model="courseInfo.subjectParentId" ......

4、修改init方法

将 this.initSubjectList() 和 this.initTeacherList()移至else

  1. init() {
  2. if (this.$route.params && this.$route.params.id) {
  3. const id = this.$route.params.id
  4. // 根据id获取课程基本信息
  5. this.fetchCourseInfoById(id)
  6. } else {
  7. this.courseInfo = { ...defaultForm }
  8. // 初始化分类列表
  9. this.initSubjectList()
  10. // 获取讲师列表
  11. this.initTeacherList()
  12. }
  13. },

5、修改fetchCourseInfoById方法

  1. fetchCourseInfoById(id) {
  2. course.getCourseInfoById(id).then(responseCourse => {
  3. this.courseInfo = responseCourse.data.item
  4. // 初始化分类列表
  5. subject.getNestedTreeList().then(responseSubject => {
  6. this.subjectNestedList = responseSubject.data.items
  7. for (let i = 0; i < this.subjectNestedList.length; i++) {
  8. if (this.subjectNestedList[i].id === this.courseInfo.subjectParentId) {
  9. this.subSubjectList = this.subjectNestedList[i].children
  10. }
  11. }
  12. })
  13. // 获取讲师列表
  14. this.initTeacherList()
  15. }).catch((response) => {
  16. this.$message({
  17. type: 'error',
  18. message: response.message
  19. })
  20. })
  21. },