Compare commits
18 Commits
feature-Po
...
feature-Po
| Author | SHA1 | Date | |
|---|---|---|---|
| 1007a2a1e4 | |||
|
|
bd12c599b0 | ||
|
|
b5a15a3f01 | ||
|
|
039443dc0f | ||
| ae0cca5437 | |||
| c329eaef79 | |||
|
|
55c7c9a03a | ||
|
|
a6716e32b6 | ||
|
|
8506fbb7e4 | ||
|
|
64bfb08257 | ||
|
|
71dbb6fc82 | ||
|
|
b6ac177148 | ||
|
|
df463f2a52 | ||
|
|
34663e8e84 | ||
|
|
176e133a57 | ||
|
|
fd3a38efb5 | ||
|
|
7d99b30d73 | ||
|
|
370e81f3c6 |
@@ -11,6 +11,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Data
|
||||
@Slf4j
|
||||
@@ -30,7 +32,6 @@ public class AliOssUtil {
|
||||
* @return 完整的文件访问URL
|
||||
*/
|
||||
public String upload(byte[] bytes, String objectName) {
|
||||
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
|
||||
@@ -147,3 +147,5 @@ public class SmsUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
33
src/main/java/com/bao/dating/config/WebConfig.java
Normal file
33
src/main/java/com/bao/dating/config/WebConfig.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.bao.dating.config;
|
||||
|
||||
|
||||
import com.bao.dating.interceptor.TokenInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private TokenInterceptor tokenInterceptor;
|
||||
|
||||
/**
|
||||
* 添加拦截器到Spring MVC配置中
|
||||
* @param registry 拦截器注册中心
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//注册自定义拦截器对象
|
||||
registry.addInterceptor(tokenInterceptor)
|
||||
// 拦截所有请求
|
||||
.addPathPatterns("/**")
|
||||
// 忽略的接口
|
||||
.excludePathPatterns(
|
||||
"/user/login",
|
||||
"/user/userRegister"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
33
src/main/java/com/bao/dating/context/UserContext.java
Normal file
33
src/main/java/com/bao/dating/context/UserContext.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.bao.dating.context;
|
||||
|
||||
/**
|
||||
* 用户上下文类
|
||||
* @author lenovo
|
||||
*/
|
||||
public class UserContext {
|
||||
|
||||
private static final ThreadLocal<Long> userHolder = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置当前线程的用户ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
public static void setUserId(Long userId) {
|
||||
userHolder.set(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的用户ID
|
||||
* @return 当前用户ID,如果未设置则返回null
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return userHolder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前线程的用户ID
|
||||
*/
|
||||
public static void clear() {
|
||||
userHolder.remove();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.common.Comments;
|
||||
import com.bao.dating.pojo.entity.Comments;
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.service.CommentsService;
|
||||
|
||||
@@ -34,26 +34,25 @@ public class PostController {
|
||||
/**
|
||||
* 发布动态接口 - JSON格式请求
|
||||
* @param postDTO 动态信息
|
||||
* @param userId 用户ID
|
||||
* @return 发布的动态对象
|
||||
*/
|
||||
@PostMapping(consumes = "application/json")
|
||||
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO, @RequestParam Long userId) {
|
||||
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO) {
|
||||
// 调用 Service 层处理发布动态业务逻辑
|
||||
Post result = postService.createPost(userId, postDTO);
|
||||
Post result = postService.createPost(postDTO);
|
||||
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除动态
|
||||
* 批量删除动态
|
||||
*
|
||||
* @param postId 动态ID
|
||||
* @param postIds 动态ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
@DeleteMapping("/{postId}")
|
||||
public Result<String> deleteById(@PathVariable Long postId){
|
||||
postService.deletePostById(postId);
|
||||
return Result.success(ResultCode.SUCCESS_DELETE, "动态删除成功", null);
|
||||
@DeleteMapping
|
||||
public Result<String> deleteById(@RequestParam List<Long> postIds){
|
||||
int deletedCount = postService.deletePostById(postIds);
|
||||
return Result.success(ResultCode.SUCCESS_DELETE, deletedCount > 0 ? "成功删除" : "删除失败,该动态不存在", null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,12 +2,15 @@ package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostFavorite;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.service.PostFavoriteService;
|
||||
import com.bao.dating.service.PostService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@@ -31,4 +34,11 @@ public class PostFavoriteController {
|
||||
Long userId = user.getUserId();
|
||||
return postFavoriteService.deletePostFavorite(userId, postId);
|
||||
}
|
||||
@GetMapping("/{user_id}/favorites")
|
||||
public Result<List<Post>> getFavorites(@PathVariable("user_id")Long postId){
|
||||
if (postId == null){
|
||||
return Result.error(ResultCode.PARAM_ERROR);
|
||||
}
|
||||
return postFavoriteService.getAllFavoritePost(postId);
|
||||
}
|
||||
}
|
||||
|
||||
48
src/main/java/com/bao/dating/controller/UserController.java
Normal file
48
src/main/java/com/bao/dating/controller/UserController.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* @param userLoginDTO 登录参数
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
||||
UserLoginVO userloginVO = userService.userLogin(userLoginDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "登录成功", userloginVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param userAccount 用户名称
|
||||
* @param userPassword 用户密码
|
||||
* @return 用户id
|
||||
*/
|
||||
@PostMapping("/userRegister")
|
||||
public Result userRegister(String userAccount, String userPassword){
|
||||
long result = userService.userRegister(userAccount, userPassword);
|
||||
if (result == -1){
|
||||
return Result.error(ResultCode.SYSTEM_ERROR);
|
||||
}
|
||||
if (result==-2){
|
||||
return Result.error(ResultCode.PARAM_ERROR);
|
||||
}
|
||||
if (result == -3){
|
||||
return Result.error(ResultCode.FAIL,"用户名称相同");
|
||||
}
|
||||
return Result.success(ResultCode.SUCCESS,"注册成功",result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.bao.dating.interceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.util.JwtUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
/**
|
||||
* HttpToken拦截器类
|
||||
* 用于拦截请求并验证JWT token的有效性,同时从token中解析用户信息
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TokenInterceptor implements HandlerInterceptor {
|
||||
/**
|
||||
* 在请求处理之前进行拦截
|
||||
* 从请求头或URL参数中获取token,验证其有效性,并将用户ID保存到ThreadLocal中
|
||||
* @param request HTTP请求对象
|
||||
* @param response HTTP响应对象
|
||||
* @param handler 处理器
|
||||
* @return 验证通过返回true,否则返回false
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
|
||||
//判断当前拦截到的是Controller的方法还是其他资源
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
//当前拦截到的不是动态方法,直接放行
|
||||
return true;
|
||||
}
|
||||
// 从 header 获取 token
|
||||
String token = request.getHeader("token");
|
||||
|
||||
try {
|
||||
log.info("jwt校验: {}", token);
|
||||
|
||||
// 验证 token 是否有效(包括是否过期)
|
||||
if (!JwtUtil.validateToken(token)) {
|
||||
log.error("Token 无效或已过期");
|
||||
response.setStatus(401);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析 token
|
||||
String userId = JwtUtil.getSubjectFromToken(token);
|
||||
log.info("用户: {}", userId);
|
||||
|
||||
// 保存 userId 到 ThreadLocal
|
||||
UserContext.setUserId(Long.valueOf(userId));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Token 校验失败: {}", e.getMessage());
|
||||
response.setStatus(401);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在请求完成之后执行清理工作
|
||||
* 清除保存在ThreadLocal中的用户ID,防止内存泄漏
|
||||
* @param request HTTP请求对象
|
||||
* @param response HTTP响应对象
|
||||
* @param handler 处理器
|
||||
* @param ex 异常对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
UserContext.clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
|
||||
import com.bao.dating.common.Comments;
|
||||
import com.bao.dating.common.Post;
|
||||
import com.bao.dating.pojo.entity.Comments;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostFavorite;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -12,4 +13,5 @@ public interface PostFavoriteMapper {
|
||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||
int addPostFavorite(PostFavorite postFavorite);
|
||||
int deletePostFavorite(@Param("postId") Long postId);
|
||||
List<Post> getAllPost(@Param("userId") Long userId);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.bao.dating.pojo.entity.Post;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface PostMapper {
|
||||
/**
|
||||
@@ -16,9 +18,9 @@ public interface PostMapper {
|
||||
/**
|
||||
* 根据ID删除动态
|
||||
*
|
||||
* @param postId 动态ID
|
||||
* @param postIds 动态ID
|
||||
*/
|
||||
void deletePostById(@Param("postId") Long postId);
|
||||
int deletePostByIds(@Param("postIds") List<Long> postIds);
|
||||
|
||||
/**
|
||||
* 根据ID查询动态
|
||||
|
||||
24
src/main/java/com/bao/dating/mapper/UserMapper.java
Normal file
24
src/main/java/com/bao/dating/mapper/UserMapper.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 用户
|
||||
*/
|
||||
User getByUsername(String username);
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
* @param user 用户对象
|
||||
* @return 受影响行数
|
||||
*/
|
||||
Long insertUser(User user);
|
||||
long getMaxUserId();
|
||||
}
|
||||
@@ -2,13 +2,14 @@ package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 动态数据传输对象
|
||||
*/
|
||||
@Data
|
||||
public class PostRequestDTO {
|
||||
public class PostRequestDTO implements Serializable {
|
||||
private String content;
|
||||
private List<String> mediaOssKeys;
|
||||
private List<String> tags;
|
||||
|
||||
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户登录数据传输对象
|
||||
*/
|
||||
@Data
|
||||
public class UserLoginDTO implements Serializable {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.bao.dating.common;
|
||||
package com.bao.dating.pojo.entity;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
@@ -10,7 +11,7 @@ import java.time.LocalDateTime;
|
||||
* 评论表
|
||||
*/
|
||||
@Data
|
||||
public class Comments {
|
||||
public class Comments implements Serializable {
|
||||
private Long comment_id; // 评论ID
|
||||
private String content; // 评论内容
|
||||
private Long user_id; // 评论人ID
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.pojo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@@ -10,7 +11,7 @@ import java.util.List;
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class Post {
|
||||
public class Post implements Serializable {
|
||||
|
||||
private Long postId;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.pojo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
@@ -9,7 +10,7 @@ import java.time.LocalDateTime;
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class PostFavorite {
|
||||
public class PostFavorite implements Serializable {
|
||||
|
||||
private Long favoriteId;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.pojo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
@@ -9,7 +10,7 @@ import java.time.LocalDateTime;
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class PostLike {
|
||||
public class PostLike implements Serializable {
|
||||
private Long likeId;
|
||||
|
||||
private Long userId;
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.pojo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -10,7 +11,7 @@ import java.time.LocalDateTime;
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class User {
|
||||
public class User implements Serializable {
|
||||
|
||||
private Long userId;
|
||||
|
||||
@@ -37,4 +38,8 @@ public class User {
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
private String userEmail;
|
||||
|
||||
private String userPhone;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@@ -9,7 +10,7 @@ import java.util.List;
|
||||
* 修改内容查询返回数据
|
||||
*/
|
||||
@Data
|
||||
public class PostEditVO {
|
||||
public class PostEditVO implements Serializable {
|
||||
private Long postId;
|
||||
private String content;
|
||||
private List<String> mediaOssKeys;
|
||||
|
||||
14
src/main/java/com/bao/dating/pojo/vo/UserLoginVO.java
Normal file
14
src/main/java/com/bao/dating/pojo/vo/UserLoginVO.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.bao.dating.pojo.vo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 登录返回数据
|
||||
*/
|
||||
@Data
|
||||
public class UserLoginVO implements Serializable {
|
||||
private Long userId;
|
||||
private String nickname;
|
||||
private String token;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
|
||||
import com.bao.dating.common.Comments;
|
||||
import com.bao.dating.pojo.entity.Comments;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface PostFavoriteService {
|
||||
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
||||
Result<?> deletePostFavorite(Long userid,Long postId);
|
||||
Result<List<Post>> getAllFavoritePost(Long userId);
|
||||
}
|
||||
|
||||
@@ -17,20 +17,18 @@ public interface PostService {
|
||||
|
||||
/**
|
||||
* 创建动态
|
||||
* @param userId 用户ID
|
||||
* @param postRequestDTO 动态数据传输对象
|
||||
* @return 创建的动态对象
|
||||
*/
|
||||
Post createPost(Long userId, PostRequestDTO postRequestDTO);
|
||||
Post createPost(PostRequestDTO postRequestDTO);
|
||||
|
||||
/**
|
||||
* 删除动态
|
||||
* @param postId 动态ID
|
||||
* 批量删除动态
|
||||
* @param postIds 动态ID
|
||||
* @return 删除的动态对象
|
||||
*/
|
||||
void deletePostById(Long postId);
|
||||
int deletePostById(List<Long> postIds);
|
||||
|
||||
void deletePostById(Integer postId);
|
||||
|
||||
/**
|
||||
* 查询动态详情(用于编辑)
|
||||
|
||||
14
src/main/java/com/bao/dating/service/UserService.java
Normal file
14
src/main/java/com/bao/dating/service/UserService.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
|
||||
public interface UserService {
|
||||
/**
|
||||
* 登录
|
||||
* @param userLoginDTO 登录参数
|
||||
* @return 登录结果
|
||||
*/
|
||||
UserLoginVO userLogin(UserLoginDTO userLoginDTO);
|
||||
long userRegister(String userAccount, String userPassword);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.common.Comments;
|
||||
import com.bao.dating.pojo.entity.Comments;
|
||||
|
||||
import com.bao.dating.mapper.CommentsMapper;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.PostFavorite;
|
||||
import com.bao.dating.service.PostFavoriteService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -58,4 +59,10 @@ public class PostFavoriteServiceImpl implements PostFavoriteService {
|
||||
postMapper.decreaseFavoriteCount(postId);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<Post>> getAllFavoritePost(Long userId) {
|
||||
List<Post> result = postFavoriteMapper.getAllPost(userId);
|
||||
return Result.success(ResultCode.SUCCESS,"查询成功",result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
@@ -12,10 +13,13 @@ import com.bao.dating.util.FileUtil;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -55,8 +59,11 @@ public class PostServiceImpl implements PostService {
|
||||
try {
|
||||
// 根据文件扩展名判断文件类型
|
||||
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
||||
// 创建目录
|
||||
String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
|
||||
// 生成唯一文件名
|
||||
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||
String newFileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||
String fileName = "post/" + dir + "/" + newFileName;
|
||||
// 获取文件字节数据
|
||||
byte[] fileBytes = file.getBytes();
|
||||
// 根据文件类型处理
|
||||
@@ -79,15 +86,15 @@ public class PostServiceImpl implements PostService {
|
||||
/**
|
||||
* 创建动态
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param postRequestDTO 动态数据传输对象
|
||||
* @return 创建的动态对象
|
||||
*/
|
||||
@Override
|
||||
public Post createPost(Long userId, PostRequestDTO postRequestDTO) {
|
||||
public Post createPost(PostRequestDTO postRequestDTO) {
|
||||
|
||||
// 创建动态对象
|
||||
Post post = new Post();
|
||||
Long userId = UserContext.getUserId();
|
||||
post.setUserId(userId);
|
||||
post.setContent(postRequestDTO.getContent());
|
||||
post.setTags(postRequestDTO.getTags());
|
||||
@@ -106,9 +113,6 @@ public class PostServiceImpl implements PostService {
|
||||
}
|
||||
|
||||
String textSuggestion = (String) textResult.get("suggestion");
|
||||
if ("block".equals(textSuggestion)) {
|
||||
throw new RuntimeException("动态内容违规,禁止发布");
|
||||
}
|
||||
|
||||
// 2. 图片审核(如果有)
|
||||
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
|
||||
@@ -153,29 +157,50 @@ public class PostServiceImpl implements PostService {
|
||||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePostById(Long postId) {
|
||||
postMapper.deletePostById(postId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除动态
|
||||
* 批量删除动态
|
||||
*
|
||||
* @param postId 动态ID
|
||||
* @param postIds 动态ID
|
||||
* @return 删除的动态对象
|
||||
*/
|
||||
@Override
|
||||
public void deletePostById(Integer postId) {
|
||||
postMapper.deletePostById(Long.valueOf(postId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostEditVO getPostForEdit(Long postId) {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int deletePostById(List<Long> postIds) {
|
||||
// 判断用户权限
|
||||
Long userId = UserContext.getUserId();
|
||||
|
||||
// 遍历所有要删除的帖子ID,验证权限
|
||||
for (Long postId : postIds) {
|
||||
Post post = postMapper.selectById(postId);
|
||||
if (post == null) {
|
||||
throw new RuntimeException("动态不存在");
|
||||
}
|
||||
// 验证用户权限
|
||||
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
|
||||
throw new RuntimeException("无权限删除此动态");
|
||||
}
|
||||
}
|
||||
// 批量删除动态
|
||||
return postMapper.deletePostByIds(postIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询动态详情(用于编辑)
|
||||
*
|
||||
* @param postId 动态ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PostEditVO getPostForEdit(Long postId) {
|
||||
Post post = postMapper.selectById(postId);
|
||||
if (post == null) {
|
||||
throw new RuntimeException("动态不存在");
|
||||
}
|
||||
// 判断用户权限
|
||||
Long userId = UserContext.getUserId();
|
||||
if (post.getUserId() == null || !post.getUserId().equals(userId)){
|
||||
throw new RuntimeException("无权限查看此动态");
|
||||
}
|
||||
PostEditVO postEditVO = new PostEditVO();
|
||||
BeanUtils.copyProperties(post, postEditVO);
|
||||
return postEditVO;
|
||||
@@ -194,6 +219,11 @@ public class PostServiceImpl implements PostService {
|
||||
if (post == null) {
|
||||
throw new RuntimeException("动态不存在");
|
||||
}
|
||||
// 判断用户权限
|
||||
Long userId = UserContext.getUserId();
|
||||
if (post.getUserId() == null || !post.getUserId().equals(userId)){
|
||||
throw new RuntimeException("无权限修改此动态");
|
||||
}
|
||||
post.setContent(postRequestDTO.getContent());
|
||||
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
|
||||
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.mapper.UserMapper;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
import com.bao.dating.util.JwtUtil;
|
||||
import com.bao.dating.util.MD5Util;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public UserLoginVO userLogin(UserLoginDTO userLoginDTO) {
|
||||
// 参数校验
|
||||
if (userLoginDTO == null || userLoginDTO.getUsername() == null || userLoginDTO.getPassword() == null) {
|
||||
throw new RuntimeException("用户名或密码不能为空");
|
||||
}
|
||||
// 查询用户
|
||||
User user = userMapper.getByUsername(userLoginDTO.getUsername());
|
||||
if (user == null){
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
// 密码校验
|
||||
boolean match = MD5Util.verifyWithSalt(
|
||||
userLoginDTO.getPassword(),
|
||||
user.getSalt(),
|
||||
user.getPasswordHash()
|
||||
);
|
||||
if (!match){
|
||||
throw new RuntimeException("密码错误");
|
||||
}
|
||||
// 生成token
|
||||
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
|
||||
// 封装返回
|
||||
UserLoginVO userLoginVO = new UserLoginVO();
|
||||
userLoginVO.setUserId(user.getUserId());
|
||||
userLoginVO.setNickname(user.getNickname());
|
||||
userLoginVO.setToken(token);
|
||||
return userLoginVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param userAccount 账户名称
|
||||
* @param userPassword 密码
|
||||
* @return 用户id
|
||||
*/
|
||||
@Override
|
||||
public long userRegister(String userAccount, String userPassword) {
|
||||
//验证非空
|
||||
if (userAccount.isEmpty() ||userPassword.isEmpty()){
|
||||
return -2;
|
||||
}
|
||||
//校验账户不能重复
|
||||
User resultUser = userMapper.getByUsername(userAccount);
|
||||
if (resultUser!=null){
|
||||
return -3;
|
||||
}
|
||||
//对密码进行加密
|
||||
String encryptPassword = MD5Util.encryptWithSalt(userPassword, "yujian");
|
||||
long maxUserId = userMapper.getMaxUserId();
|
||||
User user = new User();
|
||||
user.setUserId(maxUserId+1);
|
||||
user.setUserName(userAccount);
|
||||
user.setSalt("yujian");
|
||||
user.setPasswordHash(encryptPassword);
|
||||
user.setCreatedAt(LocalDateTime.now());
|
||||
user.setUpdatedAt(LocalDateTime.now());
|
||||
Long result = userMapper.insertUser(user);
|
||||
if (result < 0){
|
||||
return -1;
|
||||
}
|
||||
return user.getUserId();
|
||||
}
|
||||
}
|
||||
@@ -89,3 +89,5 @@ public class MD5Util {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,19 @@ spring:
|
||||
username: root
|
||||
password: JoyeeServe2025
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
password: ""
|
||||
database: 0
|
||||
timeout: 10000
|
||||
# 连接池配置(lettuce是Spring Boot默认Redis客户端,性能更优)
|
||||
lettuce:
|
||||
pool:
|
||||
max-active: 8 # 连接池最大连接数(默认8,可根据业务并发调整)
|
||||
max-wait: -1 # 连接池最大阻塞等待时间(毫秒,-1表示无限制)
|
||||
max-idle: 8 # 连接池最大空闲连接数(默认8)
|
||||
min-idle: 1 # 连接池最小空闲连接数(默认0,建议设置1-4,提高连接复用率)
|
||||
# 邮箱SMTP配置
|
||||
mail:
|
||||
host: smtp.163.com # QQ邮箱SMTP服务器地址
|
||||
@@ -46,9 +59,9 @@ aliyun:
|
||||
secret: bBHBAPiCqGyVBHUv07348wsHXkKqrk
|
||||
scenes: antispam
|
||||
# 阿里云短信服务配置
|
||||
# sms:
|
||||
# access-key-id: LTAI5t5vpcbCZwweNHEDDDaF
|
||||
# access-key-secret: bBHBAPiCqGyVBHUv07348wsHXkKqrk
|
||||
# region-id: cn-hangzhou
|
||||
# sign-name:
|
||||
# template-code: SMS_123456789
|
||||
sms:
|
||||
access-key-id: LTAI5t5vpcbCZwweNHEDDDaF
|
||||
access-key-secret: bBHBAPiCqGyVBHUv07348wsHXkKqrk
|
||||
region-id: cn-hangzhou
|
||||
sign-name: 速通互联验证码
|
||||
template-code: 100001
|
||||
|
||||
@@ -14,4 +14,8 @@
|
||||
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
||||
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
||||
</select>
|
||||
<!--查询用户收藏的所有动态-->
|
||||
<select id="getAllPost" resultType="com.bao.dating.pojo.entity.Post">
|
||||
select * from post where post_id in (select post_id from post_favorite where user_id=#{userId})
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -29,8 +29,23 @@
|
||||
</insert>
|
||||
|
||||
<!--动态删除-->
|
||||
<delete id="deletePostById">
|
||||
DELETE FROM post WHERE post_id = #{postId}
|
||||
<delete id="deletePostByIds">
|
||||
DELETE FROM post WHERE post_id IN
|
||||
<foreach item="postId" index="index" collection="postIds" separator="," open="(" close=")">
|
||||
#{postId}
|
||||
</foreach>
|
||||
</delete>
|
||||
<!--删除收藏记录-->
|
||||
<delete id="1">
|
||||
DELETE FROM post_favorite WHERE post_id = #{postId}
|
||||
</delete>
|
||||
<!--删除点赞记录-->
|
||||
<delete id="2">
|
||||
DELETE FROM post_like WHERE post_id = #{postId}
|
||||
</delete>
|
||||
<!--动态评论删除-->
|
||||
<delete id="3">
|
||||
DELETE FROM comments WHERE post_id = #{postId}
|
||||
</delete>
|
||||
|
||||
<!--动态查询-->
|
||||
|
||||
17
src/main/resources/com/bao/dating/mapper/UserMapper.xml
Normal file
17
src/main/resources/com/bao/dating/mapper/UserMapper.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||
<insert id="insertUser" useGeneratedKeys="true" parameterType="com.bao.dating.pojo.entity.User">
|
||||
insert into user(user_id,user_name,salt,password_hash,created_at,updated_at) values (#{userId},#{userName},#{salt},#{passwordHash},#{createdAt},#{updatedAt})
|
||||
</insert>
|
||||
|
||||
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||
SELECT * FROM user WHERE user_name = #{userName}
|
||||
</select>
|
||||
<select id="getMaxUserId" resultType="java.lang.Long">
|
||||
SELECT IFNULL(MAX(user_id), 0) AS max_id FROM user;
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -73,3 +73,5 @@ public class EmailAndSmsTest {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user