MCmsAction.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2012-2022 铭软科技(mingsoft.net)
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. * The above copyright notice and this permission notice shall be included in all
  11. * copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. package net.mingsoft.cms.action.web;
  20. import cn.hutool.core.util.ObjectUtil;
  21. import cn.hutool.core.util.PageUtil;
  22. import com.alibaba.fastjson.JSON;
  23. import freemarker.core.ParseException;
  24. import freemarker.template.MalformedTemplateNameException;
  25. import freemarker.template.TemplateNotFoundException;
  26. import net.mingsoft.base.constant.Const;
  27. import net.mingsoft.basic.util.BasicUtil;
  28. import net.mingsoft.cms.biz.ICategoryBiz;
  29. import net.mingsoft.cms.biz.IContentBiz;
  30. import net.mingsoft.cms.entity.CategoryEntity;
  31. import net.mingsoft.mdiy.bean.PageBean;
  32. import net.mingsoft.mdiy.biz.IModelBiz;
  33. import net.mingsoft.mdiy.entity.ModelEntity;
  34. import net.mingsoft.mdiy.util.ParserUtil;
  35. import org.apache.commons.lang3.StringUtils;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.beans.factory.annotation.Value;
  38. import org.springframework.http.MediaType;
  39. import org.springframework.stereotype.Controller;
  40. import org.springframework.web.bind.annotation.RequestMapping;
  41. import org.springframework.web.bind.annotation.RequestMethod;
  42. import org.springframework.web.bind.annotation.ResponseBody;
  43. import springfox.documentation.annotations.ApiIgnore;
  44. import javax.servlet.http.HttpServletRequest;
  45. import javax.servlet.http.HttpServletResponse;
  46. import java.io.IOException;
  47. import java.io.UnsupportedEncodingException;
  48. import java.util.ArrayList;
  49. import java.util.HashMap;
  50. import java.util.List;
  51. import java.util.Map;
  52. /**
  53. * 动态生成页面,需要后台配置自定义页数据
  54. *
  55. * @author 铭飞开源团队
  56. * @date 2018年12月17日
  57. * @date 2021年8月26日取消默认search.htm
  58. */
  59. @ApiIgnore
  60. @Controller("dynamicPageAction")
  61. @RequestMapping("/mcms")
  62. public class MCmsAction extends net.mingsoft.cms.action.BaseAction {
  63. /**
  64. * 文章管理业务处理层
  65. */
  66. @Autowired
  67. private IContentBiz contentBiz;
  68. /**
  69. * 栏目业务层
  70. */
  71. @Autowired
  72. private ICategoryBiz categoryBiz;
  73. /**
  74. * 自定义模型
  75. */
  76. @Autowired
  77. private IModelBiz modelBiz;
  78. @Value("${ms.diy.html-dir:html}")
  79. private String htmlDir;
  80. /**
  81. * 实现前端页面的文章搜索
  82. *
  83. * @param request 搜索id
  84. * @param response
  85. */
  86. @RequestMapping(value = "search",method = {RequestMethod.GET, RequestMethod.POST},produces= MediaType.TEXT_HTML_VALUE+";charset=utf-8")
  87. @ResponseBody
  88. public String search(HttpServletRequest request, HttpServletResponse response) {
  89. String search = BasicUtil.getString("tmpl", "search.htm");
  90. //设置分页类
  91. PageBean page = new PageBean();
  92. page.setSize(ParserUtil.getPageSize(search, 20));
  93. //参数集合,提供给解析使用
  94. Map<String, Object> params = new HashMap<>();
  95. // 读取请求字段
  96. Map<String, Object> field = BasicUtil.assemblyRequestMap();
  97. // 自定义字段集合
  98. Map<String, String> diyFieldName = new HashMap<String, String>();
  99. //记录自定义模型字段名
  100. List filedStr = new ArrayList<>();
  101. // 栏目对应字段的值
  102. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>();
  103. // 当前栏目
  104. CategoryEntity column = null;
  105. // 栏目对应模型
  106. ModelEntity contentModel = null;
  107. //获取栏目信息
  108. String typeId = null;
  109. String categoryIds = BasicUtil.getString("categoryIds");
  110. //List categoryIdList = CollectionUtil.newArrayList();
  111. //当传递了栏目编号,但不是栏目集合
  112. if (StringUtils.isNotBlank(categoryIds) && !categoryIds.contains(",")) {
  113. typeId = categoryIds;
  114. }
  115. // else {
  116. // //取出所有的子栏目
  117. // String[] ids = categoryIds.split(",");
  118. // List<CategoryEntity> categoryList = categoryBiz.list(Wrappers.<CategoryEntity>lambdaQuery().ne(CategoryEntity::getCategoryType, CategoryTypeEnum.LINK.toString()));
  119. //
  120. // categoryIdList = CollectionUtil.newArrayList(ids);
  121. // for(CategoryEntity c:categoryList) {
  122. // if(StringUtils.isNotEmpty(c.getParentids())) {
  123. // for(String id:ids) {
  124. // if(c.getParentids().indexOf(id)>-1) {
  125. // categoryIdList.add(c.getId());
  126. // break;
  127. // }
  128. // }
  129. // }
  130. // }
  131. // }
  132. //重新组织 ID
  133. //categoryIds = StringUtils.join(categoryIdList, ",");
  134. //根据栏目确定自定义模型
  135. if (typeId != null) {
  136. column = (CategoryEntity) categoryBiz.getById(typeId);
  137. // 获取表单类型的id
  138. if (column != null && ObjectUtil.isNotNull(column.getMdiyModelId())) {
  139. contentModel = (ModelEntity) modelBiz.getById(column.getMdiyModelId());
  140. if (contentModel != null) {
  141. // 保存自定义模型的数据
  142. Map<String, String> fieldMap = contentModel.getFieldMap();
  143. for (String s : fieldMap.keySet()) {
  144. filedStr.add(fieldMap.get(s));
  145. }
  146. // 设置自定义模型表名,方便解析的时候关联表查询
  147. params.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  148. }
  149. }
  150. // 设置栏目,方便解析的时候关联表查询
  151. params.put(ParserUtil.COLUMN, column);
  152. }
  153. // 处理读取请求字段
  154. if (field != null) {
  155. for (Map.Entry<String, Object> entry : field.entrySet()) {
  156. if (entry != null) {
  157. //空值不处理
  158. if (ObjectUtil.isNull(entry.getValue())) {
  159. continue;
  160. }
  161. // 对值进行安全处理
  162. // 处理由get方法请求中文乱码问题
  163. String value = entry.getValue().toString().replaceAll("('|\"|\\\\)", "\\\\$1");
  164. //Xss过滤
  165. value = clearXss(value);
  166. // 如果是get方法需要将请求地址参数转码
  167. if (request.getMethod().equals(RequestMethod.GET)) {
  168. try {
  169. value = new String(value.getBytes("ISO-8859-1"), Const.UTF8);
  170. } catch (UnsupportedEncodingException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. // 保存至自定义字段集合
  175. if (StringUtils.isNotBlank(value)) {
  176. diyFieldName.put(entry.getKey(), value);
  177. //判断请求中的是否是自定义模型中的字段
  178. if (filedStr.contains(entry.getKey())) {
  179. //设置自定义模型字段和值
  180. DiyModelMap diyMap = new DiyModelMap();
  181. diyMap.setKey(entry.getKey());
  182. diyMap.setValue(value);
  183. fieldValueList.add(diyMap);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. //添加自定义模型的字段和值
  190. if (fieldValueList.size() > 0) {
  191. params.put("diyModel", fieldValueList);
  192. }
  193. Map<String, Object> searchMap = field;
  194. searchMap.put("categoryIds",categoryIds);
  195. StringBuilder urlParams = new StringBuilder();
  196. searchMap.forEach((k, v) -> {
  197. //sql注入过滤
  198. if (v != null) {
  199. searchMap.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
  200. searchMap.put(k, clearXss(searchMap.get(k).toString()));
  201. if (!"size".equals(k) && !"pageNo".equals(k)) {
  202. urlParams.append(k).append("=").append(searchMap.get(k)).append("&");
  203. }
  204. }
  205. });
  206. //查询数量
  207. int count = contentBiz.getSearchCount(contentModel, fieldValueList, searchMap, BasicUtil.getApp().getAppId(), categoryIds);
  208. page.setRcount(count);
  209. params.put("search", searchMap);
  210. //站点编号
  211. if (BasicUtil.getWebsiteApp() != null) {
  212. params.put(ParserUtil.APP_DIR, BasicUtil.getWebsiteApp().getAppDir());
  213. params.put(ParserUtil.URL, BasicUtil.getWebsiteApp().getAppHostUrl());
  214. params.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  215. } else {
  216. params.put(ParserUtil.URL, BasicUtil.getUrl());
  217. params.put(ParserUtil.APP_DIR, BasicUtil.getApp().getAppDir());
  218. }
  219. searchMap.put("pageNo", 0);
  220. // ParserUtil.read(search, map, page);
  221. int total = PageUtil.totalPage(count, page.getSize());
  222. int pageNo = BasicUtil.getInt("pageNo", 1);
  223. if (pageNo >= total && total != 0) {
  224. pageNo = total;
  225. }
  226. //获取总数
  227. page.setTotal(total);
  228. page.setPageNo(pageNo);
  229. //设置分页的统一链接
  230. String url = params.get(ParserUtil.URL).toString();
  231. url = url + request.getServletPath() + "?" + urlParams;
  232. String pageNoStr = "size=" + page.getSize() + "&pageNo=";
  233. //下一页
  234. String nextUrl = url + pageNoStr + ((pageNo + 1 > total) ? total : pageNo + 1);
  235. //首页
  236. String indexUrl = url + pageNoStr + 1;
  237. //尾页
  238. String lastUrl = url + pageNoStr + total;
  239. //上一页 当前页为1时,上一页就是1
  240. String preUrl = url + pageNoStr + ((pageNo == 1) ? 1 : pageNo - 1);
  241. page.setIndexUrl(indexUrl);
  242. page.setNextUrl(nextUrl);
  243. page.setPreUrl(preUrl);
  244. page.setLastUrl(lastUrl);
  245. params.put(ParserUtil.PAGE, page);
  246. params.put(ParserUtil.HTML, htmlDir);
  247. //动态解析
  248. params.put(ParserUtil.IS_DO, false);
  249. //设置动态请求的模块路径
  250. params.put(ParserUtil.MODEL_NAME, "mcms");
  251. //解析后的内容
  252. String content = "";
  253. try {
  254. //根据模板路径,参数生成
  255. content = ParserUtil.rendering(search, params);
  256. } catch (TemplateNotFoundException e) {
  257. e.printStackTrace();
  258. } catch (MalformedTemplateNameException e) {
  259. e.printStackTrace();
  260. } catch (ParseException e) {
  261. e.printStackTrace();
  262. } catch (IOException e) {
  263. e.printStackTrace();
  264. }
  265. return content;
  266. }
  267. // 清除路径中的转义字符
  268. private String clearXss(String value) {
  269. if (value == null || "".equals(value)) {
  270. return value;
  271. }
  272. value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
  273. value = value.replaceAll("\\(", "&#40;").replace("\\)", "&#41;");
  274. value = value.replaceAll("'", "&#39;");
  275. value = value.replaceAll("eval\\((.*)\\)", "");
  276. value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']",
  277. "\"\"");
  278. value = value.replace("script", "");
  279. return value;
  280. }
  281. /**
  282. * 存储自定义模型字段和接口参数
  283. *
  284. * @author 铭软开源团队
  285. * @date 2019年3月5日
  286. */
  287. class DiyModelMap {
  288. private String key;
  289. private Object value;
  290. public String getKey() {
  291. return key;
  292. }
  293. public void setKey(String key) {
  294. this.key = key;
  295. }
  296. public Object getValue() {
  297. return value;
  298. }
  299. public void setValue(Object value) {
  300. this.value = value;
  301. }
  302. @Override
  303. public String toString() {
  304. return JSON.toJSONString(this);
  305. }
  306. }
  307. }