Compare commits
42 Commits
60df001385
...
feature-in
| Author | SHA1 | Date | |
|---|---|---|---|
| a4faa11036 | |||
| 290db24bc4 | |||
| 31b0dffd68 | |||
| 31fd23afa8 | |||
| d8d46ab089 | |||
|
|
c83d86ad1a | ||
|
|
b12128fad6 | ||
|
|
212668ae1c | ||
|
|
a648ecad2a | ||
|
|
b8ec4a434d | ||
|
|
3bc00334ea | ||
|
|
a6259875f2 | ||
| 34cad7457b | |||
| eb2b0cb999 | |||
| 61c4c9d442 | |||
| 30db5ad706 | |||
|
|
717c0a0507 | ||
|
|
413bafa275 | ||
|
|
2ce8116126 | ||
|
|
5151ccfacc | ||
|
|
ab2bde0517 | ||
|
|
c9cb410819 | ||
|
|
07a6b86d7d | ||
|
|
1cf9ba0aa9 | ||
|
|
6a68fefbd4 | ||
|
|
da66c3b048 | ||
|
|
a9730a8414 | ||
|
|
0da6cde202 | ||
|
|
59838e1f5b | ||
|
|
7601fbb3c0 | ||
|
|
170ce83fe1 | ||
|
|
aa16c30d57 | ||
|
|
8fdbad8bd5 | ||
|
|
a9598ba795 | ||
|
|
439b6789fa | ||
|
|
9c7afc4470 | ||
|
|
e92cd31c27 | ||
|
|
bd12c599b0 | ||
|
|
b5a15a3f01 | ||
|
|
039443dc0f | ||
| ae0cca5437 | |||
| c329eaef79 |
12
pom.xml
12
pom.xml
@@ -88,6 +88,12 @@
|
|||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- WebSocket 起步依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 阿里云相关依赖 -->
|
<!-- 阿里云相关依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
@@ -159,6 +165,12 @@
|
|||||||
<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>
|
||||||
|
|||||||
@@ -4,11 +4,32 @@ import org.mybatis.spring.annotation.MapperScan;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
@MapperScan("com.bao.dating.mapper")
|
@MapperScan("com.bao.dating.mapper")
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class DatingApplication {
|
public class DatingApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DatingApplication.class, args);
|
SpringApplication.run(DatingApplication.class, args);
|
||||||
|
// 读取并打印 ciallo.txt 文件内容
|
||||||
|
printCialloFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取并打印 ciallo.txt 文件内容
|
||||||
|
*/
|
||||||
|
private static void printCialloFile() {
|
||||||
|
try (InputStream inputStream = DatingApplication.class.getClassLoader().getResourceAsStream("ciallo.txt");
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("读取 ciallo.txt 文件时发生错误: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.bao.dating.common;
|
|
||||||
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
/**
|
|
||||||
* 动态表
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class Post {
|
|
||||||
private Long post_id; // 动态ID
|
|
||||||
private String content; // 动态内容
|
|
||||||
private Long user_id; // 发布人ID
|
|
||||||
private LocalDateTime created_at; // 创建时间
|
|
||||||
}
|
|
||||||
@@ -19,6 +19,14 @@ 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, "系统异常"),
|
||||||
/** 操作失败 */
|
/** 操作失败 */
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ public class SmsUtil {
|
|||||||
.setTemplateCode(templateCode != null ? templateCode : this.templateCode)
|
.setTemplateCode(templateCode != null ? templateCode : this.templateCode)
|
||||||
.setTemplateParam(templateParam);
|
.setTemplateParam(templateParam);
|
||||||
|
|
||||||
|
log.error("TemplateParam 实际值 = {}", templateParam);
|
||||||
|
|
||||||
SendSmsResponse response = client.sendSms(sendSmsRequest);
|
SendSmsResponse response = client.sendSms(sendSmsRequest);
|
||||||
|
|
||||||
if ("OK".equals(response.getBody().getCode())) {
|
if ("OK".equals(response.getBody().getCode())) {
|
||||||
@@ -145,7 +147,9 @@ public class SmsUtil {
|
|||||||
jsonBuilder.append("}");
|
jsonBuilder.append("}");
|
||||||
return sendSms(phoneNumber, templateCode, jsonBuilder.toString());
|
return sendSms(phoneNumber, templateCode, jsonBuilder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,13 @@ public class RedisConfig {
|
|||||||
// 设置redis的连接工厂对象
|
// 设置redis的连接工厂对象
|
||||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
|
||||||
// key 使用 String
|
// 设置redis key的序列化器
|
||||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||||
|
// 设置value的序列化器
|
||||||
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||||
|
// 设置hash类型的key和value的序列化器
|
||||||
|
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||||
|
|
||||||
return redisTemplate;
|
return redisTemplate;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
.addPathPatterns("/**")
|
.addPathPatterns("/**")
|
||||||
// 忽略的接口
|
// 忽略的接口
|
||||||
.excludePathPatterns(
|
.excludePathPatterns(
|
||||||
"/user/login"
|
"/user/login",
|
||||||
|
"/user/sendCode"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class PostController {
|
|||||||
@PostMapping("/deletePost")
|
@PostMapping("/deletePost")
|
||||||
public Result<String> deleteById(@RequestBody List<Long> postIds){
|
public Result<String> deleteById(@RequestBody List<Long> postIds){
|
||||||
int deletedCount = postService.deletePostById(postIds);
|
int deletedCount = postService.deletePostById(postIds);
|
||||||
return Result.success(ResultCode.SUCCESS_DELETE, deletedCount > 0 ? "成功删除" : "删除失败,该动态不存在", null);
|
return Result.success(ResultCode.SUCCESS_DELETE, "成功删除" + deletedCount + "条动态", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,6 +6,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;
|
||||||
@@ -14,7 +15,9 @@ 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;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户接口
|
* 用户接口
|
||||||
@@ -65,7 +68,6 @@ public class UserController {
|
|||||||
* @param file 头像文件
|
* @param file 头像文件
|
||||||
* @return 上传后的文件URL列表
|
* @return 上传后的文件URL列表
|
||||||
*/
|
*/
|
||||||
@Log
|
|
||||||
@PostMapping(value = "/info/uploadAvatar", consumes = "multipart/form-data")
|
@PostMapping(value = "/info/uploadAvatar", consumes = "multipart/form-data")
|
||||||
public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
|
public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
|
||||||
String fileUrl = userService.uploadAvatar(file);
|
String fileUrl = userService.uploadAvatar(file);
|
||||||
@@ -77,7 +79,6 @@ public class UserController {
|
|||||||
* @param file 背景文件
|
* @param file 背景文件
|
||||||
* @return 上传后的文件URL列表
|
* @return 上传后的文件URL列表
|
||||||
*/
|
*/
|
||||||
@Log
|
|
||||||
@PostMapping(value = "/info/uploadBackground", consumes = "multipart/form-data")
|
@PostMapping(value = "/info/uploadBackground", consumes = "multipart/form-data")
|
||||||
public Result<String> uploadBackground(@RequestParam("file") MultipartFile file) {
|
public Result<String> uploadBackground(@RequestParam("file") MultipartFile file) {
|
||||||
String fileUrl = userService.uploadBackground(file);
|
String fileUrl = userService.uploadBackground(file);
|
||||||
@@ -89,7 +90,6 @@ public class UserController {
|
|||||||
* @param userInfoUpdateDTO 用户信息更新参数
|
* @param userInfoUpdateDTO 用户信息更新参数
|
||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
@Log
|
|
||||||
@PostMapping("/info/update")
|
@PostMapping("/info/update")
|
||||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||||
Long userId = UserContext.getUserId();
|
Long userId = UserContext.getUserId();
|
||||||
@@ -97,4 +97,71 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/sendCode")
|
||||||
|
public Result sendCode(@RequestBody Map<String, String> body) {
|
||||||
|
String phone = body.get("phone");
|
||||||
|
userService.sendSmsCode(phone);
|
||||||
|
return Result.success(ResultCode.SUCCESS, "验证码发送成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/loginByCode")
|
||||||
|
public Result loginByCode(@RequestBody Map<String, String> body){
|
||||||
|
boolean ok = userService.verifyCode(body.get("phone"), body.get("code"));
|
||||||
|
return ok ? Result.success(ResultCode.SUCCESS, "登录成功") : Result.error(ResultCode.SYSTEM_ERROR, "登录失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param body 登录参数
|
||||||
|
*/
|
||||||
|
@PostMapping("/loginPhone")
|
||||||
|
public Result<?> loginPhone(@RequestBody Map<String, String> body) {
|
||||||
|
String phone = body.get("phone");
|
||||||
|
String code = body.get("code");
|
||||||
|
|
||||||
|
//校验验证码
|
||||||
|
boolean verify = userService.verifyCode(phone, code);
|
||||||
|
if (!verify){
|
||||||
|
return Result.error(ResultCode.SYSTEM_ERROR, "验证码错误或已过期");
|
||||||
|
}
|
||||||
|
//登录
|
||||||
|
UserLoginVO vo = userService.loginByPhone(phone);
|
||||||
|
return Result.success(ResultCode.SUCCESS, "登录成功", vo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/nearby")
|
||||||
|
public Result<List<UserInfoVO>> nearby(@RequestParam(defaultValue = "5") double distance){
|
||||||
|
// 获取当前线程的用户 ID
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
|
if (userId == null) {
|
||||||
|
return Result.error(ResultCode.SYSTEM_ERROR, "用户未登录");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过 UserID 获取当前用户的经纬度信息
|
||||||
|
UserInfoVO currentUser = userService.getUserInfo(userId);
|
||||||
|
if (currentUser == null) {
|
||||||
|
return Result.error(ResultCode.SYSTEM_ERROR, "用户信息获取失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前用户的经纬度
|
||||||
|
Double latitude = currentUser.getLatitude();
|
||||||
|
Double longitude = currentUser.getLongitude();
|
||||||
|
|
||||||
|
// 检查经纬度是否为空
|
||||||
|
if (latitude == null || longitude == null) {
|
||||||
|
return Result.error(ResultCode.SYSTEM_ERROR, "用户经纬度信息未完善");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 这里可以添加默认值,比如如果 latitude 或 longitude 为 null,则设置为某个默认值(例如 0)
|
||||||
|
latitude = (latitude != null) ? latitude : 0.0;
|
||||||
|
longitude = (longitude != null) ? longitude : 0.0;
|
||||||
|
|
||||||
|
// 查询附近用户
|
||||||
|
List<UserInfoVO> nearbyUsers = userService.findNearbyUsers(latitude, longitude, distance);
|
||||||
|
|
||||||
|
// 返回成功结果
|
||||||
|
return Result.success(ResultCode.SUCCESS, "查询成功", nearbyUsers);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -71,8 +71,7 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||||||
String userId = JwtUtil.getSubjectFromToken(token);
|
String userId = JwtUtil.getSubjectFromToken(token);
|
||||||
|
|
||||||
// 从Redis获取存储的token进行比对
|
// 从Redis获取存储的token进行比对
|
||||||
Object redisTokenObj = redisTemplate.opsForValue()
|
Object redisTokenObj = redisTemplate.opsForValue().get("login:token:" + userId);
|
||||||
.get("login:token:" + userId);
|
|
||||||
String redisToken = redisTokenObj != null ? redisTokenObj.toString() : null;
|
String redisToken = redisTokenObj != null ? redisTokenObj.toString() : null;
|
||||||
|
|
||||||
// 验证Redis中的token是否存在且匹配
|
// 验证Redis中的token是否存在且匹配
|
||||||
|
|||||||
@@ -5,17 +5,24 @@ import com.bao.dating.pojo.entity.Comments;
|
|||||||
import org.apache.ibatis.annotations.*;
|
import org.apache.ibatis.annotations.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@Mapper
|
||||||
public interface CommentsMapper {
|
public interface CommentsMapper {
|
||||||
// 添加评论
|
// 添加评论
|
||||||
@Insert("INSERT INTO comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
|
@Insert("INSERT INTO dating.comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
|
||||||
int addComment(Comments comments);
|
int addComment(Comments comments);
|
||||||
|
|
||||||
// 删除评论
|
// 删除评论
|
||||||
@Delete("DELETE FROM comments WHERE user_id = #{user_id}")
|
@Delete("DELETE FROM dating.comments WHERE user_id = #{user_id}")
|
||||||
int deleteComments(@Param("user_id") Long user_id);
|
int deleteComments(@Param("user_id") Long user_id);
|
||||||
|
|
||||||
// 根据动态ID查询评论列表
|
// 根据动态ID查询评论列表
|
||||||
@Select("SELECT * FROM comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
|
@Select("SELECT * FROM dating.comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
|
||||||
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
|
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据动态ID批量删除评论
|
||||||
|
* @param postIds
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int deleteCommentsByPostIds(@Param("postIds") List<Long> postIds);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,12 @@ 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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除收藏
|
||||||
|
* @param postIds
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int deleteFavoritesByPostIds(@Param("postIds") List<Long> postIds);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.bao.dating.pojo.entity.PostLike;
|
|||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface PostLikeMapper {
|
public interface PostLikeMapper {
|
||||||
/**
|
/**
|
||||||
@@ -31,4 +33,12 @@ public interface PostLikeMapper {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
|
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除点赞记录
|
||||||
|
*
|
||||||
|
* @param postIds
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int deleteLikesByPostIds(@Param("postIds") List<Long> postIds);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ public interface PostMapper {
|
|||||||
void insert(Post post);
|
void insert(Post post);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID删除动态
|
* 根据ID修改动态状态
|
||||||
*
|
*
|
||||||
* @param postIds 动态ID
|
* @param postIds 动态ID
|
||||||
*/
|
*/
|
||||||
int deletePostByIds(List<Long> postIds);
|
int updatePublicById(@Param("postIds") List<Long> postIds, @Param("userId") Long userId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID查询动态
|
* 根据ID查询动态
|
||||||
|
|||||||
@@ -2,7 +2,11 @@ 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.User;
|
import com.bao.dating.pojo.entity.User;
|
||||||
|
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户Mapper
|
* 用户Mapper
|
||||||
@@ -33,4 +37,15 @@ public interface UserMapper {
|
|||||||
*/
|
*/
|
||||||
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
|
User selectByPhone(@Param("phone") String phone);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据经纬度范围查询用户
|
||||||
|
* @param minLat 最小纬度
|
||||||
|
* @param maxLat 最大纬度
|
||||||
|
* @param minLng 最小经度
|
||||||
|
* @param maxLng 最大经度
|
||||||
|
* @return 用户列表
|
||||||
|
*/
|
||||||
|
List<UserInfoVO> findByLatLngRange(@Param("minLat") double minLat, @Param("maxLat") double maxLat, @Param("minLng") double minLng, @Param("maxLng") double maxLng);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,4 +43,8 @@ public class User implements Serializable {
|
|||||||
private String userEmail;
|
private String userEmail;
|
||||||
|
|
||||||
private String userPhone;
|
private String userPhone;
|
||||||
|
|
||||||
|
private Double latitude; // 纬度
|
||||||
|
|
||||||
|
private Double longitude; // 经度
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,4 +24,6 @@ public class UserInfoVO implements Serializable {
|
|||||||
private String signature;
|
private String signature;
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
private Double latitude;
|
||||||
|
private Double longitude;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public interface PostService {
|
|||||||
Post createPost(PostRequestDTO postRequestDTO);
|
Post createPost(PostRequestDTO postRequestDTO);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除动态
|
* 批量删除动态(将动态状态改为已删除)
|
||||||
* @param postIds 动态ID
|
* @param postIds 动态ID
|
||||||
* @return 删除的动态对象
|
* @return 删除的动态对象
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,10 +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.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
|
* @author KilLze
|
||||||
@@ -52,4 +55,19 @@ public interface UserService {
|
|||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
|
void sendSmsCode(String phone);
|
||||||
|
|
||||||
|
boolean verifyCode(String phone, String code);
|
||||||
|
|
||||||
|
UserLoginVO loginByPhone(String phone);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定经纬度范围内的用户
|
||||||
|
* @param lat 用户纬度
|
||||||
|
* @param lng 用户经度
|
||||||
|
* @param radiusKm 半径 km
|
||||||
|
* @return 用户列表
|
||||||
|
*/
|
||||||
|
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -4,17 +4,22 @@ 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.common.result.GreenAuditResult;
|
||||||
import com.bao.dating.context.UserContext;
|
import com.bao.dating.context.UserContext;
|
||||||
|
import com.bao.dating.mapper.CommentsMapper;
|
||||||
|
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||||
|
import com.bao.dating.mapper.PostLikeMapper;
|
||||||
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;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.vo.PostEditVO;
|
import com.bao.dating.pojo.vo.PostEditVO;
|
||||||
import com.bao.dating.service.PostService;
|
import com.bao.dating.service.PostService;
|
||||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||||
|
import com.bao.dating.service.UserService;
|
||||||
import com.bao.dating.util.FileUtil;
|
import com.bao.dating.util.FileUtil;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -43,6 +48,15 @@ public class PostServiceImpl implements PostService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostMapper postMapper;
|
private PostMapper postMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PostLikeMapper postLikeMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PostFavoriteMapper postFavoriteMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CommentsMapper commentsMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传媒体文件
|
* 上传媒体文件
|
||||||
* @param files 媒体文件数组
|
* @param files 媒体文件数组
|
||||||
@@ -177,7 +191,7 @@ public class PostServiceImpl implements PostService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除动态
|
* 批量删除动态(将动态状态改为已删除)
|
||||||
*
|
*
|
||||||
* @param postIds 动态ID
|
* @param postIds 动态ID
|
||||||
* @return 删除的动态对象
|
* @return 删除的动态对象
|
||||||
@@ -188,19 +202,21 @@ public class PostServiceImpl implements PostService {
|
|||||||
// 判断用户权限
|
// 判断用户权限
|
||||||
Long userId = UserContext.getUserId();
|
Long userId = UserContext.getUserId();
|
||||||
|
|
||||||
// 遍历所有要删除的帖子ID,验证权限
|
if (CollectionUtils.isEmpty(postIds)) {
|
||||||
for (Long postId : postIds) {
|
return 0;
|
||||||
Post post = postMapper.selectById(postId);
|
|
||||||
if (post == null) {
|
|
||||||
throw new RuntimeException("动态不存在");
|
|
||||||
}
|
}
|
||||||
// 验证用户权限
|
int affected = postMapper.updatePublicById(postIds, userId);
|
||||||
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
|
|
||||||
throw new RuntimeException("无权限删除此动态");
|
if (affected == 0) {
|
||||||
|
throw new RuntimeException("未删除任何动态,可能无权限或动态不存在");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// 批量删除动态
|
// 删除动态下的评论、点赞、收藏
|
||||||
return postMapper.deletePostByIds(postIds);
|
commentsMapper.deleteCommentsByPostIds(postIds);
|
||||||
|
postLikeMapper.deleteLikesByPostIds(postIds);
|
||||||
|
postFavoriteMapper.deleteFavoritesByPostIds(postIds);
|
||||||
|
|
||||||
|
return affected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.bao.dating.service.impl;
|
|||||||
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.aliyun.SmsUtil;
|
||||||
import com.bao.dating.common.result.AliOssResult;
|
import com.bao.dating.common.result.AliOssResult;
|
||||||
import com.bao.dating.common.result.GreenAuditResult;
|
import com.bao.dating.common.result.GreenAuditResult;
|
||||||
import com.bao.dating.config.RedisConfig;
|
import com.bao.dating.config.RedisConfig;
|
||||||
@@ -14,6 +15,8 @@ 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.util.*;
|
||||||
|
import com.bao.dating.util.CodeUtil;
|
||||||
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;
|
||||||
@@ -21,6 +24,7 @@ import io.jsonwebtoken.Claims;
|
|||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@@ -37,6 +41,10 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SmsUtil smsUtil;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AliOssUtil ossUtil;
|
private AliOssUtil ossUtil;
|
||||||
|
|
||||||
@@ -52,6 +60,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
*
|
*
|
||||||
@@ -311,4 +322,85 @@ public class UserServiceImpl implements UserService {
|
|||||||
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
||||||
return userInfoVO;
|
return userInfoVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发送短信验证码
|
||||||
|
@Override
|
||||||
|
public void sendSmsCode(String phone) {
|
||||||
|
//防刷:60 秒内只能发一次
|
||||||
|
String key = "sms:code:" + phone;
|
||||||
|
Boolean exists = stringRedisTemplate.hasKey(key);
|
||||||
|
if (Boolean.TRUE.equals(exists)){
|
||||||
|
throw new RuntimeException("请勿频繁发送验证码");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成验证码
|
||||||
|
String code = CodeUtil.generateCode();
|
||||||
|
// 发送短信
|
||||||
|
smsUtil.sendVerificationCode(phone, code);
|
||||||
|
//存 Redis(5分钟过期)
|
||||||
|
stringRedisTemplate.opsForValue()
|
||||||
|
.set(key,code, 5, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验验证码
|
||||||
|
@Override
|
||||||
|
public boolean verifyCode(String phone, String code) {
|
||||||
|
|
||||||
|
String key = "sms:code:" + phone;
|
||||||
|
String realCode = stringRedisTemplate.opsForValue().get(key);
|
||||||
|
//过期,未发送
|
||||||
|
if (realCode ==null){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//不匹配
|
||||||
|
if (!realCode.equals(code)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 校验成功,删除验证码(一次性)
|
||||||
|
stringRedisTemplate.delete(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserLoginVO loginByPhone(String phone) {
|
||||||
|
//根据手机号查询用户
|
||||||
|
User user = userMapper.selectByPhone(phone);
|
||||||
|
//创建token
|
||||||
|
String token = JwtUtil.generateToken(user.getUserId().toString());
|
||||||
|
//封装返回结果
|
||||||
|
UserLoginVO VO = new UserLoginVO();
|
||||||
|
VO.setUserId(user.getUserId());
|
||||||
|
VO.setToken(token);
|
||||||
|
return VO;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<UserInfoVO> findNearbyUsers(double lat, double lng, double radiusKm) {
|
||||||
|
//先用经纬度范围筛选(矩形框,提高性能)
|
||||||
|
double delta = radiusKm / 111.0; // 1° ≈ 111km
|
||||||
|
double minLat = lat - delta;// 最小纬度
|
||||||
|
double maxLat = lat + delta;// 最大纬度
|
||||||
|
double minLng = lng - delta;// 最小经度
|
||||||
|
double maxLng = lng + delta;// 最大经度
|
||||||
|
|
||||||
|
// 打印经纬度范围
|
||||||
|
System.out.println("Min Latitude: " + minLat + ", Max Latitude: " + maxLat);
|
||||||
|
System.out.println("Min Longitude: " + minLng + ", Max Longitude: " + maxLng);
|
||||||
|
|
||||||
|
List<UserInfoVO> byLatLngRange = userMapper.findByLatLngRange(minLat, maxLat, minLng, maxLng);
|
||||||
|
|
||||||
|
//精确计算距离,筛选在半径内的用户
|
||||||
|
List<UserInfoVO> result = new ArrayList<>();
|
||||||
|
for (UserInfoVO u:byLatLngRange){
|
||||||
|
// 检查用户是否有经纬度信息
|
||||||
|
if (u.getLatitude() != null && u.getLongitude() != null) {
|
||||||
|
double distance = DistanceUtil.calculate(lat, lng, u.getLatitude(), u.getLongitude());
|
||||||
|
if (distance <= radiusKm){
|
||||||
|
result.add(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
12
src/main/java/com/bao/dating/util/CodeUtil.java
Normal file
12
src/main/java/com/bao/dating/util/CodeUtil.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.bao.dating.util;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class CodeUtil {
|
||||||
|
// 生成6位数字验证码
|
||||||
|
public static String generateCode() {
|
||||||
|
Random random = new Random();
|
||||||
|
int code=100000+random.nextInt(900000);
|
||||||
|
return String.valueOf(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/main/java/com/bao/dating/util/DistanceUtil.java
Normal file
29
src/main/java/com/bao/dating/util/DistanceUtil.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.bao.dating.util;
|
||||||
|
|
||||||
|
//Haversine 公式(标准写法)
|
||||||
|
|
||||||
|
public class DistanceUtil {
|
||||||
|
private static final double EARTH_RADIUS = 6371.0; // 地球半径 km
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算两点之间距离(单位:km)
|
||||||
|
*/
|
||||||
|
public static double calculate(
|
||||||
|
double lat1, double lon1,
|
||||||
|
double lat2, double lon2) {
|
||||||
|
|
||||||
|
double dLat = Math.toRadians(lat2 - lat1);
|
||||||
|
double dLon = Math.toRadians(lon2 - lon1);
|
||||||
|
|
||||||
|
lat1 = Math.toRadians(lat1);
|
||||||
|
lat2 = Math.toRadians(lat2);
|
||||||
|
|
||||||
|
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
|
||||||
|
+ Math.cos(lat1) * Math.cos(lat2)
|
||||||
|
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||||
|
|
||||||
|
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||||
|
|
||||||
|
return EARTH_RADIUS * c;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -88,6 +88,3 @@ public class MD5Util {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,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: 127.0.0.1
|
host: 110.42.41.177
|
||||||
port: 6379
|
port: 6389
|
||||||
password: ""
|
password: ""
|
||||||
database: 0
|
database: 0
|
||||||
timeout: 10000
|
timeout: 10000
|
||||||
|
|||||||
9
src/main/resources/ciallo.txt
Normal file
9
src/main/resources/ciallo.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
▄▄▄▄ ██ ▄▄▄▄ ▄▄▄▄ ▄▄ ▄▄
|
||||||
|
██▀▀▀▀█ ▀▀ ▀▀██ ▀▀██ ██ ▄▄ ██
|
||||||
|
██▀ ████ ▄█████▄ ██ ██ ▄████▄ ▄▄▄ ▄█▀ ▄█▀ ██ █▄ ▄▄▄█ ▀█▄ ▄▄▄▄
|
||||||
|
██ ██ ▀ ▄▄▄██ ██ ██ ██▀ ▀██ ▀ ▀▀▄▄ ▄ ██ ▄█▀ ██ ██ ██ ▄▄█▀▀▀ ██ █▀▀ ▀█▄ █▄
|
||||||
|
██▄ ██ ▄██▀▀▀██ ██ ██ ██ ██ ▀▀▀ ██ ▄█▄▄▄▄▄ ▀▀ ██ ██ ██ ▀▀█▄▄▄ ██ █▀ █ ▀▀████▀
|
||||||
|
██▄▄▄▄█ ▄▄▄██▄▄▄ ██▄▄▄███ ██▄▄▄ ██▄▄▄ ▀██▄▄██▀ ▀█▄ ▀▀▀▀▀▀▀▀ ██▄██▄██ ▀▀▀█ ▄█▀ █▀▀█
|
||||||
|
▀▀▀▀ ▀▀▀▀▀▀▀▀ ▀▀▀▀ ▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ██ ▀▀▀ ▀▀▀ ██
|
||||||
|
▀▀ ▀▀
|
||||||
15
src/main/resources/com/bao/dating/mapper/CommentsMapper.xml
Normal file
15
src/main/resources/com/bao/dating/mapper/CommentsMapper.xml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.bao.dating.mapper.CommentsMapper">
|
||||||
|
|
||||||
|
<!-- 批量删除动态下的所有评论 -->
|
||||||
|
<delete id="deleteCommentsByPostIds">
|
||||||
|
DELETE FROM comments
|
||||||
|
WHERE post_id IN
|
||||||
|
<foreach collection="postIds" item="postId" open="(" close=")" separator=",">
|
||||||
|
#{postId}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
</mapper>
|
||||||
@@ -14,4 +14,14 @@
|
|||||||
<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>
|
||||||
|
|
||||||
|
<!--批量删除动态收藏-->
|
||||||
|
<delete id="deleteFavoritesByPostIds">
|
||||||
|
DELETE FROM post_favorite
|
||||||
|
WHERE post_id IN
|
||||||
|
<foreach collection="postIds" item="postId" open="(" close=")" separator=",">
|
||||||
|
#{postId}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -14,4 +14,14 @@
|
|||||||
<delete id="deleteByPostIdAndUserId">
|
<delete id="deleteByPostIdAndUserId">
|
||||||
delete from dating.post_like where post_id = #{postId} and user_id = #{userId}
|
delete from dating.post_like where post_id = #{postId} and user_id = #{userId}
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
|
<!--批量删除点赞记录-->
|
||||||
|
<delete id="deleteLikesByPostIds">
|
||||||
|
DELETE FROM post_like
|
||||||
|
WHERE post_id IN
|
||||||
|
<foreach collection="postIds" item="postId" open="(" close=")" separator=",">
|
||||||
|
#{postId}
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -28,25 +28,19 @@
|
|||||||
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
|
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<!--动态删除-->
|
<!--修改动态状态-->
|
||||||
<delete id="deletePostByIds">
|
<update id="updatePublicById">
|
||||||
DELETE FROM post WHERE post_id IN
|
UPDATE post
|
||||||
|
<set>
|
||||||
|
is_public = 3,
|
||||||
|
updated_at = NOW()
|
||||||
|
</set>
|
||||||
|
WHERE post_id IN
|
||||||
<foreach item="postId" index="index" collection="postIds" separator="," open="(" close=")">
|
<foreach item="postId" index="index" collection="postIds" separator="," open="(" close=")">
|
||||||
#{postId}
|
#{postId}
|
||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
AND user_id = #{userId}
|
||||||
<!--删除收藏记录-->
|
</update>
|
||||||
<delete id="1">
|
|
||||||
DELETE FROM post_favorite WHERE post_id = #{postId}
|
|
||||||
</delete>
|
|
||||||
<!--删除点赞记录-->
|
|
||||||
<delete id="2">
|
|
||||||
DELETE FROM post_like WHERE post_id = #{postId}
|
|
||||||
</delete>
|
|
||||||
<!--动态评论删除-->
|
|
||||||
<delete id="3">
|
|
||||||
DELETE FROM comments WHERE post_id = #{postId}
|
|
||||||
</delete>
|
|
||||||
|
|
||||||
<!--动态查询-->
|
<!--动态查询-->
|
||||||
<resultMap id="PostResultMap" type="com.bao.dating.pojo.entity.Post">
|
<resultMap id="PostResultMap" type="com.bao.dating.pojo.entity.Post">
|
||||||
|
|||||||
@@ -28,6 +28,8 @@
|
|||||||
<result property="signature" column="signature"/>
|
<result property="signature" column="signature"/>
|
||||||
<result property="createdAt" column="created_at"/>
|
<result property="createdAt" column="created_at"/>
|
||||||
<result property="updatedAt" column="updated_at"/>
|
<result property="updatedAt" column="updated_at"/>
|
||||||
|
<result property="latitude" column="user_latitude"/>
|
||||||
|
<result property="longitude" column="user_longitude"/>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
<select id="selectByUserId" resultMap="UserResultMap">
|
<select id="selectByUserId" resultMap="UserResultMap">
|
||||||
SELECT
|
SELECT
|
||||||
@@ -41,8 +43,10 @@
|
|||||||
hobbies,
|
hobbies,
|
||||||
signature,
|
signature,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at
|
updated_at,
|
||||||
FROM user WHERE user_id = #{userId}
|
user_latitude,
|
||||||
|
user_longitude
|
||||||
|
FROM dating.user WHERE user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<!--根据ID更新动态-->
|
<!--根据ID更新动态-->
|
||||||
@@ -74,4 +78,41 @@
|
|||||||
</set>
|
</set>
|
||||||
WHERE user_id = #{userId}
|
WHERE user_id = #{userId}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<select id="selectByPhone" resultType="com.bao.dating.pojo.entity.User">
|
||||||
|
select * from dating.user where user_phone =#{phone}
|
||||||
|
</select>
|
||||||
|
<resultMap id="UserInfoVOResultMap" type="com.bao.dating.pojo.vo.UserInfoVO">
|
||||||
|
<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"/>
|
||||||
|
<result property="latitude" column="user_latitude"/>
|
||||||
|
<result property="longitude" column="user_longitude"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<select id="findByLatLngRange" resultMap="UserInfoVOResultMap">
|
||||||
|
SELECT
|
||||||
|
user_id,
|
||||||
|
user_name,
|
||||||
|
nickname,
|
||||||
|
avatar_url,
|
||||||
|
background_url,
|
||||||
|
gender,
|
||||||
|
birthday,
|
||||||
|
hobbies,
|
||||||
|
signature,
|
||||||
|
created_at,
|
||||||
|
updated_at,
|
||||||
|
user_latitude,
|
||||||
|
user_longitude
|
||||||
|
FROM user WHERE user_latitude BETWEEN #{minLat} AND #{maxLat} AND user_longitude BETWEEN #{minLng} AND #{maxLng}
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
304
src/test/java/com/bao/dating/common/aliyun/OssUploadTest.java
Normal file
304
src/test/java/com/bao/dating/common/aliyun/OssUploadTest.java
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
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,3 +75,4 @@ public class EmailAndSmsTest {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user