ContentAction.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.StrUtil;
  22. import io.swagger.v3.oas.annotations.Operation;
  23. import io.swagger.v3.oas.annotations.Parameter;
  24. import io.swagger.v3.oas.annotations.Parameters;
  25. import io.swagger.v3.oas.annotations.enums.ParameterIn;
  26. import io.swagger.v3.oas.annotations.tags.Tag;
  27. import jakarta.servlet.http.HttpServletRequest;
  28. import jakarta.servlet.http.HttpServletResponse;
  29. import net.mingsoft.base.entity.ResultData;
  30. import net.mingsoft.basic.bean.EUListBean;
  31. import net.mingsoft.basic.util.BasicUtil;
  32. import net.mingsoft.cms.bean.CategoryBean;
  33. import net.mingsoft.cms.bean.ContentBean;
  34. import net.mingsoft.cms.biz.ICategoryBiz;
  35. import net.mingsoft.cms.biz.IContentBiz;
  36. import net.mingsoft.cms.biz.IHistoryLogBiz;
  37. import net.mingsoft.cms.constant.e.CategoryTypeEnum;
  38. import net.mingsoft.cms.entity.CategoryEntity;
  39. import net.mingsoft.cms.entity.ContentEntity;
  40. import net.mingsoft.cms.entity.HistoryLogEntity;
  41. import net.mingsoft.mdiy.bean.PageBean;
  42. import net.mingsoft.mdiy.biz.IModelBiz;
  43. import net.mingsoft.mdiy.entity.ModelEntity;
  44. import net.mingsoft.mdiy.util.ParserUtil;
  45. import org.apache.commons.lang3.StringUtils;
  46. import org.springframework.beans.factory.annotation.Autowired;
  47. import org.springframework.stereotype.Controller;
  48. import org.springframework.web.bind.annotation.*;
  49. import java.util.*;
  50. /**
  51. * 文章管理控制层
  52. * @author 铭飞开发团队
  53. * 创建日期:2019-11-28 15:12:32<br/>
  54. * 历史修订:<br/>
  55. */
  56. @Tag(name="前端-内容模块接口")
  57. @Controller("WebcmsContentAction")
  58. @RequestMapping("/cms/content")
  59. public class ContentAction extends net.mingsoft.cms.action.BaseAction{
  60. /**
  61. * 注入文章业务层
  62. */
  63. @Autowired
  64. private IContentBiz contentBiz;
  65. @Autowired
  66. private ICategoryBiz categoryBiz;
  67. @Autowired
  68. private IModelBiz modelBiz;
  69. @Autowired
  70. private IHistoryLogBiz historyLogBiz;
  71. /**
  72. * 查询文章列表接口
  73. * @return
  74. */
  75. @Operation(summary = "查询文章列表接口")
  76. @Parameters({
  77. @Parameter(name = "typeid", description = "所属栏目", required =false, in= ParameterIn.QUERY),
  78. @Parameter(name = "pageNo", description = "页码", required =false, in= ParameterIn.QUERY),
  79. @Parameter(name = "size", description = "一页显示数量", required =false, in= ParameterIn.QUERY),
  80. @Parameter(name = "orderby", description = "排序", required =false, in= ParameterIn.QUERY),
  81. })
  82. @RequestMapping(value = "/list",method = {RequestMethod.GET,RequestMethod.POST})
  83. @ResponseBody
  84. public ResultData list(HttpServletResponse response, HttpServletRequest request) {
  85. //会将请求参数全部转换map
  86. Map map = BasicUtil.assemblyRequestMap();
  87. String typeid = (String) map.get("typeid");
  88. if (StrUtil.isBlank(typeid)){
  89. typeid = (String) map.get("categoryId");
  90. }
  91. ContentBean content = new ContentBean();
  92. if (StrUtil.isNotBlank(typeid)){
  93. content.setCategoryId(typeid);
  94. }
  95. content.setCategoryType(CategoryTypeEnum.LIST.toString());
  96. content.setContentDisplay("0");
  97. List<CategoryBean> articleList = contentBiz.queryIdsByCategoryIdForParser(content);
  98. PageBean page = new PageBean();
  99. List filedStr = new ArrayList<>();
  100. page.setPageNo(BasicUtil.getInt("pageNo",1));
  101. page.setSize(BasicUtil.getInt("size",10));
  102. map.put("ispaging","true");
  103. map.putIfAbsent("size",page.getSize());
  104. if (BasicUtil.getWebsiteApp() != null) {
  105. map.put("appid", BasicUtil.getWebsiteApp().getId());
  106. }
  107. map.put(ParserUtil.PAGE, page);
  108. if (typeid != null) {
  109. CategoryEntity column = categoryBiz.getById(typeid);
  110. // 获取表单类型的id
  111. if (column != null && ObjectUtil.isNotNull(column.getMdiyModelId())) {
  112. ModelEntity contentModel = (ModelEntity) modelBiz.getById(column.getMdiyModelId());
  113. if (contentModel != null) {
  114. // 保存自定义模型的数据
  115. Map<String, String> fieldMap = contentModel.getFieldMap();
  116. for (String s : fieldMap.keySet()) {
  117. filedStr.add(fieldMap.get(s));
  118. }
  119. // 设置自定义模型表名,方便解析的时候关联表查询
  120. map.put(ParserUtil.TABLE_NAME, contentModel.getModelTableName());
  121. }
  122. }
  123. // 设置栏目,方便解析的时候关联表查询
  124. map.put(ParserUtil.COLUMN, column);
  125. }
  126. //实际上list是需要参数,例如分页、栏目分类、属性等待,具体看标签arclist对应的参数
  127. List contentList = contentBiz.list(map);
  128. return ResultData.build().success(new EUListBean(contentList,articleList.size()));
  129. }
  130. /**
  131. * 获取文章列表接口
  132. * @param content 文章
  133. * @return
  134. */
  135. @Operation(summary = "获取文章列表接口")
  136. @Parameter(name = "id", description = "编号", required = true, in = ParameterIn.QUERY)
  137. @GetMapping("/get")
  138. @ResponseBody
  139. public ResultData get(@ModelAttribute @Parameter(hidden = true) ContentEntity content){
  140. if(content.getId()==null) {
  141. return ResultData.build().error(getResString("err.empty",this.getResString("id")));
  142. }
  143. content.setSqlWhere("");
  144. ContentEntity _content = contentBiz.getById(content.getId());
  145. if(_content==null) {
  146. return ResultData.build().error(getResString("err.error",this.getResString("id")));
  147. }
  148. // 获取栏目数据
  149. CategoryEntity categoryEntity = categoryBiz.getById(_content.getCategoryId());
  150. ModelEntity modelEntity = new ModelEntity();
  151. if (categoryEntity != null) {
  152. modelEntity = modelBiz.getById(categoryEntity.getMdiyModelId());
  153. }
  154. // 组织map查询数据
  155. Map<String, Object> map = new HashMap<>();
  156. map.put("dataid", content.getId());
  157. // 如果没有自定义模型, 不设置自定义模型
  158. if (modelEntity != null && StringUtils.isNotBlank(modelEntity.getModelTableName())) {
  159. map.put("tableName", modelEntity.getModelTableName());
  160. }
  161. Map contentMap = contentBiz.get(map);
  162. return ResultData.build().success(contentMap);
  163. }
  164. /**
  165. * 查看文章点击数
  166. * @param contentId 文章编号
  167. * @return
  168. */
  169. @Operation(summary = "查看文章点击数")
  170. @Parameter(name = "contentId", description = "文章编号", required = true, in = ParameterIn.PATH)
  171. // 由于适配增加了对clob序列化处理,此处需要指定响应头
  172. @GetMapping(value = "/{contentId}/hit", produces = "application/javascript")
  173. @ResponseBody
  174. public String hit(@PathVariable @Parameter(hidden = true) String contentId) {
  175. if(StringUtils.isEmpty(contentId)){
  176. return "document.write(0)";
  177. }
  178. //获取ip
  179. String ip = BasicUtil.getIp();
  180. //获取端口(移动/web..)
  181. boolean isMobileDevice = BasicUtil.isMobileDevice();
  182. ContentEntity content = contentBiz.getById(contentId);
  183. if(content == null){
  184. return "document.write(0)";
  185. }
  186. //浏览数+1
  187. if(ObjectUtil.isNotEmpty(content.getContentHit())){
  188. content.setContentHit(content.getContentHit()+1);
  189. }else {
  190. content.setContentHit(1);
  191. }
  192. contentBiz.updateById(content);
  193. // cms_history 增加相应记录
  194. HistoryLogEntity entity = new HistoryLogEntity();
  195. entity.setHlIsMobile(isMobileDevice);
  196. entity.setHlIp(ip);
  197. entity.setContentId(content.getId());
  198. entity.setCreateDate(new Date());
  199. historyLogBiz.save(entity);
  200. return "document.write(" + content.getContentHit() + ")";
  201. }
  202. }