开发流程
还要统计用户的关注数、粉丝数
理清楚redis中各种key的设计方式!!!
RedisKeyUtil
//拼key的方法
// 某个用户关注的实体
// followee:userId: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
@Service
public class FollowService implements CommunityConstant {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private UserService userService;
//关注
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
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
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方法.
$(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");
}
}
在profile.html里follow-btn的前面加一行
<input type="hidden" id="entityId" th:value="${user.id}">
CommunityConstant类里添加
/**
* 实体类型:用户
*/
int ENTITY_TYPE_USER = 3;
增加其他功能
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>