From 42a0d891bce881aa7f05ea9e19e5b36fd9c02f50 Mon Sep 17 00:00:00 2001 From: TinyAnts Date: Wed, 7 Sep 2022 18:08:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E7=AB=A0=E6=94=B6?= =?UTF-8?q?=E8=97=8F=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mdd/front/LikeFrontInterceptor.java | 2 + .../com/mdd/front/config/FrontConfig.java | 1 - .../front/controller/ArticleController.java | 47 ++++++++++++-- .../mdd/front/service/IArticleService.java | 21 ++++++- .../service/impl/ArticleServiceImpl.java | 63 ++++++++++++++++++- .../front/service/impl/LoginServiceImpl.java | 4 +- 6 files changed, 125 insertions(+), 13 deletions(-) diff --git a/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java b/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java index 260f4040..39ebcbb4 100644 --- a/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java +++ b/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java @@ -77,6 +77,8 @@ public class LikeFrontInterceptor implements HandlerInterceptor { // 用户信息缓存 Object uid = RedisUtil.get(token); + System.out.println(uid); + System.out.println("哈哈哈哈哈"); Integer userId = Integer.parseInt(uid.toString()); User user = userMapper.selectOne(new QueryWrapper() .select("id,sn,username,nickname,mobile,is_disable,is_delete") diff --git a/server/like-front/src/main/java/com/mdd/front/config/FrontConfig.java b/server/like-front/src/main/java/com/mdd/front/config/FrontConfig.java index 90ce5f74..6ce75792 100644 --- a/server/like-front/src/main/java/com/mdd/front/config/FrontConfig.java +++ b/server/like-front/src/main/java/com/mdd/front/config/FrontConfig.java @@ -23,7 +23,6 @@ public class FrontConfig { "/api/login/forgotPassword", "/api/article/category", - "/api/article/collect", "/api/article/detail", "/api/article/list", }; diff --git a/server/like-front/src/main/java/com/mdd/front/controller/ArticleController.java b/server/like-front/src/main/java/com/mdd/front/controller/ArticleController.java index 454815c5..eb61b220 100644 --- a/server/like-front/src/main/java/com/mdd/front/controller/ArticleController.java +++ b/server/like-front/src/main/java/com/mdd/front/controller/ArticleController.java @@ -1,5 +1,6 @@ package com.mdd.front.controller; +import com.baomidou.mybatisplus.core.toolkit.Assert; import com.mdd.common.core.AjaxResult; import com.mdd.common.core.PageResult; import com.mdd.common.validator.annotation.IDMust; @@ -11,14 +12,15 @@ import com.mdd.front.vo.article.ArticleCollectVo; import com.mdd.front.vo.article.ArticleDetailVo; import com.mdd.front.vo.article.ArticleListVo; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; +import java.util.Map; +/** + * 文章管理 + */ @RestController @RequestMapping("/api/article") public class ArticleController { @@ -73,7 +75,42 @@ public class ArticleController { */ @GetMapping("/collect") public Object collect(@Validated PageParam pageParam) { - PageResult list = iArticleService.collect(pageParam); + Integer userId = LikeFrontThreadLocal.getUserId(); + System.out.println(userId); + PageResult list = iArticleService.collect(pageParam, userId); return AjaxResult.success(list); } + + /** + * 加入收藏 + * + * @author fzr + * @param params 参数 + * @return Object + */ + @PostMapping("/addCollect") + public Object addCollect(@RequestBody Map params) { + Assert.notNull(params.get("articleId"), "articleId参数缺失"); + Integer articleId = Integer.parseInt(params.get("articleId")); + Integer userId = LikeFrontThreadLocal.getUserId(); + iArticleService.addCollect(articleId, userId); + return AjaxResult.success(); + } + + /** + * 取消收藏 + * + * @author fzr + * @param params 参数 + * @return Object + */ + @PostMapping("/cancelCollect") + public Object cancelCollect(@RequestBody Map params) { + Assert.notNull(params.get("id"), "id参数缺失"); + Integer id = Integer.parseInt(params.get("id")); + Integer userId = LikeFrontThreadLocal.getUserId(); + iArticleService.cancelCollect(id, userId); + return AjaxResult.success(); + } + } diff --git a/server/like-front/src/main/java/com/mdd/front/service/IArticleService.java b/server/like-front/src/main/java/com/mdd/front/service/IArticleService.java index 9063b182..39d16eef 100644 --- a/server/like-front/src/main/java/com/mdd/front/service/IArticleService.java +++ b/server/like-front/src/main/java/com/mdd/front/service/IArticleService.java @@ -47,8 +47,27 @@ public interface IArticleService { * * @author fzr * @param pageParam 分页参数 + * @param userId 用户ID * @return PageResult */ - PageResult collect(PageParam pageParam); + PageResult collect(PageParam pageParam, Integer userId); + + /** + * 加入收藏 + * + * @author fzr + * @param articleId 文章ID + * @param userId 用户ID + */ + void addCollect(Integer articleId, Integer userId); + + /** + * 取消收藏 + * + * @author fzr + * @param id 主键 + * @param userId 用户ID + */ + void cancelCollect(Integer id, Integer userId); } diff --git a/server/like-front/src/main/java/com/mdd/front/service/impl/ArticleServiceImpl.java b/server/like-front/src/main/java/com/mdd/front/service/impl/ArticleServiceImpl.java index aaf82431..08963b5f 100644 --- a/server/like-front/src/main/java/com/mdd/front/service/impl/ArticleServiceImpl.java +++ b/server/like-front/src/main/java/com/mdd/front/service/impl/ArticleServiceImpl.java @@ -10,9 +10,11 @@ import com.mdd.common.core.PageResult; import com.mdd.common.entity.article.Article; import com.mdd.common.entity.article.ArticleCategory; import com.mdd.common.entity.article.ArticleCollect; +import com.mdd.common.entity.server.Sys; import com.mdd.common.mapper.article.ArticleCategoryMapper; import com.mdd.common.mapper.article.ArticleCollectMapper; import com.mdd.common.mapper.article.ArticleMapper; +import com.mdd.common.utils.StringUtil; import com.mdd.common.utils.TimeUtil; import com.mdd.common.utils.UrlUtil; import com.mdd.front.service.IArticleService; @@ -155,21 +157,23 @@ public class ArticleServiceImpl implements IArticleService { * * @author fzr * @param pageParam 分页参数 + * @param userId 用户ID * @return PageResult */ @Override - public PageResult collect(PageParam pageParam) { + public PageResult collect(PageParam pageParam, Integer userId) { Integer pageNo = pageParam.getPageNo(); Integer pageSize = pageParam.getPageSize(); MPJQueryWrapper mpjQueryWrapper = new MPJQueryWrapper<>(); mpjQueryWrapper.select("t.id,t.article_id,a.title,a.image,a.intro,a.visit,a.create_time") - .eq("t.user_id", 1) + .eq("t.user_id", userId) .eq("t.is_delete", 0) + .eq("a.is_delete", 0) .orderByDesc("t.id") .innerJoin("?_article a ON a.id=t.article_id".replace("?_", GlobalConfig.tablePrefix)); - IPage iPage = articleMapper.selectJoinPage( + IPage iPage = articleCollectMapper.selectJoinPage( new Page<>(pageNo, pageSize), ArticleCollectVo.class, mpjQueryWrapper); @@ -182,4 +186,57 @@ public class ArticleServiceImpl implements IArticleService { return PageResult.iPageHandle(iPage); } + /** + * 加入收藏 + * + * @author fzr + * @param articleId 主键 + * @param userId 用户ID + */ + @Override + public void addCollect(Integer articleId, Integer userId) { + ArticleCollect articleCollect = articleCollectMapper.selectOne( + new QueryWrapper() + .eq("article_id", articleId) + .eq("user_id", userId) + .last("limit 1")); + + if (StringUtil.isNotNull(articleCollect)) { + articleCollect.setIsDelete(0); + articleCollect.setUpdateTime(System.currentTimeMillis() / 1000); + articleCollectMapper.updateById(articleCollect); + } else { + ArticleCollect model = new ArticleCollect(); + model.setArticleId(articleId); + model.setUserId(userId); + model.setIsDelete(0); + model.setCreateTime(System.currentTimeMillis() / 1000); + model.setUpdateTime(System.currentTimeMillis() / 1000); + articleCollectMapper.insert(model); + } + } + + /** + * 取消收藏 + * + * @author fzr + * @param id 主键 + * @param userId 用户ID + */ + @Override + public void cancelCollect(Integer id, Integer userId) { + ArticleCollect articleCollect = articleCollectMapper.selectOne( + new QueryWrapper() + .eq("id", id) + .eq("user_id", userId) + .eq("is_delete", 0) + .last("limit 1")); + + Assert.notNull(articleCollect, "收藏不存在!"); + + articleCollect.setIsDelete(1); + articleCollect.setUpdateTime(System.currentTimeMillis() / 1000); + articleCollectMapper.updateById(articleCollect); + } + } diff --git a/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java b/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java index 9069a0c4..90a72b16 100644 --- a/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java +++ b/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java @@ -37,7 +37,6 @@ public class LoginServiceImpl implements ILoginService { @Resource UserAuthMapper userAuthMapper; - /** * 注册账号 * @@ -81,12 +80,11 @@ public class LoginServiceImpl implements ILoginService { @Transactional public Map mnpLogin(Map params) { Assert.notNull(params.get("code"), "code参数缺失!"); - String scene = params.get("scene"); String code = params.get("code"); String avatarUrl = params.getOrDefault("avatarUrl", ""); String nickName = params.getOrDefault("nickName", ""); String gender = params.getOrDefault("gender", "0"); - Integer client = ClientEnum.getCodeByType(scene); + Integer client = Integer.parseInt(params.getOrDefault("client", "1")); Map config = ConfigUtil.get("mp_channel"); WxMaService wxMaService = new WxMaServiceImpl();