Compare commits
17 Commits
feature-ip
...
0c4ddc2803
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c4ddc2803 | ||
|
|
f31b42a038 | ||
|
|
9cf50ce7df | ||
|
|
401c2fa8bf | ||
|
|
fec7bb04b9 | ||
|
|
cd0abad225 | ||
|
|
96b256d46e | ||
|
|
fca54a6f97 | ||
|
|
0f8f47de8e | ||
|
|
2cb8ae5c3c | ||
|
|
7abd6fe27d | ||
|
|
dfc9508827 | ||
|
|
4c70bd3c6f | ||
|
|
d3c069967e | ||
| 4a2aff888a | |||
| 0b0959fa80 | |||
|
|
4401a8a44a |
19
pom.xml
19
pom.xml
@@ -71,19 +71,6 @@
|
|||||||
<version>3.12.0</version>
|
<version>3.12.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- OkHttp(用于调用API) -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
|
||||||
<artifactId>okhttp</artifactId>
|
|
||||||
<version>4.12.0</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- AOP起步依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- 阿里云相关依赖 -->
|
<!-- 阿里云相关依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
@@ -155,12 +142,6 @@
|
|||||||
<artifactId>spring-boot-starter-mail</artifactId>
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Redis 依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@ package com.bao.dating.common;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 响应状态码枚举
|
* 响应状态码枚举
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
public enum ResultCode {
|
public enum ResultCode {
|
||||||
/** 成功 */
|
/** 成功 */
|
||||||
@@ -19,14 +18,6 @@ public enum ResultCode {
|
|||||||
FORBIDDEN(403, "无权限"),
|
FORBIDDEN(403, "无权限"),
|
||||||
/** 数据不存在 */
|
/** 数据不存在 */
|
||||||
NOT_FOUND(404, "数据不存在"),
|
NOT_FOUND(404, "数据不存在"),
|
||||||
/** 验证码已发送 */
|
|
||||||
CODE_SENT(200, "验证码已发送"),
|
|
||||||
/** 验证码错误 */
|
|
||||||
CODE_ERROR(400, "验证码错误"),
|
|
||||||
/** 验证码已过期 */
|
|
||||||
CODE_EXPIRED(400, "验证码已过期"),
|
|
||||||
/** 验证码发送失败 */
|
|
||||||
CODE_SEND_FAIL(500, "验证码发送失败"),
|
|
||||||
/** 系统异常 */
|
/** 系统异常 */
|
||||||
SYSTEM_ERROR(500, "系统异常"),
|
SYSTEM_ERROR(500, "系统异常"),
|
||||||
/** 操作失败 */
|
/** 操作失败 */
|
||||||
|
|||||||
@@ -4,17 +4,16 @@ import com.aliyun.oss.ClientException;
|
|||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSClientBuilder;
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
import com.aliyun.oss.OSSException;
|
import com.aliyun.oss.OSSException;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
/**
|
|
||||||
* 阿里云OSS工具类
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@Data
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
|||||||
@@ -149,4 +149,3 @@ public class SmsUtil {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.bao.dating.common.ip2location;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Component
|
|
||||||
@ConfigurationProperties(prefix = "ip2location.api")
|
|
||||||
public class Ip2LocationConfig {
|
|
||||||
private String key;
|
|
||||||
private String url;
|
|
||||||
private int timeout;
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package com.bao.dating.common.result;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 阿里云 OSS 文件上传结果
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
public class AliOssResult {
|
|
||||||
public static final String IMAGE = "image";
|
|
||||||
public static final String VIDEO = "video";
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
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";
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
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";
|
|
||||||
}
|
|
||||||
@@ -7,10 +7,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
/**
|
|
||||||
* WebMvc配置类
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebConfig implements WebMvcConfigurer {
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@@ -29,7 +25,7 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
.addPathPatterns("/**")
|
.addPathPatterns("/**")
|
||||||
// 忽略的接口
|
// 忽略的接口
|
||||||
.excludePathPatterns(
|
.excludePathPatterns(
|
||||||
"/user/login","/user/register","/user/emailLogin","/ip/location"
|
"/user/login"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
package com.bao.dating.context;
|
package com.bao.dating.context;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户上下文类,用于保存当前线程的用户ID
|
* 用户上下文类
|
||||||
* @author lenovo
|
* @author lenovo
|
||||||
*/
|
*/
|
||||||
public class UserContext {
|
public class UserContext {
|
||||||
|
|
||||||
private static final ThreadLocal<Long> USER_HOLDER = new ThreadLocal<>();
|
private static final ThreadLocal<Long> userHolder = new ThreadLocal<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置当前线程的用户ID
|
* 设置当前线程的用户ID
|
||||||
* @param userId 用户ID
|
* @param userId 用户ID
|
||||||
*/
|
*/
|
||||||
public static void setUserId(Long userId) {
|
public static void setUserId(Long userId) {
|
||||||
USER_HOLDER.set(userId);
|
userHolder.set(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,13 +21,13 @@ public class UserContext {
|
|||||||
* @return 当前用户ID,如果未设置则返回null
|
* @return 当前用户ID,如果未设置则返回null
|
||||||
*/
|
*/
|
||||||
public static Long getUserId() {
|
public static Long getUserId() {
|
||||||
return USER_HOLDER.get();
|
return userHolder.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清除当前线程的用户ID
|
* 清除当前线程的用户ID
|
||||||
*/
|
*/
|
||||||
public static void clear() {
|
public static void clear() {
|
||||||
USER_HOLDER.remove();
|
userHolder.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
package com.bao.dating.controller;
|
|
||||||
|
|
||||||
import com.bao.dating.common.Result;
|
|
||||||
import com.bao.dating.common.ResultCode;
|
|
||||||
import com.bao.dating.pojo.vo.IpLocationVO;
|
|
||||||
import com.bao.dating.service.Ip2LocationClientService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/ip")
|
|
||||||
public class IpLocationController {
|
|
||||||
@Autowired
|
|
||||||
private Ip2LocationClientService ip2LocationClientService;
|
|
||||||
/**
|
|
||||||
* 前端访问接口,获取IP地址位置信息
|
|
||||||
* @param ip 可选参数,要查询的IP地址
|
|
||||||
* @return IP位置信息(JSON格式)
|
|
||||||
*/
|
|
||||||
@GetMapping("/location")
|
|
||||||
public Result<?> getIpLocation(@RequestParam(required = false) String ip) {
|
|
||||||
if (ip.isEmpty()){
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// 调用工具类获取API响应
|
|
||||||
IpLocationVO ipLocationVo = ip2LocationClientService.getIpLocation(ip);
|
|
||||||
return Result.success(ResultCode.SUCCESS,ipLocationVo);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 异常处理:返回错误信息(实际项目建议封装统一响应格式)
|
|
||||||
return Result.error(ResultCode.SYSTEM_ERROR,e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,11 +13,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
|
||||||
* 动态接口
|
|
||||||
*
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/posts")
|
@RequestMapping("/posts")
|
||||||
public class PostController {
|
public class PostController {
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ package com.bao.dating.controller;
|
|||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
|
||||||
import com.bao.dating.pojo.entity.User;
|
import com.bao.dating.pojo.entity.User;
|
||||||
import com.bao.dating.service.PostFavoriteService;
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
|
import com.bao.dating.service.PostService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -16,13 +15,6 @@ import java.util.Map;
|
|||||||
public class PostFavoriteController {
|
public class PostFavoriteController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private PostFavoriteService postFavoriteService;
|
private PostFavoriteService postFavoriteService;
|
||||||
|
|
||||||
/**
|
|
||||||
* 收藏
|
|
||||||
* @param postId 动态ID
|
|
||||||
* @param user 当前登录用户对象
|
|
||||||
* @return 结果
|
|
||||||
*/
|
|
||||||
@PostMapping("/{post_id}/favorites")
|
@PostMapping("/{post_id}/favorites")
|
||||||
public Result<Map<String,Long>> addPostFavorite(@PathVariable("post_id")Long postId, User user){
|
public Result<Map<String,Long>> addPostFavorite(@PathVariable("post_id")Long postId, User user){
|
||||||
if (user == null){
|
if (user == null){
|
||||||
@@ -31,13 +23,6 @@ public class PostFavoriteController {
|
|||||||
Long userId = user.getUserId();
|
Long userId = user.getUserId();
|
||||||
return postFavoriteService.postFavorite(userId,postId);
|
return postFavoriteService.postFavorite(userId,postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 取消收藏
|
|
||||||
* @param postId 动态id
|
|
||||||
* @param user 登录用户
|
|
||||||
* @return 结果
|
|
||||||
*/
|
|
||||||
@DeleteMapping("/{post_id}/favorites")
|
@DeleteMapping("/{post_id}/favorites")
|
||||||
public Result<?> deletePostFavorite(@PathVariable("post_id")Long postId, User user){
|
public Result<?> deletePostFavorite(@PathVariable("post_id")Long postId, User user){
|
||||||
if (user == null){
|
if (user == null){
|
||||||
@@ -46,21 +31,4 @@ public class PostFavoriteController {
|
|||||||
Long userId = user.getUserId();
|
Long userId = user.getUserId();
|
||||||
return postFavoriteService.deletePostFavorite(userId, postId);
|
return postFavoriteService.deletePostFavorite(userId, postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 展示所有收藏
|
|
||||||
* @param user 当前登录用户
|
|
||||||
* @return 结果
|
|
||||||
*/
|
|
||||||
@GetMapping("/showFavorites")
|
|
||||||
public Result<List<Post>> showPostFavorites(@RequestBody User user){
|
|
||||||
//校验参数
|
|
||||||
if (user == null){
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR);
|
|
||||||
}
|
|
||||||
Long userId = user.getUserId();
|
|
||||||
List<Post> posts = postFavoriteService.selectAllFavorites(userId);
|
|
||||||
return Result.success(ResultCode.SUCCESS,posts);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.bao.dating.common.ResultCode;
|
|||||||
import com.bao.dating.context.UserContext;
|
import com.bao.dating.context.UserContext;
|
||||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
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.UserInfoVO;
|
||||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
@@ -12,12 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户接口
|
|
||||||
*
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@@ -80,42 +77,4 @@ public class UserController {
|
|||||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||||
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户注册
|
|
||||||
* @param userName 用户名称
|
|
||||||
* @param userPassword 用户密码
|
|
||||||
* @return 返回
|
|
||||||
*/
|
|
||||||
@PostMapping("/register")
|
|
||||||
public Result<?> userRegister(@RequestParam String userName , @RequestParam String userPassword){
|
|
||||||
//校验参数是否为空
|
|
||||||
if (userName.isEmpty() || userPassword.isEmpty()){
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR);
|
|
||||||
}
|
|
||||||
if (!userService.registerUser(userName,userPassword)){
|
|
||||||
return Result.error(ResultCode.FAIL);
|
|
||||||
}
|
|
||||||
return Result.success(ResultCode.SUCCESS,"用户注册成功",null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过邮箱登录
|
|
||||||
* @param email 邮箱
|
|
||||||
* @param code 验证码
|
|
||||||
* @return 用户信息
|
|
||||||
*/
|
|
||||||
@PostMapping("/emailLogin")
|
|
||||||
public Result<UserLoginVO> emailLogin(@RequestParam String email , @RequestParam String code){
|
|
||||||
//校验参数是否为空
|
|
||||||
if (email.isEmpty() || code.isEmpty()){
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR);
|
|
||||||
}
|
|
||||||
UserLoginVO userLoginVO = userService.emailLogin(email, code);
|
|
||||||
if (userLoginVO == null){
|
|
||||||
return Result.error(ResultCode.FAIL,"请先注册用户或添加邮箱");
|
|
||||||
}
|
|
||||||
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,90 +0,0 @@
|
|||||||
package com.bao.dating.controller;
|
|
||||||
|
|
||||||
import com.bao.dating.common.Result;
|
|
||||||
import com.bao.dating.common.ResultCode;
|
|
||||||
import com.bao.dating.service.VerificationCodeService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码控制器
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api/verification")
|
|
||||||
public class VerificationCodeController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VerificationCodeService verificationCodeService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送邮箱验证码
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @return 结果
|
|
||||||
*/
|
|
||||||
@PostMapping("/send-email-code")
|
|
||||||
public Result<String> sendEmailCode(@RequestParam String email) {
|
|
||||||
// 参数校验
|
|
||||||
if (!StringUtils.hasText(email)) {
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR, "邮箱地址不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 简单的邮箱格式校验
|
|
||||||
if (!isValidEmail(email)) {
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR, "邮箱格式不正确");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发送验证码
|
|
||||||
boolean success = verificationCodeService.sendEmailCode(email);
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
return Result.success(ResultCode.CODE_SENT, "验证码已发送到您的邮箱,请查收");
|
|
||||||
} else {
|
|
||||||
return Result.error(ResultCode.CODE_SEND_FAIL, "验证码发送失败,请稍后重试");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证邮箱验证码
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @param code 验证码
|
|
||||||
* @return 结果
|
|
||||||
*/
|
|
||||||
@PostMapping("/verify-email-code")
|
|
||||||
public Result<String> verifyEmailCode(@RequestParam String email, @RequestParam String code) {
|
|
||||||
// 参数校验
|
|
||||||
if (!StringUtils.hasText(email)) {
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR, "邮箱地址不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!StringUtils.hasText(code)) {
|
|
||||||
return Result.error(ResultCode.PARAM_ERROR, "验证码不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证验证码
|
|
||||||
boolean success = verificationCodeService.verifyEmailCode(email, code);
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
return Result.success(ResultCode.SUCCESS, "验证码验证成功");
|
|
||||||
} else {
|
|
||||||
return Result.error(ResultCode.CODE_ERROR, "验证码错误或已过期");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 简单的邮箱格式校验
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @return 是否有效
|
|
||||||
*/
|
|
||||||
private boolean isValidEmail(String email) {
|
|
||||||
if (email == null || email.trim().isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// 简单的邮箱格式校验:包含@和.
|
|
||||||
return email.contains("@") && email.contains(".") && email.length() > 5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
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, "系统内部错误");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,16 +13,11 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@MappedJdbcTypes(JdbcType.VARCHAR) // 也可以使用JdbcType.JSON,如果数据库支持的话
|
||||||
/**
|
|
||||||
* List类型转换成JSON类型
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
|
||||||
@MappedTypes(List.class)
|
@MappedTypes(List.class)
|
||||||
public class ListToJsonTypeHandler implements TypeHandler<List<String>> {
|
public class ListToJsonTypeHandler implements TypeHandler<List<String>> {
|
||||||
|
|
||||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
||||||
@@ -31,7 +26,7 @@ public class ListToJsonTypeHandler implements TypeHandler<List<String>> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String json = OBJECT_MAPPER.writeValueAsString(parameter);
|
String json = objectMapper.writeValueAsString(parameter);
|
||||||
ps.setString(i, json);
|
ps.setString(i, json);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new SQLException("Error converting list to JSON", e);
|
throw new SQLException("Error converting list to JSON", e);
|
||||||
@@ -61,7 +56,7 @@ public class ListToJsonTypeHandler implements TypeHandler<List<String>> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return OBJECT_MAPPER.readValue(json, new TypeReference<List<String>>() {});
|
return objectMapper.readValue(json, new TypeReference<List<String>>() {});
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new SQLException("Error converting JSON to list", e);
|
throw new SQLException("Error converting JSON to list", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
|
|
||||||
import com.bao.dating.context.UserContext;
|
import com.bao.dating.context.UserContext;
|
||||||
import com.bao.dating.util.JwtUtil;
|
import com.bao.dating.util.JwtUtil;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.method.HandlerMethod;
|
import org.springframework.web.method.HandlerMethod;
|
||||||
@@ -13,7 +14,6 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
|||||||
/**
|
/**
|
||||||
* HttpToken拦截器类
|
* HttpToken拦截器类
|
||||||
* 用于拦截请求并验证JWT token的有效性,同时从token中解析用户信息
|
* 用于拦截请求并验证JWT token的有效性,同时从token中解析用户信息
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -43,7 +43,7 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||||||
|
|
||||||
// 验证 token 是否有效(包括是否过期)
|
// 验证 token 是否有效(包括是否过期)
|
||||||
if (!JwtUtil.validateToken(token)) {
|
if (!JwtUtil.validateToken(token)) {
|
||||||
log.error("Token无效或已过期");
|
log.error("Token 无效或已过期");
|
||||||
response.setStatus(401);
|
response.setStatus(401);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
import com.bao.dating.pojo.entity.Post;
|
|
||||||
import com.bao.dating.pojo.entity.PostFavorite;
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
@@ -11,10 +10,6 @@ import java.util.List;
|
|||||||
public interface PostFavoriteMapper {
|
public interface PostFavoriteMapper {
|
||||||
//查询当前已收藏所有用户
|
//查询当前已收藏所有用户
|
||||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||||
//添加收藏
|
|
||||||
int addPostFavorite(PostFavorite postFavorite);
|
int addPostFavorite(PostFavorite postFavorite);
|
||||||
//删除收藏
|
|
||||||
int deletePostFavorite(@Param("postId") Long postId);
|
int deletePostFavorite(@Param("postId") Long postId);
|
||||||
//查询用户所有收藏
|
|
||||||
List<Post> showAllFavorites(@Param("userid")Long userid);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,17 +6,12 @@ import org.apache.ibatis.annotations.Param;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
|
||||||
* 动态Mapper
|
|
||||||
*
|
|
||||||
* @author KilLze lanyangyang-yzx
|
|
||||||
*/
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface PostMapper {
|
public interface PostMapper {
|
||||||
/**
|
/**
|
||||||
* 插入动态
|
* 插入动态
|
||||||
*
|
*
|
||||||
* @param post 动态
|
* @param post
|
||||||
*/
|
*/
|
||||||
void insert(Post post);
|
void insert(Post post);
|
||||||
|
|
||||||
@@ -30,23 +25,24 @@ public interface PostMapper {
|
|||||||
/**
|
/**
|
||||||
* 根据ID查询动态
|
* 根据ID查询动态
|
||||||
*
|
*
|
||||||
* @param postId 动态ID
|
* @param postId
|
||||||
* @return 动态
|
* @return
|
||||||
*/
|
*/
|
||||||
Post selectById(Long postId);
|
Post selectById(Long postId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID更新动态
|
* 根据ID更新动态
|
||||||
*
|
*
|
||||||
* @param post 动态
|
* @param post
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
void updateById(Post post);
|
void updateById(Post post);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询点赞数
|
* 查询点赞数
|
||||||
*
|
*
|
||||||
* @param postId 动态ID
|
* @param postId
|
||||||
* @return 点赞数
|
* @return
|
||||||
*/
|
*/
|
||||||
int selectLikeCount(Long postId);
|
int selectLikeCount(Long postId);
|
||||||
|
|
||||||
@@ -76,8 +72,8 @@ public interface PostMapper {
|
|||||||
/**
|
/**
|
||||||
* 查询点赞数
|
* 查询点赞数
|
||||||
*
|
*
|
||||||
* @param postId 动态ID
|
* @param postId
|
||||||
* @return 点赞数
|
* @return
|
||||||
*/
|
*/
|
||||||
int selectFavoriteCount(Long postId);
|
int selectFavoriteCount(Long postId);
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.entity.User;
|
import com.bao.dating.pojo.entity.User;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户Mapper
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserMapper {
|
public interface UserMapper {
|
||||||
|
|
||||||
@@ -30,29 +26,8 @@ public interface UserMapper {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新用户信息
|
* 更新用户信息
|
||||||
* @param userInfoUpdateDTO 用户信息更新参数
|
* @param userInfoUpdateDTO
|
||||||
*/
|
*/
|
||||||
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加用户
|
|
||||||
* @param user 用户对象
|
|
||||||
* @return 受影响行数
|
|
||||||
*/
|
|
||||||
int saveUser(User user);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询最大用户id
|
|
||||||
* @return 用户id
|
|
||||||
*/
|
|
||||||
Long selectMaxId();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据邮箱查询用户
|
|
||||||
* @param email 用户邮箱
|
|
||||||
* @return 用户信息
|
|
||||||
*/
|
|
||||||
User selectByUserEmailUser(@Param("userEmail") String email);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,9 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 动态数据传输对象
|
* 动态数据传输对象
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class PostRequestDTO implements Serializable{
|
public class PostRequestDTO implements Serializable {
|
||||||
private String content;
|
private String content;
|
||||||
private List<String> mediaOssKeys;
|
private List<String> mediaOssKeys;
|
||||||
private List<String> tags;
|
private List<String> tags;
|
||||||
|
|||||||
@@ -6,10 +6,6 @@ import java.time.LocalDate;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户信息更新数据传输对象
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@Data
|
@Data
|
||||||
public class UserInfoUpdateDTO {
|
public class UserInfoUpdateDTO {
|
||||||
private Long userId;
|
private Long userId;
|
||||||
@@ -22,4 +18,6 @@ public class UserInfoUpdateDTO {
|
|||||||
private List<String> hobbies;
|
private List<String> hobbies;
|
||||||
private String signature;
|
private String signature;
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
|
/** 需要清空的字段 */
|
||||||
|
private List<String> clearFields;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import java.io.Serializable;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录数据传输对象
|
* 用户登录数据传输对象
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class UserLoginDTO implements Serializable {
|
public class UserLoginDTO implements Serializable {
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
package com.bao.dating.pojo.vo;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public class IpLocationVO {
|
|
||||||
// 国家
|
|
||||||
@JsonProperty("country_name")
|
|
||||||
private String countryName;
|
|
||||||
// 省份/地区
|
|
||||||
@JsonProperty("region_name")
|
|
||||||
private String regionName;
|
|
||||||
// 城市
|
|
||||||
@JsonProperty("city_name")
|
|
||||||
private String cityName;
|
|
||||||
// 纬度
|
|
||||||
private String latitude;
|
|
||||||
// 经度
|
|
||||||
private String longitude;
|
|
||||||
// ISP运营商
|
|
||||||
@JsonProperty("isp")
|
|
||||||
private String isp;
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,6 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改内容查询返回数据
|
* 修改内容查询返回数据
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class PostEditVO implements Serializable {
|
public class PostEditVO implements Serializable {
|
||||||
|
|||||||
@@ -2,17 +2,15 @@ package com.bao.dating.pojo.vo;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户信息VO
|
* 用户信息VO
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class UserInfoVO implements Serializable {
|
public class UserInfoVO {
|
||||||
private Long userId;
|
private Long userId;
|
||||||
private String userName;
|
private String userName;
|
||||||
private String nickname;
|
private String nickname;
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import java.io.Serializable;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录返回数据
|
* 登录返回数据
|
||||||
* @author KilLze
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class UserLoginVO implements Serializable {
|
public class UserLoginVO implements Serializable {
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.bao.dating.service;
|
|
||||||
|
|
||||||
import com.bao.dating.pojo.vo.IpLocationVO;
|
|
||||||
|
|
||||||
public interface Ip2LocationClientService {
|
|
||||||
IpLocationVO getIpLocation(String ip)throws Exception;
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface PostFavoriteService {
|
public interface PostFavoriteService {
|
||||||
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
||||||
Result<?> deletePostFavorite(Long userid,Long postId);
|
Result<?> deletePostFavorite(Long userid,Long postId);
|
||||||
List<Post> selectAllFavorites(Long userid);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
|
||||||
* 动态服务
|
|
||||||
* @author bao KilLze lanyangyang-yzx yang
|
|
||||||
*/
|
|
||||||
public interface PostService {
|
public interface PostService {
|
||||||
/**
|
/**
|
||||||
* 上传媒体文件
|
* 上传媒体文件
|
||||||
@@ -37,7 +33,6 @@ public interface PostService {
|
|||||||
/**
|
/**
|
||||||
* 查询动态详情(用于编辑)
|
* 查询动态详情(用于编辑)
|
||||||
* @param postId 动态ID
|
* @param postId 动态ID
|
||||||
* @return 动态详情
|
|
||||||
*/
|
*/
|
||||||
PostEditVO getPostForEdit(Long postId);
|
PostEditVO getPostForEdit(Long postId);
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ package com.bao.dating.service;
|
|||||||
|
|
||||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
import com.bao.dating.pojo.entity.User;
|
import com.bao.dating.pojo.vo.PostEditVO;
|
||||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
/**
|
import java.util.List;
|
||||||
* 用户服务接口
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
/**
|
/**
|
||||||
* 登录
|
* 登录
|
||||||
@@ -22,7 +20,6 @@ public interface UserService {
|
|||||||
/**
|
/**
|
||||||
* 查询个人信息
|
* 查询个人信息
|
||||||
* @param userId 动态ID
|
* @param userId 动态ID
|
||||||
* @return 个人信息
|
|
||||||
*/
|
*/
|
||||||
UserInfoVO getUserInfo(Long userId);
|
UserInfoVO getUserInfo(Long userId);
|
||||||
|
|
||||||
@@ -43,22 +40,6 @@ public interface UserService {
|
|||||||
/**
|
/**
|
||||||
* 更新用户信息
|
* 更新用户信息
|
||||||
* @param userInfoUpdateDTO 用户信息
|
* @param userInfoUpdateDTO 用户信息
|
||||||
* @return 更新后的用户信息
|
|
||||||
*/
|
*/
|
||||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户注册
|
|
||||||
* @param userName 用户民称
|
|
||||||
* @return 用户信息
|
|
||||||
*/
|
|
||||||
Boolean registerUser(String userName,String userPassword);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 邮箱登录
|
|
||||||
* @param email 邮箱
|
|
||||||
* @param code 验证码
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
UserLoginVO emailLogin(String email , String code);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
package com.bao.dating.service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码服务接口
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
public interface VerificationCodeService {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送邮箱验证码
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @return 是否发送成功
|
|
||||||
*/
|
|
||||||
boolean sendEmailCode(String email);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证邮箱验证码
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @param code 验证码
|
|
||||||
* @return 是否验证成功
|
|
||||||
*/
|
|
||||||
boolean verifyEmailCode(String email, String code);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成验证码
|
|
||||||
* @param length 验证码长度(默认6位)
|
|
||||||
* @return 验证码字符串
|
|
||||||
*/
|
|
||||||
String generateCode(int length);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package com.bao.dating.service.impl;
|
|
||||||
|
|
||||||
import com.bao.dating.common.ip2location.Ip2LocationConfig;
|
|
||||||
import com.bao.dating.pojo.vo.IpLocationVO;
|
|
||||||
import com.bao.dating.service.Ip2LocationClientService;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class Ip2LocationClientServiceImpl implements Ip2LocationClientService {
|
|
||||||
@Autowired
|
|
||||||
private Ip2LocationConfig ip2LocationConfig;
|
|
||||||
|
|
||||||
// Jackson的ObjectMapper,用于JSON解析
|
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调用API并只返回核心位置信息
|
|
||||||
* @param ip 要查询的IP地址
|
|
||||||
* @return 精简的位置信息实体类
|
|
||||||
* @throws Exception 异常
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public IpLocationVO getIpLocation(String ip) throws Exception {
|
|
||||||
// 1. 构建请求URL
|
|
||||||
URIBuilder uriBuilder = new URIBuilder(ip2LocationConfig.getUrl());
|
|
||||||
uriBuilder.addParameter("key", ip2LocationConfig.getKey());
|
|
||||||
if (ip != null && !ip.trim().isEmpty()) {
|
|
||||||
uriBuilder.addParameter("ip", ip);
|
|
||||||
}
|
|
||||||
URI uri = uriBuilder.build();
|
|
||||||
|
|
||||||
// 2. 配置超时时间(逻辑和之前一致)
|
|
||||||
RequestConfig requestConfig = RequestConfig.custom()
|
|
||||||
.setConnectTimeout(ip2LocationConfig.getTimeout())
|
|
||||||
.setSocketTimeout(ip2LocationConfig.getTimeout())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// 3. 发送请求并解析响应
|
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
||||||
HttpGet httpGet = new HttpGet(uri);
|
|
||||||
httpGet.setConfig(requestConfig);
|
|
||||||
|
|
||||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
||||||
if (response.getStatusLine().getStatusCode() == 200) {
|
|
||||||
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
||||||
// 核心:将完整JSON解析为只包含位置信息的VO类
|
|
||||||
return objectMapper.readValue(jsonStr, IpLocationVO.class);
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException("调用API失败,状态码:" + response.getStatusLine().getStatusCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ import com.bao.dating.common.Result;
|
|||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
import com.bao.dating.mapper.PostFavoriteMapper;
|
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||||
import com.bao.dating.mapper.PostMapper;
|
import com.bao.dating.mapper.PostMapper;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
|
||||||
import com.bao.dating.pojo.entity.PostFavorite;
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import com.bao.dating.service.PostFavoriteService;
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -59,18 +58,4 @@ public class PostFavoriteServiceImpl implements PostFavoriteService {
|
|||||||
postMapper.decreaseFavoriteCount(postId);
|
postMapper.decreaseFavoriteCount(postId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 展示所有收藏
|
|
||||||
* @param userid 用户id
|
|
||||||
* @return 查询到的结果
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Post> selectAllFavorites(Long userid) {
|
|
||||||
if (userid == null){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return postFavoriteMapper.showAllFavorites(userid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.bao.dating.service.impl;
|
|||||||
|
|
||||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||||
import com.bao.dating.common.result.GreenAuditResult;
|
|
||||||
import com.bao.dating.context.UserContext;
|
import com.bao.dating.context.UserContext;
|
||||||
import com.bao.dating.mapper.PostMapper;
|
import com.bao.dating.mapper.PostMapper;
|
||||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||||
@@ -26,7 +25,7 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* 动态服务实现类
|
* 动态服务实现类
|
||||||
*
|
*
|
||||||
* @author KilLze yang
|
* @author KilLze
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class PostServiceImpl implements PostService {
|
public class PostServiceImpl implements PostService {
|
||||||
@@ -145,10 +144,10 @@ public class PostServiceImpl implements PostService {
|
|||||||
String imageSuggestion = (String) imageResult.get("suggestion");
|
String imageSuggestion = (String) imageResult.get("suggestion");
|
||||||
|
|
||||||
// 根据审核结果设置状态
|
// 根据审核结果设置状态
|
||||||
if (GreenAuditResult.BLOCK.equals(textSuggestion) || GreenAuditResult.BLOCK.equals(imageSuggestion)) {
|
if ("block".equals(textSuggestion) || "block".equals(imageSuggestion)) {
|
||||||
// 审核未通过,允许用户修改
|
// 审核未通过,允许用户修改
|
||||||
post.setIsPublic(2);
|
post.setIsPublic(2);
|
||||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion) || GreenAuditResult.REVIEW.equals(imageSuggestion)) {
|
} else if ("review".equals(textSuggestion) || "review".equals(imageSuggestion)) {
|
||||||
// 待审核,需人工审核
|
// 待审核,需人工审核
|
||||||
post.setIsPublic(1);
|
post.setIsPublic(1);
|
||||||
} else {
|
} else {
|
||||||
@@ -157,10 +156,10 @@ public class PostServiceImpl implements PostService {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 只有文本内容的情况
|
// 只有文本内容的情况
|
||||||
if (GreenAuditResult.BLOCK.equals(textSuggestion)) {
|
if ("block".equals(textSuggestion)) {
|
||||||
// 审核未通过,允许用户修改
|
// 审核未通过,允许用户修改
|
||||||
post.setIsPublic(2);
|
post.setIsPublic(2);
|
||||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion)) {
|
} else if ("review".equals(textSuggestion)) {
|
||||||
// 待审核,需人工审核
|
// 待审核,需人工审核
|
||||||
post.setIsPublic(1);
|
post.setIsPublic(1);
|
||||||
} else {
|
} else {
|
||||||
@@ -269,10 +268,10 @@ public class PostServiceImpl implements PostService {
|
|||||||
String imageSuggestion = (String) imageResult.get("suggestion");
|
String imageSuggestion = (String) imageResult.get("suggestion");
|
||||||
|
|
||||||
// 根据审核结果设置状态
|
// 根据审核结果设置状态
|
||||||
if (GreenAuditResult.BLOCK.equals(textSuggestion) || GreenAuditResult.BLOCK.equals(imageSuggestion)) {
|
if ("block".equals(textSuggestion) || "block".equals(imageSuggestion)) {
|
||||||
// 审核未通过,允许用户修改
|
// 审核未通过,允许用户修改
|
||||||
post.setIsPublic(2);
|
post.setIsPublic(2);
|
||||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion) || GreenAuditResult.REVIEW.equals(imageSuggestion)) {
|
} else if ("review".equals(textSuggestion) || "review".equals(imageSuggestion)) {
|
||||||
// 待审核,需人工审核
|
// 待审核,需人工审核
|
||||||
post.setIsPublic(1);
|
post.setIsPublic(1);
|
||||||
} else {
|
} else {
|
||||||
@@ -281,10 +280,10 @@ public class PostServiceImpl implements PostService {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 只有文本内容的情况
|
// 只有文本内容的情况
|
||||||
if (GreenAuditResult.BLOCK.equals(textSuggestion)) {
|
if ("block".equals(textSuggestion)) {
|
||||||
// 审核未通过,允许用户修改
|
// 审核未通过,允许用户修改
|
||||||
post.setIsPublic(2);
|
post.setIsPublic(2);
|
||||||
} else if (GreenAuditResult.REVIEW.equals(textSuggestion)) {
|
} else if ("review".equals(textSuggestion)) {
|
||||||
// 待审核,需人工审核
|
// 待审核,需人工审核
|
||||||
post.setIsPublic(1);
|
post.setIsPublic(1);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
import com.bao.dating.common.Result;
|
|
||||||
import com.bao.dating.common.ResultCode;
|
|
||||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
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.context.UserContext;
|
||||||
import com.bao.dating.mapper.UserMapper;
|
import com.bao.dating.mapper.UserMapper;
|
||||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||||
@@ -15,7 +11,6 @@ import com.bao.dating.pojo.entity.User;
|
|||||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
import com.bao.dating.service.VerificationCodeService;
|
|
||||||
import com.bao.dating.util.FileUtil;
|
import com.bao.dating.util.FileUtil;
|
||||||
import com.bao.dating.util.JwtUtil;
|
import com.bao.dating.util.JwtUtil;
|
||||||
import com.bao.dating.util.MD5Util;
|
import com.bao.dating.util.MD5Util;
|
||||||
@@ -25,7 +20,9 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -51,9 +48,6 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VerificationCodeService verificationCodeService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
*
|
*
|
||||||
@@ -126,11 +120,12 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String fileType = FileUtil.getFileType(originalFilename);
|
String fileType = FileUtil.getFileType(originalFilename);
|
||||||
if (!AliOssResult.IMAGE.equals(fileType)) {
|
if (!"image".equals(fileType)) {
|
||||||
throw new RuntimeException("仅支持图片上传");
|
throw new RuntimeException("仅支持图片上传");
|
||||||
}
|
}
|
||||||
|
|
||||||
//生成 OSS 路径
|
//生成 OSS 路径
|
||||||
|
String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
|
||||||
String extension = FileUtil.getFileExtension(originalFilename);
|
String extension = FileUtil.getFileExtension(originalFilename);
|
||||||
String fileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
|
String fileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
|
||||||
Long userId = UserContext.getUserId();
|
Long userId = UserContext.getUserId();
|
||||||
@@ -170,7 +165,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String fileType = FileUtil.getFileType(originalFilename);
|
String fileType = FileUtil.getFileType(originalFilename);
|
||||||
if (!AliOssResult.IMAGE.equals(fileType)) {
|
if (!"image".equals(fileType)) {
|
||||||
throw new RuntimeException("仅支持图片上传");
|
throw new RuntimeException("仅支持图片上传");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,10 +228,10 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
String suggestion = (String) textResult.get("suggestion");
|
String suggestion = (String) textResult.get("suggestion");
|
||||||
|
|
||||||
if (GreenAuditResult.BLOCK.equals(suggestion)) {
|
if ("block".equals(suggestion)) {
|
||||||
throw new RuntimeException("用户信息包含违规内容,修改失败");
|
throw new RuntimeException("用户信息包含违规内容,修改失败");
|
||||||
}
|
}
|
||||||
if (GreenAuditResult.REVIEW.equals(suggestion)) {
|
if ("review".equals(suggestion)) {
|
||||||
throw new RuntimeException("用户信息需要人工审核,暂无法修改");
|
throw new RuntimeException("用户信息需要人工审核,暂无法修改");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -259,10 +254,10 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
String suggestion = (String) imageResult.get("suggestion");
|
String suggestion = (String) imageResult.get("suggestion");
|
||||||
|
|
||||||
if (GreenAuditResult.BLOCK.equals(suggestion)) {
|
if ("block".equals(suggestion)) {
|
||||||
throw new RuntimeException("头像或背景图不合规,修改失败");
|
throw new RuntimeException("头像或背景图不合规,修改失败");
|
||||||
}
|
}
|
||||||
if (GreenAuditResult.REVIEW.equals(suggestion)) {
|
if ("review".equals(suggestion)) {
|
||||||
throw new RuntimeException("头像或背景图需要人工审核,暂无法修改");
|
throw new RuntimeException("头像或背景图需要人工审核,暂无法修改");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -281,59 +276,4 @@ public class UserServiceImpl implements UserService {
|
|||||||
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
||||||
return userInfoVO;
|
return userInfoVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询用户
|
|
||||||
* @param userName 用户民称
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Boolean registerUser(String userName,String userPassword) {
|
|
||||||
//校验参数是否为空
|
|
||||||
if (userName.isEmpty() || userPassword.isEmpty()){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//产看数据库是否存在已注册用户
|
|
||||||
User user = userMapper.getByUsername(userName);
|
|
||||||
if (user != null){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//将用户数据存入苏数据库
|
|
||||||
String salt = "lyy123";
|
|
||||||
String passwordHash = MD5Util.encryptWithSalt(userPassword, salt);
|
|
||||||
//查询最大用户id
|
|
||||||
Long maxId = userMapper.selectMaxId();
|
|
||||||
User saveUser = new User();
|
|
||||||
saveUser.setUserId(maxId+1);
|
|
||||||
saveUser.setUserName(userName);
|
|
||||||
saveUser.setPasswordHash(passwordHash);
|
|
||||||
saveUser.setSalt(salt);
|
|
||||||
saveUser.setUpdatedAt(LocalDateTime.now());
|
|
||||||
saveUser.setCreatedAt(LocalDateTime.now());
|
|
||||||
int count = userMapper.saveUser(saveUser);
|
|
||||||
return count > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 邮箱登录
|
|
||||||
* @param email 邮箱
|
|
||||||
* @param code 验证码
|
|
||||||
* @return 脱敏用户信息
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public UserLoginVO emailLogin(String email, String code) {
|
|
||||||
User user = userMapper.selectByUserEmailUser(email);
|
|
||||||
if (user == null)
|
|
||||||
return null;
|
|
||||||
boolean flag = verificationCodeService.verifyEmailCode(email, code);
|
|
||||||
if (!flag)
|
|
||||||
return null;
|
|
||||||
// 生成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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,122 +0,0 @@
|
|||||||
package com.bao.dating.service.impl;
|
|
||||||
|
|
||||||
import com.bao.dating.service.VerificationCodeService;
|
|
||||||
import com.bao.dating.util.EmailUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码服务实现类
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
public class VerificationCodeServiceImpl implements VerificationCodeService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private EmailUtil emailUtil;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private StringRedisTemplate redisTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Redis中验证码的key前缀
|
|
||||||
*/
|
|
||||||
private static final String CODE_KEY_PREFIX = "email:code:";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码过期时间(分钟)
|
|
||||||
*/
|
|
||||||
private static final long CODE_EXPIRE_MINUTES = 10;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码长度
|
|
||||||
*/
|
|
||||||
private static final int CODE_LENGTH = 6;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送邮箱验证码
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @return 是否发送成功
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean sendEmailCode(String email) {
|
|
||||||
try {
|
|
||||||
// 生成验证码
|
|
||||||
String code = generateCode(CODE_LENGTH);
|
|
||||||
|
|
||||||
// 存储到Redis,设置过期时间
|
|
||||||
String key = CODE_KEY_PREFIX + email;
|
|
||||||
redisTemplate.opsForValue().set(key, code, CODE_EXPIRE_MINUTES, TimeUnit.MINUTES);
|
|
||||||
|
|
||||||
// 发送邮件
|
|
||||||
boolean sendResult = emailUtil.sendVerificationCode(email, code);
|
|
||||||
|
|
||||||
if (sendResult) {
|
|
||||||
log.info("邮箱验证码发送成功,邮箱:{},验证码:{}", email, code);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
// 如果发送失败,删除Redis中的验证码
|
|
||||||
redisTemplate.delete(key);
|
|
||||||
log.error("邮箱验证码发送失败,邮箱:{}", email);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("发送邮箱验证码异常,邮箱:{},异常信息:{}", email, e.getMessage(), e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证邮箱验证码
|
|
||||||
* @param email 邮箱地址
|
|
||||||
* @param code 验证码
|
|
||||||
* @return 是否验证成功
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean verifyEmailCode(String email, String code) {
|
|
||||||
try {
|
|
||||||
String key = CODE_KEY_PREFIX + email;
|
|
||||||
String storedCode = redisTemplate.opsForValue().get(key);
|
|
||||||
|
|
||||||
if (storedCode == null) {
|
|
||||||
log.warn("验证码不存在或已过期,邮箱:{}", email);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (storedCode.equals(code)) {
|
|
||||||
// 验证成功后,删除验证码(防止重复使用)
|
|
||||||
redisTemplate.delete(key);
|
|
||||||
log.info("邮箱验证码验证成功,邮箱:{}", email);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
log.warn("邮箱验证码错误,邮箱:{},输入的验证码:{},正确的验证码:{}", email, code, storedCode);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("验证邮箱验证码异常,邮箱:{},异常信息:{}", email, e.getMessage(), e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成验证码
|
|
||||||
* @param length 验证码长度
|
|
||||||
* @return 验证码字符串
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String generateCode(int length) {
|
|
||||||
Random random = new Random();
|
|
||||||
StringBuilder code = new StringBuilder();
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
code.append(random.nextInt(10)); // 生成0-9的随机数字
|
|
||||||
}
|
|
||||||
return code.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.bao.dating.util;
|
package com.bao.dating.util;
|
||||||
|
|
||||||
import com.bao.dating.common.result.FileResult;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件工具类
|
* 文件工具类
|
||||||
* @author KilLze
|
* @author KilLze
|
||||||
@@ -15,9 +13,9 @@ public class FileUtil {
|
|||||||
public static String getFileType(String fileUrl) {
|
public static String getFileType(String fileUrl) {
|
||||||
String extension = getFileExtension(fileUrl);
|
String extension = getFileExtension(fileUrl);
|
||||||
|
|
||||||
if (FileResult.JPG.equals(extension) || FileResult.JPEG.equals(extension) || FileResult.PNG.equals(extension) || FileResult.GIF.equals(extension)) {
|
if (extension.equals("jpg") || extension.equals("jpeg") || extension.equals("png") || extension.equals("gif")) {
|
||||||
return "image";
|
return "image";
|
||||||
} else if (FileResult.MP4.equals(extension) || FileResult.AVI.equals(extension) || FileResult.MOV.equals(extension)) {
|
} else if (extension.equals("mp4") || extension.equals("avi") || extension.equals("mov")) {
|
||||||
return "video";
|
return "video";
|
||||||
}
|
}
|
||||||
return "unknown";
|
return "unknown";
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ spring:
|
|||||||
password: JoyeeServe2025
|
password: JoyeeServe2025
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
redis:
|
redis:
|
||||||
host: 110.42.41.177
|
host: 127.0.0.1
|
||||||
port: 6389
|
port: 6379
|
||||||
password: ""
|
password: ""
|
||||||
database: 0
|
database: 0
|
||||||
timeout: 10000
|
timeout: 10000
|
||||||
@@ -65,11 +65,3 @@ aliyun:
|
|||||||
region-id: cn-hangzhou
|
region-id: cn-hangzhou
|
||||||
sign-name: 速通互联验证码
|
sign-name: 速通互联验证码
|
||||||
template-code: 100001
|
template-code: 100001
|
||||||
|
|
||||||
# ip2location.io 相关配置
|
|
||||||
ip2location:
|
|
||||||
api:
|
|
||||||
key: 95F4AB991174E296AFD5AD0EF927B2ED # ip2location.io API密钥
|
|
||||||
url: https://api.ip2location.io/
|
|
||||||
timeout: 5000 # 请求超时时间(毫秒)
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,4 @@
|
|||||||
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
||||||
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
||||||
</select>
|
</select>
|
||||||
<!-- 查询所有收藏动态 -->
|
|
||||||
<select id="showAllFavorites" 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>
|
</mapper>
|
||||||
@@ -82,10 +82,10 @@
|
|||||||
<update id="updateById">
|
<update id="updateById">
|
||||||
UPDATE post
|
UPDATE post
|
||||||
<set>
|
<set>
|
||||||
<if test="content != null and content != '' ">
|
<if test="content != null">
|
||||||
content = #{content},
|
content = #{content},
|
||||||
</if>
|
</if>
|
||||||
<if test="tags != null and tags != '' ">
|
<if test="tags != null">
|
||||||
tags = #{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
tags = #{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
</if>
|
</if>
|
||||||
<if test="mediaOssKeys != null">
|
<if test="mediaOssKeys != null">
|
||||||
|
|||||||
@@ -3,10 +3,6 @@
|
|||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||||
<!-- 向数据库中添加用户 -->
|
|
||||||
<insert id="saveUser">
|
|
||||||
insert into user(user_id,user_name,password_hash,salt,created_at,updated_at) values (#{userId},#{userName},#{passwordHash},#{salt},#{createdAt},#{updatedAt})
|
|
||||||
</insert>
|
|
||||||
|
|
||||||
<!--根据用户名查询用户-->
|
<!--根据用户名查询用户-->
|
||||||
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||||
@@ -49,16 +45,6 @@
|
|||||||
FROM user WHERE user_id = #{userId}
|
FROM user WHERE user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<!-- 查询最大用户id -->
|
|
||||||
<select id="selectMaxId" resultType="java.lang.Long">
|
|
||||||
SELECT MAX(user_id) FROM user
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<!-- 根据邮箱查询用户信息 -->
|
|
||||||
<select id="selectByUserEmailUser" resultType="com.bao.dating.pojo.entity.User">
|
|
||||||
select * from user where user_email = #{userEmail}
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<!--根据ID更新动态-->
|
<!--根据ID更新动态-->
|
||||||
<update id="updateUserInfoByUserId">
|
<update id="updateUserInfoByUserId">
|
||||||
UPDATE user
|
UPDATE user
|
||||||
|
|||||||
@@ -1,304 +0,0 @@
|
|||||||
package com.bao.dating.common.aliyun;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* OSS图片上传测试类
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@SpringBootTest
|
|
||||||
public class OssUploadTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AliOssUtil aliOssUtil;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试上传本地图片文件
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testUploadLocalImage() {
|
|
||||||
try {
|
|
||||||
// 本地图片文件路径(请修改为实际存在的图片路径)
|
|
||||||
String localImagePath = "D:/image/image.jpg"; // 请修改为实际路径
|
|
||||||
|
|
||||||
File imageFile = new File(localImagePath);
|
|
||||||
if (!imageFile.exists()) {
|
|
||||||
System.out.println("图片文件不存在: " + localImagePath);
|
|
||||||
System.out.println("请修改localImagePath为实际存在的图片路径");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取文件字节
|
|
||||||
byte[] imageBytes = Files.readAllBytes(Paths.get(localImagePath));
|
|
||||||
|
|
||||||
// 生成OSS对象名称(使用时间戳和UUID确保唯一性)
|
|
||||||
String objectName = generateObjectName("test", "jpg");
|
|
||||||
|
|
||||||
// 上传到OSS
|
|
||||||
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
|
|
||||||
|
|
||||||
System.out.println("========== 本地图片上传测试 ==========");
|
|
||||||
System.out.println("本地文件路径: " + localImagePath);
|
|
||||||
System.out.println("文件大小: " + imageBytes.length + " 字节");
|
|
||||||
System.out.println("OSS对象名称: " + objectName);
|
|
||||||
System.out.println("文件访问URL: " + fileUrl);
|
|
||||||
System.out.println("上传结果: 成功");
|
|
||||||
System.out.println("=====================================");
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("上传失败: " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试从网络URL下载图片并上传到OSS
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testUploadImageFromUrl() {
|
|
||||||
try {
|
|
||||||
// 网络图片URL(请修改为实际的图片URL)
|
|
||||||
String imageUrl = "https://example.com/image.jpg"; // 请修改为实际URL
|
|
||||||
|
|
||||||
System.out.println("========== 网络图片上传测试 ==========");
|
|
||||||
System.out.println("开始下载图片: " + imageUrl);
|
|
||||||
|
|
||||||
// 从URL下载图片
|
|
||||||
URL url = new URL(imageUrl);
|
|
||||||
try (InputStream inputStream = url.openStream()) {
|
|
||||||
byte[] imageBytes = readAllBytes(inputStream);
|
|
||||||
|
|
||||||
// 从URL中提取文件扩展名
|
|
||||||
String extension = getFileExtensionFromUrl(imageUrl);
|
|
||||||
if (extension.isEmpty()) {
|
|
||||||
extension = "jpg"; // 默认扩展名
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成OSS对象名称
|
|
||||||
String objectName = generateObjectName("download", extension);
|
|
||||||
|
|
||||||
// 上传到OSS
|
|
||||||
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
|
|
||||||
|
|
||||||
System.out.println("图片下载成功");
|
|
||||||
System.out.println("文件大小: " + imageBytes.length + " 字节");
|
|
||||||
System.out.println("OSS对象名称: " + objectName);
|
|
||||||
System.out.println("文件访问URL: " + fileUrl);
|
|
||||||
System.out.println("上传结果: 成功");
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("=====================================");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("上传失败: " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试上传不同格式的图片
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testUploadDifferentImageFormats() {
|
|
||||||
String[] imagePaths = {
|
|
||||||
"D:/test/image1.jpg", // 请修改为实际路径
|
|
||||||
"D:/test/image2.png", // 请修改为实际路径
|
|
||||||
"D:/test/image3.gif" // 请修改为实际路径
|
|
||||||
};
|
|
||||||
|
|
||||||
System.out.println("========== 多格式图片上传测试 ==========");
|
|
||||||
|
|
||||||
for (String imagePath : imagePaths) {
|
|
||||||
try {
|
|
||||||
File imageFile = new File(imagePath);
|
|
||||||
if (!imageFile.exists()) {
|
|
||||||
System.out.println("跳过不存在的文件: " + imagePath);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取文件
|
|
||||||
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
|
|
||||||
|
|
||||||
// 获取文件扩展名
|
|
||||||
String extension = getFileExtension(imagePath);
|
|
||||||
|
|
||||||
// 生成OSS对象名称
|
|
||||||
String objectName = generateObjectName("format-test", extension);
|
|
||||||
|
|
||||||
// 上传
|
|
||||||
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
|
|
||||||
|
|
||||||
System.out.println("\n文件: " + imagePath);
|
|
||||||
System.out.println("格式: " + extension);
|
|
||||||
System.out.println("大小: " + imageBytes.length + " 字节");
|
|
||||||
System.out.println("URL: " + fileUrl);
|
|
||||||
System.out.println("状态: 成功");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("\n文件: " + imagePath);
|
|
||||||
System.out.println("状态: 失败 - " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("\n=====================================");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试上传图片到指定目录
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testUploadImageToDirectory() {
|
|
||||||
try {
|
|
||||||
// 本地图片文件路径
|
|
||||||
String localImagePath = "D:/test/image.jpg"; // 请修改为实际路径
|
|
||||||
|
|
||||||
File imageFile = new File(localImagePath);
|
|
||||||
if (!imageFile.exists()) {
|
|
||||||
System.out.println("图片文件不存在: " + localImagePath);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取文件
|
|
||||||
byte[] imageBytes = Files.readAllBytes(Paths.get(localImagePath));
|
|
||||||
|
|
||||||
// 上传到指定目录(例如:images/2025/12/24/xxx.jpg)
|
|
||||||
String directory = "images/" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
|
|
||||||
String fileName = UUID.randomUUID().toString() + ".jpg";
|
|
||||||
String objectName = directory + "/" + fileName;
|
|
||||||
|
|
||||||
// 上传
|
|
||||||
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
|
|
||||||
|
|
||||||
System.out.println("========== 目录上传测试 ==========");
|
|
||||||
System.out.println("目录: " + directory);
|
|
||||||
System.out.println("文件名: " + fileName);
|
|
||||||
System.out.println("完整路径: " + objectName);
|
|
||||||
System.out.println("文件访问URL: " + fileUrl);
|
|
||||||
System.out.println("上传结果: 成功");
|
|
||||||
System.out.println("=================================");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("上传失败: " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试使用FileInputStream上传
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testUploadWithFileInputStream() {
|
|
||||||
try {
|
|
||||||
// 本地图片文件路径
|
|
||||||
String localImagePath = "D:/test/image.jpg"; // 请修改为实际路径
|
|
||||||
|
|
||||||
File imageFile = new File(localImagePath);
|
|
||||||
if (!imageFile.exists()) {
|
|
||||||
System.out.println("图片文件不存在: " + localImagePath);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用FileInputStream读取文件
|
|
||||||
try (FileInputStream fis = new FileInputStream(imageFile)) {
|
|
||||||
byte[] imageBytes = new byte[(int) imageFile.length()];
|
|
||||||
fis.read(imageBytes);
|
|
||||||
|
|
||||||
// 生成OSS对象名称
|
|
||||||
String objectName = generateObjectName("stream", "jpg");
|
|
||||||
|
|
||||||
// 上传
|
|
||||||
String fileUrl = aliOssUtil.upload(imageBytes, objectName);
|
|
||||||
|
|
||||||
System.out.println("========== FileInputStream上传测试 ==========");
|
|
||||||
System.out.println("文件路径: " + localImagePath);
|
|
||||||
System.out.println("文件大小: " + imageBytes.length + " 字节");
|
|
||||||
System.out.println("OSS对象名称: " + objectName);
|
|
||||||
System.out.println("文件访问URL: " + fileUrl);
|
|
||||||
System.out.println("上传结果: 成功");
|
|
||||||
System.out.println("===========================================");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("上传失败: " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成OSS对象名称
|
|
||||||
* @param prefix 前缀
|
|
||||||
* @param extension 文件扩展名
|
|
||||||
* @return 对象名称
|
|
||||||
*/
|
|
||||||
private String generateObjectName(String prefix, String extension) {
|
|
||||||
// 格式: prefix/yyyyMMdd/HHmmss-uuid.extension
|
|
||||||
String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd/HHmmss"));
|
|
||||||
String uuid = UUID.randomUUID().toString().substring(0, 8);
|
|
||||||
return String.format("%s/%s-%s.%s", prefix, dateTime, uuid, extension);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从文件路径获取扩展名
|
|
||||||
* @param filePath 文件路径
|
|
||||||
* @return 扩展名
|
|
||||||
*/
|
|
||||||
private String getFileExtension(String filePath) {
|
|
||||||
int lastDot = filePath.lastIndexOf('.');
|
|
||||||
if (lastDot > 0 && lastDot < filePath.length() - 1) {
|
|
||||||
return filePath.substring(lastDot + 1).toLowerCase();
|
|
||||||
}
|
|
||||||
return "jpg"; // 默认扩展名
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从URL获取文件扩展名
|
|
||||||
* @param url URL地址
|
|
||||||
* @return 扩展名
|
|
||||||
*/
|
|
||||||
private String getFileExtensionFromUrl(String url) {
|
|
||||||
try {
|
|
||||||
// 移除查询参数
|
|
||||||
String path = url.split("\\?")[0];
|
|
||||||
int lastDot = path.lastIndexOf('.');
|
|
||||||
int lastSlash = path.lastIndexOf('/');
|
|
||||||
|
|
||||||
if (lastDot > lastSlash && lastDot < path.length() - 1) {
|
|
||||||
return path.substring(lastDot + 1).toLowerCase();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 忽略异常
|
|
||||||
}
|
|
||||||
return "jpg"; // 默认扩展名
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从InputStream读取所有字节(兼容方法)
|
|
||||||
* @param inputStream 输入流
|
|
||||||
* @return 字节数组
|
|
||||||
* @throws IOException IO异常
|
|
||||||
*/
|
|
||||||
private byte[] readAllBytes(InputStream inputStream) throws IOException {
|
|
||||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
||||||
byte[] data = new byte[8192]; // 8KB缓冲区
|
|
||||||
int nRead;
|
|
||||||
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
|
|
||||||
buffer.write(data, 0, nRead);
|
|
||||||
}
|
|
||||||
return buffer.toByteArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
package com.bao.dating.service;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码服务测试类
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@SpringBootTest
|
|
||||||
public class VerificationCodeServiceTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VerificationCodeService verificationCodeService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试发送邮箱验证码
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testSendEmailCode() {
|
|
||||||
String email = "test@example.com"; // 请修改为实际邮箱地址
|
|
||||||
|
|
||||||
System.out.println("========== 发送邮箱验证码测试 ==========");
|
|
||||||
System.out.println("邮箱地址: " + email);
|
|
||||||
|
|
||||||
boolean result = verificationCodeService.sendEmailCode(email);
|
|
||||||
|
|
||||||
System.out.println("发送结果: " + (result ? "成功" : "失败"));
|
|
||||||
System.out.println("=====================================");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试验证邮箱验证码
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testVerifyEmailCode() {
|
|
||||||
String email = "test@example.com"; // 请修改为实际邮箱地址
|
|
||||||
String code = "123456"; // 请修改为实际收到的验证码
|
|
||||||
|
|
||||||
System.out.println("========== 验证邮箱验证码测试 ==========");
|
|
||||||
System.out.println("邮箱地址: " + email);
|
|
||||||
System.out.println("验证码: " + code);
|
|
||||||
|
|
||||||
boolean result = verificationCodeService.verifyEmailCode(email, code);
|
|
||||||
|
|
||||||
System.out.println("验证结果: " + (result ? "成功" : "失败"));
|
|
||||||
System.out.println("=====================================");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试完整流程:发送验证码 -> 验证验证码
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testCompleteFlow() {
|
|
||||||
String email = "test@example.com"; // 请修改为实际邮箱地址
|
|
||||||
|
|
||||||
System.out.println("========== 完整流程测试 ==========");
|
|
||||||
System.out.println("邮箱地址: " + email);
|
|
||||||
|
|
||||||
// 1. 发送验证码
|
|
||||||
System.out.println("\n1. 发送验证码...");
|
|
||||||
boolean sendResult = verificationCodeService.sendEmailCode(email);
|
|
||||||
System.out.println("发送结果: " + (sendResult ? "成功" : "失败"));
|
|
||||||
|
|
||||||
if (sendResult) {
|
|
||||||
// 2. 等待用户输入验证码(这里模拟,实际应该从控制台或API获取)
|
|
||||||
System.out.println("\n2. 请查看邮箱获取验证码,然后手动测试验证功能");
|
|
||||||
System.out.println(" 使用 testVerifyEmailCode() 方法进行验证");
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("\n=====================================");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试生成验证码
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testGenerateCode() {
|
|
||||||
System.out.println("========== 生成验证码测试 ==========");
|
|
||||||
|
|
||||||
// 测试不同长度的验证码
|
|
||||||
for (int length = 4; length <= 8; length++) {
|
|
||||||
String code = verificationCodeService.generateCode(length);
|
|
||||||
System.out.println(length + "位验证码: " + code);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("=====================================");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -75,4 +75,3 @@ public class EmailAndSmsTest {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user