[AppleScript] 纯文本查看 复制代码
public String getPageHtml(String pageId){
//获取页面模型数据
Map model =
this.
getModelByPageId(pageId);
if(model == null){
//获取页面模型数据为空
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_DATAISNULL);
}
//获取页面模板
String templateContent
=
getTemplateByPageId(pageId);
if(StringUtils.isEmpty(templateContent)){
//页面模板为空
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_TEMPLATEISNULL);
}
//执行静态化
String html =
generateHtml(templateContent, model);
if(StringUtils.isEmpty(html)){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_HTMLISNULL);
}
return html;
}
//页面静态化
public String generateHtml(String template,Map model){
try {
//生成配置类
Configuration configuration = new Configuration(Configuration.
getVersion());
//模板加载器
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.
putTemplate(
"
template
"
,template);
//配置模板加载器
configuration.setTemplateLoader(stringTemplateLoader);
//获取模板
Template template1 =
configuration.
getTemplate(
"
template
"
);
String html = FreeMarkerTemplateUtils.
processTemplateIntoString(template1, model);
return html;
} catch (Exception e) {
e.
printStackTrace();
}
return null;
}
//获取页面模板
public String getTemplateByPageId(String pageId){
//查询页面信息
CmsPage cmsPage
=
this.
getById(pageId);
if(cmsPage
== null){
//页面不存在
ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
}
//页面模板
String templateId
=
cmsPage.
getTemplateId();
if(StringUtils.isEmpty(templateId)){
//页面模板为空
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_TEMPLATEISNULL);
}
Optional<CmsTemplate> optional =
cmsTemplateRepository
.findById(templateId);
if(optional.isPresent()){
CmsTemplate cmsTemplate
=
optional.
get();
//模板文件id
String templateFileId
=
cmsTemplate.
getTemplateFileId();
//取出模板文件内容
GridFSFile gridFSFile
=
gridFsTemplate.findOne(Query
.
query(Criteria.where(
"
_id
"
)
.is(templateFileId)));
//打开下载流对象
GridFSDownloadStream gridFSDownloadStream =
gridFSBucket.openDownloadStream(gridFSFile.
getObjectId());
//创建GridFsResource
GridFsResource gridFsResource
= new GridFsResource(gridFSFile,gridFSDownloadStream);
try {
String content
= IOUtils.toString(gridFsResource.
getInputStream(),
"
utf
‐
8
"
);
return content;
} catch (IOException e) {
e.
printStackTrace();
}
}
return null;
}
//获取页面模型数据
public Map getModelByPageId(String pageId){
//查询页面信息
CmsPage cmsPage
=
this.
getById(pageId);
if(cmsPage
== null){
//页面不存在
ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
}
//取出dataUrl
String dataUrl =
cmsPage.
getDataUrl();
if(StringUtils.isEmpty(dataUrl)){
ExceptionCast.cast(CmsCode.CMS_GENERATEHTML_DATAURLISNULL);
}
ResponseEntity<Map> forEntity
= restTemplate.
getForEntity(dataUrl, Map
.class);
Map body
= forEntity
.
getBody();
return body;
}