网关配置
nacos: leadnews-app-gateway
# 用户行为微服务
- id: leadnews-behavior
uri: lb://leadnews-behavior
predicates:
- Path=/behavior/**
filters:
- StripPrefix= 1
nacos: leadnews-behavior
在配置文件中去除数据源自动配置
spring:
# redis配置
redis:
host: localhost
# password:
port: 6379
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
点赞
- 当前登录的用户点击了”赞“,就要保存当前行为数据
- 可以取消点赞
public ResponseResult likesBehavior(LikesBehaviorDto dto) {
// 1. 检查参数
if(dto == null || dto.getArticleId() == null || checkParam(dto)){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
// 2. 是否登录
ApUser user = AppThreadLocalUtil.getUser();
if(user == null){
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}
// 3. 点赞, 保存数据
if(dto.getOperation() == 0){
// 点赞
Object obj = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
if(obj != null){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已点赞");
}
// 保存当前key
log.info("保存当前key:{} ,{}, {}", BehaviorConstants.LIKE_BEHAVIOR+dto.getArticleId(), user.getId(), dto);
cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));
}else{
// 取消点赞
// 删除当前key
log.info("删除当前key:{}, {}", BehaviorConstants.LIKE_BEHAVIOR+dto.getArticleId(), user.getId());
cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
private boolean checkParam(LikesBehaviorDto dto) {
if(dto.getType() > 2 || dto.getType() < 0 || dto.getOperation() > 1 || dto.getOperation() < 0){
return true;
}
return false;
}
阅读
- 当用户查看了某一篇文章,需要记录当前用户查看的次数
public ResponseResult readBehavior(ReadBehaviorDto dto) {
// 1. 检查次数
if(dto == null || dto.getArticleId() == null || dto.getCount() == null){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
// 2. 是否登录
ApUser user = AppThreadLocalUtil.getUser();
if(user == null){
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}
// 3. 阅读, 保存数据
// 3.1 更新阅读次数
String readBehaviorJson = (String) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
if(StringUtils.isNotBlank(readBehaviorJson)){
ReadBehaviorDto readBehaviorDto = JSON.parseObject(readBehaviorJson, ReadBehaviorDto.class);
dto.setCount(readBehaviorDto.getCount()+dto.getCount());
}
// 3.2 保存数据
// 保存当前key
log.info("保存当前key:{} {} {}", BehaviorConstants.READ_BEHAVIOR+dto.getArticleId(), user.getId(), dto);
cacheService.hPut(BehaviorConstants.READ_BEHAVIOR+dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
不喜欢
用户点击了不喜欢, 就不再给当前用户推荐这一类型的文章信息
public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
// 1. 检查次数
if(dto == null || dto.getArticleId() == null || dto.getType() > 1 || dto.getType() < 0){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
// 2. 是否登录
ApUser user = AppThreadLocalUtil.getUser();
if(user == null){
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}
// 3. 不喜欢文章, 保存数据
if(dto.getType() == 0){
// 不喜欢
Object o = cacheService.hGet(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
if(o != null){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已经不喜欢");
}
log.info("保存当前key:{} ,{}, {}", BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId(), user.getId(), dto);
cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));
}else{
// 取消不喜欢
log.info("删除当前key:{}, {}", BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId(), user.getId());
cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
关注
- 当前登录后的用户可以关注作者,也可以取消关注作者
- 从用户的角度出发:一个用户可以关注其他多个作者 —— 我的关注
- 从作者的角度出发:一个用户(同是作者)也可以拥有很多个粉丝 —— 我的粉丝
public ResponseResult userFollow(UserRelationDto dto) {
// 1. 检查参数
if(dto == null || checkParam(dto)){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
// 2. 是否登录
ApUser user = AppThreadLocalUtil.getUser();
if(user == null){
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}
// 3. 关注, 保存数据
if(dto.getOperation() == 0){
// 关注
// 将作者写入我的关注中
cacheService.zAdd(BehaviorConstants.APUSER_FOLLOW_RELATION+user.getId(), dto.getAuthorId().toString(), System.currentTimeMillis());
// 将我写入作者的粉丝中
cacheService.zAdd(BehaviorConstants.APUSER_FANS_RELATION+dto.getAuthorId(), user.getId().toString(), System.currentTimeMillis());
}else{
// 取消关注
cacheService.zRemove(BehaviorConstants.APUSER_FOLLOW_RELATION+user.getId(), dto.getAuthorId().toString());
cacheService.zRemove(BehaviorConstants.APUSER_FANS_RELATION+dto.getAuthorId(), user.getId().toString());
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
private boolean checkParam(UserRelationDto dto) {
if(dto.getArticleId() == null || dto.getAuthorId() == null){
return true;
}
if(dto.getOperation() > 1 || dto.getOperation() < 0){
return true;
}
return false;
}
收藏
- 记录当前登录人收藏的文章
public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
// 1. 检查参数
if(dto == null || checkParam(dto)){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
// 2. 是否登录
ApUser user = AppThreadLocalUtil.getUser();
if(user == null){
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}
// 3. 文章收藏, 数据保存
if(dto.getOperation() == 0){
// 收藏
// 判断是否已经收藏
String collectionJson = (String) cacheService.hGet(BehaviorConstants.COLLECTION_BEHAVIOR + dto.getEntryId(), user.getId().toString());
if(StringUtils.isNotBlank(collectionJson)){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已收藏");
}
log.info("文章收藏,保存key:{},{},{}",BehaviorConstants.COLLECTION_BEHAVIOR + user.getId(),dto.getEntryId().toString(), JSON.toJSONString(dto));
cacheService.hPut(BehaviorConstants.COLLECTION_BEHAVIOR + user.getId(),dto.getEntryId().toString(), JSON.toJSONString(dto));
}else{
// 取消收藏
log.info("文章收藏,删除key:{},{},{}",BehaviorConstants.COLLECTION_BEHAVIOR + user.getId(),dto.getEntryId().toString());
cacheService.hDelete(BehaviorConstants.COLLECTION_BEHAVIOR + user.getId(),dto.getEntryId().toString());
}
return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
}
文章详情-行为数据回显
展示文章的关系,app端用户必须登录,判断当前用户是否已经关注该文章的作者、是否收藏了此文章、是否点赞了文章、是否不喜欢该文章等
public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
// 1. 检查数据
if(dto == null || dto.getArticleId() == null || dto.getAuthorId() == null){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
// 2. 是否登录
ApUser user = AppThreadLocalUtil.getUser();
if(user == null){
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}
// 3. 回显数据
Map<String, Boolean> map = new HashMap<>();
Boolean islike = false, isunlike = false, iscollection = false, isfollow = false;
// 3.1 是否点赞
String likeJson = (String) cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId(), user.getId().toString());
if(likeJson != null){
islike = true;
}
// 3.2 是否不喜欢
String unLikeJson = (String) cacheService.hGet(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId(), user.getId().toString());
if(unLikeJson != null){
isunlike = true;
}
// 3.3 是否收藏
String collectiontJson = (String) cacheService.hGet(BehaviorConstants.COLLECTION_BEHAVIOR + user.getId(), dto.getArticleId().toString());
if(collectiontJson != null){
iscollection = true;
}
// 3.4 是否关注
Double score = cacheService.zScore(BehaviorConstants.APUSER_FOLLOW_RELATION + user.getId(), dto.getAuthorId().toString());
if(score != null){
isfollow = true;
}
map.put("islike", islike);
map.put("isunlike", isunlike);
map.put("iscollection", iscollection);
map.put("isfollow", isfollow);
return ResponseResult.okResult(map);
}
来源
黑马程序员. 黑马头条文章来源:https://www.toymoban.com/news/detail-687472.html
Gitee
https://gitee.com/yu-ba-ba-ba/leadnews文章来源地址https://www.toymoban.com/news/detail-687472.html
到了这里,关于JavaWeb_LeadNews_Day9-Redis实现用户行为的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!