需求
(1)开发关注、取消关注功能
(2)统计用户的关注数、粉丝数。
关键
(1)若A关注了B,则A是B的Follower(粉丝),B是A的Followee(目标)。
(2)关注的目标可以试试用户、帖子、题目等,在实现时将这些目标抽象为实体。
在RedisKeyUtil类里,添加
private static final String PREFIX_FOLLOWEE = "followee";
private static final String PREFIX_FOLLOWER = "follower"; //为了统计方便,定义两个(关注者和被关注者)
//某个用户关注的实体
// followee:usrId:entityType -> zset(entityId,now)
public static String getFolloweeKey(int userId, int entityType) {
return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
}
//某个实体拥有的粉丝
//follower:entityType:entityId -> zset(userId,now)
public static String getFollowerKey(int entityType, int entityId) {
return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
}
新建FollowService类,添加
public void follow(int userId, int entityType, int entityId){
redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType); //这个存的是关注的实体目标
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
operations.multi();
operations.opsForZSet().add(followeeKey,entityId,System.currentTimeMillis()); //存上系统时间以用来排序
operations.opsForZSet().add(followerKey,userId,System.currentTimeMillis());
return operations.exec();
}
});
}
public void unfollow(int userId, int entityType, int entityId){
redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType);
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
operations.multi();
operations.opsForZSet().remove(followeeKey,entityId);
operations.opsForZSet().remove(followerKey,userId);
return operations.exec();
}
});
}
新建FollowController类,添加
@RequestMapping(path = "/follow", method = RequestMethod.POST)
@ResponseBody //是异步请求(页面局部刷新),所以加上@ResponseBody
public String follow(int entityType, int entityId){ //应该用拦截器(不登录就不能使用这个功能)
User user = hostHolder.getUser();
followService.follow(user.getId(),entityType,entityId);
return CommunityUtil.getJSONString(0,"已关注");
}
@RequestMapping(path = "/unfollow", method = RequestMethod.POST)
@ResponseBody //是异步请求,所以加上@ResponseBody
public String unfollow(int entityType, int entityId){ //应该用拦截器(不登录就不能使用这个功能)
User user = hostHolder.getUser();
followService.unfollow(user.getId(),entityType,entityId);
return CommunityUtil.getJSONString(0,"已取消关注");
}
在profile.html,“关注TA”那里,按钮对应的事件调用profile.js里的follow方法,
在CommunityConstant类里添加
/**
* 实体类型:用户
*/
int ENTITY_TYPE_USER = 3;
在profile.html里follow-btn的前面加一行
<input type="hidden" id="entityId" th:value="${user.id}">
则profile.js可以这么写
$(function(){
$(".follow-btn").click(follow);
});
function follow() {
var btn = this;
if($(btn).hasClass("btn-info")) {
// 关注TA
$.post(
CONTEXT_PATH + "/follow",
{"entityType":3,"entityId":$(btn).prev().val()}, //prev()表示取btn前一个节点的值
function(data){
data = $.parseJSON(data); //解析成js对象
if(data.code==0){
window.location.reload(); //刷新页面
}else{
alert(data.msg);
}
}
);
// $(btn).text("已关注").removeClass("btn-info").addClass("btn-secondary");
} else {
// 取消关注
$.post(
CONTEXT_PATH + "/unfollow",
{"entityType":3,"entityId":$(btn).prev().val()},
function(data){
data = $.parseJSON(data);
if(data.code==0){
window.location.reload();
}else{
alert(data.msg);
}
}
);
// $(btn).text("关注TA").removeClass("btn-secondary").addClass("btn-info");
}
}
在FollowService类里,添加
//查询某个用户关注的实体的数量
public long findFolloweeCount(int userId, int entityType){
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
return redisTemplate.opsForZSet().zCard(followeeKey);
}
// 查询实体的粉丝的数量
public long findFollowerCount(int entityType, int entityId){
String followerKey = RedisKeyUtil.getFollowerKey(entityType,entityId);
return redisTemplate.opsForZSet().zCard(followerKey);
}
// 查询当前用户是否关注该实体
public boolean hasFollowed(int userId, int entityType, int entityId){
String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType);
return redisTemplate.opsForZSet().score(followeeKey,entityId) != null;
}
在UserController类getProfilePage方法里,添加
//关注数量
long followeeCount = followService.findFolloweeCount(userId,ENTITY_TYPE_USER);
model.addAttribute("followeeCount",followeeCount);
//粉丝数量
long followerCount = followService.findFollowerCount(ENTITY_TYPE_USER,userId);
model.addAttribute("followerCount",followerCount);
//是否已关注
boolean hasFollowed = false;
if(hostHolder.getUser()!=null){
hasFollowed = followService.hasFollowed(hostHolder.getUser().getId(),ENTITY_TYPE_USER,userId);
}
model.addAttribute("hasFollowed",hasFollowed);
在profile.html里,把关注数量、是否已关注等相应信息进行修改。以及修改样式,以便在未关注是是一种样式,已关注时是另一种样式
<button type="button" th:class="|btn ${hasFollowed?'btn-secondary':'btn-info'} btn-sm float-right mr-5 follow-btn|"
th:text="${hasFollowed?'已关注':'关注TA'}" th:if="${loginUser!=null&&loginUser.id!=user.id}">关注TA</button>