package com.ruoyi.web.controller.api; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import java.util.Date; import java.util.List; import java.util.Arrays; import com.ruoyi.app.domain.TbNews; import com.ruoyi.app.service.ITbNewsService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import com.ruoyi.common.utils.StringUtils; import lombok.RequiredArgsConstructor; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 资讯 * * @author lsw * @date 2020-10-12 */ @RequiredArgsConstructor(onConstructor_ = @Autowired) @RestController @RequestMapping("/system/news") public class TbNewsController extends BaseController { private final ITbNewsService iTbNewsService; /** * 查询资讯列表 */ @PreAuthorize("@ss.hasPermi('system:news:list')") @GetMapping("/list") public TableDataInfo list(TbNews tbNews) { startPage(); LambdaQueryWrapper lqw = new LambdaQueryWrapper(); if (StringUtils.isNotBlank(tbNews.getTitle())) { lqw.like(TbNews::getTitle, tbNews.getTitle()); } if (tbNews.getState() != null) { lqw.eq(TbNews::getState, tbNews.getState()); } List list = iTbNewsService.list(lqw); return getDataTable(list); } /** * 获取资讯详细信息 */ @PreAuthorize("@ss.hasPermi('system:news:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return AjaxResult.success(iTbNewsService.getById(id)); } /** * 外部获取资讯详细信息(用于app分享资讯) */ @GetMapping(value = "/detail/{id}") public AjaxResult detail(@PathVariable("id") Long id) { TbNews news = iTbNewsService.getById(id); if (news != null && news.getState() == 0) { return AjaxResult.success(iTbNewsService.getById(id)); } else { return AjaxResult.success(); } } /** * 新增资讯 */ @PreAuthorize("@ss.hasPermi('system:news:add')") @Log(title = "资讯", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody TbNews tbNews) { tbNews.setCreateTime(new Date()); tbNews.setDescs(StripHT(tbNews.getContents())); return toAjax(iTbNewsService.save(tbNews) ? 1 : 0); } /** * 修改资讯 */ @PreAuthorize("@ss.hasPermi('system:news:edit')") @Log(title = "资讯", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody TbNews tbNews) { tbNews.setDescs(StripHT(tbNews.getContents())); tbNews.setUpdateTime(new Date()); return toAjax(iTbNewsService.updateById(tbNews) ? 1 : 0); } /** * 删除资讯 */ @PreAuthorize("@ss.hasPermi('system:news:remove')") @Log(title = "资讯", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(iTbNewsService.removeByIds(Arrays.asList(ids)) ? 1 : 0); } //从html中提取纯文本作为摘要 private String StripHT(String strHtml) { String txtcontent = strHtml.replaceAll("]+>", ""); //剔出的标签 txtcontent = txtcontent.replaceAll("\\s*|\t|\r|\n", "");//去除字符串中的空格,回车,换行符,制表符 return txtcontent.substring(0, txtcontent.length() > 80 ? 80 : txtcontent.length()); } }