其他分享
首页 > 其他分享> > 仿牛客网社区项目(二十四)关注

仿牛客网社区项目(二十四)关注

作者:互联网

关注

  1. 修改RedisKeyUtil

    
    public class RedisKeyUtil {
    
        private static final String SPLIT = ":";
        private static final String PREFIX_ENTITY_LIKE = "like:entity";
        private static final String PREFIX_USER_LIKE = "like:user";
        private static final String PREFIX_FOLLOWEE = "followee";
        private static final String PREFIX_FOLLOWER = "follower";
    
        // 某个实体的赞
        // like:entity:entityType:entityId -> set(userId)
        public static String getEntityLikeKey(int entityType, int entityId) {
            return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
        }
    
        // 某个用户的赞
        // like:user:userId -> int
        public static String getUserLikeKey(int userId) {
            return PREFIX_USER_LIKE + SPLIT + userId;
        }
    
        // 某个用户关注的实体
        //这里实体的可以为用户,帖子 等
        // 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;
        }
    }
    
    
  2. 编写业务层逻辑 FollwService

    @Service
    public class FollowService {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        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();
                }
            });
        }
    
        // 查询关注的实体的数量
        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;
        }
    
    }
    
    
    1. 编写表现层逻辑
    package com.nowcoder.community.controller;
    
    @Controller
    public class FollowController {
    
        @Autowired
        private FollowService followService;
    
        @Autowired
        private HostHolder hostHolder;
    
        @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, "已取消关注!");
        }
    
    }
    
    
  3. 修改UserController的个人页面请求

     @RequestMapping(path = "/profile/{userId}", method = RequestMethod.GET)
        public String getProfilePage(@PathVariable("userId") int userId, Model model) {
            User user = userService.findUserById(userId);
            if (user == null) {
                throw new RuntimeException("该用户不存在!");
            }
    
            // 用户
            model.addAttribute("user", user);
            // 点赞数量
            int likeCount = likeService.findUserLikeCount(userId);
            model.addAttribute("likeCount", likeCount);
    
            // 关注数量
            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);
    
            return "/site/profile";
        }
    
  4. 接下来去处理静态页面,关注采用的是异步的方式 所以先编写对应的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()},
		    function(data) {
		        data = $.parseJSON(data);
		        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");
	}
}
  1. 修改个人主页html profile.html 84行
<div class="media mt-5">
    <img th:src="${user.headerUrl}" class="align-self-start mr-4 rounded-circle" alt="用户头像" style="width:50px;">
    <div class="media-body">
        <h5 class="mt-0 text-warning">
            <span th:utext="${user.username}">nowcoder</span>
            <input type="hidden" id="entityId" th:value="${user.id}">
            <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>
        </h5>
        <div class="text-muted mt-3">
            <span>注册于 <i class="text-muted" th:text="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}">2015-06-12 15:20:12</i></span>
        </div>
        <div class="text-muted mt-3 mb-5">
            <span>关注了 <a class="text-primary" href="followee.html" th:text="${followeeCount}">5</a> 人</span>
            <span class="ml-4">关注者 <a class="text-primary" href="follower.html" th:text="${followerCount}">123</a> 人</span>
            <span class="ml-4">获得了 <i class="text-danger" th:text="${likeCount}">87</i> 个赞</span>
        </div>
    </div>
</div>
</div>
</div>

okl

标签:仿牛,String,int,entityType,userId,客网,二十四,entityId,public
来源: https://www.cnblogs.com/nevererror/p/16219104.html