A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

5.4 课程预览服务端
5.4.1 Api定义
此Api是课程管理前端请求服务端进行课程预览的Api 请求:课程Id
响应:课程预览Url 1、定义响应类型
[AppleScript] 纯文本查看 复制代码
@Data
 @ToString 
@NoArgsConstructor public class CoursePublishResult extends ResponseResult {   
  String previewUrl;   
  public CoursePublishResult(ResultCode resultCode,String previewUrl) {  
       super(resultCode);    
     this.previewUrl = previewUrl; 
    } }

2、接口定义如下

[AppleScript] 纯文本查看 复制代码
@ApiOperation("预览课程") 
public CoursePublishResult preview(String id);

5.4.2 创建 Feign Client
在课程管理工程创建CMS服务的Feign Client,通过此Client远程请求cms添加页面。

[AppleScript] 纯文本查看 复制代码
@FeignClient(value = XcServiceList.XC_SERVICE_MANAGE_CMS)
  public interface CmsPageClient{
     //保存页面  
    @PostMapping("/cms/page/save")  
    public CmsPageResult save(@RequestBody CmsPage cmsPage);  } 

5.4.3  Service
1、配置添加页面参数信息
在application.yml中配置:

[AppleScript] 纯文本查看 复制代码
course‐publish: 
 siteId: 5b30cba5f58b4411fc6cb1e5 
  templateId: 5b345a6b94db44269cb2bfec   
previewUrl: [url]http://www.xuecheng.com/cms/preview/[/url]  
 pageWebPath: /course/detail/  
 pagePhysicalPath: /course/detail/  
 dataUrlPre: http://localhost:31200/course/courseview/

2、代码如下:

[AppleScript] 纯文本查看 复制代码
@Value("${course‐publish.dataUrlPre}")  
   private String publish_dataUrlPre;  
   @Value("${course‐publish.pagePhysicalPath}")   
  private String publish_page_physicalpath;  
   @Value("${course‐publish.pageWebPath}")  
   private String publish_page_webpath; 
    @Value("${course‐publish.siteId}") 
   private String publish_siteId; 
    @Value("${course‐publish.templateId}")   
  private String publish_templateId;   
  @Value("${course‐publish.previewUrl}")  
   private String previewUrl;   
       //根据id查询课程基本信息   
  public CourseBase findCourseBaseById(String courseId){    
     Optional<CourseBase>
 baseOptional = courseBaseRepository.findById(courseId);
        if(baseOptional.isPresent()){

[AppleScript] 纯文本查看 复制代码
 CourseBase courseBase = baseOptional.get();   
          return courseBase;    
     }     
    ExceptionCast.cast(CourseCode.COURSE_GET_NOTEXISTS);    
     return null; 
    }    
 //课程预览  
   public CoursePublishResult preview(String courseId){  
       CourseBase one = this.findCourseBaseById(courseId);   
        //发布课程预览页面   
      CmsPage cmsPage = new CmsPage();  
       //站点      
   cmsPage.setSiteId(publish_siteId);//课程预览站点   
      //模板      
   cmsPage.setTemplateId(publish_templateId);    
     //页面名称   
      cmsPage.setPageName(courseId+".html");  
       //页面别名    
     cmsPage.setPageAliase(one.getName());       
  //页面访问路径   
      cmsPage.setPageWebPath(publish_page_webpath);  
       //页面存储路径    
     cmsPage.setPagePhysicalPath(publish_page_physicalpath);  
       //数据url        
 cmsPage.setDataUrl(publish_dataUrlPre+courseId); 
        //远程请求cms保存页面信息     
    CmsPageResult cmsPageResult = cmsPageClient.save(cmsPage);    
     if(!cmsPageResult.isSuccess()){    
         return new CoursePublishResult(CommonCode.FAIL,null);    
     }     
    //页面id     
    String pageId = cmsPageResult.getCmsPage().getPageId();     
    //页面url    
     String pageUrl = previewUrl+pageId;   
      return new CoursePublishResult(CommonCode.SUCCESS,pageUrl);  
     }

5.4.4 Controller
[AppleScript] 纯文本查看 复制代码
@Override 
@PostMapping("/preview/{id}") public CoursePublishResult 
preview(@PathVariable("id") String id) { 
    return courseService.preview(id); }


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马