ArticleAction.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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;
  19. import java.io.File;
  20. import java.sql.Timestamp;
  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.apache.shiro.authz.annotation.RequiresPermissions;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.stereotype.Controller;
  33. import org.springframework.ui.ModelMap;
  34. import org.springframework.web.bind.annotation.ModelAttribute;
  35. import org.springframework.web.bind.annotation.PathVariable;
  36. import org.springframework.web.bind.annotation.RequestBody;
  37. import org.springframework.web.bind.annotation.RequestMapping;
  38. import com.alibaba.fastjson.JSONArray;
  39. import net.mingsoft.base.entity.BaseEntity;
  40. import net.mingsoft.base.filter.DateValueFilter;
  41. import net.mingsoft.base.filter.DoubleValueFilter;
  42. import net.mingsoft.basic.action.BaseAction;
  43. import net.mingsoft.basic.bean.EUListBean;
  44. import net.mingsoft.basic.biz.IColumnBiz;
  45. import net.mingsoft.basic.entity.ColumnEntity;
  46. import net.mingsoft.basic.util.ArrysUtil;
  47. import net.mingsoft.basic.util.BasicUtil;
  48. import net.mingsoft.basic.util.FileUtil;
  49. import net.mingsoft.basic.util.StringUtil;
  50. import net.mingsoft.cms.biz.IArticleBiz;
  51. import net.mingsoft.cms.constant.ModelCode;
  52. import net.mingsoft.cms.constant.e.ColumnTypeEnum;
  53. import net.mingsoft.cms.entity.ArticleEntity;
  54. import net.mingsoft.mdiy.biz.IContentModelBiz;
  55. import net.mingsoft.mdiy.biz.IContentModelFieldBiz;
  56. import net.mingsoft.mdiy.entity.ContentModelEntity;
  57. import net.mingsoft.mdiy.entity.ContentModelFieldEntity;
  58. import net.mingsoft.mdiy.util.DictUtil;
  59. import net.mingsoft.mdiy.util.ParserUtil;
  60. /**
  61. * @ClassName: ArticleAction
  62. * @Description:TODO 文章管理
  63. * @author: 铭飞开发团队
  64. * @date: 2018年1月31日 下午2:51:39
  65. *
  66. * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved.
  67. */
  68. @Controller
  69. @RequestMapping("/${ms.manager.path}/cms/article")
  70. public class ArticleAction extends BaseAction {
  71. /**
  72. * 上传路径
  73. */
  74. @Value("${ms.upload.path}")
  75. private String uploadFloderPath;
  76. /**
  77. * 业务层的注入
  78. */
  79. @Autowired
  80. private IColumnBiz columnBiz;
  81. /**
  82. * 文章管理业务处理层
  83. */
  84. @Autowired
  85. private IArticleBiz articleBiz;
  86. /**
  87. * 字段管理业务层
  88. */
  89. @Autowired
  90. private IContentModelFieldBiz fieldBiz;
  91. /**
  92. * 内容管理业务层
  93. */
  94. @Autowired
  95. private IContentModelBiz contentBiz;
  96. /**
  97. * 判断是否为checkbox类型
  98. */
  99. private static final int checkBox = 11;
  100. /**
  101. * 加载页面显示所有文章信息
  102. *
  103. * @param request
  104. * @return 返回文章页面显示地址
  105. */
  106. @SuppressWarnings("static-access")
  107. @RequestMapping("/index")
  108. public String index(HttpServletRequest request, ModelMap mode, HttpServletResponse response) {
  109. // 获取站点id
  110. int appId = BasicUtil.getAppId();
  111. List<ColumnEntity> list = columnBiz.queryAll(appId, this.getModelCodeId(request, ModelCode.CMS_COLUMN));
  112. request.setAttribute("listColumn", JSONArray.toJSONString(list));
  113. // 返回路径
  114. return "/cms/article/index"; // 这里表示显示/manager/cms/article/article_list.ftl
  115. }
  116. /**
  117. * 返回一个文章列表框架和一些基础数据
  118. * @param article
  119. * @param request
  120. * @param mode
  121. * @param response
  122. * @param categoryId
  123. * @return 返回一个文章列表界面
  124. */
  125. @RequestMapping("/{categoryId}/main")
  126. public String main(@ModelAttribute ArticleEntity article, HttpServletRequest request, ModelMap mode,
  127. HttpServletResponse response, @PathVariable int categoryId) {
  128. String articleType = request.getParameter("articleType");
  129. String isParent = BasicUtil.getString("isParent", "false");
  130. mode.addAttribute("isParent", isParent);
  131. //使用糊涂工具排序使全部属性排在第一个
  132. mode.addAttribute("articleTypeList", DictUtil.list("文章属性"));
  133. mode.addAttribute("articleType", articleType);
  134. mode.addAttribute("categoryId", categoryId);
  135. //返回文章页面显示地址
  136. return "/cms/article/article_main";
  137. }
  138. /**
  139. * 加载页面显示所有文章信息
  140. *
  141. * @param request
  142. * @return 返回文章页面显示数据
  143. */
  144. @RequestMapping("/{categoryId}/list")
  145. public void list(@ModelAttribute ArticleEntity article, HttpServletRequest request, ModelMap mode,
  146. HttpServletResponse response, @PathVariable int categoryId) {
  147. int[] basicCategoryIds = null;
  148. String articleType = article.getArticleType();
  149. if(StringUtils.isEmpty(articleType)){
  150. articleType = BasicUtil.getString("articleTypeStr");
  151. }
  152. if(!StringUtils.isEmpty(articleType) && articleType.equals("a")){
  153. articleType = null;
  154. }
  155. if(categoryId > 0){
  156. basicCategoryIds = columnBiz.queryChildrenCategoryIds(categoryId, BasicUtil.getAppId(),
  157. BasicUtil.getModelCodeId(ModelCode.CMS_COLUMN));
  158. }
  159. int appId = BasicUtil.getAppId();
  160. BasicUtil.startPage();
  161. article.setBasicDisplay(-1);
  162. //查询文章列表
  163. List<ArticleEntity> articleList = articleBiz.query(appId, basicCategoryIds, articleType, null, null, true, null, null, article);
  164. EUListBean _list = new EUListBean(articleList, (int) BasicUtil.endPage(articleList).getTotal());
  165. //将数据以json数据的形式返回
  166. this.outJson(response, net.mingsoft.base.util.JSONArray.toJSONString(_list, new DoubleValueFilter(),new DateValueFilter("yyyy-MM-dd")));
  167. }
  168. /**
  169. * 添加文章页面
  170. *
  171. * @return 保存文章的页面地址
  172. */
  173. @SuppressWarnings("static-access")
  174. @RequestMapping("/add")
  175. public String add(ModelMap mode, HttpServletRequest request) {
  176. int categoryId = BasicUtil.getInt("categoryId", 0);
  177. String categoryTitle = request.getParameter("categoryTitle");
  178. // 文章属性
  179. mode.addAttribute("articleType", DictUtil.list("文章属性"));
  180. // 站点ID
  181. int appId = BasicUtil.getAppId();
  182. List<ColumnEntity> list = columnBiz.queryAll(appId, this.getModelCodeId(request, ModelCode.CMS_COLUMN));
  183. mode.addAttribute("appId", appId);
  184. mode.addAttribute("listColumn", JSONArray.toJSONString(list));
  185. boolean isEditCategory = false; // 新增,不是单篇
  186. int columnType=1;//新增,不是单篇
  187. if(categoryId != 0){
  188. // 获取栏目id
  189. ColumnEntity column = (ColumnEntity) columnBiz.getEntity(categoryId);
  190. columnType = column.getColumnType();
  191. // 判断栏目是否为"",如果是"",就重新赋值
  192. if (StringUtils.isBlank(categoryTitle)) {
  193. categoryTitle = column.getCategoryTitle();
  194. }
  195. // 判断栏目是否是单篇
  196. if (column != null && column.getColumnType() == ColumnTypeEnum.COLUMN_TYPE_COVER.toInt()) {
  197. isEditCategory = true; // 是单页
  198. columnType = column.getColumnType();;
  199. }
  200. }
  201. mode.addAttribute("categoryTitle", categoryTitle);
  202. mode.addAttribute("isEditCategory", isEditCategory); // 新增状态
  203. mode.addAttribute("columnType", columnType);
  204. mode.addAttribute("categoryId", categoryId);
  205. mode.addAttribute("articleImagesUrl", uploadFloderPath+net.mingsoft.base.constant.Const.SEPARATOR
  206. +BasicUtil.getAppId()+net.mingsoft.base.constant.Const.SEPARATOR);
  207. // 添加一个空的article实体
  208. ArticleEntity article = new ArticleEntity();
  209. mode.addAttribute("article", article);
  210. // 返回路径
  211. return "/cms/article/article_form"; // 这里表示显示/manager/cms/article/article_save.ftl
  212. }
  213. /**
  214. * 获取表单信息进行保存
  215. *
  216. * @param article
  217. * 文章对象
  218. */
  219. @RequestMapping("/save")
  220. @RequiresPermissions("article:save")
  221. public void save(@ModelAttribute ArticleEntity article, HttpServletRequest request, HttpServletResponse response) {
  222. // 获取站点id
  223. int appId = BasicUtil.getAppId();
  224. // 验证文章,文章自由排序,栏目id
  225. if (!validateForm(article, response)) {
  226. this.outJson(response, ModelCode.CMS_ARTICLE, false);
  227. }
  228. article.setBasicUpdateTime(new Timestamp(System.currentTimeMillis()));
  229. // 文章类型
  230. String langtyp[] = request.getParameterValues("articleType");
  231. if (langtyp != null) {
  232. StringBuffer sb = new StringBuffer();
  233. for (int j = 0; j < langtyp.length; j++) {
  234. sb.append(langtyp[j] + ",");
  235. }
  236. }
  237. String checkboxType = BasicUtil.getString("checkboxType");
  238. //如果选择一个属性不做排序操作
  239. if(!StringUtils.isEmpty(checkboxType) && checkboxType.length()>2){
  240. // 文章类型排序
  241. article.setArticleType(ArrysUtil.sort(checkboxType, ",")+",");
  242. }else{
  243. article.setArticleType(checkboxType);
  244. }
  245. // 问题:由于上传的图片路径后面可能带有|符合。所以要进行将“|”替换空
  246. // 空值判断
  247. if (!StringUtils.isBlank(article.getBasicThumbnails())) {
  248. article.setBasicThumbnails(article.getBasicThumbnails().replace("|", ""));
  249. }
  250. ColumnEntity column = (ColumnEntity) columnBiz.getEntity(article.getBasicCategoryId());
  251. article.setColumn(column);
  252. // 添加文章所属的站点id
  253. article.setArticleWebId(appId);
  254. // 绑定模块编号
  255. article.setBasicModelId(BasicUtil.getInt("modelId"));
  256. // 保存文章实体
  257. articleBiz.saveBasic(article);
  258. if (column.getColumnType() == ColumnTypeEnum.COLUMN_TYPE_LIST.toInt()) {// 列表
  259. article.setArticleUrl(column.getColumnPath() + File.separator + article.getBasicId() + ParserUtil.HTML_SUFFIX);
  260. } else if (column.getColumnType() == ColumnTypeEnum.COLUMN_TYPE_COVER.toInt()) {// 单篇
  261. article.setArticleUrl(column.getColumnPath() + File.separator + ParserUtil.INDEX+ParserUtil.HTML_SUFFIX);
  262. }
  263. articleBiz.updateBasic(article);
  264. // 判断栏目是否存在新增字段
  265. if (column.getColumnContentModelId() != 0) {
  266. // 保存所有的字段信息
  267. List<BaseEntity> listField = fieldBiz.queryListByCmid(column.getColumnContentModelId());
  268. // 获取内容模型实体
  269. ContentModelEntity contentModel = (ContentModelEntity) contentBiz
  270. .getEntity(column.getColumnContentModelId());
  271. if (contentModel != null) {
  272. // 保存新增字段的信息
  273. Map<String, Object> param = this.checkField(listField, request, article.getBasicId());
  274. fieldBiz.insertBySQL(contentModel.getCmTableName(), param);
  275. }
  276. }
  277. //
  278. if (article.getColumn().getColumnType() == ColumnTypeEnum.COLUMN_TYPE_COVER.toInt()) {
  279. this.outJson(response, ModelCode.CMS_ARTICLE, true, "" + article.getColumn().getCategoryId(), article.getBasicId());
  280. } else {
  281. this.outJson(response, ModelCode.CMS_ARTICLE, true, article.getColumn().getCategoryId()+"", "");
  282. }
  283. }
  284. /**
  285. * 验证表单
  286. *
  287. * @param article
  288. * @param response
  289. * @return 返回Boolean类型 true:通过,false:有错
  290. */
  291. public boolean validateForm(ArticleEntity article, HttpServletResponse response) {
  292. // 对表单数据进行再次验证
  293. // 验证文章标题是否为空
  294. if (StringUtils.isBlank(article.getBasicTitle())) {
  295. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  296. getResString("err.empty", this.getResString("basicTitle")));
  297. return false;
  298. }
  299. // 验证文章所属是否为0
  300. if (article.getBasicCategoryId() == 0) {
  301. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  302. getResString("err.empty", this.getResString("basicCategoryId")));
  303. return false;
  304. }
  305. // 验证文章标题长度,若超过定义长度则截取
  306. if (!StringUtil.checkLength(article.getBasicTitle(), 1, 300)) {
  307. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  308. getResString("err.length", this.getResString("basicTitle"), "1", "300"));
  309. return false;
  310. }
  311. // 验证文章来源长度,若超过定义长度则截取
  312. if (!StringUtils.isBlank(article.getArticleSource())
  313. && !StringUtil.checkLength(article.getArticleSource(), 1, 300)) {
  314. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  315. getResString("err.length", this.getResString("articleSource"), "1", "300"));
  316. return false;
  317. }
  318. // 验证文章作者长度,若超过定义长度则截取
  319. if (!StringUtils.isBlank(article.getArticleAuthor())
  320. && !StringUtil.checkLength(article.getArticleAuthor(), 1, 12)) {
  321. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  322. getResString("err.length", this.getResString("articleAuthor"), "1", "12"));
  323. return false;
  324. }
  325. // 验证文章描述长度,若超过定义长度则截取
  326. if (!StringUtils.isBlank(article.getBasicDescription())
  327. && !StringUtil.checkLength(article.getBasicDescription(), 1, 400)) {
  328. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  329. getResString("err.length", this.getResString("basicDescription"), "1", "400"));
  330. return false;
  331. }
  332. // 验证文章关键字长度,若超过定义长度则截取
  333. if (!StringUtils.isBlank(article.getArticleKeyword())
  334. && !StringUtil.checkLength(article.getArticleKeyword(), 1, 155)) {
  335. this.outJson(response, ModelCode.CMS_ARTICLE, false,
  336. getResString("err.length", this.getResString("articleKeyword"), "1", "155"));
  337. return false;
  338. }
  339. return true;
  340. }
  341. /**
  342. * 更新文章
  343. *
  344. * @param basicId
  345. * 文章id
  346. * @param article
  347. * 文章实体
  348. * @param request
  349. * @param response
  350. */
  351. @RequestMapping("/{basicId}/update")
  352. @RequiresPermissions("article:update")
  353. public void update(@PathVariable int basicId, @ModelAttribute ArticleEntity article, HttpServletRequest request,
  354. HttpServletResponse response) {
  355. // 获取站点id
  356. int appId = BasicUtil.getAppId();
  357. article.setBasicUpdateTime(new Timestamp(System.currentTimeMillis()));
  358. // 文章类型
  359. String checkboxType = BasicUtil.getString("checkboxType");
  360. //如果选择一个属性不做排序操作
  361. if(!StringUtils.isEmpty(checkboxType) && checkboxType.length()>2){
  362. // 文章类型排序
  363. article.setArticleType(ArrysUtil.sort(checkboxType, ",")+",");
  364. }else{
  365. article.setArticleType(checkboxType);
  366. }
  367. // 问题:由于上传的图片路径后面可能带有|符合。所以要进行将“|”替换空
  368. // 空值判断
  369. if (!StringUtils.isBlank(article.getBasicThumbnails())) {
  370. article.setBasicThumbnails(article.getBasicThumbnails().replace("|", ""));
  371. }
  372. // 获取更改前的文章实体
  373. ArticleEntity oldArticle = (ArticleEntity) articleBiz.getEntity(basicId);
  374. // 获取栏目实体
  375. ColumnEntity column = (ColumnEntity) columnBiz.getEntity(article.getBasicCategoryId());
  376. if (oldArticle != null) {
  377. // 获取更改前的文章所属栏目实体
  378. ColumnEntity oldColumn = (ColumnEntity) columnBiz.getEntity(oldArticle.getBasicCategoryId());
  379. // 通过表单类型id判断是否更改了表单类型,如果更改则先删除记录
  380. if (oldColumn.getColumnContentModelId() != column.getColumnContentModelId()) {
  381. // 获取旧的内容模型id
  382. ContentModelEntity contentModel = (ContentModelEntity) contentBiz
  383. .getEntity(oldColumn.getColumnContentModelId());
  384. // 删除旧的内容模型中保存的值
  385. Map<String, Integer> wheres = new HashMap<String, Integer>();
  386. wheres.put("basicId", article.getBasicId());
  387. if (contentModel != null) {
  388. fieldBiz.deleteBySQL(contentModel.getCmTableName(), wheres);
  389. }
  390. // 判断栏目是否存在新增字段
  391. if (column.getColumnContentModelId() != 0) {
  392. // 保存所有的字段信息
  393. List<BaseEntity> listField = fieldBiz.queryListByCmid(column.getColumnContentModelId());
  394. ContentModelEntity newContentModel = (ContentModelEntity) contentBiz
  395. .getEntity(column.getColumnContentModelId());
  396. if (newContentModel != null) {
  397. Map<String, Object> param = this.checkField(listField, request, article.getBasicId());
  398. fieldBiz.insertBySQL(newContentModel.getCmTableName(), param);
  399. }
  400. }
  401. }
  402. }
  403. //判断是否修改了所属栏目
  404. if(oldArticle.getBasicCategoryId()!=article.getBasicCategoryId()){
  405. //拼接栏目路径和文章编号及文件后缀
  406. article.setArticleUrl(column.getColumnPath() + File.separator + article.getBasicId() + ParserUtil.HTML_SUFFIX);
  407. }
  408. // 添加文章所属的站点id
  409. article.setArticleWebId(appId);
  410. // 设置文章所属的栏目实体
  411. article.setColumn(column);
  412. article.setBasicUpdateTime(new Date());
  413. String articleType = request.getParameter("articleTypeJson");
  414. articleBiz.updateBasic(article);
  415. // 判断该文章是否存在新增字段
  416. if (column.getColumnContentModelId() != 0) {
  417. // 保存所有的字段信息
  418. List<BaseEntity> listField = fieldBiz.queryListByCmid(column.getColumnContentModelId());
  419. // // update中的where条件
  420. Map<String, Integer> where = new HashMap<String, Integer>();
  421. // 压入默认的basicId字段
  422. where.put("basicId", article.getBasicId());
  423. // 遍历字段的信息
  424. Map<String, Object> param = this.checkField(listField, request, article.getBasicId());
  425. ContentModelEntity contentModel = (ContentModelEntity) contentBiz
  426. .getEntity(column.getColumnContentModelId());
  427. if (contentModel != null) {
  428. // 遍历所有的字段实体,得到字段名列表信息
  429. List<String> listFieldName = new ArrayList<String>();
  430. listFieldName.add("basicId");
  431. // 查询新增字段的信息
  432. List fieldLists = fieldBiz.queryBySQL(contentModel.getCmTableName(), listFieldName, where);
  433. // 判断新增字段表中是否存在该文章,不存在则保存,否则更新
  434. if (fieldLists == null || fieldLists.size() == 0) {
  435. fieldBiz.insertBySQL(contentModel.getCmTableName(), param);
  436. } else {
  437. fieldBiz.updateBySQL(contentModel.getCmTableName(), param, where);
  438. }
  439. }
  440. }
  441. switch (column.getColumnType()) {
  442. case ColumnEntity.COLUMN_TYPE_COVER:
  443. this.outJson(response, ModelCode.CMS_ARTICLE, true, column.getCategoryId() + "", "");
  444. break;
  445. case ColumnEntity.COLUMN_TYPE_LIST:
  446. this.outJson(response, ModelCode.CMS_ARTICLE, true, column.getCategoryId() + "",
  447. this.redirectBack(request, false));
  448. }
  449. }
  450. /**
  451. * 显示更新内容
  452. *
  453. * @param request
  454. * @return 修改文章的页面地址
  455. */
  456. @RequestMapping("/{id}/edit")
  457. public String edit(@PathVariable int id, ModelMap model, HttpServletRequest request) {
  458. // 如果_categoryId大于0表示是编辑封面栏目,应该先查询分类下面的唯一一篇文章
  459. String categoryTitle = request.getParameter("categoryTitle");
  460. // 板块id
  461. int categoryId = BasicUtil.getInt("categoryId", 0);
  462. ArticleEntity articleEntity = null;
  463. int appId = BasicUtil.getAppId();
  464. model.addAttribute("appId", appId);
  465. model.addAttribute("articleImagesUrl", "/upload/"+BasicUtil.getAppId()+"/");
  466. if (categoryId > 0) { // 分类获取文章
  467. articleEntity = articleBiz.getByCategoryId(categoryId);
  468. ColumnEntity column = articleEntity.getColumn();
  469. int columnType = column.getColumnType();
  470. model.addAttribute("article", articleEntity);
  471. // 文章属性
  472. model.addAttribute("articleType", DictUtil.list("文章属性"));
  473. model.addAttribute("categoryTitle", categoryTitle);
  474. model.addAttribute("categoryId", categoryId);// 编辑封面
  475. model.addAttribute("isEditCategory", true);// 编辑封面
  476. model.addAttribute("columnType", columnType);
  477. return "/cms/article/article_form";
  478. } else if (id > 0) { // 文章id获取
  479. // 允许编辑文章时更改分类
  480. List<ColumnEntity> list = columnBiz.queryAll(appId, this.getModelCodeId(request, ModelCode.CMS_COLUMN));
  481. @SuppressWarnings("static-access")
  482. String listJsonString = JSONArray.toJSONString(list);
  483. request.setAttribute("listColumn", listJsonString);
  484. // 文章属性
  485. model.addAttribute("articleType", DictUtil.list("文章属性"));
  486. articleEntity = (ArticleEntity) articleBiz.getEntity(id);
  487. model.addAttribute("article", articleEntity);
  488. // 判断是否是封面类型的栏目,如果是封面类型的栏目有些信息需要屏蔽,例如分类
  489. ColumnEntity column = articleEntity.getColumn();
  490. int columnType = column.getColumnType();
  491. if (column.getColumnType() == ColumnEntity.ColumnTypeEnum.COLUMN_TYPE_COVER.toInt()) {
  492. model.addAttribute("categoryTitle", categoryTitle);
  493. model.addAttribute("categoryId", column.getCategoryId());// 编辑封面
  494. model.addAttribute("isEditCategory", true);// 编辑封面
  495. } else {
  496. model.addAttribute("categoryTitle", articleEntity.getColumn().getCategoryTitle());
  497. model.addAttribute("isEditCategory", false);// 编辑文章
  498. }
  499. model.addAttribute("columnType", columnType);
  500. model.addAttribute("categoryId", column.getCategoryId());// 编辑封面
  501. return "/cms/article/article_form";
  502. } else {// 非法
  503. // return "/cms/article/article_form");
  504. return this.redirectBack(request, true);
  505. }
  506. }
  507. /**
  508. * 删除文章
  509. *
  510. * @param request
  511. * @param response
  512. * @return
  513. */
  514. @RequestMapping("/delete")
  515. @RequiresPermissions("article:del")
  516. public void delete(@RequestBody List<ArticleEntity> articles, HttpServletRequest request, HttpServletResponse response) {
  517. int appId = BasicUtil.getAppId();
  518. int[] ids = new int[articles.size()];
  519. //循环获取id数据
  520. for(int i=0;i<articles.size();i++){
  521. ids[i] = articles.get(i).getArticleID();
  522. }
  523. if (ids.length == 0 || ids == null) {
  524. this.outJson(response, ModelCode.CMS_ARTICLE, false, "", this.redirectBack(request, false));
  525. return;
  526. }
  527. // 删除多个帖子
  528. articleBiz.deleteBasic(ids);
  529. FileUtil.del(articles);
  530. this.outJson(response, ModelCode.CMS_ARTICLE, true, "", this.redirectBack(request, false));
  531. }
  532. /**
  533. * 遍历出所有文章新增字段的信息
  534. *
  535. * @param listField
  536. * :字段列表
  537. * @param request
  538. * @param articleId
  539. * 文章id
  540. * @return 字段信息
  541. */
  542. private Map checkField(List<BaseEntity> listField, HttpServletRequest request, int articleId) {
  543. Map<String, Object> mapParams = new HashMap();
  544. // 压入默认的basicId字段
  545. mapParams.put("basicId", articleId);
  546. // 遍历字段名
  547. for (int i = 0; i < listField.size(); i++) {
  548. ContentModelFieldEntity field = (ContentModelFieldEntity) listField.get(i);
  549. String fieldName = field.getFieldFieldName();
  550. // 判断字段类型是否为checkbox类型
  551. if (field.getFieldType() == checkBox) {
  552. String langtyp[] = request.getParameterValues(field.getFieldFieldName());
  553. if (langtyp != null) {
  554. StringBuffer sb = new StringBuffer();
  555. for (int j = 0; j < langtyp.length; j++) {
  556. sb.append(langtyp[j] + ",");
  557. }
  558. mapParams.put(field.getFieldFieldName(), sb.toString());
  559. } else {
  560. mapParams.put(field.getFieldFieldName(), langtyp);
  561. }
  562. } else {
  563. if (StringUtils.isBlank(request.getParameter(field.getFieldFieldName()))) {
  564. mapParams.put(field.getFieldFieldName(), null);
  565. } else {
  566. mapParams.put(field.getFieldFieldName(), request.getParameter(field.getFieldFieldName()));
  567. }
  568. }
  569. }
  570. return mapParams;
  571. }
  572. /**
  573. * 查询单页栏目是否绑定了文章
  574. *
  575. * @param article
  576. * 文章对象
  577. */
  578. @RequestMapping("/{id}/queryColumnArticle")
  579. public void queryColumnArticle(@PathVariable int id, HttpServletResponse response) {
  580. List articls = articleBiz.queryListByColumnId(id);
  581. if (articls == null || articls.size() == 0) {
  582. this.outJson(response, ModelCode.CMS_ARTICLE, true, null);
  583. } else {
  584. this.outJson(response, ModelCode.CMS_ARTICLE, false, null);
  585. }
  586. }
  587. }