MCmsAction.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2012-present 铭软科技(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 cn.hutool.json.JSONUtil;
  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.ConfigUtil;
  35. import net.mingsoft.mdiy.util.ParserUtil;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.beans.factory.annotation.Value;
  39. import org.springframework.http.MediaType;
  40. import org.springframework.stereotype.Controller;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestMethod;
  43. import org.springframework.web.bind.annotation.ResponseBody;
  44. import springfox.documentation.annotations.ApiIgnore;
  45. import javax.servlet.http.HttpServletRequest;
  46. import javax.servlet.http.HttpServletResponse;
  47. import java.io.IOException;
  48. import java.io.UnsupportedEncodingException;
  49. import java.util.ArrayList;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. /**
  54. * 动态生成页面,需要后台配置自定义页数据
  55. *
  56. * @author 铭飞开源团队
  57. * @date 2018年12月17日
  58. * @date 2021年8月26日取消默认search.htm
  59. */
  60. @ApiIgnore
  61. @Controller("dynamicPageAction")
  62. @RequestMapping("/mcms")
  63. public class MCmsAction extends net.mingsoft.cms.action.BaseAction {
  64. /**
  65. * 文章管理业务处理层
  66. */
  67. @Autowired
  68. private IContentBiz contentBiz;
  69. /**
  70. * 栏目业务层
  71. */
  72. @Autowired
  73. private ICategoryBiz categoryBiz;
  74. /**
  75. * 自定义模型
  76. */
  77. @Autowired
  78. private IModelBiz modelBiz;
  79. @Value("${ms.diy.html-dir:html}")
  80. private String htmlDir;
  81. /**
  82. * 实现前端页面的文章搜索
  83. * @return 渲染后的搜索页面
  84. */
  85. @RequestMapping(value = "search",method = {RequestMethod.GET, RequestMethod.POST},produces= MediaType.TEXT_HTML_VALUE+";charset=utf-8")
  86. @ResponseBody
  87. public String search(HttpServletRequest request, HttpServletResponse response) {
  88. String search = BasicUtil.getString("tmpl", "search.htm");
  89. //设置分页类
  90. PageBean page = new PageBean();
  91. page.setSize(ParserUtil.getPageSize(search, 20));
  92. //参数集合,提供给解析使用
  93. Map<String, Object> params = new HashMap<>();
  94. // 读取请求字段
  95. Map<String, Object> field = BasicUtil.assemblyRequestMap();
  96. // 自定义字段集合
  97. Map<String, String> diyFieldName = new HashMap<String, String>();
  98. //记录自定义模型字段名
  99. List filedStr = new ArrayList<>();
  100. // 栏目对应字段的值
  101. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>();
  102. // 当前栏目
  103. CategoryEntity column = null;
  104. // 栏目对应模型
  105. ModelEntity contentModel = null;
  106. //获取栏目信息
  107. String typeId = null;
  108. String categoryIds = BasicUtil.getString("categoryIds");
  109. if ("null".equals(categoryIds)){
  110. categoryIds = null;
  111. }
  112. //当传递了栏目编号,但不是栏目集合
  113. if (StringUtils.isNotBlank(categoryIds) && !categoryIds.contains(",")) {
  114. typeId = categoryIds;
  115. }
  116. //根据栏目确定自定义模型
  117. if (typeId != null) {
  118. column = (CategoryEntity) categoryBiz.getById(typeId);
  119. // 获取表单类型的id
  120. if (column != null && ObjectUtil.isNotNull(column.getMdiyModelId())) {
  121. contentModel = (ModelEntity) modelBiz.getById(column.getMdiyModelId());
  122. if (contentModel != null) {
  123. // 保存自定义模型的数据
  124. Map<String, String> fieldMap = contentModel.getFieldMap();
  125. for (String s : fieldMap.keySet()) {
  126. filedStr.add(fieldMap.get(s));
  127. }
  128. // 设置自定义模型表名,方便解析的时候关联表查询
  129. params.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  130. }
  131. }
  132. // 设置栏目,方便解析的时候关联表查询
  133. params.put(ParserUtil.COLUMN, column);
  134. }
  135. // 处理读取请求字段
  136. if (field != null) {
  137. for (Map.Entry<String, Object> entry : field.entrySet()) {
  138. if (entry != null) {
  139. //空值不处理
  140. if (ObjectUtil.isNull(entry.getValue())) {
  141. continue;
  142. }
  143. // 对值进行安全处理
  144. // 处理由get方法请求中文乱码问题
  145. String value = entry.getValue().toString().replaceAll("('|\"|\\\\)", "\\\\$1");
  146. //Xss过滤
  147. value = clearXss(value);
  148. // 如果是get方法需要将请求地址参数转码
  149. if (request.getMethod().equals(RequestMethod.GET)) {
  150. try {
  151. value = new String(value.getBytes("ISO-8859-1"), Const.UTF8);
  152. } catch (UnsupportedEncodingException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. // 保存至自定义字段集合
  157. if (StringUtils.isNotBlank(value)) {
  158. diyFieldName.put(entry.getKey(), value);
  159. //判断请求中的是否是自定义模型中的字段
  160. if (filedStr.contains(entry.getKey())) {
  161. //设置自定义模型字段和值
  162. DiyModelMap diyMap = new DiyModelMap();
  163. diyMap.setKey(entry.getKey());
  164. diyMap.setValue(value);
  165. fieldValueList.add(diyMap);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. //添加自定义模型的字段和值
  172. if (fieldValueList.size() > 0) {
  173. params.put("diyModel", fieldValueList);
  174. }
  175. Map<String, Object> searchMap = field;
  176. String contentTag = BasicUtil.getString("content_tag");
  177. if (StringUtils.isNotBlank(contentTag)){
  178. searchMap.put("content_tag", contentTag);
  179. }
  180. searchMap.put("categoryIds",categoryIds);
  181. StringBuilder urlParams = new StringBuilder();
  182. searchMap.forEach((k, v) -> {
  183. //sql注入过滤
  184. if (v != null) {
  185. searchMap.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
  186. searchMap.put(k, clearXss(searchMap.get(k).toString()));
  187. if (!"size".equals(k) && !"pageNo".equals(k)) {
  188. urlParams.append(k).append("=").append(searchMap.get(k)).append("&");
  189. }
  190. }
  191. });
  192. //查询数量 站群会自动拼appId区分
  193. int count = contentBiz.getSearchCount(contentModel, fieldValueList, searchMap, categoryIds);
  194. page.setRcount(count);
  195. params.put("search", searchMap);
  196. //站点编号
  197. Boolean shortSwitch = ConfigUtil.getBoolean("短链配置", "shortLinkSwitch");
  198. if (BasicUtil.getWebsiteApp() != null) {
  199. params.put(ParserUtil.APP_DIR, BasicUtil.getWebsiteApp().getAppDir());
  200. params.put(ParserUtil.URL, BasicUtil.getWebsiteApp().getAppHostUrl());
  201. params.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  202. } else if (shortSwitch){
  203. params.put(ParserUtil.URL, BasicUtil.getUrl());
  204. params.put(ParserUtil.APP_DIR, "");
  205. } else {
  206. params.put(ParserUtil.URL, BasicUtil.getUrl());
  207. params.put(ParserUtil.APP_DIR, BasicUtil.getApp().getAppDir());
  208. }
  209. params.put(ParserUtil.SHORT_SWITCH, shortSwitch);
  210. //对项目名预处理
  211. String contextPath = BasicUtil.getContextPath();
  212. if (StringUtils.isNotBlank(contextPath) && "/".equalsIgnoreCase(contextPath) ){
  213. contextPath = "";
  214. }
  215. params.putIfAbsent(ParserUtil.CONTEXT_PATH, contextPath);
  216. searchMap.put("pageNo", 0);
  217. // ParserUtil.read(search, map, page);
  218. int total = PageUtil.totalPage(count, page.getSize());
  219. int pageNo = BasicUtil.getInt("pageNo", 1);
  220. if (pageNo >= total && total != 0) {
  221. pageNo = total;
  222. }
  223. //获取总数
  224. page.setTotal(total);
  225. page.setPageNo(pageNo);
  226. //设置分页的统一链接
  227. String url = request.getServletPath() + "?" + urlParams;
  228. String pageNoStr = "size=" + page.getSize() + "&pageNo=";
  229. //下一页
  230. String nextUrl = url + pageNoStr + ((pageNo + 1 > total) ? total : pageNo + 1);
  231. //首页
  232. String indexUrl = url + pageNoStr + 1;
  233. //尾页
  234. String lastUrl = url + pageNoStr + total;
  235. //上一页 当前页为1时,上一页就是1
  236. String preUrl = url + pageNoStr + ((pageNo == 1) ? 1 : pageNo - 1);
  237. page.setIndexUrl(indexUrl);
  238. page.setNextUrl(nextUrl);
  239. page.setPreUrl(preUrl);
  240. page.setLastUrl(lastUrl);
  241. params.put(ParserUtil.PAGE, page);
  242. params.put(ParserUtil.HTML, htmlDir);
  243. //动态解析
  244. params.put(ParserUtil.IS_DO, false);
  245. //设置动态请求的模块路径
  246. params.put(ParserUtil.MODEL_NAME, "mcms");
  247. //解析后的内容
  248. String content = "";
  249. try {
  250. //根据模板路径,参数生成
  251. content = ParserUtil.rendering(search, params);
  252. } catch (TemplateNotFoundException e) {
  253. e.printStackTrace();
  254. } catch (MalformedTemplateNameException e) {
  255. e.printStackTrace();
  256. } catch (ParseException e) {
  257. e.printStackTrace();
  258. } catch (IOException e) {
  259. e.printStackTrace();
  260. }
  261. return content;
  262. }
  263. // 清除路径中的转义字符
  264. private String clearXss(String value) {
  265. if (value == null || "".equals(value)) {
  266. return value;
  267. }
  268. value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
  269. value = value.replaceAll("\\(", "&#40;").replace("\\)", "&#41;");
  270. value = value.replaceAll("'", "&#39;");
  271. value = value.replaceAll("eval\\((.*)\\)", "");
  272. value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']",
  273. "\"\"");
  274. value = value.replace("script", "");
  275. return value;
  276. }
  277. /**
  278. * 存储自定义模型字段和接口参数
  279. *
  280. * @author 铭软开源团队
  281. * @date 2019年3月5日
  282. */
  283. class DiyModelMap {
  284. private String key;
  285. private Object value;
  286. public String getKey() {
  287. return key;
  288. }
  289. public void setKey(String key) {
  290. this.key = key;
  291. }
  292. public Object getValue() {
  293. return value;
  294. }
  295. public void setValue(Object value) {
  296. this.value = value;
  297. }
  298. @Override
  299. public String toString() {
  300. return JSONUtil.toJsonStr(this);
  301. }
  302. }
  303. }