目录
3.在课程发布CoursePublishService类中定义接口:
4.Service接口开发,在CoursePublishService类中添加课程发布接口
一、模块需求分析
课程信息编辑完毕即可发布课程,发布课程相当于一个确认操作,课程发布后学习者在网站可以搜索到课程,然后查看课程的详细信息,进一步选课、支付、在线学习。
教学机构确认课程内容无误,提交审核,平台运营人员对课程内容审核,审核通过后教学机构人员发布课程成功。
课程发布模块共包括三块功能:课程预览、课程审核、课程发布。
二、课程预览
1.课程预览需求分析
课程预览就是把课程的相关信息进行整合,在课程详情界面进行展示,通过课程预览页面查看信息是否存在问题。
说明如下:
1.1、点击课程预览,通过Nginx、后台服务网关请求内容管理服务进行课程预览。
1.2、内容管理服务查询课程相关信息进行整合,并通过模板引擎技术在服务端渲染生成页面,返回给浏览器。
1.3、通过课程预览页面点击”马上学习“打开视频播放页面。
1.4、视频播放页面通过Nginx请求后台服务网关,查询课程信息展示课程计划目录,请求媒资服务查询课程计划绑定的视频文件地址,在线浏览播放视频。
2.使用Freemarker模板引擎渲染前端页面
2.1、在nacos为内容管理接口层配置freemarker,公用配置组新加一个freemarker-config-dev.yaml
spring:
freemarker:
enabled: true
cache: false #关闭模板缓存,方便测试
settings:
template_update_delay: 0
suffix: .ftl #页面模板后缀名
charset: UTF-8
template-loader-path: classpath:/templates/ #页面模板位置(默认为 classpath:/templates/)
resources:
add-mappings: false #关闭项目中的静态资源映射(static、resources文件夹下的资源)
2.2、在内容管理接口工程添加freemarker-config-dev.yaml
- data-id: freemarker-config-dev.yaml
group: xuecheng-plus-common
refresh: true
2.3、添加模板 ,从课程资料目录下获取课程预览页面course_template.html,拷贝至内容管理的接口工程的resources/templates下,并将其在本目录复制一份命名为course_template.ftl
3.定义接口
3.1、接口层Controller调用Service方法获取模板引擎需要的模型数据
@GetMapping("/coursepreview/{courseId}")
public ModelAndView preview(@PathVariable("courseId") Long courseId) {
ModelAndView modelAndView = new ModelAndView();
//查询课程的信息作为模型数据
CoursePreviewDto coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
//指定模型
modelAndView.addObject("model", coursePreviewInfo);
//指定模板
modelAndView.setViewName("course_template");//根据视图名称加.ftl找到模板
return modelAndView;
}
3.2、定义数据模型
package com.xuecheng.content.model.dto;
import lombok.Data;
import java.util.List;
/**
* @description 课程预览模型类
*/
@Data
public class CoursePreviewDto {
//课程基本信息,营销信息
private CourseBaseInfoDto courseBase;
//课程计划信息
private List<TeachplanDto> teachplans;
//课程师资信息...
}
3.3、定义Service接口
在CoursePublishService接口中添加获取课程预览信息的方法
/**
* @description 获取课程预览信息
* @param courseId 课程id
* @return com.xuecheng.content.model.dto.CoursePreviewDto
*/
public CoursePreviewDto getCoursePreviewInfo(Long courseId);
3.4、实现Service方法
@Override
public CoursePreviewDto getCoursePreviewInfo(Long courseId) {
CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
//课程基本信息,营销信息
CourseBaseInfoDto courseBaseInfo = courseBaseInfoService.getCourseBaseInfo(courseId);
coursePreviewDto.setCourseBase(courseBaseInfo);
//课程计划信息
List<TeachplanDto> teachplanTree = teachplanService.findTeachplanTree(courseId);
coursePreviewDto.setTeachplans(teachplanTree);
return coursePreviewDto;
}
4.视频播放页面接口开发
在此页面需要从后台获取课程信息、根据课程计划获取对应的视频地址,这两个接口编写如下:
4.1、获取课程信息接口:/open/content/course/whole/{courseId}
package com.xuecheng.content.api;
import com.alibaba.fastjson.JSON;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
/**
* @description 课程发布相关接口
*/
@Controller
public class CoursePublishController {
@Autowired
CoursePublishService coursePublishService;
@ApiOperation("获取课程发布信息")
@ResponseBody
@GetMapping("/course/whole/{courseId}")
public CoursePreviewDto getCoursePublish(@PathVariable("courseId") Long courseId) {
//封装数据
CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
//查询课程发布表
// CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);
//先从缓存查询,缓存中有直接返回,没有再查询数据库
CoursePublish coursePublish = coursePublishService.getCoursePublishCache(courseId);
if(coursePublish == null){
return coursePreviewDto;
}
//开始向coursePreviewDto填充数据
CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
BeanUtils.copyProperties(coursePublish,courseBaseInfoDto);
//课程计划信息
String teachplanJson = coursePublish.getTeachplan();
//转成List<TeachplanDto>
List<TeachplanDto> teachplanDtos = JSON.parseArray(teachplanJson, TeachplanDto.class);
coursePreviewDto.setCourseBase(courseBaseInfoDto);
coursePreviewDto.setTeachplans(teachplanDtos);
return coursePreviewDto;
}
}
4.2、根据课程计划获取视频地址接口:/open/media/preview/{mediaId},在媒资管理服务media-api工程定义MediaOpenController类
package com.xuecheng.media.api;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
@RestController
@RequestMapping("/open")
public class MediaOpenController {
@Autowired
MediaFileService mediaFileService;
@ApiOperation("预览文件")
@GetMapping("/preview/{mediaId}")
public RestResponse<String> getPlayUrlByMediaId(@PathVariable String mediaId) {
//查询媒资文件信息
MediaFiles mediaFiles = mediaFileService.getFileById(mediaId);
if (mediaFiles == null) {
return RestResponse.validfail("找不到视频");
}
//取出视频播放地址
String url = mediaFiles.getUrl();
if (StringUtils.isEmpty(url)) {
return RestResponse.validfail("该视频正在处理中");
}
return RestResponse.success(mediaFiles.getUrl());
}
}
三、课程审核
1.业务流程
根据模块需求分析,课程发布前要先审核,审核通过方可发布。下图是课程审核及发布的流程图:
下边是课程状态的转化关系:
说明如下:
1.1、一门课程新增后它的审核状为”未提交“,发布状态为”未发布“。
1.2、课程信息编辑完成,教学机构人员执行”提交审核“操作。此时课程的审核状态为”已提交“。
1.3、当课程状态为已提交时运营平台人员对课程进行审核。
1.4、运营平台人员审核课程,结果有两个:审核通过、审核不通过。
1.5、课程审核过后不管状态是通过还是不通过,教学机构可以再次修改课程并提交审核,此时课程状态为”已提交“。此时运营平台人员再次审核课程。
1.6、课程审核通过,教学机构人员可以发布课程,发布成功后课程的发布状态为”已发布“。
1.7、课程发布后通过”下架“操作可以更改课程发布状态为”下架“
1.8、课程下架后通过”上架“操作可以再次发布课程,上架后课程发布状态为“发布”。
2.接口定义
定义提交课程审核的接口,在课程发布Controller中定义接口。
@ResponseBody
@PostMapping("/courseaudit/commit/{courseId}")
public void commitAudit(@PathVariable("courseId") Long courseId) {
Long companyId = 1232141425L;
coursePublishService.commitAudit(companyId, courseId);
}
3.在课程发布CoursePublishService类中定义接口:
/**
* @description 提交审核
* @param courseId 课程id
* @return void
*/
public void commitAudit(Long companyId,Long courseId);
4.实现Service接口
@Transactional
@Override
public void commitAudit(Long companyId, Long courseId) {
CourseBaseInfoDto courseBaseInfo = courseBaseInfoService.getCourseBaseInfo(courseId);
if (courseBaseInfo == null) {
XueChengPlusException.cast("课程找不到");
}
//审核状态