增加文章收藏处理

This commit is contained in:
TinyAnts 2022-09-07 18:08:40 +08:00
parent 706a53193f
commit 42a0d891bc
6 changed files with 125 additions and 13 deletions

View File

@ -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<User>()
.select("id,sn,username,nickname,mobile,is_disable,is_delete")

View File

@ -23,7 +23,6 @@ public class FrontConfig {
"/api/login/forgotPassword",
"/api/article/category",
"/api/article/collect",
"/api/article/detail",
"/api/article/list",
};

View File

@ -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<ArticleCollectVo> list = iArticleService.collect(pageParam);
Integer userId = LikeFrontThreadLocal.getUserId();
System.out.println(userId);
PageResult<ArticleCollectVo> list = iArticleService.collect(pageParam, userId);
return AjaxResult.success(list);
}
/**
* 加入收藏
*
* @author fzr
* @param params 参数
* @return Object
*/
@PostMapping("/addCollect")
public Object addCollect(@RequestBody Map<String, String> 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<String, String> 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();
}
}

View File

@ -47,8 +47,27 @@ public interface IArticleService {
*
* @author fzr
* @param pageParam 分页参数
* @param userId 用户ID
* @return PageResult<ArticleListVo>
*/
PageResult<ArticleCollectVo> collect(PageParam pageParam);
PageResult<ArticleCollectVo> 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);
}

View File

@ -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<ArticleCollectVo>
*/
@Override
public PageResult<ArticleCollectVo> collect(PageParam pageParam) {
public PageResult<ArticleCollectVo> collect(PageParam pageParam, Integer userId) {
Integer pageNo = pageParam.getPageNo();
Integer pageSize = pageParam.getPageSize();
MPJQueryWrapper<ArticleCollect> 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<ArticleCollectVo> iPage = articleMapper.selectJoinPage(
IPage<ArticleCollectVo> 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<ArticleCollect>()
.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<ArticleCollect>()
.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);
}
}

View File

@ -37,7 +37,6 @@ public class LoginServiceImpl implements ILoginService {
@Resource
UserAuthMapper userAuthMapper;
/**
* 注册账号
*
@ -81,12 +80,11 @@ public class LoginServiceImpl implements ILoginService {
@Transactional
public Map<String, Object> mnpLogin(Map<String, String> 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<String, String> config = ConfigUtil.get("mp_channel");
WxMaService wxMaService = new WxMaServiceImpl();