Compare commits
5 Commits
c83d86ad1a
...
123321
| Author | SHA1 | Date | |
|---|---|---|---|
| 88abc41a9e | |||
| 290db24bc4 | |||
| 31b0dffd68 | |||
| 31fd23afa8 | |||
| d8d46ab089 |
@@ -75,6 +75,8 @@ public class SmsUtil {
|
||||
.setTemplateCode(templateCode != null ? templateCode : this.templateCode)
|
||||
.setTemplateParam(templateParam);
|
||||
|
||||
log.error("TemplateParam 实际值 = {}", templateParam);
|
||||
|
||||
SendSmsResponse response = client.sendSms(sendSmsRequest);
|
||||
|
||||
if ("OK".equals(response.getBody().getCode())) {
|
||||
@@ -145,6 +147,7 @@ public class SmsUtil {
|
||||
jsonBuilder.append("}");
|
||||
return sendSms(phoneNumber, templateCode, jsonBuilder.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
16
src/main/java/com/bao/dating/config/AliyunOSSConfig.java
Normal file
16
src/main/java/com/bao/dating/config/AliyunOSSConfig.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.bao.dating.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "aliyun.oss")
|
||||
public class AliyunOSSConfig {
|
||||
private String endpoint;
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String bucketName;
|
||||
|
||||
}
|
||||
@@ -29,7 +29,9 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
.addPathPatterns("/**")
|
||||
// 忽略的接口
|
||||
.excludePathPatterns(
|
||||
"/user/login"
|
||||
"/user/login",
|
||||
"/user/sendCode",
|
||||
"/download/{postId}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -87,4 +90,20 @@ public class PostController {
|
||||
PostEditVO result = postService.updatePost(postId, postRequestDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "动态更新成功", result);
|
||||
}
|
||||
|
||||
@GetMapping("/download/{postId}")
|
||||
public void downloadPostImage(@PathVariable Long postId, HttpServletResponse response) throws Exception {
|
||||
try {
|
||||
//Service 返回已经加好水印的图片
|
||||
BufferedImage image = postService.downloadWithWatermark(postId);
|
||||
//设置响应头,触发浏览器下载
|
||||
response.setContentType("image/jpeg");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=post_" + postId + ".jpg");
|
||||
//输出到浏览器
|
||||
ImageIO.write(image, "jpg", response.getOutputStream());
|
||||
response.getOutputStream().flush();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
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.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 用户接口
|
||||
@@ -65,7 +68,6 @@ public class UserController {
|
||||
* @param file 头像文件
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
@Log
|
||||
@PostMapping(value = "/info/uploadAvatar", consumes = "multipart/form-data")
|
||||
public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
|
||||
String fileUrl = userService.uploadAvatar(file);
|
||||
@@ -77,7 +79,6 @@ public class UserController {
|
||||
* @param file 背景文件
|
||||
* @return 上传后的文件URL列表
|
||||
*/
|
||||
@Log
|
||||
@PostMapping(value = "/info/uploadBackground", consumes = "multipart/form-data")
|
||||
public Result<String> uploadBackground(@RequestParam("file") MultipartFile file) {
|
||||
String fileUrl = userService.uploadBackground(file);
|
||||
@@ -89,7 +90,6 @@ public class UserController {
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
@Log
|
||||
@PostMapping("/info/update")
|
||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
Long userId = UserContext.getUserId();
|
||||
@@ -97,4 +97,71 @@ public class UserController {
|
||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,18 +5,18 @@ import com.bao.dating.pojo.entity.Comments;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
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);
|
||||
|
||||
// 删除评论
|
||||
@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);
|
||||
|
||||
// 根据动态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);
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface PostFavoriteMapper {
|
||||
public interface PostFavoriteMapper {
|
||||
//查询当前已收藏所有用户
|
||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||
int addPostFavorite(PostFavorite postFavorite);
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 动态Mapper
|
||||
@@ -95,4 +96,12 @@ public interface PostMapper {
|
||||
* @return 影响行数
|
||||
*/
|
||||
int decreaseFavoriteCount(Long postId);
|
||||
|
||||
/**
|
||||
* 根据动态id查询用户名和媒体信息
|
||||
*
|
||||
* @param postId 动态id
|
||||
* @return 用户名和媒体信息
|
||||
*/
|
||||
Map<String, Object> getUsernameByUserId(Long postId);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,11 @@ package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
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.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户Mapper
|
||||
@@ -33,4 +37,15 @@ public interface UserMapper {
|
||||
*/
|
||||
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 userPhone;
|
||||
|
||||
private Double latitude; // 纬度
|
||||
|
||||
private Double longitude; // 经度
|
||||
}
|
||||
|
||||
@@ -24,4 +24,6 @@ public class UserInfoVO implements Serializable {
|
||||
private String signature;
|
||||
private LocalDateTime updatedAt;
|
||||
private LocalDateTime createdAt;
|
||||
private Double latitude;
|
||||
private Double longitude;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.vo.PostEditVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -55,4 +56,11 @@ public interface PostService {
|
||||
* @return 用户id
|
||||
*/
|
||||
Long selectUserIdByPostId(Long postId);
|
||||
|
||||
/**
|
||||
* 下载动态图片并添加水印
|
||||
* @param postId 动态ID
|
||||
* @return 带水印的图片
|
||||
*/
|
||||
BufferedImage downloadWithWatermark(Long postId) throws Exception;
|
||||
}
|
||||
@@ -2,10 +2,13 @@ package com.bao.dating.service;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户服务接口
|
||||
* @author KilLze
|
||||
@@ -52,4 +55,19 @@ public interface UserService {
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -10,11 +10,13 @@ import com.bao.dating.mapper.PostLikeMapper;
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||
import com.bao.dating.pojo.entity.Post;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.PostEditVO;
|
||||
import com.bao.dating.service.PostService;
|
||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||
import com.bao.dating.service.UserService;
|
||||
import com.bao.dating.util.FileUtil;
|
||||
import com.bao.dating.util.WatermarkUtil;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -22,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -57,6 +60,9 @@ public class PostServiceImpl implements PostService {
|
||||
@Autowired
|
||||
private CommentsMapper commentsMapper;
|
||||
|
||||
@Autowired
|
||||
private WatermarkUtil watermarkUtil;
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
* @param files 媒体文件数组
|
||||
@@ -331,4 +337,61 @@ public class PostServiceImpl implements PostService {
|
||||
return postMapper.selectUserIdByPostId(postId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载动态图片并添加水印
|
||||
*
|
||||
* @param postId 动态ID
|
||||
* @return 带水印的图片
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public BufferedImage downloadWithWatermark(Long postId) throws Exception {
|
||||
// 一次性查出 动态图片 + 作者信息
|
||||
Map<String, Object> map = postMapper.getUsernameByUserId(postId);
|
||||
|
||||
if (map == null || map.isEmpty()) {
|
||||
Post post = postMapper.selectById(postId);
|
||||
if (post == null) {
|
||||
throw new RuntimeException("未找到指定postId的帖子: " + postId);
|
||||
}
|
||||
throw new RuntimeException("未找到与postId相关的用户和媒体信息: " + postId);
|
||||
}
|
||||
|
||||
String mediaUrl = (String) map.get("media_oss_keys");
|
||||
String username = (String) map.get("user_name");
|
||||
Object userIdObj = map.get("user_id");
|
||||
|
||||
if (mediaUrl == null || username == null || userIdObj == null) {
|
||||
throw new RuntimeException("用户或媒体信息不完整: " + map);
|
||||
}
|
||||
|
||||
mediaUrl = mediaUrl.trim();
|
||||
if (mediaUrl.isEmpty()) {
|
||||
throw new RuntimeException("媒体URL为空,postId: " + postId);
|
||||
}
|
||||
|
||||
Long userId = userIdObj instanceof Number
|
||||
? ((Number) userIdObj).longValue()
|
||||
: Long.valueOf(userIdObj.toString());
|
||||
|
||||
// 解析 OSS ObjectKey(支持完整URL和直接存key两种)
|
||||
String cleanUrl = mediaUrl.split("\\?")[0]; // 去掉 ? 后面的参数
|
||||
String objectKey;
|
||||
|
||||
if (cleanUrl.startsWith("http")) {
|
||||
// https://xxx.oss-cn-xxx.aliyuncs.com/post/xxx.jpg → post/xxx.jpg
|
||||
objectKey = cleanUrl.substring(cleanUrl.indexOf(".com/") + 5);
|
||||
} else {
|
||||
objectKey = cleanUrl;
|
||||
}
|
||||
|
||||
if (objectKey.trim().isEmpty()) {
|
||||
throw new RuntimeException("解析后的ObjectKey为空,url: " + mediaUrl);
|
||||
}
|
||||
|
||||
// 下载并动态加水印(只给下载的人看,OSS原图不改,数据库不动)
|
||||
return watermarkUtil.downloadAndWatermark(objectKey, username, userId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.bao.dating.service.impl;
|
||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||
import com.bao.dating.common.aliyun.SmsUtil;
|
||||
import com.bao.dating.common.result.AliOssResult;
|
||||
import com.bao.dating.common.result.GreenAuditResult;
|
||||
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.UserLoginVO;
|
||||
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.JwtUtil;
|
||||
import com.bao.dating.util.MD5Util;
|
||||
@@ -21,6 +24,7 @@ import io.jsonwebtoken.Claims;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -37,6 +41,10 @@ import java.util.concurrent.TimeUnit;
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private SmsUtil smsUtil;
|
||||
|
||||
@Autowired
|
||||
private AliOssUtil ossUtil;
|
||||
|
||||
@@ -52,6 +60,9 @@ public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
@@ -311,4 +322,85 @@ public class UserServiceImpl implements UserService {
|
||||
BeanUtils.copyProperties(updatedUser, 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
64
src/main/java/com/bao/dating/util/WatermarkUtil.java
Normal file
64
src/main/java/com/bao/dating/util/WatermarkUtil.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.bao.dating.util;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.bao.dating.config.AliyunOSSConfig;
|
||||
import com.bao.dating.mapper.PostMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Component
|
||||
public class WatermarkUtil {
|
||||
|
||||
@Autowired
|
||||
private AliyunOSSConfig aliyunOSSConfig;
|
||||
|
||||
|
||||
public BufferedImage downloadAndWatermark(String objectKey, String username, Long userId) throws Exception {
|
||||
OSS ossClient = new OSSClientBuilder().build(
|
||||
aliyunOSSConfig.getEndpoint(),
|
||||
aliyunOSSConfig.getAccessKeyId(),
|
||||
aliyunOSSConfig.getAccessKeySecret()
|
||||
);
|
||||
|
||||
InputStream inputStream = ossClient.getObject(aliyunOSSConfig.getBucketName(), objectKey).getObjectContent();
|
||||
BufferedImage image = ImageIO.read(inputStream);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
|
||||
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
|
||||
// 字体小一点
|
||||
Font font = new Font("微软雅黑", Font.BOLD, 24);
|
||||
g2d.setFont(font);
|
||||
|
||||
String text = "作者:" + username + " (ID:" + userId + ")";
|
||||
|
||||
FontMetrics fm = g2d.getFontMetrics();
|
||||
int textWidth = fm.stringWidth(text);
|
||||
int textHeight = fm.getHeight();
|
||||
|
||||
// 右下角留边距
|
||||
int x = image.getWidth() - textWidth - 20;
|
||||
int y = image.getHeight() - 20;
|
||||
|
||||
// 黑色描边
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawString(text, x - 1, y - 1);
|
||||
g2d.drawString(text, x + 1, y - 1);
|
||||
g2d.drawString(text, x - 1, y + 1);
|
||||
g2d.drawString(text, x + 1, y + 1);
|
||||
|
||||
// 白色正文
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawString(text, x, y);
|
||||
|
||||
g2d.dispose();
|
||||
ossClient.shutdown();
|
||||
return image;
|
||||
}
|
||||
}
|
||||
@@ -122,5 +122,11 @@
|
||||
<select id="selectFavoriteCount" resultType="java.lang.Integer">
|
||||
select dating.post.favorite_count from dating.post where post.post_id = #{postId}
|
||||
</select>
|
||||
<select id="getUsernameByUserId" resultType="map">
|
||||
SELECT u.user_name, u.user_id, p.media_oss_keys
|
||||
FROM post p
|
||||
LEFT JOIN user u ON p.user_id = u.user_id
|
||||
WHERE p.post_id = #{postId}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -28,6 +28,8 @@
|
||||
<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="selectByUserId" resultMap="UserResultMap">
|
||||
SELECT
|
||||
@@ -41,8 +43,10 @@
|
||||
hobbies,
|
||||
signature,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM user WHERE user_id = #{userId}
|
||||
updated_at,
|
||||
user_latitude,
|
||||
user_longitude
|
||||
FROM dating.user WHERE user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<!--根据ID更新动态-->
|
||||
@@ -74,4 +78,43 @@
|
||||
</set>
|
||||
WHERE user_id = #{userId}
|
||||
</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>
|
||||
Reference in New Issue
Block a user