查询点赞的内容
This commit is contained in:
@@ -30,7 +30,8 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
// 忽略的接口
|
||||
.excludePathPatterns(
|
||||
"/user/login",
|
||||
"/user/sendCode"
|
||||
"/user/sendCode",
|
||||
"/user/loginByCode"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@ package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.mapper.PostLikeMapper;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.service.PostLikeService;
|
||||
import com.bao.dating.util.JwtUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -44,4 +48,11 @@ public class PostLikeController {
|
||||
Long userId = body.get("user_id");
|
||||
postLikeService.unlike(postId,userId);
|
||||
}
|
||||
|
||||
@GetMapping("/getMyLikes")
|
||||
public Result<?> getMyLikes(){
|
||||
Long userId = UserContext.getUserId();
|
||||
List<Post> posts = postLikeService.listUserLikes(userId);
|
||||
return Result.success(ResultCode.SUCCESS, "获取成功", posts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostLike;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface PostLikeMapper {
|
||||
/**
|
||||
@@ -31,4 +34,12 @@ public interface PostLikeMapper {
|
||||
* @return
|
||||
*/
|
||||
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
|
||||
|
||||
/**
|
||||
* 查询点赞内容
|
||||
* @param postId
|
||||
* @return
|
||||
*/
|
||||
|
||||
List<Post> selectLikeCount(@Param("postId") Long postId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostLikeService {
|
||||
/**
|
||||
@@ -19,4 +22,9 @@ public interface PostLikeService {
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
void unlike(Long postId, Long userId);
|
||||
|
||||
/**
|
||||
* 查看用户所有点赞
|
||||
*/
|
||||
List<Post> listUserLikes(Long userId);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.mapper.PostLikeMapper;
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostLike;
|
||||
import com.bao.dating.service.PostLikeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -11,11 +12,13 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class PostLikeServiceImpl implements PostLikeService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
@@ -24,6 +27,8 @@ public class PostLikeServiceImpl implements PostLikeService {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 点赞指定的 post
|
||||
*
|
||||
@@ -74,4 +79,16 @@ public class PostLikeServiceImpl implements PostLikeService {
|
||||
postMapper.decreaseLikeCount(postId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户点赞列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 统一返回结果
|
||||
*/
|
||||
@Override
|
||||
public List<Post> listUserLikes(Long userId) {
|
||||
return postLikeMapper.selectLikeCount(userId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -290,22 +290,35 @@ public class UserServiceImpl implements UserService {
|
||||
// 发送短信验证码
|
||||
@Override
|
||||
public void sendSmsCode(String phone) {
|
||||
//防刷:60 秒内只能发一次
|
||||
String key = "sms:code:" + phone;
|
||||
Boolean exists = stringRedisTemplate.hasKey(key);
|
||||
if (Boolean.TRUE.equals(exists)){
|
||||
throw new RuntimeException("请勿频繁发送验证码");
|
||||
String timeKey = "sms:time:" + phone; // 用于记录上次发送时间的key
|
||||
|
||||
// 检查是否在60秒内已经发送过
|
||||
Boolean timeKeyExists = stringRedisTemplate.hasKey(timeKey);
|
||||
if (Boolean.TRUE.equals(timeKeyExists)) {
|
||||
String lastSendTimeStr = stringRedisTemplate.opsForValue().get(timeKey);
|
||||
long lastSendTime = Long.parseLong(lastSendTimeStr);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
// 如果距离上次发送不足60秒,抛出异常
|
||||
if (currentTime - lastSendTime < 60 * 1000) {
|
||||
throw new RuntimeException("请勿频繁发送验证码");
|
||||
}
|
||||
}
|
||||
|
||||
// 生成验证码
|
||||
String code = CodeUtil.generateCode();
|
||||
// 发送短信
|
||||
smsUtil.sendVerificationCode(phone, code);
|
||||
//存 Redis(5分钟过期)
|
||||
|
||||
// 存储验证码到Redis,5分钟过期
|
||||
stringRedisTemplate.opsForValue()
|
||||
.set(key,code, 5, TimeUnit.MINUTES);
|
||||
.set(key, code, 5, TimeUnit.MINUTES);
|
||||
// 存储发送时间戳,60秒过期(与验证码分开管理过期时间)
|
||||
stringRedisTemplate.opsForValue()
|
||||
.set(timeKey, String.valueOf(System.currentTimeMillis()), 60, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
// 校验验证码
|
||||
// 验证验证码
|
||||
@Override
|
||||
public boolean verifyCode(String phone, String code) {
|
||||
|
||||
|
||||
@@ -14,4 +14,8 @@
|
||||
<delete id="deleteByPostIdAndUserId">
|
||||
delete from dating.post_like where post_id = #{postId} and user_id = #{userId}
|
||||
</delete>
|
||||
|
||||
<select id="selectLikeCount" resultType="com.bao.dating.pojo.entity.Post">
|
||||
select p.* from dating.post p inner join dating.post_like l on p.post_id = l.post_id where l.user_id = #{postId} ORDER BY l.created_at DESC
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user