Compare commits
26 Commits
feature-Po
...
3d8a32cbf7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d8a32cbf7 | ||
|
|
34f41d61e2 | ||
|
|
0d166aa400 | ||
|
|
44c0b3611d | ||
|
|
f98b0e26f2 | ||
|
|
79345eb93e | ||
|
|
bfd6674dd9 | ||
|
|
70a1d0012e | ||
|
|
cc88ec820c | ||
|
|
0c4ddc2803 | ||
|
|
f31b42a038 | ||
|
|
9cf50ce7df | ||
|
|
401c2fa8bf | ||
|
|
fec7bb04b9 | ||
|
|
cd0abad225 | ||
|
|
96b256d46e | ||
|
|
fca54a6f97 | ||
|
|
0f8f47de8e | ||
|
|
2cb8ae5c3c | ||
|
|
7abd6fe27d | ||
|
|
dfc9508827 | ||
|
|
4c70bd3c6f | ||
|
|
d3c069967e | ||
| 4a2aff888a | |||
| 0b0959fa80 | |||
|
|
4401a8a44a |
6
pom.xml
6
pom.xml
@@ -71,6 +71,12 @@
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- AOP起步依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里云相关依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
|
||||
11
src/main/java/com/bao/dating/anno/Log.java
Normal file
11
src/main/java/com/bao/dating/anno/Log.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.bao.dating.anno;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Log {
|
||||
}
|
||||
62
src/main/java/com/bao/dating/aspect/OperateLogAspect.java
Normal file
62
src/main/java/com/bao/dating/aspect/OperateLogAspect.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.bao.dating.aspect;
|
||||
|
||||
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.mapper.OperateLogMapper;
|
||||
import com.bao.dating.pojo.entity.OperateLog;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class OperateLogAspect {
|
||||
|
||||
@Autowired
|
||||
private OperateLogMapper operateLogMapper;
|
||||
|
||||
@Around("@annotation(com.bao.dating.anno.Log)")
|
||||
public Object logOperate(ProceedingJoinPoint pjp) throws Throwable{
|
||||
|
||||
// 记录方法开始的时间
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// 执行目标方法
|
||||
Object result = pjp.proceed();
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
long costTime = endTime - startTime;
|
||||
|
||||
|
||||
// 构建日志对象
|
||||
OperateLog operatelog = new OperateLog();
|
||||
operatelog.setOperateUserId(getUserId());
|
||||
operatelog.setOperateTime(LocalDateTime.now());
|
||||
operatelog.setClassName(pjp.getTarget().getClass().getName());
|
||||
operatelog.setMethodName(pjp.getSignature().getName());
|
||||
operatelog.setMethodParams(Arrays.toString(pjp.getArgs()));
|
||||
operatelog.setReturnValue(result != null ? result.toString() : "void");
|
||||
operatelog.setCostTime(costTime);
|
||||
|
||||
log.info("记录操作日志: {}", operatelog);
|
||||
|
||||
operateLogMapper.insert(operatelog);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Long getUserId() {
|
||||
return UserContext.getUserId();
|
||||
}
|
||||
|
||||
}
|
||||
31
src/main/java/com/bao/dating/aspect/RecordTimeAspect.java
Normal file
31
src/main/java/com/bao/dating/aspect/RecordTimeAspect.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.bao.dating.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 记录方法运行耗时
|
||||
* @author KilLze
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class RecordTimeAspect {
|
||||
@Around("execution(* com.bao.dating.service.impl.*.*(..))")
|
||||
public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
|
||||
//1. 记录方法运行的开始时间
|
||||
long begin = System.currentTimeMillis();
|
||||
|
||||
//2. 执行原始的方法
|
||||
Object result = pjp.proceed();
|
||||
|
||||
//3. 记录方法运行的结束时间, 记录耗时
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("方法 {} 执行耗时: {}ms", pjp.getSignature() ,end-begin);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,27 @@
|
||||
package com.bao.dating.common;
|
||||
|
||||
/**
|
||||
* 响应状态码枚举
|
||||
* @author KilLze
|
||||
*/
|
||||
public enum ResultCode {
|
||||
/** 成功 */
|
||||
SUCCESS(200, "成功"),
|
||||
/** 请求已成功处理 */
|
||||
SUCCESS_REVIEW(201, "请求已成功处理"),
|
||||
/** 删除成功 */
|
||||
SUCCESS_DELETE(204, "删除成功"),
|
||||
/** 参数错误 */
|
||||
PARAM_ERROR(400, "参数错误"),
|
||||
/** 未登录或 Token 失效 */
|
||||
UNAUTHORIZED(401, "未登录或 Token 失效"),
|
||||
/** 无权限 */
|
||||
FORBIDDEN(403, "无权限"),
|
||||
/** 数据不存在 */
|
||||
NOT_FOUND(404, "数据不存在"),
|
||||
/** 系统异常 */
|
||||
SYSTEM_ERROR(500, "系统异常"),
|
||||
/** 操作失败 */
|
||||
FAIL(500, "操作失败");
|
||||
|
||||
private final int code;
|
||||
|
||||
@@ -4,16 +4,17 @@ import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 阿里云OSS工具类
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
@Slf4j
|
||||
@Component
|
||||
|
||||
11
src/main/java/com/bao/dating/common/result/AliOssResult.java
Normal file
11
src/main/java/com/bao/dating/common/result/AliOssResult.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.bao.dating.common.result;
|
||||
|
||||
/**
|
||||
* 阿里云 OSS 文件上传结果
|
||||
* @author KilLze
|
||||
*/
|
||||
public class AliOssResult {
|
||||
public static final String IMAGE = "image";
|
||||
public static final String VIDEO = "video";
|
||||
|
||||
}
|
||||
15
src/main/java/com/bao/dating/common/result/FileResult.java
Normal file
15
src/main/java/com/bao/dating/common/result/FileResult.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.bao.dating.common.result;
|
||||
|
||||
/**
|
||||
* 文件上传结果
|
||||
* @author KilLze
|
||||
*/
|
||||
public class FileResult {
|
||||
public static final String JPG = "jpg";
|
||||
public static final String JPEG = "jpeg";
|
||||
public static final String PNG = "png";
|
||||
public static final String GIF = "gif";
|
||||
public static final String MP4 = "mp4";
|
||||
public static final String AVI = "avi";
|
||||
public static final String MOV = "mov";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.bao.dating.common.result;
|
||||
|
||||
/**
|
||||
* 阿里云敏感内容审核结果
|
||||
* @author KilLze
|
||||
*/
|
||||
public class GreenAuditResult {
|
||||
public static final String PASS = "pass";
|
||||
public static final String REVIEW = "review";
|
||||
public static final String BLOCK = "block";
|
||||
}
|
||||
35
src/main/java/com/bao/dating/config/WebConfig.java
Normal file
35
src/main/java/com/bao/dating/config/WebConfig.java
Normal file
@@ -0,0 +1,35 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* WebMvc配置类
|
||||
* @author KilLze
|
||||
*/
|
||||
@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"
|
||||
);
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
/**
|
||||
* 用户上下文类,用于保存当前线程的用户ID
|
||||
* @author lenovo
|
||||
*/
|
||||
public class UserContext {
|
||||
|
||||
private static final ThreadLocal<Long> USER_HOLDER = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置当前线程的用户ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
public static void setUserId(Long userId) {
|
||||
USER_HOLDER.set(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的用户ID
|
||||
* @return 当前用户ID,如果未设置则返回null
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return USER_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前线程的用户ID
|
||||
*/
|
||||
public static void clear() {
|
||||
USER_HOLDER.remove();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
|
||||
import com.bao.dating.anno.Log;
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||
@@ -13,6 +14,11 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 动态接口
|
||||
*
|
||||
* @author KilLze
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/posts")
|
||||
public class PostController {
|
||||
@@ -25,23 +31,24 @@ public class PostController {
|
||||
* @param files 媒体文件数组
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
@Log
|
||||
@PostMapping(value = "/upload", consumes = "multipart/form-data")
|
||||
public Result<List<String>> uploadMedia(@RequestParam("files") MultipartFile[] files) {
|
||||
List<String> fileUrls = postService.uploadMedia(files);
|
||||
return Result.success(ResultCode.SUCCESS_REVIEW, "文件上传成功", fileUrls);
|
||||
return Result.success(ResultCode.SUCCESS, "文件上传成功", fileUrls);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布动态接口 - JSON格式请求
|
||||
* @param postDTO 动态信息
|
||||
* @param userId 用户ID
|
||||
* @return 发布的动态对象
|
||||
*/
|
||||
@PostMapping(consumes = "application/json")
|
||||
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO, @RequestParam Long userId) {
|
||||
@Log
|
||||
@PostMapping( "/createPost")
|
||||
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO) {
|
||||
// 调用 Service 层处理发布动态业务逻辑
|
||||
Post result = postService.createPost(userId, postDTO);
|
||||
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
||||
Post result = postService.createPost(postDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "动态发布成功,等待审核。", result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,8 +57,9 @@ public class PostController {
|
||||
* @param postIds 动态ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
@DeleteMapping
|
||||
public Result<String> deleteById(@RequestParam List<Long> postIds){
|
||||
@Log
|
||||
@PostMapping("/deletePost")
|
||||
public Result<String> deleteById(@RequestBody List<Long> postIds){
|
||||
int deletedCount = postService.deletePostById(postIds);
|
||||
return Result.success(ResultCode.SUCCESS_DELETE, deletedCount > 0 ? "成功删除" : "删除失败,该动态不存在", null);
|
||||
}
|
||||
@@ -61,7 +69,7 @@ public class PostController {
|
||||
* @param postId 动态ID
|
||||
* @return 动态对象
|
||||
*/
|
||||
@GetMapping("/{postId}")
|
||||
@PostMapping("/{postId}")
|
||||
public Result<PostEditVO> getPostById(@PathVariable Long postId) {
|
||||
PostEditVO postEditVO = postService.getPostForEdit(postId);
|
||||
return Result.success(ResultCode.SUCCESS,"查询成功", postEditVO);
|
||||
@@ -73,9 +81,10 @@ public class PostController {
|
||||
* @param postRequestDTO 动态信息
|
||||
* @return 更新后的动态对象
|
||||
*/
|
||||
@PutMapping("/{postId}")
|
||||
@Log
|
||||
@PostMapping("/{postId}/updatePost")
|
||||
public Result<PostEditVO> updatePost(@PathVariable Long postId, @RequestBody PostRequestDTO postRequestDTO) {
|
||||
PostEditVO result = postService.updatePost(postId, postRequestDTO);
|
||||
return Result.success(ResultCode.SUCCESS_REVIEW, "动态更新成功", result);
|
||||
return Result.success(ResultCode.SUCCESS, "动态更新成功", result);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,86 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
import com.bao.dating.anno.Log;
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 用户接口
|
||||
*
|
||||
* @author KilLze
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @return 用户信息
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
public Result<UserInfoVO> getUserInfo() {
|
||||
Long userId = UserContext.getUserId();
|
||||
UserInfoVO userInfoVO = userService.getUserInfo(userId);
|
||||
return Result.success(ResultCode.SUCCESS, "获取用户信息成功", userInfoVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传头像接口
|
||||
* @param file 头像文件
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
@Log
|
||||
@PostMapping(value = "/info/uploadAvatar", consumes = "multipart/form-data")
|
||||
public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
|
||||
String fileUrl = userService.uploadAvatar(file);
|
||||
return Result.success(ResultCode.SUCCESS, "头像上传成功", fileUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传背景接口
|
||||
* @param file 背景文件
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
@Log
|
||||
@PostMapping(value = "/info/uploadBackground", consumes = "multipart/form-data")
|
||||
public Result<String> uploadBackground(@RequestParam("file") MultipartFile file) {
|
||||
String fileUrl = userService.uploadBackground(file);
|
||||
return Result.success(ResultCode.SUCCESS, "背景上传成功", fileUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
@Log
|
||||
@PostMapping("/info/update")
|
||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
Long userId = UserContext.getUserId();
|
||||
userInfoUpdateDTO.setUserId(userId);
|
||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
||||
}
|
||||
}
|
||||
|
||||
109
src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java
Normal file
109
src/main/java/com/bao/dating/handler/GlobalExceptionHandler.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package com.bao.dating.handler;
|
||||
|
||||
import com.bao.dating.common.Result;
|
||||
import com.bao.dating.common.ResultCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
* 统一处理控制器层抛出的异常
|
||||
* @author KilLze
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* 处理参数验证失败异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
log.error("参数验证失败: {}", e.getMessage());
|
||||
return Result.error(ResultCode.PARAM_ERROR, e.getBindingResult().getFieldError().getDefaultMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求参数缺失异常
|
||||
*/
|
||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||
public Result<String> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
|
||||
log.error("请求参数缺失: 参数名={}, 参数类型={}", e.getParameterName(), e.getParameterType());
|
||||
return Result.error(ResultCode.PARAM_ERROR, "缺少必需的请求参数: " + e.getParameterName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求参数类型不匹配异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||
public Result<String> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
|
||||
log.error("请求参数类型不匹配: {}", e.getMessage());
|
||||
return Result.error(ResultCode.PARAM_ERROR, "请求参数类型错误: " + e.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求体缺失或格式错误异常
|
||||
*/
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public Result<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||
log.error("请求体格式错误: {}", e.getMessage());
|
||||
return Result.error(ResultCode.PARAM_ERROR, "请求体格式错误或缺失");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理不支持的HTTP请求方法异常
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
public Result<String> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
||||
log.error("不支持的HTTP请求方法: {}", e.getMethod());
|
||||
return Result.error(ResultCode.PARAM_ERROR, "不支持的请求方法: " + e.getMethod());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理404异常
|
||||
*/
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
public Result<String> handleNoHandlerFoundException(HttpServletRequest request, NoHandlerFoundException e) {
|
||||
log.error("请求的接口不存在: {} {}", request.getMethod(), request.getRequestURI());
|
||||
return Result.error(ResultCode.NOT_FOUND, "请求的接口不存在");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据库唯一约束违反异常
|
||||
*/
|
||||
@ExceptionHandler(DuplicateKeyException.class)
|
||||
public Result<String> handleDuplicateKeyException(DuplicateKeyException e) {
|
||||
log.error("数据库唯一约束违反: {}", e.getMessage());
|
||||
return Result.error(ResultCode.FAIL, "数据已存在,操作失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运行时异常
|
||||
*/
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public Result<String> handleRuntimeException(RuntimeException e) {
|
||||
log.error("运行时异常: ", e);
|
||||
return Result.error(ResultCode.SYSTEM_ERROR, e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理通用异常
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Result<String> handleException(HttpServletRequest request, Exception e) {
|
||||
log.error("系统异常 [{} {}]: ", request.getMethod(), request.getRequestURI(), e);
|
||||
return Result.error(ResultCode.SYSTEM_ERROR, "系统内部错误");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.bao.dating.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* List类型转换成JSON类型
|
||||
* @author KilLze
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
@MappedTypes(List.class)
|
||||
public class ListToJsonTypeHandler implements TypeHandler<List<String>> {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
||||
if (parameter == null || parameter.isEmpty()) {
|
||||
ps.setNull(i, java.sql.Types.VARCHAR); // 或者 Types.JSON 如果数据库支持
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String json = OBJECT_MAPPER.writeValueAsString(parameter);
|
||||
ps.setString(i, json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SQLException("Error converting list to JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
|
||||
String json = rs.getString(columnName);
|
||||
return convertJsonToList(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
String json = rs.getString(columnIndex);
|
||||
return convertJsonToList(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getResult(java.sql.CallableStatement cs, int columnIndex) throws SQLException {
|
||||
String json = cs.getString(columnIndex);
|
||||
return convertJsonToList(json);
|
||||
}
|
||||
|
||||
private List<String> convertJsonToList(String json) throws SQLException {
|
||||
if (json == null || json.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(json, new TypeReference<List<String>>() {});
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SQLException("Error converting JSON to list", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,7 @@ import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -22,6 +19,11 @@ import java.util.List;
|
||||
public class ListToVarcharTypeHandler implements TypeHandler<List<String>> {
|
||||
@Override
|
||||
public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
|
||||
// 允许 null
|
||||
if (strings == null || strings.isEmpty()) {
|
||||
preparedStatement.setNull(i, Types.VARCHAR);
|
||||
return;
|
||||
}
|
||||
// 遍历List类型的入参,拼装为String类型,使用Statement对象插入数据库
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int j = 0; j < strings.size(); j++) {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
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中解析用户信息
|
||||
* @author KilLze
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
19
src/main/java/com/bao/dating/mapper/OperateLogMapper.java
Normal file
19
src/main/java/com/bao/dating/mapper/OperateLogMapper.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
|
||||
import com.bao.dating.pojo.entity.OperateLog;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 操作日志Mapper
|
||||
* @author KilLze
|
||||
*/
|
||||
@Mapper
|
||||
public interface OperateLogMapper {
|
||||
|
||||
@Insert("insert into operate_log (operate_user_id, operate_time, class_name, method_name, method_params, return_value, cost_time) " +
|
||||
"values (#{operateUserId}, #{operateTime}, #{className}, #{methodName}, #{methodParams}, #{returnValue}, #{costTime});")
|
||||
public void insert(OperateLog log);
|
||||
|
||||
}
|
||||
@@ -6,12 +6,17 @@ import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 动态Mapper
|
||||
*
|
||||
* @author KilLze lanyangyang-yzx
|
||||
*/
|
||||
@Mapper
|
||||
public interface PostMapper {
|
||||
/**
|
||||
* 插入动态
|
||||
*
|
||||
* @param post
|
||||
* @param post 动态
|
||||
*/
|
||||
void insert(Post post);
|
||||
|
||||
@@ -20,29 +25,28 @@ public interface PostMapper {
|
||||
*
|
||||
* @param postIds 动态ID
|
||||
*/
|
||||
int deletePostByIds(@Param("postIds") List<Long> postIds);
|
||||
int deletePostByIds(List<Long> postIds);
|
||||
|
||||
/**
|
||||
* 根据ID查询动态
|
||||
*
|
||||
* @param postId
|
||||
* @return
|
||||
* @param postId 动态ID
|
||||
* @return 动态
|
||||
*/
|
||||
Post selectById(@Param("postId") Long postId);
|
||||
Post selectById(Long postId);
|
||||
|
||||
/**
|
||||
* 根据ID更新动态
|
||||
*
|
||||
* @param post
|
||||
* @return
|
||||
* @param post 动态
|
||||
*/
|
||||
void updateById(Post post);
|
||||
|
||||
/**
|
||||
* 查询点赞数
|
||||
*
|
||||
* @param postId
|
||||
* @return
|
||||
* @param postId 动态ID
|
||||
* @return 点赞数
|
||||
*/
|
||||
int selectLikeCount(Long postId);
|
||||
|
||||
@@ -72,8 +76,8 @@ public interface PostMapper {
|
||||
/**
|
||||
* 查询点赞数
|
||||
*
|
||||
* @param postId
|
||||
* @return
|
||||
* @param postId 动态ID
|
||||
* @return 点赞数
|
||||
*/
|
||||
int selectFavoriteCount(Long postId);
|
||||
|
||||
|
||||
@@ -1,7 +1,36 @@
|
||||
package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 用户Mapper
|
||||
* @author KilLze
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 用户
|
||||
*/
|
||||
User getByUsername(String username);
|
||||
|
||||
/**
|
||||
* 根据用户id查询用户信息
|
||||
*
|
||||
* @param userid 用户id
|
||||
* @return 用户
|
||||
*/
|
||||
User selectByUserId(Long userid);
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
*/
|
||||
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||
|
||||
}
|
||||
|
||||
@@ -7,9 +7,10 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* 动态数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class PostRequestDTO implements Serializable {
|
||||
public class PostRequestDTO implements Serializable{
|
||||
private String content;
|
||||
private List<String> mediaOssKeys;
|
||||
private List<String> tags;
|
||||
|
||||
25
src/main/java/com/bao/dating/pojo/dto/UserInfoUpdateDTO.java
Normal file
25
src/main/java/com/bao/dating/pojo/dto/UserInfoUpdateDTO.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户信息更新数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class UserInfoUpdateDTO {
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String nickname;
|
||||
private String avatarUrl;
|
||||
private String backgroundUrl;
|
||||
private Integer gender;
|
||||
private LocalDate birthday;
|
||||
private List<String> hobbies;
|
||||
private String signature;
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
15
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
15
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.bao.dating.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户登录数据传输对象
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class UserLoginDTO implements Serializable {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
28
src/main/java/com/bao/dating/pojo/entity/OperateLog.java
Normal file
28
src/main/java/com/bao/dating/pojo/entity/OperateLog.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.bao.dating.pojo.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 操作日志
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class OperateLog {
|
||||
/** ID */
|
||||
private Long id;
|
||||
/** 操作人ID */
|
||||
private Long operateUserId;
|
||||
/** 操作时间 */
|
||||
private LocalDateTime operateTime;
|
||||
/** 操作类名 */
|
||||
private String className;
|
||||
/** 操作方法名 */
|
||||
private String methodName;
|
||||
/** 操作方法参数 */
|
||||
private String methodParams;
|
||||
/** 操作方法返回值 */
|
||||
private String returnValue;
|
||||
/** 操作耗时 */
|
||||
private Long costTime;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import lombok.Data;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户表
|
||||
@@ -31,11 +32,15 @@ public class User implements Serializable {
|
||||
|
||||
private LocalDate birthday;
|
||||
|
||||
private String hobbies;
|
||||
private List<String> hobbies;
|
||||
|
||||
private String signature;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
private String userEmail;
|
||||
|
||||
private String userPhone;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* 修改内容查询返回数据
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class PostEditVO implements Serializable {
|
||||
|
||||
27
src/main/java/com/bao/dating/pojo/vo/UserInfoVO.java
Normal file
27
src/main/java/com/bao/dating/pojo/vo/UserInfoVO.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.bao.dating.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户信息VO
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class UserInfoVO implements Serializable {
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String nickname;
|
||||
private String avatarUrl;
|
||||
private String backgroundUrl;
|
||||
private Integer gender;
|
||||
private LocalDate birthday;
|
||||
private List<String> hobbies;
|
||||
private String signature;
|
||||
private LocalDateTime updatedAt;
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 登录返回数据
|
||||
* @author KilLze
|
||||
*/
|
||||
@Data
|
||||
public class UserLoginVO implements Serializable {
|
||||
|
||||
@@ -7,6 +7,10 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 动态服务
|
||||
* @author bao KilLze lanyangyang-yzx yang
|
||||
*/
|
||||
public interface PostService {
|
||||
/**
|
||||
* 上传媒体文件
|
||||
@@ -17,11 +21,10 @@ public interface PostService {
|
||||
|
||||
/**
|
||||
* 创建动态
|
||||
* @param userId 用户ID
|
||||
* @param postRequestDTO 动态数据传输对象
|
||||
* @return 创建的动态对象
|
||||
*/
|
||||
Post createPost(Long userId, PostRequestDTO postRequestDTO);
|
||||
Post createPost(PostRequestDTO postRequestDTO);
|
||||
|
||||
/**
|
||||
* 批量删除动态
|
||||
@@ -34,6 +37,7 @@ public interface PostService {
|
||||
/**
|
||||
* 查询动态详情(用于编辑)
|
||||
* @param postId 动态ID
|
||||
* @return 动态详情
|
||||
*/
|
||||
PostEditVO getPostForEdit(Long postId);
|
||||
|
||||
|
||||
@@ -1,4 +1,48 @@
|
||||
package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 用户服务接口
|
||||
* @author KilLze
|
||||
*/
|
||||
public interface UserService {
|
||||
/**
|
||||
* 登录
|
||||
* @param userLoginDTO 登录参数
|
||||
* @return 登录结果
|
||||
*/
|
||||
UserLoginVO userLogin(UserLoginDTO userLoginDTO);
|
||||
|
||||
/**
|
||||
* 查询个人信息
|
||||
* @param userId 动态ID
|
||||
* @return 个人信息
|
||||
*/
|
||||
UserInfoVO getUserInfo(Long userId);
|
||||
|
||||
/**
|
||||
* 上传头像
|
||||
* @param file 头像文件
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
String uploadAvatar(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 上传背景
|
||||
* @param file 背景文件
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
String uploadBackground(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param userInfoUpdateDTO 用户信息
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||
import com.bao.dating.common.result.GreenAuditResult;
|
||||
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;
|
||||
@@ -19,15 +21,12 @@ 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;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 动态服务实现类
|
||||
*
|
||||
* @author KilLze
|
||||
* @author KilLze yang
|
||||
*/
|
||||
@Service
|
||||
public class PostServiceImpl implements PostService {
|
||||
@@ -51,49 +50,70 @@ public class PostServiceImpl implements PostService {
|
||||
*/
|
||||
@Override
|
||||
public List<String> uploadMedia(MultipartFile[] files) {
|
||||
// 如果没有文件,则返回空列表
|
||||
if (files == null || files.length == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 创建媒体文件列表
|
||||
List<String> mediaUrls = new ArrayList<>();
|
||||
if (files != null && files.length > 0) {
|
||||
|
||||
for (MultipartFile file : files) {
|
||||
if (!file.isEmpty()) {
|
||||
// 跳过空文件
|
||||
if (file == null || file.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
// 获取文件名并跳过空文件
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
if (originalFilename == null) {
|
||||
continue;
|
||||
}
|
||||
// 校验文件类型
|
||||
String fileType = FileUtil.getFileType(originalFilename);
|
||||
if (!"image".equals(fileType) && !"video".equals(fileType)) {
|
||||
throw new RuntimeException("不支持的文件类型:" + originalFilename);
|
||||
}
|
||||
|
||||
// 创建目录
|
||||
String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
|
||||
// 获取文件扩展名
|
||||
String extension = FileUtil.getFileExtension(originalFilename);
|
||||
// 生成唯一文件名
|
||||
String newFileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
|
||||
// 获取用户ID
|
||||
Long userId = UserContext.getUserId();
|
||||
// 创建文件名
|
||||
String fileName = "post/" + userId + "/" + dir + "/" + newFileName;
|
||||
|
||||
try {
|
||||
// 根据文件扩展名判断文件类型
|
||||
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
||||
// 创建目录
|
||||
String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
|
||||
// 生成唯一文件名
|
||||
String newFileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||
String fileName = "post/" + dir + "/" + newFileName;
|
||||
// 获取文件字节数据
|
||||
byte[] fileBytes = file.getBytes();
|
||||
// 根据文件类型处理
|
||||
String ossUrl = "";
|
||||
if ("image".equals(fileType) || "video".equals(fileType)) {
|
||||
// 上传图片或视频
|
||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||
// 上传图片或视频
|
||||
String ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||
if (ossUrl == null || ossUrl.isEmpty()) {
|
||||
throw new RuntimeException("文件上传失败:" + originalFilename);
|
||||
}
|
||||
// 添加上传后的 URL
|
||||
mediaUrls.add(ossUrl);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// 统一异常处理
|
||||
throw new RuntimeException("上传媒体文件失败:" + originalFilename, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mediaUrls;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建动态
|
||||
*
|
||||
* @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());
|
||||
@@ -125,10 +145,10 @@ public class PostServiceImpl implements PostService {
|
||||
String imageSuggestion = (String) imageResult.get("suggestion");
|
||||
|
||||
// 根据审核结果设置状态
|
||||
if ("block".equals(textSuggestion) || "block".equals(imageSuggestion)) {
|
||||
if (GreenAuditResult.BLOCK.equals(textSuggestion) || GreenAuditResult.BLOCK.equals(imageSuggestion)) {
|
||||
// 审核未通过,允许用户修改
|
||||
post.setIsPublic(2);
|
||||
} else if ("review".equals(textSuggestion) || "review".equals(imageSuggestion)) {
|
||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion) || GreenAuditResult.REVIEW.equals(imageSuggestion)) {
|
||||
// 待审核,需人工审核
|
||||
post.setIsPublic(1);
|
||||
} else {
|
||||
@@ -137,10 +157,10 @@ public class PostServiceImpl implements PostService {
|
||||
}
|
||||
} else {
|
||||
// 只有文本内容的情况
|
||||
if ("block".equals(textSuggestion)) {
|
||||
if (GreenAuditResult.BLOCK.equals(textSuggestion)) {
|
||||
// 审核未通过,允许用户修改
|
||||
post.setIsPublic(2);
|
||||
} else if ("review".equals(textSuggestion)) {
|
||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion)) {
|
||||
// 待审核,需人工审核
|
||||
post.setIsPublic(1);
|
||||
} else {
|
||||
@@ -165,6 +185,20 @@ public class PostServiceImpl implements PostService {
|
||||
@Override
|
||||
@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);
|
||||
}
|
||||
@@ -173,15 +207,19 @@ public class PostServiceImpl implements PostService {
|
||||
* 查询动态详情(用于编辑)
|
||||
*
|
||||
* @param postId 动态ID
|
||||
* @return
|
||||
* @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;
|
||||
@@ -191,7 +229,7 @@ public class PostServiceImpl implements PostService {
|
||||
* 修改动态
|
||||
* @param postId 动态ID
|
||||
* @param postRequestDTO 修改的动态数据传输对象
|
||||
* @return
|
||||
* @return 修改的动态对象
|
||||
*/
|
||||
@Override
|
||||
public PostEditVO updatePost(Long postId, PostRequestDTO postRequestDTO) {
|
||||
@@ -200,10 +238,14 @@ public class PostServiceImpl implements PostService {
|
||||
if (post == null) {
|
||||
throw new RuntimeException("动态不存在");
|
||||
}
|
||||
post.setContent(postRequestDTO.getContent());
|
||||
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
|
||||
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
|
||||
// 判断用户权限
|
||||
Long userId = UserContext.getUserId();
|
||||
if (post.getUserId() == null || !post.getUserId().equals(userId)){
|
||||
throw new RuntimeException("无权限修改此动态");
|
||||
}
|
||||
post.setContent(postRequestDTO.getContent());
|
||||
// 如果请求中的mediaOssKeys不为null(即使是空列表),则更新为新值
|
||||
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
|
||||
|
||||
// 1. 文本内容审核
|
||||
Map textResult;
|
||||
@@ -215,11 +257,11 @@ public class PostServiceImpl implements PostService {
|
||||
// 文本审核结果
|
||||
String textSuggestion = (String) textResult.get("suggestion");
|
||||
|
||||
// 2. 图片审核(如果有)
|
||||
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
|
||||
// 2. 图片审核(如果有媒体文件)
|
||||
if (post.getMediaOssKeys() != null && !post.getMediaOssKeys().isEmpty()) {
|
||||
Map imageResult;
|
||||
try {
|
||||
imageResult = greenImageScan.imageScan(postRequestDTO.getMediaOssKeys());
|
||||
imageResult = greenImageScan.imageScan(post.getMediaOssKeys());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -227,10 +269,10 @@ public class PostServiceImpl implements PostService {
|
||||
String imageSuggestion = (String) imageResult.get("suggestion");
|
||||
|
||||
// 根据审核结果设置状态
|
||||
if ("block".equals(textSuggestion) || "block".equals(imageSuggestion)) {
|
||||
if (GreenAuditResult.BLOCK.equals(textSuggestion) || GreenAuditResult.BLOCK.equals(imageSuggestion)) {
|
||||
// 审核未通过,允许用户修改
|
||||
post.setIsPublic(2);
|
||||
} else if ("review".equals(textSuggestion) || "review".equals(imageSuggestion)) {
|
||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion) || GreenAuditResult.REVIEW.equals(imageSuggestion)) {
|
||||
// 待审核,需人工审核
|
||||
post.setIsPublic(1);
|
||||
} else {
|
||||
@@ -239,10 +281,10 @@ public class PostServiceImpl implements PostService {
|
||||
}
|
||||
} else {
|
||||
// 只有文本内容的情况
|
||||
if ("block".equals(textSuggestion)) {
|
||||
if (GreenAuditResult.BLOCK.equals(textSuggestion)) {
|
||||
// 审核未通过,允许用户修改
|
||||
post.setIsPublic(2);
|
||||
} else if ("review".equals(textSuggestion)) {
|
||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion)) {
|
||||
// 待审核,需人工审核
|
||||
post.setIsPublic(1);
|
||||
} else {
|
||||
|
||||
@@ -1,13 +1,278 @@
|
||||
package com.bao.dating.service.impl;
|
||||
|
||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||
import com.bao.dating.common.result.AliOssResult;
|
||||
import com.bao.dating.common.result.GreenAuditResult;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.mapper.UserMapper;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
import com.bao.dating.util.FileUtil;
|
||||
import com.bao.dating.util.JwtUtil;
|
||||
import com.bao.dating.util.MD5Util;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 用户服务实现类
|
||||
*
|
||||
* @author KilLze
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private AliOssUtil ossUtil;
|
||||
|
||||
@Autowired
|
||||
private GreenTextScan greenTextScan;
|
||||
|
||||
@Autowired
|
||||
private GreenImageScan greenImageScan;
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param userLoginDTO 登录参数
|
||||
* @return 登录信息
|
||||
*/
|
||||
@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 userId 用户ID
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Override
|
||||
public UserInfoVO getUserInfo(Long userId) {
|
||||
User user = userMapper.selectByUserId(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
UserInfoVO userInfoVO = new UserInfoVO();
|
||||
BeanUtils.copyProperties(user, userInfoVO);
|
||||
return userInfoVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传头像接口
|
||||
*
|
||||
* @param file 头像文件
|
||||
* @return 上传后的文件URL
|
||||
*/
|
||||
@Override
|
||||
public String uploadAvatar(MultipartFile file) {
|
||||
// 参数校验
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new RuntimeException("图片不存在");
|
||||
}
|
||||
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
if (originalFilename == null) {
|
||||
throw new RuntimeException("文件名非法");
|
||||
}
|
||||
|
||||
String fileType = FileUtil.getFileType(originalFilename);
|
||||
if (!AliOssResult.IMAGE.equals(fileType)) {
|
||||
throw new RuntimeException("仅支持图片上传");
|
||||
}
|
||||
|
||||
//生成 OSS 路径
|
||||
String extension = FileUtil.getFileExtension(originalFilename);
|
||||
String fileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
|
||||
Long userId = UserContext.getUserId();
|
||||
String objectKey = "user/" + userId + "/avatar/" + fileName;
|
||||
|
||||
try {
|
||||
byte[] fileBytes = file.getBytes();
|
||||
String ossUrl = ossUtil.upload(fileBytes, objectKey);
|
||||
|
||||
if (ossUrl == null || ossUrl.isEmpty()) {
|
||||
throw new RuntimeException("图片上传失败");
|
||||
}
|
||||
|
||||
return ossUrl;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("上传图片失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传背景图片
|
||||
*
|
||||
* @param file 背景图片文件
|
||||
* @return 上传后的文件URL
|
||||
*/
|
||||
@Override
|
||||
public String uploadBackground(MultipartFile file) {
|
||||
// 参数校验
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new RuntimeException("图片不存在");
|
||||
}
|
||||
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
if (originalFilename == null) {
|
||||
throw new RuntimeException("文件名非法");
|
||||
}
|
||||
|
||||
String fileType = FileUtil.getFileType(originalFilename);
|
||||
if (!AliOssResult.IMAGE.equals(fileType)) {
|
||||
throw new RuntimeException("仅支持图片上传");
|
||||
}
|
||||
|
||||
//生成 OSS 路径
|
||||
String extension = FileUtil.getFileExtension(originalFilename);
|
||||
String fileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
|
||||
Long userId = UserContext.getUserId();
|
||||
String objectKey = "user/" + userId + "/background/" + fileName;
|
||||
|
||||
try {
|
||||
byte[] fileBytes = file.getBytes();
|
||||
String ossUrl = ossUtil.upload(fileBytes, objectKey);
|
||||
|
||||
if (ossUrl == null || ossUrl.isEmpty()) {
|
||||
throw new RuntimeException("图片上传失败");
|
||||
}
|
||||
|
||||
return ossUrl;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("上传图片失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
*
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
*/
|
||||
@Override
|
||||
public UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
Long userId = userInfoUpdateDTO.getUserId();
|
||||
User user = userMapper.selectByUserId(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
|
||||
// 将需要审核的内容合并成一个文本,用于减少调用次数
|
||||
StringBuilder textBuilder = new StringBuilder();
|
||||
|
||||
if (userInfoUpdateDTO.getNickname() != null && !userInfoUpdateDTO.getNickname().isEmpty()) {
|
||||
textBuilder.append(userInfoUpdateDTO.getNickname()).append(" ");
|
||||
}
|
||||
if (userInfoUpdateDTO.getHobbies() != null && !userInfoUpdateDTO.getHobbies().isEmpty()) {
|
||||
// 将爱好列表转换为字符串,用空格分隔
|
||||
String hobbiesStr = String.join(" ", userInfoUpdateDTO.getHobbies());
|
||||
textBuilder.append(hobbiesStr).append(" ");
|
||||
}
|
||||
if (userInfoUpdateDTO.getSignature() != null && !userInfoUpdateDTO.getSignature().isEmpty()) {
|
||||
textBuilder.append(userInfoUpdateDTO.getSignature()).append(" ");
|
||||
}
|
||||
// 文本审核
|
||||
if (textBuilder.length() > 0) {
|
||||
Map textResult;
|
||||
try {
|
||||
textResult = greenTextScan.greeTextScan(textBuilder.toString());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("用户信息文本审核失败");
|
||||
}
|
||||
|
||||
String suggestion = (String) textResult.get("suggestion");
|
||||
|
||||
if (GreenAuditResult.BLOCK.equals(suggestion)) {
|
||||
throw new RuntimeException("用户信息包含违规内容,修改失败");
|
||||
}
|
||||
if (GreenAuditResult.REVIEW.equals(suggestion)) {
|
||||
throw new RuntimeException("用户信息需要人工审核,暂无法修改");
|
||||
}
|
||||
}
|
||||
|
||||
// 图片审核
|
||||
List<String> imageKeys = new ArrayList<>();
|
||||
if (userInfoUpdateDTO.getAvatarUrl() != null && !userInfoUpdateDTO.getAvatarUrl().isEmpty()) {
|
||||
imageKeys.add(userInfoUpdateDTO.getAvatarUrl());
|
||||
}
|
||||
if (userInfoUpdateDTO.getBackgroundUrl() != null && !userInfoUpdateDTO.getBackgroundUrl().isEmpty()) {
|
||||
imageKeys.add(userInfoUpdateDTO.getBackgroundUrl());
|
||||
}
|
||||
if (!imageKeys.isEmpty()) {
|
||||
Map imageResult;
|
||||
try {
|
||||
imageResult = greenImageScan.imageScan(imageKeys);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("用户图片审核失败");
|
||||
}
|
||||
|
||||
String suggestion = (String) imageResult.get("suggestion");
|
||||
|
||||
if (GreenAuditResult.BLOCK.equals(suggestion)) {
|
||||
throw new RuntimeException("头像或背景图不合规,修改失败");
|
||||
}
|
||||
if (GreenAuditResult.REVIEW.equals(suggestion)) {
|
||||
throw new RuntimeException("头像或背景图需要人工审核,暂无法修改");
|
||||
}
|
||||
}
|
||||
// 默认昵称兜底
|
||||
if (userInfoUpdateDTO.getNickname() == null || userInfoUpdateDTO.getNickname().trim().isEmpty()) {
|
||||
userInfoUpdateDTO.setNickname(user.getUserName());
|
||||
}
|
||||
userInfoUpdateDTO.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
// 更新数据库
|
||||
userMapper.updateUserInfoByUserId(userInfoUpdateDTO);
|
||||
|
||||
// 封装返回结果
|
||||
User updatedUser = userMapper.selectByUserId(userInfoUpdateDTO.getUserId());
|
||||
UserInfoVO userInfoVO = new UserInfoVO();
|
||||
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
||||
return userInfoVO;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.bao.dating.util;
|
||||
|
||||
import com.bao.dating.common.result.FileResult;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
* @author KilLze
|
||||
@@ -13,9 +15,9 @@ public class FileUtil {
|
||||
public static String getFileType(String fileUrl) {
|
||||
String extension = getFileExtension(fileUrl);
|
||||
|
||||
if (extension.equals("jpg") || extension.equals("jpeg") || extension.equals("png") || extension.equals("gif")) {
|
||||
if (FileResult.JPG.equals(extension) || FileResult.JPEG.equals(extension) || FileResult.PNG.equals(extension) || FileResult.GIF.equals(extension)) {
|
||||
return "image";
|
||||
} else if (extension.equals("mp4") || extension.equals("avi") || extension.equals("mov")) {
|
||||
} else if (FileResult.MP4.equals(extension) || FileResult.AVI.equals(extension) || FileResult.MOV.equals(extension)) {
|
||||
return "video";
|
||||
}
|
||||
return "unknown";
|
||||
|
||||
@@ -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服务器地址
|
||||
|
||||
@@ -82,10 +82,10 @@
|
||||
<update id="updateById">
|
||||
UPDATE post
|
||||
<set>
|
||||
<if test="content != null">
|
||||
<if test="content != null and content != '' ">
|
||||
content = #{content},
|
||||
</if>
|
||||
<if test="tags != null">
|
||||
<if test="tags != null and tags != '' ">
|
||||
tags = #{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||
</if>
|
||||
<if test="mediaOssKeys != null">
|
||||
|
||||
@@ -4,5 +4,74 @@
|
||||
|
||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||
|
||||
<!--根据用户名查询用户-->
|
||||
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||
SELECT
|
||||
user_id,
|
||||
user_name,
|
||||
password_hash,
|
||||
salt,
|
||||
nickname
|
||||
FROM user WHERE user_name = #{userName}
|
||||
</select>
|
||||
|
||||
<!--根据用户id查询用户信息-->
|
||||
<resultMap id="UserResultMap" type="com.bao.dating.pojo.entity.User">
|
||||
<id property="userId" column="user_id"/>
|
||||
<result property="userName" column="user_name"/>
|
||||
<result property="nickname" column="nickname"/>
|
||||
<result property="avatarUrl" column="avatar_url"/>
|
||||
<result property="backgroundUrl" column="background_url"/>
|
||||
<result property="gender" column="gender"/>
|
||||
<result property="birthday" column="birthday"/>
|
||||
<result property="hobbies" column="hobbies" typeHandler="com.bao.dating.handler.ListToJsonTypeHandler"/>
|
||||
<result property="signature" column="signature"/>
|
||||
<result property="createdAt" column="created_at"/>
|
||||
<result property="updatedAt" column="updated_at"/>
|
||||
</resultMap>
|
||||
<select id="selectByUserId" resultMap="UserResultMap">
|
||||
SELECT
|
||||
user_id,
|
||||
user_name,
|
||||
nickname,
|
||||
avatar_url,
|
||||
background_url,
|
||||
gender,
|
||||
birthday,
|
||||
hobbies,
|
||||
signature,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM user WHERE user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<!--根据ID更新动态-->
|
||||
<update id="updateUserInfoByUserId">
|
||||
UPDATE user
|
||||
<set>
|
||||
<if test="nickname != null">
|
||||
nickname = #{nickname},
|
||||
</if>
|
||||
<if test="avatarUrl != null">
|
||||
avatar_url = #{avatarUrl},
|
||||
</if>
|
||||
<if test="backgroundUrl != null">
|
||||
background_url = #{backgroundUrl},
|
||||
</if>
|
||||
<if test="gender != null">
|
||||
gender = #{gender},
|
||||
</if>
|
||||
<if test="birthday != null">
|
||||
birthday = #{birthday},
|
||||
</if>
|
||||
<if test="hobbies != null">
|
||||
hobbies = #{hobbies, typeHandler=com.bao.dating.handler.ListToJsonTypeHandler},
|
||||
</if>
|
||||
<if test="signature != null">
|
||||
signature = #{signature},
|
||||
</if>
|
||||
updated_at = #{updatedAt}
|
||||
</set>
|
||||
WHERE user_id = #{userId}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user