TbNewsController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.ruoyi.web.controller.api;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Arrays;
  6. import com.ruoyi.app.domain.TbNews;
  7. import com.ruoyi.app.service.ITbNewsService;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import com.ruoyi.common.utils.StringUtils;
  11. import lombok.RequiredArgsConstructor;
  12. import org.springframework.security.access.prepost.PreAuthorize;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.PutMapping;
  17. import org.springframework.web.bind.annotation.DeleteMapping;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import com.ruoyi.common.annotation.Log;
  23. import com.ruoyi.common.core.controller.BaseController;
  24. import com.ruoyi.common.core.domain.AjaxResult;
  25. import com.ruoyi.common.enums.BusinessType;
  26. import com.ruoyi.common.utils.poi.ExcelUtil;
  27. import com.ruoyi.common.core.page.TableDataInfo;
  28. /**
  29. * 资讯
  30. *
  31. * @author lsw
  32. * @date 2020-10-12
  33. */
  34. @RequiredArgsConstructor(onConstructor_ = @Autowired)
  35. @RestController
  36. @RequestMapping("/system/news")
  37. public class TbNewsController extends BaseController {
  38. private final ITbNewsService iTbNewsService;
  39. /**
  40. * 查询资讯列表
  41. */
  42. @PreAuthorize("@ss.hasPermi('system:news:list')")
  43. @GetMapping("/list")
  44. public TableDataInfo list(TbNews tbNews) {
  45. startPage();
  46. LambdaQueryWrapper<TbNews> lqw = new LambdaQueryWrapper<TbNews>();
  47. if (StringUtils.isNotBlank(tbNews.getTitle())) {
  48. lqw.like(TbNews::getTitle, tbNews.getTitle());
  49. }
  50. if (tbNews.getState() != null) {
  51. lqw.eq(TbNews::getState, tbNews.getState());
  52. }
  53. List<TbNews> list = iTbNewsService.list(lqw);
  54. return getDataTable(list);
  55. }
  56. /**
  57. * 获取资讯详细信息
  58. */
  59. @PreAuthorize("@ss.hasPermi('system:news:query')")
  60. @GetMapping(value = "/{id}")
  61. public AjaxResult getInfo(@PathVariable("id") Long id) {
  62. return AjaxResult.success(iTbNewsService.getById(id));
  63. }
  64. /**
  65. * 外部获取资讯详细信息(用于app分享资讯)
  66. */
  67. @GetMapping(value = "/detail/{id}")
  68. public AjaxResult detail(@PathVariable("id") Long id) {
  69. TbNews news = iTbNewsService.getById(id);
  70. if (news != null && news.getState() == 0) {
  71. return AjaxResult.success(iTbNewsService.getById(id));
  72. } else {
  73. return AjaxResult.success();
  74. }
  75. }
  76. /**
  77. * 新增资讯
  78. */
  79. @PreAuthorize("@ss.hasPermi('system:news:add')")
  80. @Log(title = "资讯", businessType = BusinessType.INSERT)
  81. @PostMapping
  82. public AjaxResult add(@RequestBody TbNews tbNews) {
  83. tbNews.setCreateTime(new Date());
  84. tbNews.setDescs(StripHT(tbNews.getContents()));
  85. return toAjax(iTbNewsService.save(tbNews) ? 1 : 0);
  86. }
  87. /**
  88. * 修改资讯
  89. */
  90. @PreAuthorize("@ss.hasPermi('system:news:edit')")
  91. @Log(title = "资讯", businessType = BusinessType.UPDATE)
  92. @PutMapping
  93. public AjaxResult edit(@RequestBody TbNews tbNews) {
  94. tbNews.setDescs(StripHT(tbNews.getContents()));
  95. tbNews.setUpdateTime(new Date());
  96. return toAjax(iTbNewsService.updateById(tbNews) ? 1 : 0);
  97. }
  98. /**
  99. * 删除资讯
  100. */
  101. @PreAuthorize("@ss.hasPermi('system:news:remove')")
  102. @Log(title = "资讯", businessType = BusinessType.DELETE)
  103. @DeleteMapping("/{ids}")
  104. public AjaxResult remove(@PathVariable Long[] ids) {
  105. return toAjax(iTbNewsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
  106. }
  107. //从html中提取纯文本作为摘要
  108. private String StripHT(String strHtml) {
  109. String txtcontent = strHtml.replaceAll("</?[^>]+>", ""); //剔出<html>的标签
  110. txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");//去除字符串中的空格,回车,换行符,制表符
  111. return txtcontent.substring(0, txtcontent.length() > 80 ? 80 : txtcontent.length());
  112. }
  113. }