MCmsAction.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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.mingsoft.base.constant.Const;
  25. import net.mingsoft.basic.util.BasicUtil;
  26. import net.mingsoft.basic.util.StringUtil;
  27. import net.mingsoft.cms.bean.ContentBean;
  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.cms.entity.ContentEntity;
  32. import net.mingsoft.cms.util.CmsParserUtil;
  33. import net.mingsoft.mdiy.bean.PageBean;
  34. import net.mingsoft.mdiy.biz.IModelBiz;
  35. import net.mingsoft.mdiy.biz.IPageBiz;
  36. import net.mingsoft.mdiy.entity.ModelEntity;
  37. import net.mingsoft.mdiy.util.ParserUtil;
  38. import org.apache.commons.lang3.StringUtils;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.stereotype.Controller;
  41. import org.springframework.web.bind.annotation.GetMapping;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RequestMethod;
  44. import org.springframework.web.bind.annotation.ResponseBody;
  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.*;
  50. import java.util.regex.Matcher;
  51. import java.util.regex.Pattern;
  52. /**
  53. * 动态生成页面,需要后台配置自定义页数据
  54. *
  55. * @author 铭飞开源团队
  56. * @date 2018年12月17日
  57. */
  58. @Controller("dynamicPageAction")
  59. @RequestMapping("/mcms")
  60. public class MCmsAction extends net.mingsoft.cms.action.BaseAction {
  61. /**
  62. * 自定义页面业务层
  63. */
  64. @Autowired
  65. private IPageBiz pageBiz;
  66. /**
  67. * 文章管理业务处理层
  68. */
  69. @Autowired
  70. private IContentBiz contentBiz;
  71. /**
  72. * 栏目业务层
  73. */
  74. @Autowired
  75. private ICategoryBiz categoryBiz;
  76. /**
  77. * 搜索标签;
  78. */
  79. public static final String SEARCH = "search";
  80. /**
  81. * 自定义模型
  82. */
  83. @Autowired
  84. private IModelBiz modelBiz;
  85. /**
  86. * 动态列表页
  87. */
  88. @GetMapping("/index.do")
  89. public void index(HttpServletRequest req, HttpServletResponse resp) {
  90. Map map = BasicUtil.assemblyRequestMap();
  91. map.forEach((k,v)->{
  92. //sql注入过滤
  93. if(sqlFilter(v.toString())){
  94. map.put(k,"");
  95. }
  96. });
  97. map.put(ParserUtil.URL, BasicUtil.getUrl());
  98. //动态解析
  99. map.put(ParserUtil.IS_DO,true);
  100. //设置动态请求的模块路径
  101. map.put(ParserUtil.MODEL_NAME, "mcms");
  102. //解析后的内容
  103. String content = "";
  104. try {
  105. //根据模板路径,参数生成
  106. content = CmsParserUtil.generate(ParserUtil.INDEX+ParserUtil.HTM_SUFFIX, map);
  107. } catch (TemplateNotFoundException e) {
  108. e.printStackTrace();
  109. } catch (MalformedTemplateNameException e) {
  110. e.printStackTrace();
  111. } catch (ParseException e) {
  112. e.printStackTrace();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. this.outString(resp, content);
  117. }
  118. /**
  119. * 动态列表页
  120. * @param req
  121. * @param resp
  122. */
  123. @GetMapping("/list.do")
  124. public void list(HttpServletRequest req, HttpServletResponse resp) {
  125. Map map = BasicUtil.assemblyRequestMap();
  126. //获取栏目编号
  127. int typeId = BasicUtil.getInt(ParserUtil.TYPE_ID,0);
  128. int size = BasicUtil.getInt(ParserUtil.SIZE,10);
  129. //获取文章总数
  130. List<ContentBean> columnArticles = contentBiz.queryIdsByCategoryIdForParser(String.valueOf(typeId), null, null);
  131. //判断栏目下是否有文章
  132. if(columnArticles.size()==0){
  133. this.outJson(resp, false);
  134. }
  135. //设置分页类
  136. PageBean page = new PageBean();
  137. int total = PageUtil.totalPage(columnArticles.size(), size);
  138. map.put(ParserUtil.COLUMN, columnArticles.get(0));
  139. //获取总数
  140. page.setTotal(total);
  141. //设置栏目编号
  142. map.put(ParserUtil.TYPE_ID, typeId);
  143. //设置列表当前页
  144. map.put(ParserUtil.PAGE_NO, BasicUtil.getInt(ParserUtil.PAGE_NO,1));
  145. map.put(ParserUtil.URL, BasicUtil.getUrl());
  146. map.put(ParserUtil.PAGE, page);
  147. //动态解析
  148. map.put(ParserUtil.IS_DO,true);
  149. //设置动态请求的模块路径
  150. map.put(ParserUtil.MODEL_NAME, "mcms");
  151. //解析后的内容
  152. String content = "";
  153. try {
  154. //根据模板路径,参数生成
  155. content = CmsParserUtil.generate(columnArticles.get(0).getCategoryListUrl(),map);
  156. } catch (TemplateNotFoundException e) {
  157. e.printStackTrace();
  158. } catch (MalformedTemplateNameException e) {
  159. e.printStackTrace();
  160. } catch (ParseException e) {
  161. e.printStackTrace();
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. }
  165. this.outString(resp, content);
  166. }
  167. /**
  168. * 动态详情页
  169. * @param id 文章编号
  170. */
  171. @GetMapping("/view.do")
  172. public void view(String orderby,String order,HttpServletRequest req, HttpServletResponse resp) {
  173. //参数文章编号
  174. ContentEntity article = (ContentEntity) contentBiz.getEntity(BasicUtil.getInt(ParserUtil.ID));
  175. if(ObjectUtil.isNull(article)){
  176. this.outJson(resp,false,getResString("err.empty", this.getResString("id")));
  177. return;
  178. }
  179. if(StringUtils.isNotBlank(order)){
  180. //防注入
  181. if(!order.toLowerCase().equals("asc")&&!order.toLowerCase().equals("desc")){
  182. this.outJson(resp,false,getResString("err.error", this.getResString("order")));
  183. return;
  184. }
  185. }
  186. if(sqlFilter(orderby)){
  187. orderby = "id";
  188. }
  189. PageBean page = new PageBean();
  190. //根据文章编号查询栏目详情模版
  191. CategoryEntity column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(article.getContentCategoryId()));
  192. //解析后的内容
  193. String content = "";
  194. Map map = BasicUtil.assemblyRequestMap();
  195. map.forEach((k,v)->{
  196. //sql注入过滤
  197. if(sqlFilter(v.toString())){
  198. map.put(k,"");
  199. }
  200. });
  201. //动态解析
  202. map.put(ParserUtil.IS_DO,true);
  203. //设置动态请求的模块路径
  204. map.put(ParserUtil.MODEL_NAME, "mcms");
  205. map.put(ParserUtil.URL, BasicUtil.getUrl());
  206. map.put(ParserUtil.PAGE, page);
  207. map.put(ParserUtil.ID, article.getId());
  208. List<ContentBean> articleIdList = contentBiz.queryIdsByCategoryIdForParser(column.getCategoryId(), null, null,orderby,order);
  209. Map<Object, Object> contentModelMap = new HashMap<Object, Object>();
  210. ModelEntity contentModel = null;
  211. for (int artId = 0; artId < articleIdList.size();) {
  212. //如果不是当前文章则跳过
  213. if(articleIdList.get(artId).getArticleId() != Integer.parseInt(article.getId())){
  214. artId++;
  215. continue;
  216. }
  217. // 文章的栏目路径
  218. String articleColumnPath = articleIdList.get(artId).getCategoryPath();
  219. // 文章的栏目模型编号
  220. String columnContentModelId = articleIdList.get(artId).getMdiyModelId();
  221. Map<String, Object> parserParams = new HashMap<String, Object>();
  222. parserParams.put(ParserUtil.COLUMN, articleIdList.get(artId));
  223. // 判断当前栏目是否有自定义模型
  224. if ( StringUtils.isNotBlank(columnContentModelId)) {
  225. // 通过当前栏目的模型编号获取,自定义模型表名
  226. if (contentModelMap.containsKey(columnContentModelId)) {
  227. parserParams.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  228. } else {
  229. // 通过栏目模型编号获取自定义模型实体
  230. contentModel=(ModelEntity)modelBiz.getEntity(Integer.parseInt(columnContentModelId));
  231. // 将自定义模型编号设置为key值
  232. contentModelMap.put(columnContentModelId, contentModel.getModelTableName());
  233. parserParams.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  234. }
  235. }
  236. // 第一篇文章没有上一篇
  237. if (artId > 0) {
  238. ContentBean preCaBean = articleIdList.get(artId - 1);
  239. //判断当前文档是否与上一页文章在同一栏目下,并且不能使用父栏目字符串,因为父栏目中没有所属栏目编号
  240. if(articleColumnPath.contains(preCaBean.getCategoryId()+"")){
  241. page.setPreId(preCaBean.getArticleId());
  242. }
  243. }
  244. // 最后一篇文章没有下一篇
  245. if (artId + 1 < articleIdList.size()) {
  246. ContentBean nextCaBean = articleIdList.get(artId + 1);
  247. //判断当前文档是否与下一页文章在同一栏目下并且不能使用父栏目字符串,因为父栏目中没有所属栏目编号
  248. if(articleColumnPath.contains(nextCaBean.getCategoryId()+"")){
  249. page.setNextId(nextCaBean.getArticleId());
  250. }
  251. }
  252. break;
  253. }
  254. try {
  255. //根据模板路径,参数生成
  256. content = CmsParserUtil.generate(column.getCategoryUrl(), map);
  257. } catch (TemplateNotFoundException e) {
  258. e.printStackTrace();
  259. } catch (MalformedTemplateNameException e) {
  260. e.printStackTrace();
  261. } catch (ParseException e) {
  262. e.printStackTrace();
  263. } catch (IOException e) {
  264. e.printStackTrace();
  265. }
  266. this.outString(resp, content);
  267. }
  268. /**
  269. * 实现前端页面的文章搜索
  270. *
  271. * @param request
  272. * 搜索id
  273. * @param response
  274. */
  275. @RequestMapping(value = "search")
  276. @ResponseBody
  277. public void search(HttpServletRequest request, HttpServletResponse response) throws IOException {
  278. Map<String, Object> map = new HashMap<>();
  279. // 读取请求字段
  280. Map<String, Object> field = BasicUtil.assemblyRequestMap();
  281. // 自定义字段集合
  282. Map<String, String> diyFieldName = new HashMap<String, String>();
  283. CategoryEntity column = null; // 当前栏目
  284. ModelEntity contentModel = null; // 栏目对应模型
  285. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>(); // 栏目对应字段的值
  286. int typeId = 0;
  287. String categoryIds = BasicUtil.getString("categoryId");
  288. //当传递了栏目编号,但不是栏目集合
  289. if(!StringUtil.isBlank(categoryIds) && !categoryIds.contains(",")){
  290. typeId = Integer.parseInt(categoryIds);
  291. }
  292. //记录自定义模型字段名
  293. List filedStr = new ArrayList<>();
  294. //根据栏目确定自定义模型
  295. if(typeId>0){
  296. column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(typeId+""));
  297. // 获取表单类型的id
  298. if (column != null&&ObjectUtil.isNotNull(column.getMdiyModelId())) {
  299. contentModel = (ModelEntity) modelBiz.getEntity(Integer.parseInt(column.getMdiyModelId()));
  300. if (contentModel != null) {
  301. Map<String,String> fieldMap = contentModel.getFieldMap();
  302. for (String s : fieldMap.keySet()) {
  303. filedStr.add(fieldMap.get(s));
  304. }
  305. map.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  306. }
  307. }
  308. map.put(ParserUtil.COLUMN, column);
  309. }
  310. // 遍历取字段集合
  311. if (field != null) {
  312. for (Map.Entry<String, Object> entry : field.entrySet()) {
  313. if (entry != null) {
  314. String value = entry.getValue().toString(); // 处理由get方法请求中文乱码问题
  315. if (ObjectUtil.isNull(value)) {
  316. continue;
  317. }
  318. if (request.getMethod().equals(RequestMethod.GET)) { // 如果是get方法需要将请求地址参数转码
  319. try {
  320. value = new String(value.getBytes("ISO-8859-1"), Const.UTF8);
  321. } catch (UnsupportedEncodingException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. // 保存至自定义字段集合
  326. if (!StringUtil.isBlank(value)) {
  327. diyFieldName.put(entry.getKey(), value);
  328. //判断请求中的是否是自定义模型中的字段
  329. if(filedStr.contains(entry.getKey())){
  330. //设置自定义模型字段和值
  331. DiyModelMap diyMap = new DiyModelMap();
  332. diyMap.setKey(entry.getKey());
  333. diyMap.setValue(value);
  334. fieldValueList.add(diyMap);
  335. }
  336. }
  337. }
  338. }
  339. }
  340. //添加自定义模型的字段和值
  341. if(fieldValueList.size()>0){
  342. map.put("diyModel", fieldValueList);
  343. }
  344. //设置分页类
  345. PageBean page = new PageBean();
  346. Map<String, Object> searchMap = field;
  347. searchMap.forEach((k,v)->{
  348. //sql注入过滤
  349. if(sqlFilter(v.toString())){
  350. searchMap.put(k,"");
  351. }
  352. });
  353. //查询数量
  354. int count= contentBiz.getSearchCount(contentModel,fieldValueList,searchMap,BasicUtil.getAppId(),categoryIds);
  355. map.put(ParserUtil.URL, BasicUtil.getUrl());
  356. map.put(SEARCH, searchMap);
  357. map.put(ParserUtil.APP_ID, BasicUtil.getAppId());
  358. map.put(ParserUtil.PAGE, page);
  359. map.put(ParserUtil.HTML, ParserUtil.HTML);
  360. //动态解析
  361. map.put(ParserUtil.IS_DO,false);
  362. //设置动态请求的模块路径
  363. map.put(ParserUtil.MODEL_NAME, "mcms");
  364. searchMap.put(ParserUtil.PAGE_NO, 0);
  365. ParserUtil.read(SEARCH+ParserUtil.HTM_SUFFIX,map, page);
  366. int total = PageUtil.totalPage(count, page.getSize());
  367. int pageNo = BasicUtil.getInt(ParserUtil.PAGE_NO, 1);
  368. if(pageNo >= total && total!=0) {
  369. pageNo = total;
  370. }
  371. //获取总数
  372. page.setTotal(total);
  373. page.setPageNo(pageNo);
  374. String str = ParserUtil.PAGE_NO+","+ParserUtil.SIZE;
  375. //设置分页的统一链接
  376. String url = BasicUtil.getUrl()+request.getServletPath() +"?" + BasicUtil.assemblyRequestUrlParams(str.split(","));
  377. String pageNoStr = "&"+ParserUtil.SIZE+"="+page.getSize()+"&"+ParserUtil.PAGE_NO+"=";
  378. //下一页
  379. String nextUrl = url + pageNoStr+((pageNo+1 > total)?total:pageNo+1);
  380. //首页
  381. String indexUrl = url + pageNoStr + 1;
  382. //尾页
  383. String lastUrl = url + pageNoStr + total;
  384. //上一页 当前页为1时,上一页就是1
  385. String preUrl = url + pageNoStr + ((pageNo==1) ? 1:pageNo-1);
  386. page.setIndexUrl(indexUrl);
  387. page.setNextUrl(nextUrl);
  388. page.setPreUrl(preUrl);
  389. page.setLastUrl(lastUrl);
  390. searchMap.put(ParserUtil.PAGE_NO, pageNo);
  391. //解析后的内容
  392. String content = "";
  393. try {
  394. //根据模板路径,参数生成
  395. content = CmsParserUtil.generate(SEARCH+ParserUtil.HTM_SUFFIX,map);
  396. } catch (TemplateNotFoundException e) {
  397. e.printStackTrace();
  398. } catch (MalformedTemplateNameException e) {
  399. e.printStackTrace();
  400. } catch (ParseException e) {
  401. e.printStackTrace();
  402. } catch (IOException e) {
  403. e.printStackTrace();
  404. }
  405. this.outString(response, content);
  406. }
  407. /**
  408. * sql语句检测,存在返回true
  409. * @param str
  410. * @return
  411. */
  412. public static boolean sqlFilter(String str){
  413. Pattern pattern= Pattern.compile("\\b(and|exec|insert|select|drop|grant|alter|delete|update|count|chr|mid|master|truncate|char|declare|or)\\b|(\\*|;|\\+|'|%)");
  414. Matcher matcher=pattern.matcher(str);
  415. return matcher.find();
  416. }
  417. private Map get(String key, List<Map> fields) {
  418. for (Map field : fields) {
  419. if(key.equals(field.get("key"))){
  420. return field;
  421. }
  422. }
  423. return null;
  424. }
  425. /**
  426. * 存储自定义模型字段和接口参数
  427. * @author 铭飞开源团队
  428. * @date 2019年3月5日
  429. */
  430. public class DiyModelMap {
  431. String key;
  432. Object value;
  433. public String getKey() {
  434. return key;
  435. }
  436. public void setKey(String key) {
  437. this.key = key;
  438. }
  439. public Object getValue() {
  440. return value;
  441. }
  442. public void setValue(Object value) {
  443. this.value = value;
  444. }
  445. }
  446. }