|
@@ -1,21 +1,18 @@
|
|
|
package com.ruoyi.uniapp.controller;
|
|
package com.ruoyi.uniapp.controller;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.Date;
|
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
-import com.github.pagehelper.PageHelper;
|
|
|
|
|
import com.ruoyi.common.core.web.page.TableDataInfo;
|
|
import com.ruoyi.common.core.web.page.TableDataInfo;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
|
|
import com.ruoyi.common.core.web.controller.BaseController;
|
|
import com.ruoyi.common.core.web.controller.BaseController;
|
|
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
-import com.ruoyi.common.security.utils.SecurityUtils;
|
|
|
|
|
-import com.ruoyi.uniapp.domain.KnowledgeArticle;
|
|
|
|
|
-import com.ruoyi.uniapp.domain.KnowledgeImage;
|
|
|
|
|
-import com.ruoyi.uniapp.domain.vo.KnowledgeArticleVO;
|
|
|
|
|
-import com.ruoyi.uniapp.service.IKnowledgeService;
|
|
|
|
|
|
|
+import com.ruoyi.common.log.annotation.Log;
|
|
|
|
|
+import com.ruoyi.common.log.enums.BusinessType;
|
|
|
|
|
+import com.ruoyi.uniapp.domain.KnowledgeContent;
|
|
|
|
|
+import com.ruoyi.uniapp.service.IKnowledgeContentService;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 知识内容控制器
|
|
* 知识内容控制器
|
|
@@ -25,217 +22,145 @@ import com.ruoyi.uniapp.service.IKnowledgeService;
|
|
|
public class KnowledgeController extends BaseController {
|
|
public class KnowledgeController extends BaseController {
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private IKnowledgeService knowledgeService;
|
|
|
|
|
-
|
|
|
|
|
|
|
+ private IKnowledgeContentService knowledgeContentService;
|
|
|
/**
|
|
/**
|
|
|
- * 获取农技知识列表
|
|
|
|
|
|
|
+ * 查询知识内容列表
|
|
|
*/
|
|
*/
|
|
|
- @GetMapping("/tech")
|
|
|
|
|
- public TableDataInfo getTechList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
|
|
|
|
|
- @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
|
|
|
|
|
- Integer userId = null;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- // 未登录或token无效
|
|
|
|
|
|
|
+ @GetMapping("/list")
|
|
|
|
|
+ public AjaxResult list(Integer page, Integer pageSize, String category)
|
|
|
|
|
+ {
|
|
|
|
|
+ startPage();
|
|
|
|
|
+
|
|
|
|
|
+ KnowledgeContent knowledgeContent = new KnowledgeContent();
|
|
|
|
|
+ if (category != null) {
|
|
|
|
|
+ knowledgeContent.setContentCategory(category);
|
|
|
}
|
|
}
|
|
|
- PageHelper.startPage(ObjectUtil.defaultIfNull(pageNum, 1), ObjectUtil.defaultIfNull(pageSize, 10));
|
|
|
|
|
-
|
|
|
|
|
- List<KnowledgeArticleVO> list = knowledgeService.getTechList(pageNum, pageSize, userId);
|
|
|
|
|
-
|
|
|
|
|
- return getDataTable(list);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ List<KnowledgeContent> list = knowledgeContentService.selectKnowledgeContentList(knowledgeContent);
|
|
|
|
|
+ TableDataInfo dataTable = getDataTable(list);
|
|
|
|
|
+
|
|
|
|
|
+ // 构造与前端一致的数据格式
|
|
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
|
|
+ data.put("list", dataTable.getRows());
|
|
|
|
|
+ data.put("total", dataTable.getTotal());
|
|
|
|
|
+
|
|
|
|
|
+ return AjaxResult.success(data);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * 获取政策解读列表
|
|
|
|
|
|
|
+ * 获取知识内容详情
|
|
|
*/
|
|
*/
|
|
|
- @GetMapping("/policy")
|
|
|
|
|
- public TableDataInfo getPolicyList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
|
|
|
|
|
- @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
|
|
|
|
|
- Integer userId = null;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- // 未登录或token无效
|
|
|
|
|
- }
|
|
|
|
|
- PageHelper.startPage(ObjectUtil.defaultIfNull(pageNum, 1), ObjectUtil.defaultIfNull(pageSize, 10));
|
|
|
|
|
-
|
|
|
|
|
- List<KnowledgeArticleVO> list = knowledgeService.getPolicyList(pageNum, pageSize, userId);
|
|
|
|
|
- return getDataTable(list);
|
|
|
|
|
|
|
+ @GetMapping(value = "/detail")
|
|
|
|
|
+ public AjaxResult getInfo(Integer id, String type)
|
|
|
|
|
+ {
|
|
|
|
|
+ return AjaxResult.success(knowledgeContentService.selectKnowledgeContentById(id));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 获取知识详情
|
|
|
|
|
|
|
+ * 更新阅读量
|
|
|
*/
|
|
*/
|
|
|
- @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
- public AjaxResult getArticleDetail(@PathVariable("id") Integer id) {
|
|
|
|
|
- Integer userId = null;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- // 未登录或token无效
|
|
|
|
|
|
|
+ @PostMapping("/view")
|
|
|
|
|
+ public AjaxResult view(@RequestBody Map<String, Object> params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Integer id = (Integer) params.get("id");
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ return AjaxResult.error("参数错误");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- KnowledgeArticleVO article = knowledgeService.getArticleDetail(id, userId);
|
|
|
|
|
- if (article == null) {
|
|
|
|
|
- return AjaxResult.error("文章不存在");
|
|
|
|
|
- }
|
|
|
|
|
- return AjaxResult.success(article);
|
|
|
|
|
|
|
+ knowledgeContentService.incrementViewCount(id);
|
|
|
|
|
+ return AjaxResult.success();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 获取文章相关图片
|
|
|
|
|
|
|
+ * 点赞内容
|
|
|
*/
|
|
*/
|
|
|
- @GetMapping("/images/{articleId}")
|
|
|
|
|
- public AjaxResult getArticleImages(@PathVariable("articleId") Integer articleId) {
|
|
|
|
|
- List<KnowledgeImage> images = knowledgeService.getArticleImages(articleId);
|
|
|
|
|
- return AjaxResult.success(images);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取轮播图
|
|
|
|
|
- */
|
|
|
|
|
- @GetMapping("/carousel")
|
|
|
|
|
- public AjaxResult getCarouselImages() {
|
|
|
|
|
- List<KnowledgeImage> carouselImages = knowledgeService.getCarouselImages();
|
|
|
|
|
- return AjaxResult.success(carouselImages);
|
|
|
|
|
|
|
+ @PostMapping("/like")
|
|
|
|
|
+ public AjaxResult like(@RequestBody Map<String, Object> params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Integer id = (Integer) params.get("id");
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ return AjaxResult.error("参数错误");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 实际业务中可能需要记录用户的点赞状态
|
|
|
|
|
+ return AjaxResult.success();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 创建文章
|
|
|
|
|
|
|
+ * 取消点赞
|
|
|
*/
|
|
*/
|
|
|
- @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
- public AjaxResult createArticle(@RequestBody KnowledgeArticle article) {
|
|
|
|
|
- try {
|
|
|
|
|
- // 设置文章的创建者
|
|
|
|
|
- Long userId = SecurityUtils.getUserId();
|
|
|
|
|
-
|
|
|
|
|
- // 设置发布日期和状态
|
|
|
|
|
- article.setPublishDate(new Date());
|
|
|
|
|
- if (article.getStatus() == null) {
|
|
|
|
|
- article.setStatus(1); // 默认已发布状态
|
|
|
|
|
- }
|
|
|
|
|
- if (article.getViewCount() == null) {
|
|
|
|
|
- article.setViewCount(0);
|
|
|
|
|
- }
|
|
|
|
|
- if (article.getLikeCount() == null) {
|
|
|
|
|
- article.setLikeCount(0);
|
|
|
|
|
- }
|
|
|
|
|
- if (article.getIsRecommend() == null) {
|
|
|
|
|
- article.setIsRecommend(0); // 默认不推荐
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 确保HTML内容不被转义
|
|
|
|
|
- // 使用原始内容,不做任何处理
|
|
|
|
|
-
|
|
|
|
|
- // 保存文章
|
|
|
|
|
- int result = knowledgeService.insertKnowledgeArticle(article);
|
|
|
|
|
-
|
|
|
|
|
- if (result > 0) {
|
|
|
|
|
- return AjaxResult.success("创建成功", article.getId());
|
|
|
|
|
- } else {
|
|
|
|
|
- return AjaxResult.error("创建失败");
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- logger.error("创建文章失败: ", e);
|
|
|
|
|
- return AjaxResult.error("创建文章失败: " + e.getMessage());
|
|
|
|
|
|
|
+ @PostMapping("/unlike")
|
|
|
|
|
+ public AjaxResult unlike(@RequestBody Map<String, Object> params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Integer id = (Integer) params.get("id");
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ return AjaxResult.error("参数错误");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 实际业务中可能需要删除用户的点赞记录
|
|
|
|
|
+ return AjaxResult.success();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 更新文章
|
|
|
|
|
|
|
+ * 收藏内容
|
|
|
*/
|
|
*/
|
|
|
- @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
- public AjaxResult updateArticle(@RequestBody KnowledgeArticle article) {
|
|
|
|
|
- try {
|
|
|
|
|
- if (article.getId() == null) {
|
|
|
|
|
- return AjaxResult.error("文章ID不能为空");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 验证文章是否存在
|
|
|
|
|
- KnowledgeArticle existingArticle = knowledgeService.selectKnowledgeArticleById(article.getId());
|
|
|
|
|
- if (existingArticle == null) {
|
|
|
|
|
- return AjaxResult.error("文章不存在");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 确保HTML内容不被转义
|
|
|
|
|
- // 使用原始内容,不做任何处理
|
|
|
|
|
-
|
|
|
|
|
- // 更新文章
|
|
|
|
|
- int result = knowledgeService.updateKnowledgeArticle(article);
|
|
|
|
|
-
|
|
|
|
|
- if (result > 0) {
|
|
|
|
|
- return AjaxResult.success("更新成功");
|
|
|
|
|
- } else {
|
|
|
|
|
- return AjaxResult.error("更新失败");
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- logger.error("更新文章失败: ", e);
|
|
|
|
|
- return AjaxResult.error("更新文章失败: " + e.getMessage());
|
|
|
|
|
|
|
+ @PostMapping("/collect")
|
|
|
|
|
+ public AjaxResult collect(@RequestBody Map<String, Object> params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Integer id = (Integer) params.get("id");
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ return AjaxResult.error("参数错误");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 实际业务中可能需要记录用户的收藏状态
|
|
|
|
|
+ return AjaxResult.success();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 点赞文章
|
|
|
|
|
|
|
+ * 取消收藏
|
|
|
*/
|
|
*/
|
|
|
- @PostMapping("/like/{id}")
|
|
|
|
|
- public AjaxResult likeArticle(@PathVariable("id") Integer id) {
|
|
|
|
|
- Integer userId;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- return AjaxResult.error("请先登录");
|
|
|
|
|
|
|
+ @PostMapping("/uncollect")
|
|
|
|
|
+ public AjaxResult uncollect(@RequestBody Map<String, Object> params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Integer id = (Integer) params.get("id");
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ return AjaxResult.error("参数错误");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- int result = knowledgeService.likeArticle(id, userId, true);
|
|
|
|
|
- return AjaxResult.success("点赞成功", result);
|
|
|
|
|
|
|
+ // 实际业务中可能需要删除用户的收藏记录
|
|
|
|
|
+ return AjaxResult.success();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * 取消点赞
|
|
|
|
|
|
|
+ * 新增知识内容
|
|
|
*/
|
|
*/
|
|
|
- @DeleteMapping("/like/{id}")
|
|
|
|
|
- public AjaxResult cancelLikeArticle(@PathVariable("id") Integer id) {
|
|
|
|
|
- Integer userId;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- return AjaxResult.error("请先登录");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int result = knowledgeService.likeArticle(id, userId, false);
|
|
|
|
|
- return AjaxResult.success("取消点赞成功", result);
|
|
|
|
|
|
|
+// @PreAuthorize(hasPermi = "uniapp:knowledge:add")
|
|
|
|
|
+ @Log(title = "知识内容", businessType = BusinessType.INSERT)
|
|
|
|
|
+ @PostMapping
|
|
|
|
|
+ public AjaxResult add(@RequestBody KnowledgeContent knowledgeContent)
|
|
|
|
|
+ {
|
|
|
|
|
+ return toAjax(knowledgeContentService.insertKnowledgeContent(knowledgeContent));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * 收藏文章
|
|
|
|
|
|
|
+ * 修改知识内容
|
|
|
*/
|
|
*/
|
|
|
- @PostMapping("/favorite/{id}")
|
|
|
|
|
- public AjaxResult favoriteArticle(@PathVariable("id") Integer id) {
|
|
|
|
|
- Integer userId;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- return AjaxResult.error("请先登录");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int result = knowledgeService.favoriteArticle(id, userId, true);
|
|
|
|
|
- return AjaxResult.success("收藏成功", result);
|
|
|
|
|
|
|
+// @PreAuthorize(hasPermi = "uniapp:knowledge:edit")
|
|
|
|
|
+ @Log(title = "知识内容", businessType = BusinessType.UPDATE)
|
|
|
|
|
+ @PostMapping("/update")
|
|
|
|
|
+ public AjaxResult edit(@RequestBody KnowledgeContent knowledgeContent)
|
|
|
|
|
+ {
|
|
|
|
|
+ return toAjax(knowledgeContentService.updateKnowledgeContent(knowledgeContent));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * 取消收藏
|
|
|
|
|
|
|
+ * 删除知识内容
|
|
|
*/
|
|
*/
|
|
|
- @DeleteMapping("/favorite/{id}")
|
|
|
|
|
- public AjaxResult cancelFavoriteArticle(@PathVariable("id") Integer id) {
|
|
|
|
|
- Integer userId;
|
|
|
|
|
- try {
|
|
|
|
|
- userId = SecurityUtils.getUserId().intValue();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- return AjaxResult.error("请先登录");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int result = knowledgeService.favoriteArticle(id, userId, false);
|
|
|
|
|
- return AjaxResult.success("取消收藏成功", result);
|
|
|
|
|
|
|
+// @PreAuthorize(hasPermi = "uniapp:knowledge:remove")
|
|
|
|
|
+ @Log(title = "知识内容", businessType = BusinessType.DELETE)
|
|
|
|
|
+ @PostMapping("/delete/{ids}")
|
|
|
|
|
+ public AjaxResult remove(@PathVariable Integer[] ids)
|
|
|
|
|
+ {
|
|
|
|
|
+ return toAjax(knowledgeContentService.deleteKnowledgeContentByIds(ids));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|