MCmsAction.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /**
  2. The MIT License (MIT) * Copyright (c) 2016 铭飞科技(mingsoft.net)
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. * this software and associated documentation files (the "Software"), to deal in
  5. * the Software without restriction, including without limitation the rights to
  6. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. * the Software, and to permit persons to whom the Software is furnished to do so,
  8. * subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in all
  10. * copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  13. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  15. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. package net.mingsoft.cms.action.web;
  19. import cn.hutool.core.util.ObjectUtil;
  20. import cn.hutool.core.util.PageUtil;
  21. import freemarker.core.ParseException;
  22. import freemarker.template.MalformedTemplateNameException;
  23. import freemarker.template.TemplateNotFoundException;
  24. import net.bytebuddy.implementation.bytecode.Throw;
  25. import net.mingsoft.base.constant.Const;
  26. import net.mingsoft.basic.exception.BusinessException;
  27. import net.mingsoft.basic.util.BasicUtil;
  28. import net.mingsoft.basic.util.StringUtil;
  29. import net.mingsoft.cms.bean.CategoryBean;
  30. import net.mingsoft.cms.bean.ContentBean;
  31. import net.mingsoft.cms.biz.ICategoryBiz;
  32. import net.mingsoft.cms.biz.IContentBiz;
  33. import net.mingsoft.cms.entity.CategoryEntity;
  34. import net.mingsoft.cms.entity.ContentEntity;
  35. import net.mingsoft.cms.util.CmsParserUtil;
  36. import net.mingsoft.mdiy.bean.AttributeBean;
  37. import net.mingsoft.mdiy.bean.PageBean;
  38. import net.mingsoft.mdiy.biz.IModelBiz;
  39. import net.mingsoft.mdiy.biz.IPageBiz;
  40. import net.mingsoft.mdiy.entity.ModelEntity;
  41. import net.mingsoft.mdiy.util.ParserUtil;
  42. import org.apache.commons.lang3.StringUtils;
  43. import org.springframework.beans.factory.annotation.Autowired;
  44. import org.springframework.stereotype.Controller;
  45. import org.springframework.web.bind.annotation.GetMapping;
  46. import org.springframework.web.bind.annotation.RequestMapping;
  47. import org.springframework.web.bind.annotation.RequestMethod;
  48. import org.springframework.web.bind.annotation.ResponseBody;
  49. import javax.servlet.http.HttpServletRequest;
  50. import javax.servlet.http.HttpServletResponse;
  51. import java.io.IOException;
  52. import java.io.UnsupportedEncodingException;
  53. import java.util.ArrayList;
  54. import java.util.HashMap;
  55. import java.util.List;
  56. import java.util.Map;
  57. /**
  58. * 动态生成页面,需要后台配置自定义页数据
  59. *
  60. * @author 铭飞开源团队
  61. * @date 2018年12月17日
  62. */
  63. @Controller("dynamicPageAction")
  64. @RequestMapping("/mcms")
  65. public class MCmsAction extends net.mingsoft.cms.action.BaseAction {
  66. /**
  67. * 自定义页面业务层
  68. */
  69. @Autowired
  70. private IPageBiz pageBiz;
  71. /**
  72. * 文章管理业务处理层
  73. */
  74. @Autowired
  75. private IContentBiz contentBiz;
  76. /**
  77. * 栏目业务层
  78. */
  79. @Autowired
  80. private ICategoryBiz categoryBiz;
  81. /**
  82. * 搜索标签;
  83. */
  84. public static final String SEARCH = "search";
  85. /**
  86. * 自定义模型
  87. */
  88. @Autowired
  89. private IModelBiz modelBiz;
  90. /**
  91. * 动态列表页
  92. */
  93. @GetMapping("/index.do")
  94. @ResponseBody
  95. public String index(HttpServletRequest req, HttpServletResponse resp) {
  96. Map map = BasicUtil.assemblyRequestMap();
  97. map.forEach((k,v)->{
  98. map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  99. });
  100. map.put(ParserUtil.URL, BasicUtil.getUrl());
  101. //动态解析
  102. map.put(ParserUtil.IS_DO,true);
  103. //设置动态请求的模块路径
  104. map.put(ParserUtil.MODEL_NAME, "mcms");
  105. //解析后的内容
  106. String content = "";
  107. try {
  108. //根据模板路径,参数生成
  109. content = CmsParserUtil.generate(ParserUtil.INDEX+ParserUtil.HTM_SUFFIX, map);
  110. } catch (TemplateNotFoundException e) {
  111. e.printStackTrace();
  112. } catch (MalformedTemplateNameException e) {
  113. e.printStackTrace();
  114. } catch (ParseException e) {
  115. e.printStackTrace();
  116. } catch (IOException e) {
  117. e.printStackTrace();
  118. }
  119. return content;
  120. }
  121. /**
  122. * 动态列表页
  123. * @param req
  124. * @param resp
  125. */
  126. @ResponseBody
  127. @GetMapping("/list.do")
  128. public String list(HttpServletRequest req, HttpServletResponse resp) {
  129. Map map = BasicUtil.assemblyRequestMap();
  130. map.forEach((k,v)->{
  131. map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  132. });
  133. //获取栏目编号
  134. int typeId = BasicUtil.getInt(ParserUtil.TYPE_ID,0);
  135. int size = BasicUtil.getInt(ParserUtil.SIZE,10);
  136. ContentBean contentBean = new ContentBean();
  137. contentBean.setContentCategoryId(String.valueOf(typeId));
  138. //获取文章总数
  139. List<CategoryBean> columnArticles = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  140. //判断栏目下是否有文章
  141. if(columnArticles.size()==0){
  142. return "";
  143. }
  144. //设置分页类
  145. PageBean page = new PageBean();
  146. int total = PageUtil.totalPage(columnArticles.size(), size);
  147. map.put(ParserUtil.COLUMN, columnArticles.get(0));
  148. //获取总数
  149. page.setTotal(total);
  150. //设置栏目编号
  151. map.put(ParserUtil.TYPE_ID, typeId);
  152. //设置列表当前页
  153. map.put(ParserUtil.PAGE_NO, BasicUtil.getInt(ParserUtil.PAGE_NO,1));
  154. map.put(ParserUtil.URL, BasicUtil.getUrl());
  155. map.put(ParserUtil.PAGE, page);
  156. //动态解析
  157. map.put(ParserUtil.IS_DO,true);
  158. //设置动态请求的模块路径
  159. map.put(ParserUtil.MODEL_NAME, "mcms");
  160. //解析后的内容
  161. String content = "";
  162. try {
  163. //根据模板路径,参数生成
  164. content = CmsParserUtil.generate(columnArticles.get(0).getCategoryListUrl(),map);
  165. } catch (TemplateNotFoundException e) {
  166. e.printStackTrace();
  167. } catch (MalformedTemplateNameException e) {
  168. e.printStackTrace();
  169. } catch (ParseException e) {
  170. e.printStackTrace();
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. }
  174. return content;
  175. }
  176. /**
  177. * 动态详情页
  178. * @param id 文章编号
  179. */
  180. @GetMapping("/view.do")
  181. @ResponseBody
  182. public String view(String orderby,String order,HttpServletRequest req, HttpServletResponse resp) {
  183. //参数文章编号
  184. ContentEntity article = (ContentEntity) contentBiz.getEntity(BasicUtil.getInt(ParserUtil.ID));
  185. if(ObjectUtil.isNull(article)){
  186. throw new BusinessException(this.getResString("err.empty", this.getResString("id"))) ;
  187. }
  188. if(StringUtils.isNotBlank(order)){
  189. //防注入
  190. if(!order.toLowerCase().equals("asc")&&!order.toLowerCase().equals("desc")){
  191. throw new BusinessException(this.getResString("err.error", this.getResString("order")));
  192. }
  193. }
  194. orderby= orderby.replaceAll("('|\"|\\\\)","\\\\$1");
  195. PageBean page = new PageBean();
  196. //用于详情上下页获取当前文章列表对应的分类,根据文章查询只能获取自身分类
  197. String typeId = BasicUtil.getString(ParserUtil.TYPE_ID,article.getContentCategoryId());
  198. //根据文章编号查询栏目详情模版
  199. CategoryEntity column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(typeId));
  200. //解析后的内容
  201. String content = "";
  202. Map map = BasicUtil.assemblyRequestMap();
  203. map.forEach((k,v)->{
  204. //sql注入过滤
  205. map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  206. });
  207. //动态解析
  208. map.put(ParserUtil.IS_DO,true);
  209. //设置栏目编号
  210. map.put(ParserUtil.TYPE_ID, typeId);
  211. //设置动态请求的模块路径
  212. map.put(ParserUtil.MODEL_NAME, "mcms");
  213. map.put(ParserUtil.URL, BasicUtil.getUrl());
  214. map.put(ParserUtil.PAGE, page);
  215. map.put(ParserUtil.ID, article.getId());
  216. ContentBean contentBean = new ContentBean();
  217. contentBean.setContentCategoryId(String.valueOf(typeId));
  218. contentBean.setOrderBy(orderby);
  219. contentBean.setOrder(order);
  220. List<CategoryBean> articleIdList = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  221. Map<Object, Object> contentModelMap = new HashMap<Object, Object>();
  222. ModelEntity contentModel = null;
  223. for (int artId = 0; artId < articleIdList.size();) {
  224. //如果不是当前文章则跳过
  225. if(articleIdList.get(artId).getArticleId() != Integer.parseInt(article.getId())){
  226. artId++;
  227. continue;
  228. }
  229. // 文章的栏目路径
  230. String categoryParentId = articleIdList.get(artId).getId() ;
  231. if(StringUtils.isNotBlank(articleIdList.get(artId).getCategoryParentId())){
  232. categoryParentId += ','+articleIdList.get(artId).getCategoryParentId();
  233. }
  234. // 文章的栏目模型编号
  235. String columnContentModelId = articleIdList.get(artId).getMdiyModelId();
  236. Map<String, Object> parserParams = new HashMap<String, Object>();
  237. parserParams.put(ParserUtil.COLUMN, articleIdList.get(artId));
  238. // 判断当前栏目是否有自定义模型
  239. if ( StringUtils.isNotBlank(columnContentModelId) && Integer.parseInt(columnContentModelId)>0) {
  240. // 通过当前栏目的模型编号获取,自定义模型表名
  241. if (contentModelMap.containsKey(columnContentModelId)) {
  242. parserParams.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  243. } else {
  244. // 通过栏目模型编号获取自定义模型实体
  245. contentModel=(ModelEntity)modelBiz.getEntity(Integer.parseInt(columnContentModelId));
  246. // 将自定义模型编号设置为key值
  247. contentModelMap.put(columnContentModelId, contentModel.getModelTableName());
  248. parserParams.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  249. }
  250. }
  251. // 第一篇文章没有上一篇
  252. if (artId > 0) {
  253. CategoryBean preCaBean = articleIdList.get(artId - 1);
  254. page.setPreId(preCaBean.getArticleId());
  255. }
  256. // 最后一篇文章没有下一篇
  257. if (artId + 1 < articleIdList.size()) {
  258. CategoryBean nextCaBean = articleIdList.get(artId + 1);
  259. page.setNextId(nextCaBean.getArticleId());
  260. }
  261. break;
  262. }
  263. try {
  264. //根据模板路径,参数生成
  265. content = CmsParserUtil.generate(column.getCategoryUrl(), map);
  266. } catch (TemplateNotFoundException e) {
  267. e.printStackTrace();
  268. } catch (MalformedTemplateNameException e) {
  269. e.printStackTrace();
  270. } catch (ParseException e) {
  271. e.printStackTrace();
  272. } catch (IOException e) {
  273. e.printStackTrace();
  274. }
  275. return content;
  276. }
  277. /**
  278. * 实现前端页面的文章搜索
  279. *
  280. * @param request
  281. * 搜索id
  282. * @param response
  283. */
  284. @RequestMapping(value = "search")
  285. @ResponseBody
  286. public String search(HttpServletRequest request, HttpServletResponse response) throws IOException {
  287. Map<String, Object> map = new HashMap<>();
  288. // 读取请求字段
  289. Map<String, Object> field = BasicUtil.assemblyRequestMap();
  290. // 自定义字段集合
  291. Map<String, String> diyFieldName = new HashMap<String, String>();
  292. CategoryEntity column = null; // 当前栏目
  293. ModelEntity contentModel = null; // 栏目对应模型
  294. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>(); // 栏目对应字段的值
  295. int typeId = 0;
  296. String categoryIds = BasicUtil.getString("categoryId");
  297. //当传递了栏目编号,但不是栏目集合
  298. if(!StringUtil.isBlank(categoryIds) && !categoryIds.contains(",")){
  299. typeId = Integer.parseInt(categoryIds);
  300. }
  301. String url = BasicUtil.getUrl();
  302. //记录自定义模型字段名
  303. List filedStr = new ArrayList<>();
  304. //根据栏目确定自定义模型
  305. if(typeId>0){
  306. column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(typeId+""));
  307. // 获取表单类型的id
  308. if (column != null&&ObjectUtil.isNotNull(column.getMdiyModelId())) {
  309. contentModel = (ModelEntity) modelBiz.getEntity(Integer.parseInt(column.getMdiyModelId()));
  310. if (contentModel != null) {
  311. Map<String,String> fieldMap = contentModel.getFieldMap();
  312. for (String s : fieldMap.keySet()) {
  313. filedStr.add(fieldMap.get(s));
  314. }
  315. map.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  316. }
  317. }
  318. map.put(ParserUtil.COLUMN, column);
  319. }
  320. // 遍历取字段集合
  321. if (field != null) {
  322. for (Map.Entry<String, Object> entry : field.entrySet()) {
  323. if (entry != null) {
  324. if (ObjectUtil.isNull(entry.getValue())) {
  325. continue;
  326. }
  327. String value = entry.getValue().toString().replaceAll("('|\"|\\\\)","\\\\$1"); // 处理由get方法请求中文乱码问题
  328. value=clearXss(value);
  329. if (request.getMethod().equals(RequestMethod.GET)) { // 如果是get方法需要将请求地址参数转码
  330. try {
  331. value = new String(value.getBytes("ISO-8859-1"), Const.UTF8);
  332. } catch (UnsupportedEncodingException e) {
  333. e.printStackTrace();
  334. }
  335. }
  336. // 保存至自定义字段集合
  337. if (!StringUtil.isBlank(value)) {
  338. diyFieldName.put(entry.getKey(), value);
  339. //判断请求中的是否是自定义模型中的字段
  340. if(filedStr.contains(entry.getKey())){
  341. //设置自定义模型字段和值
  342. DiyModelMap diyMap = new DiyModelMap();
  343. diyMap.setKey(entry.getKey());
  344. diyMap.setValue(value);
  345. fieldValueList.add(diyMap);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. //添加自定义模型的字段和值
  352. if(fieldValueList.size()>0){
  353. map.put("diyModel", fieldValueList);
  354. }
  355. //设置分页类
  356. PageBean page = new PageBean();
  357. Map<String, Object> searchMap = field;
  358. StringBuilder urlParams=new StringBuilder();
  359. searchMap.forEach((k,v)->{
  360. //sql注入过滤
  361. if(v!=null){
  362. searchMap.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  363. searchMap.put(k,clearXss(searchMap.get(k).toString()));
  364. if(!ParserUtil.SIZE.equals(k)&&!ParserUtil.PAGE_NO.equals(k)){
  365. urlParams.append(k).append("=").append(searchMap.get(k)).append("&");
  366. }
  367. }
  368. });
  369. //查询数量
  370. int count= contentBiz.getSearchCount(contentModel,fieldValueList,searchMap,BasicUtil.getAppId(),categoryIds);
  371. map.put(ParserUtil.URL, url);
  372. map.put(SEARCH, searchMap);
  373. map.put(ParserUtil.APP_ID, BasicUtil.getAppId());
  374. map.put(ParserUtil.PAGE, page);
  375. map.put(ParserUtil.HTML, ParserUtil.HTML);
  376. //动态解析
  377. map.put(ParserUtil.IS_DO,false);
  378. //设置动态请求的模块路径
  379. map.put(ParserUtil.MODEL_NAME, "mcms");
  380. searchMap.put(ParserUtil.PAGE_NO, 0);
  381. AttributeBean attributeBean = new AttributeBean();
  382. ParserUtil.read(SEARCH+ParserUtil.HTM_SUFFIX,map, page,attributeBean);
  383. int total = PageUtil.totalPage(count, page.getSize());
  384. int pageNo = BasicUtil.getInt(ParserUtil.PAGE_NO, 1);
  385. if(pageNo >= total && total!=0) {
  386. pageNo = total;
  387. }
  388. //获取总数
  389. page.setTotal(total);
  390. page.setPageNo(pageNo);
  391. //设置分页的统一链接
  392. url = url +request.getServletPath() +"?" + urlParams;
  393. String pageNoStr = ParserUtil.SIZE+"="+page.getSize()+"&"+ParserUtil.PAGE_NO+"=";
  394. //下一页
  395. String nextUrl = url + pageNoStr+((pageNo+1 > total)?total:pageNo+1);
  396. //首页
  397. String indexUrl = url + pageNoStr + 1;
  398. //尾页
  399. String lastUrl = url + pageNoStr + total;
  400. //上一页 当前页为1时,上一页就是1
  401. String preUrl = url + pageNoStr + ((pageNo==1) ? 1:pageNo-1);
  402. page.setIndexUrl(indexUrl);
  403. page.setNextUrl(nextUrl);
  404. page.setPreUrl(preUrl);
  405. page.setLastUrl(lastUrl);
  406. //解析后的内容
  407. String content = "";
  408. try {
  409. //根据模板路径,参数生成
  410. content = CmsParserUtil.generate(SEARCH+ParserUtil.HTM_SUFFIX,map);
  411. } catch (TemplateNotFoundException e) {
  412. e.printStackTrace();
  413. } catch (MalformedTemplateNameException e) {
  414. e.printStackTrace();
  415. } catch (ParseException e) {
  416. e.printStackTrace();
  417. } catch (IOException e) {
  418. e.printStackTrace();
  419. }
  420. return content;
  421. }
  422. // 清除路径中的转义字符
  423. private String clearXss(String value) {
  424. if (value == null || "".equals(value)) {
  425. return value;
  426. }
  427. value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
  428. value = value.replaceAll("\\(", "&#40;").replace("\\)", "&#41;");
  429. value = value.replaceAll("'", "&#39;");
  430. value = value.replaceAll("eval\\((.*)\\)", "");
  431. value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']",
  432. "\"\"");
  433. value = value.replace("script", "");
  434. return value;
  435. }
  436. /**
  437. * 存储自定义模型字段和接口参数
  438. * @author 铭飞开源团队
  439. * @date 2019年3月5日
  440. */
  441. public class DiyModelMap {
  442. String key;
  443. Object value;
  444. public String getKey() {
  445. return key;
  446. }
  447. public void setKey(String key) {
  448. this.key = key;
  449. }
  450. public Object getValue() {
  451. return value;
  452. }
  453. public void setValue(Object value) {
  454. this.value = value;
  455. }
  456. }
  457. }