* 'develop' of https://gitee.com/likeshop_gitee/likeadmin-java:
  增加注册时的客户端
  增加文章收藏处理
  增加收藏
  调整接口
This commit is contained in:
linjinyuan 2022-09-07 18:37:29 +08:00
commit b96c43e051
12 changed files with 217 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import com.mdd.common.entity.user.User;
import com.mdd.common.enums.HttpEnum;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.utils.RedisUtil;
import com.mdd.common.utils.StringUtil;
import com.mdd.front.config.FrontConfig;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
@ -45,13 +46,21 @@ public class LikeFrontInterceptor implements HandlerInterceptor {
}
// 免登录接口
String token = request.getHeader("token");
List<String> notLoginUri = Arrays.asList(FrontConfig.notLoginUri);
if (notLoginUri.contains(request.getRequestURI())) {
if (StringUtil.isNotEmpty(token)) {
Object uid = RedisUtil.get(token);
if (uid != null) {
Integer userId = Integer.parseInt(uid.toString());
LikeFrontThreadLocal.put("userId", userId);
}
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
// Token是否为空
String token = request.getHeader("token");
if (StringUtils.isBlank(token)) {
AjaxResult result = AjaxResult.failed(HttpEnum.TOKEN_EMPTY.getCode(), HttpEnum.TOKEN_EMPTY.getMsg());
response.getWriter().print(JSON.toJSONString(result));
@ -68,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

@ -1,6 +1,7 @@
package com.mdd.front;
import java.util.LinkedHashMap;
import java.util.Map;
public class LikeFrontThreadLocal {
@ -31,18 +32,22 @@ public class LikeFrontThreadLocal {
* 获取本地线程
*/
public static Object get(String key) {
return MY_LOCAL.get().getOrDefault(key, "");
Map<String, Object> map = MY_LOCAL.get();
if (map == null) {
return null;
}
return map.getOrDefault(key, "");
}
/**
* 获取用户ID
*/
public static Integer getUserId() {
String adminId = LikeFrontThreadLocal.get("userId").toString();
if (adminId.equals("")) {
Object adminId = LikeFrontThreadLocal.get("userId");
if (adminId == null || adminId.toString().equals("")) {
return 0;
}
return Integer.parseInt(adminId);
return Integer.parseInt(adminId.toString());
}
/**

View File

@ -14,6 +14,7 @@ public class FrontConfig {
"/api/index",
"/api/config",
"/api/policy",
"/api/search",
"/api/decorate",
"/api/sms/send",
@ -22,7 +23,6 @@ public class FrontConfig {
"/api/login/forgotPassword",
"/api/article/category",
"/api/article/collect",
"/api/article/detail",
"/api/article/list",
};

View File

@ -1,8 +1,10 @@
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;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IArticleService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.article.ArticleCateVo;
@ -10,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 {
@ -46,7 +49,8 @@ public class ArticleController {
@GetMapping("/list")
public Object list(@Validated PageParam pageParam,
@RequestParam(value = "cid", defaultValue = "0") Integer cid) {
PageResult<ArticleListVo> list = iArticleService.list(pageParam, cid);
Integer userId = LikeFrontThreadLocal.getUserId();
PageResult<ArticleListVo> list = iArticleService.list(pageParam, cid, userId);
return AjaxResult.success(list);
}
@ -71,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

@ -13,6 +13,9 @@ import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* 主页管理
*/
@RestController
@RequestMapping("/api")
public class IndexController {

View File

@ -28,9 +28,10 @@ public interface IArticleService {
* @author fzr
* @param pageParam 分页参数
* @param cid 分类ID
* @param userId 用户ID
* @return PageResult<ArticleListVo>
*/
PageResult<ArticleListVo> list(PageParam pageParam, Integer cid);
PageResult<ArticleListVo> list(PageParam pageParam, Integer cid, Integer userId);
/**
* 文章详情
@ -46,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

@ -3,9 +3,27 @@ package com.mdd.front.service;
import com.mdd.front.vo.user.UserCenterVo;
import com.mdd.front.vo.user.UserInfoVo;
/**
* 用户服务接口类
*/
public interface IUserService {
/**
* 个人中心
*
* @author fzr
* @param userId 用户
* @return UserCenterVo
*/
UserCenterVo center(Integer userId);
/**
* 个人信息
*
* @author fzr
* @param userId 用户ID
* @return UserInfoVo
*/
UserInfoVo info(Integer userId);
}

View File

@ -10,8 +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;
@ -20,6 +23,7 @@ import com.mdd.front.vo.article.ArticleCateVo;
import com.mdd.front.vo.article.ArticleCollectVo;
import com.mdd.front.vo.article.ArticleDetailVo;
import com.mdd.front.vo.article.ArticleListVo;
import net.sf.jsqlparser.statement.create.table.Index;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@ -40,6 +44,9 @@ public class ArticleServiceImpl implements IArticleService {
@Resource
ArticleCategoryMapper articleCategoryMapper;
@Resource
ArticleCollectMapper articleCollectMapper;
/**
* 文章分类
*
@ -71,10 +78,11 @@ public class ArticleServiceImpl implements IArticleService {
* @author fzr
* @param pageParam 分页参数
* @param cid 分类ID
* @param userId 用户ID
* @return PageResult<ArticleListVo>
*/
@Override
public PageResult<ArticleListVo> list(PageParam pageParam, Integer cid) {
public PageResult<ArticleListVo> list(PageParam pageParam, Integer cid, Integer userId) {
Integer pageNo = pageParam.getPageNo();
Integer pageSize = pageParam.getPageSize();
@ -86,13 +94,34 @@ public class ArticleServiceImpl implements IArticleService {
IPage<Article> iPage = articleMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
List<Integer> ids = new LinkedList<>();
List<ArticleListVo> list = new LinkedList<>();
for (Article article : iPage.getRecords()) {
ArticleListVo vo = new ArticleListVo();
BeanUtils.copyProperties(article, vo);
vo.setCollect(false);
vo.setImage(UrlUtil.toAbsoluteUrl(article.getImage()));
vo.setCreateTime(TimeUtil.timestampToDate(article.getCreateTime()));
list.add(vo);
ids.add(article.getId());
}
if (userId != null && userId > 0 && ids.size() > 0) {
List<ArticleCollect> articleCollects = articleCollectMapper.selectList(
new QueryWrapper<ArticleCollect>()
.eq("user_id", userId)
.eq("is_delete", 0)
.in("article_id", ids));
List<Integer> collects = new LinkedList<>();
for (ArticleCollect c : articleCollects) {
collects.add(c.getArticleId());
}
for (ArticleListVo vo : list) {
vo.setCollect(collects.contains(vo.getId()));
}
}
return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
@ -123,19 +152,28 @@ public class ArticleServiceImpl implements IArticleService {
return vo;
}
/**
* 收藏列表
*
* @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);
@ -148,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;
/**
* 注册账号
*
@ -64,7 +63,7 @@ public class LoginServiceImpl implements ILoginService {
user.setUsername(regParam.getUsername());
user.setPassword(pwd);
user.setSalt(salt);
user.setChannel(0);
user.setChannel(regParam.getClient());
user.setCreateTime(System.currentTimeMillis() / 1000);
user.setUpdateTime(System.currentTimeMillis() / 1000);
userMapper.insert(user);
@ -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();

View File

@ -15,10 +15,12 @@ import com.mdd.front.vo.user.UserCenterVo;
import com.mdd.front.vo.user.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
/**
* 用户服务实现类
*/
@Service
public class UserServiceImpl implements IUserService {

View File

@ -1,5 +1,6 @@
package com.mdd.front.validate;
import com.mdd.common.validator.annotation.IntegerContains;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@ -32,4 +33,9 @@ public class RegParam implements Serializable {
@Length(min = 6, max = 12, message = "密码必须在6~12个字符内")
private String password;
@NotNull(message = "client参数缺失")
@NotEmpty(message = "客户端不能为空")
@IntegerContains(values = {1, 2, 3, 4, 5, 6}, message = "不是合法客户端")
private Integer client;
}

View File

@ -14,6 +14,7 @@ public class ArticleListVo implements Serializable {
private String image;
private String intro;
private Integer visit;
private Boolean collect;
private String createTime;
}