ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-05-03 19:32:08  阅读:155  来源: 互联网

标签:仿牛 String int entityType userId 客网 二十四 entityId public


关注

  • 需求
    • 开发关注、取消关注功能。
    • 统计用户的关注数、粉丝数。
  • 关注
    • 若A关注了B,则A是B的Follower(粉丝),B是A的Followee(目标)。
    • 关注的目标可以是用户、帖子、题目等,在实现时将这些目标抽象为实体。
  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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有