Compare commits
3 Commits
4b96a49b27
...
c01824513b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c01824513b | ||
|
|
9e8d35d5b3 | ||
|
|
347c56dd40 |
9
pom.xml
9
pom.xml
@@ -50,18 +50,23 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 阿里云相关依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
<version>3.17.4</version>
|
<version>3.17.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun</groupId>
|
<groupId>com.aliyun</groupId>
|
||||||
<artifactId>green20220302</artifactId>
|
<artifactId>green20220302</artifactId>
|
||||||
<version>3.0.1</version>
|
<version>3.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ 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.dto.PostRequestDTO;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.service.PostService;
|
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 org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/posts")
|
@RequestMapping("/posts")
|
||||||
@@ -15,10 +19,26 @@ public class PostController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostService postService;
|
private PostService postService;
|
||||||
|
|
||||||
@PostMapping
|
/**
|
||||||
public Result<Post> createPost(Post post) {
|
* 上传媒体文件接口
|
||||||
|
* @param files 媒体文件数组
|
||||||
|
* @return 上传后的文件URL列表
|
||||||
|
*/
|
||||||
|
@PostMapping(value = "/upload", consumes = "multipart/form-data")
|
||||||
|
public Result<List<String>> uploadMedia(@RequestParam("files") MultipartFile[] files) {
|
||||||
|
List<String> fileUrls = postService.uploadMedia(files);
|
||||||
|
return Result.success(ResultCode.SUCCESS_REVIEW, "文件上传成功", fileUrls);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发布动态接口 - JSON格式请求
|
||||||
|
* @param postRequestDTO 动态信息
|
||||||
|
* @return 发布的动态对象
|
||||||
|
*/
|
||||||
|
@PostMapping(consumes = "application/json")
|
||||||
|
public Result<Post> createPostJson(@RequestBody PostRequestDTO postRequestDTO) {
|
||||||
// 调用 Service 层处理发布动态业务逻辑
|
// 调用 Service 层处理发布动态业务逻辑
|
||||||
Post result = postService.createPost(post);
|
Post result = postService.createPost(postRequestDTO);
|
||||||
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.bao.dating.handler;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||||
|
import org.apache.ibatis.type.MappedTypes;
|
||||||
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List类型转换成Varchar类型
|
||||||
|
* @author KilLze
|
||||||
|
*/
|
||||||
|
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||||
|
@MappedTypes(List.class)
|
||||||
|
public class ListToVarcharTypeHandler implements TypeHandler<List<String>> {
|
||||||
|
@Override
|
||||||
|
public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
|
||||||
|
// 遍历List类型的入参,拼装为String类型,使用Statement对象插入数据库
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (int j = 0; j < strings.size(); j++) {
|
||||||
|
if (j == strings.size() - 1) {
|
||||||
|
sb.append(strings.get(j));
|
||||||
|
} else {
|
||||||
|
sb.append(strings.get(j)).append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preparedStatement.setString(i, sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
|
||||||
|
// 获取String类型的结果,使用","分割为List后返回
|
||||||
|
String resultString = resultSet.getString(s);
|
||||||
|
if (StringUtils.isNotEmpty(resultString)) {
|
||||||
|
return Arrays.asList(resultString.split(","));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
|
||||||
|
// 获取String类型的结果,使用","分割为List后返回
|
||||||
|
String resultString = resultSet.getString(i);
|
||||||
|
if (StringUtils.isNotEmpty(resultString)) {
|
||||||
|
return Arrays.asList(resultString.split(","));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
|
||||||
|
// 获取String类型的结果,使用","分割为List后返回
|
||||||
|
String resultString = callableStatement.getString(i);
|
||||||
|
if (StringUtils.isNotEmpty(resultString)) {
|
||||||
|
return Arrays.asList(resultString.split(","));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/main/java/com/bao/dating/pojo/dto/PostRequestDTO.java
Normal file
14
src/main/java/com/bao/dating/pojo/dto/PostRequestDTO.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bao.dating.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PostRequestDTO {
|
||||||
|
private Integer userId;
|
||||||
|
private String content;
|
||||||
|
private List<String> tags;
|
||||||
|
private List<String> mediaUrls;
|
||||||
|
}
|
||||||
@@ -12,9 +12,9 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
public class Post {
|
public class Post {
|
||||||
|
|
||||||
private Long postId;
|
private Integer postId;
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
|||||||
@@ -11,11 +11,11 @@ import java.time.LocalDateTime;
|
|||||||
@Data
|
@Data
|
||||||
public class PostFavorite {
|
public class PostFavorite {
|
||||||
|
|
||||||
private Long favoriteId;
|
private Integer favoriteId;
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private Long postId;
|
private Integer postId;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import java.time.LocalDateTime;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class PostLike {
|
public class PostLike {
|
||||||
private Long likeId;
|
private Integer likeId;
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private Long postId;
|
private Integer postId;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.time.LocalDateTime;
|
|||||||
@Data
|
@Data
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,23 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface PostService {
|
public interface PostService {
|
||||||
Post createPost(Post post);
|
/**
|
||||||
|
* 上传媒体文件
|
||||||
|
* @param files 媒体文件数组
|
||||||
|
* @return 上传后的文件URL列表
|
||||||
|
*/
|
||||||
|
List<String> uploadMedia(MultipartFile[] files);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建动态
|
||||||
|
* @param postRequestDTO 动态请求数据传输对象
|
||||||
|
* @return 创建的动态对象
|
||||||
|
*/
|
||||||
|
Post createPost(PostRequestDTO postRequestDTO);
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,26 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
import com.bao.dating.mapper.PostMapper;
|
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.Post;
|
||||||
import com.bao.dating.service.PostService;
|
import com.bao.dating.service.PostService;
|
||||||
import com.bao.dating.util.AliOssUtil;
|
import com.bao.dating.util.AliOssUtil;
|
||||||
import com.bao.dating.util.FileUtil;
|
import com.bao.dating.util.FileUtil;
|
||||||
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.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态服务实现类
|
||||||
|
*
|
||||||
|
* @author KilLze
|
||||||
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class PostServiceImpl implements PostService {
|
public class PostServiceImpl implements PostService {
|
||||||
|
|
||||||
@@ -22,42 +30,64 @@ public class PostServiceImpl implements PostService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PostMapper postMapper;
|
private PostMapper postMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传媒体文件
|
||||||
|
* @param files 媒体文件数组
|
||||||
|
* @return 上传后的文件URL列表
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Post createPost(Post post) {
|
public List<String> uploadMedia(MultipartFile[] files) {
|
||||||
// 处理图片和视频上传到 OSS
|
List<String> mediaUrls = new ArrayList<>();
|
||||||
List<String> mediaOssKeys = new ArrayList<>();
|
if (files != null && files.length > 0) {
|
||||||
|
for (MultipartFile file : files) {
|
||||||
// 判断媒体文件是否为空
|
if (!file.isEmpty()) {
|
||||||
if (post.getMediaOssKeys() != null && !post.getMediaOssKeys().isEmpty()) {
|
try {
|
||||||
|
|
||||||
// 上传媒体文件并判断文件类型
|
|
||||||
for (String mediaUrl : post.getMediaOssKeys()) {
|
|
||||||
// 根据文件扩展名判断文件类型
|
// 根据文件扩展名判断文件类型
|
||||||
String fileType = FileUtil.getFileType(mediaUrl);
|
String fileType = FileUtil.getFileType(file.getOriginalFilename());
|
||||||
|
|
||||||
// 生成唯一文件名
|
// 生成唯一文件名
|
||||||
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(mediaUrl);
|
String fileName = UUID.randomUUID().toString() + "." + FileUtil.getFileExtension(file.getOriginalFilename());
|
||||||
|
|
||||||
// 获取文件字节数据
|
// 获取文件字节数据
|
||||||
byte[] fileBytes = mediaUrl.getBytes();
|
byte[] fileBytes = file.getBytes();
|
||||||
|
|
||||||
// 根据文件类型处理
|
// 根据文件类型处理
|
||||||
String ossUrl = "";
|
String ossUrl = "";
|
||||||
if ("image".equals(fileType)) {
|
if ("image".equals(fileType) || "video".equals(fileType)) {
|
||||||
// 上传图片
|
// 上传图片或视频
|
||||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
|
||||||
} else if ("video".equals(fileType)) {
|
|
||||||
// 上传视频
|
|
||||||
ossUrl = ossUtil.upload(fileBytes, fileName);
|
ossUrl = ossUtil.upload(fileBytes, fileName);
|
||||||
}
|
}
|
||||||
// 添加上传后的 URL
|
// 添加上传后的 URL
|
||||||
mediaOssKeys.add(ossUrl);
|
mediaUrls.add(ossUrl);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
post.setMediaOssKeys(mediaOssKeys);
|
}
|
||||||
|
}
|
||||||
|
return mediaUrls;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建动态
|
||||||
|
*
|
||||||
|
* @param postRequestDTO 动态请求数据传输对象
|
||||||
|
* @return 创建的动态对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Post createPost(PostRequestDTO postRequestDTO) {
|
||||||
|
Post post = new Post();
|
||||||
|
post.setUserId(postRequestDTO.getUserId());
|
||||||
|
post.setContent(postRequestDTO.getContent());
|
||||||
|
post.setTags(postRequestDTO.getTags());
|
||||||
|
post.setMediaOssKeys(new ArrayList<>());
|
||||||
|
|
||||||
|
// 如果有传入的媒体链接,则使用它们
|
||||||
|
if (postRequestDTO.getMediaUrls() != null && !postRequestDTO.getMediaUrls().isEmpty()) {
|
||||||
|
post.setMediaOssKeys(postRequestDTO.getMediaUrls());
|
||||||
|
}
|
||||||
|
|
||||||
post.setIsPublic(0);
|
post.setIsPublic(0);
|
||||||
post.setUpdatedAt(LocalDateTime.now());
|
post.setUpdatedAt(LocalDateTime.now());
|
||||||
post.setCreatedAt(LocalDateTime.now());
|
post.setCreatedAt(LocalDateTime.now());
|
||||||
|
|
||||||
// 保存动态到数据库
|
// 保存动态到数据库
|
||||||
postMapper.insert(post);
|
postMapper.insert(post);
|
||||||
return post;
|
return post;
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ public class AliOssUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
* @param bytes
|
* @param bytes 文件字节数组
|
||||||
* @param objectName
|
* @param objectName 对象名称
|
||||||
* @return
|
* @return 完整的文件访问URL
|
||||||
*/
|
*/
|
||||||
public String upload(byte[] bytes, String objectName) {
|
public String upload(byte[] bytes, String objectName) {
|
||||||
|
|
||||||
@@ -45,6 +45,7 @@ public class AliOssUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 文件访问路径规则 https://BucketName.Endpoint/ObjectName
|
// 文件访问路径规则 https://BucketName.Endpoint/ObjectName
|
||||||
|
// 构造完整的文件访问URL,格式如:https://oss.yourdomain.com/upload/img12345.jpg
|
||||||
StringBuilder stringBuilder = new StringBuilder("https://");
|
StringBuilder stringBuilder = new StringBuilder("https://");
|
||||||
stringBuilder
|
stringBuilder
|
||||||
.append(bucketName)
|
.append(bucketName)
|
||||||
@@ -52,8 +53,10 @@ public class AliOssUtil {
|
|||||||
.append(endpoint)
|
.append(endpoint)
|
||||||
.append("/")
|
.append("/")
|
||||||
.append(objectName);
|
.append(objectName);
|
||||||
log.info("文件上传到:{}", stringBuilder.toString());
|
|
||||||
|
|
||||||
return stringBuilder.toString();
|
String fileUrl = stringBuilder.toString();
|
||||||
|
log.info("文件上传到:{}", fileUrl);
|
||||||
|
|
||||||
|
return fileUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ spring:
|
|||||||
datasource:
|
datasource:
|
||||||
url: jdbc:mysql://114.55.250.24:3306/dating?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
|
url: jdbc:mysql://114.55.250.24:3306/dating?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
|
||||||
username: root
|
username: root
|
||||||
password: root
|
password: rJ6DBTYrFCpjdsxy2sBV
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
# MyBatis 配置
|
# MyBatis 配置
|
||||||
@@ -19,7 +19,7 @@ mybatis:
|
|||||||
# 阿里云 OSS 配置
|
# 阿里云 OSS 配置
|
||||||
aliyun:
|
aliyun:
|
||||||
oss:
|
oss:
|
||||||
endpoint: oss-cn-hangzhou.aliyuncs.com
|
endpoint: oss-cn-beijing.aliyuncs.com
|
||||||
access-key-id: LTAI5tKo9TpWH1aW6JxWm1Gp
|
access-key-id: LTAI5tKo9TpWH1aW6JxWm1Gp
|
||||||
access-key-secret: LHk9DdHECKCwIdaIM9fkGgEuowt18W
|
access-key-secret: LHk9DdHECKCwIdaIM9fkGgEuowt18W
|
||||||
bucket-name: heimato
|
bucket-name: heimato
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="com.bao.dating.mapper.PostMapper">
|
|
||||||
|
|
||||||
|
<mapper namespace="com.bao.dating.mapper.PostMapper">
|
||||||
<!-- 动态添加 -->
|
<!-- 动态添加 -->
|
||||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insert" useGeneratedKeys="true" keyProperty="postId">
|
||||||
INSERT INTO post
|
INSERT INTO post
|
||||||
(user_id, content,
|
(user_id, content,
|
||||||
<if test="mediaOssKeys != null and mediaOssKeys != ''">
|
<if test="mediaOssKeys != null and mediaOssKeys != ''">
|
||||||
@@ -16,7 +16,15 @@
|
|||||||
</if>
|
</if>
|
||||||
is_public, like_count, favorite_count, created_at, updated_at)
|
is_public, like_count, favorite_count, created_at, updated_at)
|
||||||
VALUES
|
VALUES
|
||||||
(#{userId}, #{content}, #{mediaOssKeys}, #{tags}, #{location}, #{isPublic}, 0, 0, #{created_at}, #{updated_at})
|
(#{userId}, #{content},
|
||||||
|
<if test="mediaOssKeys != null and mediaOssKeys != ''">
|
||||||
|
#{mediaOssKeys, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
|
</if>
|
||||||
|
#{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
|
<if test="location != null and location != ''">
|
||||||
|
#{location},
|
||||||
|
</if>
|
||||||
|
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user