我的关注
This commit is contained in:
parent
b077443687
commit
a76e5306cf
@ -1,20 +1,20 @@
|
||||
package org.jeecg.modules.aiol.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.system.query.QueryGenerator;
|
||||
import org.jeecg.common.system.query.QueryRuleEnum;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import org.jeecg.modules.aiol.entity.AiolUserFollow;
|
||||
import org.jeecg.modules.aiol.entity.UserFollow;
|
||||
import org.jeecg.modules.aiol.service.IAiolUserFollowService;
|
||||
import org.jeecg.common.constant.CommonConstant;
|
||||
import org.jeecg.common.system.util.JwtUtil;
|
||||
@ -26,6 +26,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jeecg.modules.system.entity.SysUser;
|
||||
import org.jeecg.modules.system.service.impl.SysUserServiceImpl;
|
||||
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
||||
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||
@ -33,16 +35,18 @@ import org.jeecgframework.poi.excel.entity.ImportParams;
|
||||
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
||||
import org.jeecg.common.system.base.controller.JeecgController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import java.util.Date;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
|
||||
|
||||
/**
|
||||
* @Description: 关注关系
|
||||
* @Author: jeecg-boot
|
||||
@ -58,6 +62,11 @@ public class AiolUserFollowController extends JeecgController<AiolUserFollow, IA
|
||||
private IAiolUserFollowService aiolUserFollowService;
|
||||
@Autowired
|
||||
private ISysBaseAPI sysBaseApi;
|
||||
@Autowired
|
||||
private SysUserServiceImpl sysUserService;
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 分页列表查询
|
||||
@ -205,6 +214,20 @@ public class AiolUserFollowController extends JeecgController<AiolUserFollow, IA
|
||||
userFollow.setCreateTime(new Date());
|
||||
|
||||
aiolUserFollowService.save(userFollow);
|
||||
|
||||
// 添加到Redis缓存
|
||||
String recentFollowKey = "user:recent:follow:" + sysUser.getId();
|
||||
String frequentVisitKey = "user:frequent:visit:" + sysUser.getId();
|
||||
long currentTime = System.currentTimeMillis();
|
||||
// 使用RedisTemplate操作Redis
|
||||
// 存储最近关注(使用时间戳作为分数)
|
||||
redisTemplate.opsForZSet().add(recentFollowKey, followedId, currentTime);
|
||||
// 存储访问频率(初始值为1)
|
||||
redisTemplate.opsForZSet().add(frequentVisitKey, followedId, 1);
|
||||
// 设置过期时间(例如30天)
|
||||
redisTemplate.expire(recentFollowKey, 30, TimeUnit.DAYS);
|
||||
redisTemplate.expire(frequentVisitKey, 30, TimeUnit.DAYS);
|
||||
|
||||
return Result.OK("关注成功!");
|
||||
|
||||
} catch (Exception e) {
|
||||
@ -213,6 +236,90 @@ public class AiolUserFollowController extends JeecgController<AiolUserFollow, IA
|
||||
}
|
||||
}
|
||||
|
||||
//查询关注列表
|
||||
@AutoLog(value = "关注关系-查询关注列表")
|
||||
@Operation(summary = "查询关注列表", description = "当前登录用户关注的所有用户")
|
||||
@GetMapping(value = "/followList")
|
||||
public Result<List<?>> followList(HttpServletRequest req) {
|
||||
try {
|
||||
// 1. 获取当前登录用户信息
|
||||
String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
|
||||
String username = JwtUtil.getUsername(token);
|
||||
LoginUser sysUser = sysBaseApi.getUserByName(username);
|
||||
if (sysUser == null) {
|
||||
return Result.error("用户未登录或登录已过期");
|
||||
}
|
||||
// 2. 查询关注列表
|
||||
QueryWrapper<AiolUserFollow> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("follower_id", sysUser.getId());
|
||||
List<AiolUserFollow> list = aiolUserFollowService.list(queryWrapper);
|
||||
if (list != null && !list.isEmpty()) {
|
||||
// 使用Stream提取被关注者的ID列表
|
||||
List<String> followedIds = list.stream()
|
||||
.map(AiolUserFollow::getFollowedId)
|
||||
.collect(Collectors.toList());
|
||||
// 根据被关注者的ID列表查询用户信息
|
||||
List<SysUser> followedUsers = sysUserService.listByIds(followedIds);
|
||||
if (followedUsers != null && !followedUsers.isEmpty()) {
|
||||
List<UserFollow> userFollows = new ArrayList<>();
|
||||
for (SysUser followedUser : followedUsers) {
|
||||
UserFollow userFollow = new UserFollow();
|
||||
userFollow.setId(followedUser.getId());
|
||||
userFollow.setIsFollow(true);
|
||||
userFollow.setRealName(followedUser.getRealname());
|
||||
userFollow.setAvatar(followedUser.getAvatar());
|
||||
userFollows.add(userFollow);
|
||||
}
|
||||
return Result.OK(userFollows);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
//查询最近关注
|
||||
@AutoLog(value = "关注关系-查询最近关注")
|
||||
@Operation(summary = "查询最近关注", description = "当前登录用户最近关注的用户")
|
||||
@GetMapping(value = "/recentFollowList")
|
||||
public Result<List<?>> recentFollowList(HttpServletRequest req) {
|
||||
try {
|
||||
// 1. 获取当前登录用户信息
|
||||
String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
|
||||
String username = JwtUtil.getUsername(token);
|
||||
LoginUser sysUser = sysBaseApi.getUserByName(username);
|
||||
if (sysUser == null) {
|
||||
return Result.error("用户未登录或登录已过期");
|
||||
}
|
||||
// 2. 查询最近关注列表
|
||||
String recentFollowKey = "user:recent:follow:" + sysUser.getId();
|
||||
Set<Object> objects = redisTemplate.opsForZSet().range(recentFollowKey, 0, -1);
|
||||
Set<String> followedIds = objects.stream()
|
||||
.map(obj -> String.valueOf(obj))
|
||||
.collect(Collectors.toSet());
|
||||
if (followedIds != null && !followedIds.isEmpty()) {
|
||||
// 根据被关注者的ID列表查询用户信息
|
||||
List<SysUser> followedUsers = sysUserService.listByIds(followedIds);
|
||||
if (followedUsers != null && !followedUsers.isEmpty()) {
|
||||
List<UserFollow> userFollows = new ArrayList<>();
|
||||
for (SysUser followedUser : followedUsers) {
|
||||
UserFollow userFollow = new UserFollow();
|
||||
userFollow.setId(followedUser.getId());
|
||||
userFollow.setIsFollow(true);
|
||||
userFollow.setRealName(followedUser.getRealname());
|
||||
userFollow.setAvatar(followedUser.getAvatar());
|
||||
userFollows.add(userFollow);
|
||||
}
|
||||
return Result.OK(userFollows);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
return Result.OK();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消关注用户
|
||||
*
|
||||
@ -241,6 +348,13 @@ public class AiolUserFollowController extends JeecgController<AiolUserFollow, IA
|
||||
|
||||
boolean removed = aiolUserFollowService.remove(deleteWrapper);
|
||||
if (removed) {
|
||||
// 从Redis中删除关注记录
|
||||
String recentFollowKey = "user:recent:follow:" + sysUser.getId();
|
||||
String frequentVisitKey = "user:frequent:visit:" + sysUser.getId();
|
||||
// 从最近关注列表中删除
|
||||
redisTemplate.opsForZSet().remove(recentFollowKey, followedId);
|
||||
// 从访问频率列表中删除
|
||||
redisTemplate.opsForZSet().remove(frequentVisitKey, followedId);
|
||||
return Result.OK("取消关注成功!");
|
||||
} else {
|
||||
return Result.error("未找到关注关系");
|
||||
|
@ -0,0 +1,18 @@
|
||||
package org.jeecg.modules.aiol.entity;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserFollow {
|
||||
@Schema(description = "id")
|
||||
private String id;
|
||||
@Schema(description = "用户名称")
|
||||
private String realName;
|
||||
@Schema(description = "头像")
|
||||
private String avatar;
|
||||
@Schema(description = "是否关注")
|
||||
private Boolean isFollow;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user