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.PageBean;
  37. import net.mingsoft.mdiy.biz.IModelBiz;
  38. import net.mingsoft.mdiy.biz.IPageBiz;
  39. import net.mingsoft.mdiy.entity.ModelEntity;
  40. import net.mingsoft.mdiy.util.ParserUtil;
  41. import org.apache.commons.lang3.StringUtils;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.stereotype.Controller;
  44. import org.springframework.web.bind.annotation.GetMapping;
  45. import org.springframework.web.bind.annotation.RequestMapping;
  46. import org.springframework.web.bind.annotation.RequestMethod;
  47. import org.springframework.web.bind.annotation.ResponseBody;
  48. import javax.servlet.http.HttpServletRequest;
  49. import javax.servlet.http.HttpServletResponse;
  50. import java.io.IOException;
  51. import java.io.UnsupportedEncodingException;
  52. import java.util.ArrayList;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. /**
  57. * 动态生成页面,需要后台配置自定义页数据
  58. *
  59. * @author 铭飞开源团队
  60. * @date 2018年12月17日
  61. */
  62. @Controller("dynamicPageAction")
  63. @RequestMapping("/mcms")
  64. public class MCmsAction extends net.mingsoft.cms.action.BaseAction {
  65. /**
  66. * 自定义页面业务层
  67. */
  68. @Autowired
  69. private IPageBiz pageBiz;
  70. /**
  71. * 文章管理业务处理层
  72. */
  73. @Autowired
  74. private IContentBiz contentBiz;
  75. /**
  76. * 栏目业务层
  77. */
  78. @Autowired
  79. private ICategoryBiz categoryBiz;
  80. /**
  81. * 搜索标签;
  82. */
  83. public static final String SEARCH = "search";
  84. /**
  85. * 自定义模型
  86. */
  87. @Autowired
  88. private IModelBiz modelBiz;
  89. /**
  90. * 动态列表页
  91. */
  92. @GetMapping("/index.do")
  93. @ResponseBody
  94. public String index(HttpServletRequest req, HttpServletResponse resp) {
  95. Map map = BasicUtil.assemblyRequestMap();
  96. map.forEach((k,v)->{
  97. map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  98. });
  99. map.put(ParserUtil.URL, BasicUtil.getUrl());
  100. //动态解析
  101. map.put(ParserUtil.IS_DO,true);
  102. //设置动态请求的模块路径
  103. map.put(ParserUtil.MODEL_NAME, "mcms");
  104. //解析后的内容
  105. String content = "";
  106. try {
  107. //根据模板路径,参数生成
  108. content = CmsParserUtil.generate(ParserUtil.INDEX+ParserUtil.HTM_SUFFIX, map);
  109. } catch (TemplateNotFoundException e) {
  110. e.printStackTrace();
  111. } catch (MalformedTemplateNameException e) {
  112. e.printStackTrace();
  113. } catch (ParseException e) {
  114. e.printStackTrace();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. return content;
  119. }
  120. /**
  121. * 动态列表页
  122. * @param req
  123. * @param resp
  124. */
  125. @ResponseBody
  126. @GetMapping("/list.do")
  127. public String list(HttpServletRequest req, HttpServletResponse resp) {
  128. Map map = BasicUtil.assemblyRequestMap();
  129. map.forEach((k,v)->{
  130. map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  131. });
  132. //获取栏目编号
  133. int typeId = BasicUtil.getInt(ParserUtil.TYPE_ID,0);
  134. int size = BasicUtil.getInt(ParserUtil.SIZE,10);
  135. ContentBean contentBean = new ContentBean();
  136. contentBean.setCategoryId(String.valueOf(typeId));
  137. //获取文章总数
  138. List<CategoryBean> columnArticles = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  139. //判断栏目下是否有文章
  140. if(columnArticles.size()==0){
  141. return "";
  142. }
  143. //设置分页类
  144. PageBean page = new PageBean();
  145. int total = PageUtil.totalPage(columnArticles.size(), size);
  146. map.put(ParserUtil.COLUMN, columnArticles.get(0));
  147. //获取总数
  148. page.setTotal(total);
  149. //设置栏目编号
  150. map.put(ParserUtil.TYPE_ID, typeId);
  151. //设置列表当前页
  152. map.put(ParserUtil.PAGE_NO, BasicUtil.getInt(ParserUtil.PAGE_NO,1));
  153. map.put(ParserUtil.URL, BasicUtil.getUrl());
  154. map.put(ParserUtil.PAGE, page);
  155. //动态解析
  156. map.put(ParserUtil.IS_DO,true);
  157. //设置动态请求的模块路径
  158. map.put(ParserUtil.MODEL_NAME, "mcms");
  159. //解析后的内容
  160. String content = "";
  161. try {
  162. //根据模板路径,参数生成
  163. content = CmsParserUtil.generate(columnArticles.get(0).getCategoryListUrl(),map);
  164. } catch (TemplateNotFoundException e) {
  165. e.printStackTrace();
  166. } catch (MalformedTemplateNameException e) {
  167. e.printStackTrace();
  168. } catch (ParseException e) {
  169. e.printStackTrace();
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. return content;
  174. }
  175. /**
  176. * 动态详情页
  177. * @param id 文章编号
  178. */
  179. @GetMapping("/view.do")
  180. @ResponseBody
  181. public String view(String orderby,String order,HttpServletRequest req, HttpServletResponse resp) {
  182. //参数文章编号
  183. ContentEntity article = (ContentEntity) contentBiz.getEntity(BasicUtil.getInt(ParserUtil.ID));
  184. if(ObjectUtil.isNull(article)){
  185. throw new BusinessException(this.getResString("err.empty", this.getResString("id"))) ;
  186. }
  187. if(StringUtils.isNotBlank(order)){
  188. //防注入
  189. if(!order.toLowerCase().equals("asc")&&!order.toLowerCase().equals("desc")){
  190. throw new BusinessException(this.getResString("err.error", this.getResString("order")));
  191. }
  192. }
  193. orderby= orderby.replaceAll("('|\"|\\\\)","\\\\$1");
  194. PageBean page = new PageBean();
  195. //用于详情上下页获取当前文章列表对应的分类,根据文章查询只能获取自身分类
  196. String typeId = BasicUtil.getString(ParserUtil.TYPE_ID,article.getCategoryId());
  197. //根据文章编号查询栏目详情模版
  198. CategoryEntity column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(typeId));
  199. //解析后的内容
  200. String content = "";
  201. Map map = BasicUtil.assemblyRequestMap();
  202. map.forEach((k,v)->{
  203. //sql注入过滤
  204. map.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  205. });
  206. //动态解析
  207. map.put(ParserUtil.IS_DO,true);
  208. //设置栏目编号
  209. map.put(ParserUtil.TYPE_ID, typeId);
  210. //设置动态请求的模块路径
  211. map.put(ParserUtil.MODEL_NAME, "mcms");
  212. map.put(ParserUtil.URL, BasicUtil.getUrl());
  213. map.put(ParserUtil.PAGE, page);
  214. map.put(ParserUtil.ID, article.getId());
  215. ContentBean contentBean = new ContentBean();
  216. contentBean.setCategoryId(String.valueOf(typeId));
  217. contentBean.setOrderBy(orderby);
  218. contentBean.setOrder(order);
  219. List<CategoryBean> articleIdList = contentBiz.queryIdsByCategoryIdForParser(contentBean);
  220. Map<Object, Object> contentModelMap = new HashMap<Object, Object>();
  221. ModelEntity contentModel = null;
  222. for (int artId = 0; artId < articleIdList.size();) {
  223. //如果不是当前文章则跳过
  224. if(articleIdList.get(artId).getArticleId().equals(article.getId())){
  225. artId++;
  226. continue;
  227. }
  228. // 文章的栏目路径
  229. String categoryParentId = articleIdList.get(artId).getId() ;
  230. if(StringUtils.isNotBlank(articleIdList.get(artId).getCategoryParentId())){
  231. categoryParentId += ','+articleIdList.get(artId).getCategoryParentId();
  232. }
  233. // 文章的栏目模型编号
  234. Integer columnContentModelId = articleIdList.get(artId).getMdiyModelId();
  235. Map<String, Object> parserParams = new HashMap<String, Object>();
  236. parserParams.put(ParserUtil.COLUMN, articleIdList.get(artId));
  237. // 判断当前栏目是否有自定义模型
  238. if ( columnContentModelId != null && columnContentModelId > 0) {
  239. // 通过当前栏目的模型编号获取,自定义模型表名
  240. if (contentModelMap.containsKey(columnContentModelId)) {
  241. parserParams.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  242. } else {
  243. // 通过栏目模型编号获取自定义模型实体
  244. contentModel=(ModelEntity)modelBiz.getEntity(columnContentModelId);
  245. // 将自定义模型编号设置为key值
  246. contentModelMap.put(columnContentModelId, contentModel.getModelTableName());
  247. parserParams.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  248. }
  249. }
  250. // 第一篇文章没有上一篇
  251. if (artId > 0) {
  252. CategoryBean preCaBean = articleIdList.get(artId - 1);
  253. page.setPreId(preCaBean.getArticleId());
  254. }
  255. // 最后一篇文章没有下一篇
  256. if (artId + 1 < articleIdList.size()) {
  257. CategoryBean nextCaBean = articleIdList.get(artId + 1);
  258. page.setNextId(nextCaBean.getArticleId());
  259. }
  260. break;
  261. }
  262. try {
  263. //根据模板路径,参数生成
  264. content = CmsParserUtil.generate(column.getCategoryUrl(), map);
  265. } catch (TemplateNotFoundException e) {
  266. e.printStackTrace();
  267. } catch (MalformedTemplateNameException e) {
  268. e.printStackTrace();
  269. } catch (ParseException e) {
  270. e.printStackTrace();
  271. } catch (IOException e) {
  272. e.printStackTrace();
  273. }
  274. return content;
  275. }
  276. /**
  277. * 实现前端页面的文章搜索
  278. *
  279. * @param request
  280. * 搜索id
  281. * @param response
  282. */
  283. @RequestMapping(value = "search")
  284. @ResponseBody
  285. public String search(HttpServletRequest request, HttpServletResponse response) throws IOException {
  286. Map<String, Object> map = new HashMap<>();
  287. // 读取请求字段
  288. Map<String, Object> field = BasicUtil.assemblyRequestMap();
  289. // 自定义字段集合
  290. Map<String, String> diyFieldName = new HashMap<String, String>();
  291. CategoryEntity column = null; // 当前栏目
  292. ModelEntity contentModel = null; // 栏目对应模型
  293. List<DiyModelMap> fieldValueList = new ArrayList<DiyModelMap>(); // 栏目对应字段的值
  294. int typeId = 0;
  295. String categoryIds = BasicUtil.getString("categoryId");
  296. //当传递了栏目编号,但不是栏目集合
  297. if(!StringUtil.isBlank(categoryIds) && !categoryIds.contains(",")){
  298. typeId = Integer.parseInt(categoryIds);
  299. }
  300. String url = BasicUtil.getUrl();
  301. //记录自定义模型字段名
  302. List filedStr = new ArrayList<>();
  303. //根据栏目确定自定义模型
  304. if(typeId>0){
  305. column = (CategoryEntity) categoryBiz.getEntity(Integer.parseInt(typeId+""));
  306. // 获取表单类型的id
  307. if (column != null&&ObjectUtil.isNotNull(column.getMdiyModelId())) {
  308. contentModel = (ModelEntity) modelBiz.getEntity(column.getMdiyModelId());
  309. if (contentModel != null) {
  310. Map<String,String> fieldMap = contentModel.getFieldMap();
  311. for (String s : fieldMap.keySet()) {
  312. filedStr.add(fieldMap.get(s));
  313. }
  314. map.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  315. }
  316. }
  317. map.put(ParserUtil.COLUMN, column);
  318. }
  319. // 遍历取字段集合
  320. if (field != null) {
  321. for (Map.Entry<String, Object> entry : field.entrySet()) {
  322. if (entry != null) {
  323. if (ObjectUtil.isNull(entry.getValue())) {
  324. continue;
  325. }
  326. String value = entry.getValue().toString().replaceAll("('|\"|\\\\)","\\\\$1"); // 处理由get方法请求中文乱码问题
  327. value=clearXss(value);
  328. if (request.getMethod().equals(RequestMethod.GET)) { // 如果是get方法需要将请求地址参数转码
  329. try {
  330. value = new String(value.getBytes("ISO-8859-1"), Const.UTF8);
  331. } catch (UnsupportedEncodingException e) {
  332. e.printStackTrace();
  333. }
  334. }
  335. // 保存至自定义字段集合
  336. if (!StringUtil.isBlank(value)) {
  337. diyFieldName.put(entry.getKey(), value);
  338. //判断请求中的是否是自定义模型中的字段
  339. if(filedStr.contains(entry.getKey())){
  340. //设置自定义模型字段和值
  341. DiyModelMap diyMap = new DiyModelMap();
  342. diyMap.setKey(entry.getKey());
  343. diyMap.setValue(value);
  344. fieldValueList.add(diyMap);
  345. }
  346. }
  347. }
  348. }
  349. }
  350. //添加自定义模型的字段和值
  351. if(fieldValueList.size()>0){
  352. map.put("diyModel", fieldValueList);
  353. }
  354. //设置分页类
  355. PageBean page = new PageBean();
  356. Map<String, Object> searchMap = field;
  357. StringBuilder urlParams=new StringBuilder();
  358. searchMap.forEach((k,v)->{
  359. //sql注入过滤
  360. if(v!=null){
  361. searchMap.put(k,v.toString().replaceAll("('|\"|\\\\)","\\\\$1"));
  362. searchMap.put(k,clearXss(searchMap.get(k).toString()));
  363. if(!ParserUtil.SIZE.equals(k)&&!ParserUtil.PAGE_NO.equals(k)){
  364. urlParams.append(k).append("=").append(searchMap.get(k)).append("&");
  365. }
  366. }
  367. });
  368. //查询数量
  369. int count= contentBiz.getSearchCount(contentModel,fieldValueList,searchMap,BasicUtil.getApp().getAppId(),categoryIds);
  370. map.put(ParserUtil.URL, url);
  371. map.put(SEARCH, searchMap);
  372. if(BasicUtil.getWebsiteApp() != null){
  373. map.put(ParserUtil.APP_ID, BasicUtil.getWebsiteApp().getAppId());
  374. }
  375. map.put(ParserUtil.PAGE, page);
  376. map.put(ParserUtil.HTML, ParserUtil.HTML);
  377. //动态解析
  378. map.put(ParserUtil.IS_DO,false);
  379. //设置动态请求的模块路径
  380. map.put(ParserUtil.MODEL_NAME, "mcms");
  381. searchMap.put(ParserUtil.PAGE_NO, 0);
  382. ParserUtil.read(SEARCH+ParserUtil.HTM_SUFFIX,map, page);
  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. }