/**
* The MIT License (MIT) * Copyright (c) 2016 铭飞科技(mingsoft.net)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.mingsoft.cms.action.web;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
import net.bytebuddy.implementation.bytecode.Throw;
import net.mingsoft.base.constant.Const;
import net.mingsoft.basic.exception.BusinessException;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.cms.bean.CategoryBean;
import net.mingsoft.cms.bean.ContentBean;
import net.mingsoft.cms.biz.ICategoryBiz;
import net.mingsoft.cms.biz.IContentBiz;
import net.mingsoft.cms.entity.CategoryEntity;
import net.mingsoft.cms.entity.ContentEntity;
import net.mingsoft.cms.util.CmsParserUtil;
import net.mingsoft.mdiy.bean.PageBean;
import net.mingsoft.mdiy.biz.IModelBiz;
import net.mingsoft.mdiy.biz.IPageBiz;
import net.mingsoft.mdiy.entity.ModelEntity;
import net.mingsoft.mdiy.util.ParserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 动态生成页面,需要后台配置自定义页数据
*
* @author 铭飞开源团队
* @date 2018年12月17日
*/
@Controller("dynamicPageAction")
@RequestMapping("/mcms")
public class MCmsAction extends net.mingsoft.cms.action.BaseAction {
/**
* 自定义页面业务层
*/
@Autowired
private IPageBiz pageBiz;
/**
* 文章管理业务处理层
*/
@Autowired
private IContentBiz contentBiz;
/**
* 栏目业务层
*/
@Autowired
private ICategoryBiz categoryBiz;
/**
* 搜索标签;
*/
public static final String SEARCH = "search";
/**
* 自定义模型
*/
@Autowired
private IModelBiz modelBiz;
@Value("${ms.diy.html-dir:html}")
private String htmlDir;
/**
* 动态列表页
*/
@GetMapping("/index.do")
@ResponseBody
public String index(HttpServletRequest req, HttpServletResponse resp) {
Map map = BasicUtil.assemblyRequestMap();
map.forEach((k, v) -> {
map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
});
map.put(ParserUtil.URL, BasicUtil.getUrl());
//动态解析
map.put(ParserUtil.IS_DO, true);
//设置动态请求的模块路径
map.put(ParserUtil.MODEL_NAME, "mcms");
//解析后的内容
String content = "";
try {
//根据模板路径,参数生成
content = CmsParserUtil.generate(ParserUtil.INDEX + ParserUtil.HTM_SUFFIX, map,htmlDir);
} catch (TemplateNotFoundException e) {
e.printStackTrace();
} catch (MalformedTemplateNameException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
/**
* 动态列表页
*
* @param req
* @param resp
*/
@ResponseBody
@GetMapping("/list.do")
public String list(HttpServletRequest req, HttpServletResponse resp) {
Map map = BasicUtil.assemblyRequestMap();
map.forEach((k, v) -> {
map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
});
//获取栏目编号
int typeId = BasicUtil.getInt(ParserUtil.TYPE_ID, 0);
int size = BasicUtil.getInt("size", 10);
ContentBean contentBean = new ContentBean();
contentBean.setCategoryId(String.valueOf(typeId));
//获取文章总数
List columnArticles = contentBiz.queryIdsByCategoryIdForParser(contentBean);
//判断栏目下是否有文章
if (columnArticles.size() == 0) {
return "";
}
//设置分页类
PageBean page = new PageBean();
page.setPageNo(BasicUtil.getPageNo());
int total = PageUtil.totalPage(columnArticles.size(), size);
map.put(ParserUtil.COLUMN, columnArticles.get(0));
//获取总数
page.setTotal(total);
//设置栏目编号
map.put(ParserUtil.TYPE_ID, typeId);
map.put(ParserUtil.URL, BasicUtil.getUrl());
map.put(ParserUtil.PAGE, page);
//动态解析
map.put(ParserUtil.IS_DO, true);
//设置动态请求的模块路径
map.put(ParserUtil.MODEL_NAME, "mcms");
//解析后的内容
String content = "";
try {
//根据模板路径,参数生成
content = CmsParserUtil.generate(columnArticles.get(0).getCategoryListUrl(),map,htmlDir);
} catch (TemplateNotFoundException e) {
e.printStackTrace();
} catch (MalformedTemplateNameException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
/**
* 动态详情页
*/
@GetMapping("/view.do")
@ResponseBody
public String view(String orderby, String order, HttpServletRequest req, HttpServletResponse resp) {
//参数文章编号
ContentEntity article = (ContentEntity) contentBiz.getEntity(BasicUtil.getInt(ParserUtil.ID));
if (ObjectUtil.isNull(article)) {
throw new BusinessException(this.getResString("err.empty", this.getResString("id")));
}
if (StringUtils.isNotBlank(order)) {
//防注入
if (!order.toLowerCase().equals("asc") && !order.toLowerCase().equals("desc")) {
throw new BusinessException(this.getResString("err.error", this.getResString("order")));
}
}
orderby = orderby.replaceAll("('|\"|\\\\)", "\\\\$1");
PageBean page = new PageBean();
//用于详情上下页获取当前文章列表对应的分类,根据文章查询只能获取自身分类
String typeId = BasicUtil.getString(ParserUtil.TYPE_ID, article.getCategoryId());
//根据文章编号查询栏目详情模版
CategoryEntity column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(typeId));
//解析后的内容
String content = "";
Map map = BasicUtil.assemblyRequestMap();
map.forEach((k, v) -> {
//sql注入过滤
map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
});
//动态解析
map.put(ParserUtil.IS_DO, true);
//设置栏目编号
map.put(ParserUtil.TYPE_ID, typeId);
//设置动态请求的模块路径
map.put(ParserUtil.MODEL_NAME, "mcms");
map.put(ParserUtil.URL, BasicUtil.getUrl());
map.put(ParserUtil.PAGE, page);
map.put(ParserUtil.ID, article.getId());
ContentBean contentBean = new ContentBean();
contentBean.setCategoryId(String.valueOf(typeId));
contentBean.setOrderBy(orderby);
contentBean.setOrder(order);
List articleIdList = contentBiz.queryIdsByCategoryIdForParser(contentBean);
Map