优化审核,审核未通过依旧会加入数据库,并将动态状态改为2(未通过),若有嫌疑则改为1进行人工审核,通过则为0

新增动态修改功能,可以修改文本,图片,标签,修改内容若审核未通过则将状态改为2,若有嫌疑则改为1进行人工审核,通过则改为0
This commit is contained in:
KilLze
2025-12-19 20:21:47 +08:00
parent 756fbd1a31
commit 0ce96c3104
7 changed files with 276 additions and 48 deletions

View File

@@ -5,6 +5,7 @@ import com.bao.dating.common.Result;
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.vo.PostEditVO;
import com.bao.dating.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -32,13 +33,14 @@ public class PostController {
/**
* 发布动态接口 - JSON格式请求
* @param postRequestDTO 动态信息
* @param postDTO 动态信息
* @param userId 用户ID
* @return 发布的动态对象
*/
@PostMapping(consumes = "application/json")
public Result<Post> createPostJson(@RequestBody PostRequestDTO postRequestDTO) {
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO, @RequestParam Long userId) {
// 调用 Service 层处理发布动态业务逻辑
Post result = postService.createPost(postRequestDTO);
Post result = postService.createPost(userId, postDTO);
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
}
@@ -49,8 +51,31 @@ public class PostController {
* @return 删除结果
*/
@DeleteMapping("/{postId}")
public Result<String> deleteById(@PathVariable Integer postId){
public Result<String> deleteById(@PathVariable Long postId){
postService.deletePostById(postId);
return Result.success(ResultCode.SUCCESS_DELETE, "动态删除成功", null);
}
/**
* 根据ID查询动态接口
* @param postId 动态ID
* @return 动态对象
*/
@GetMapping("/{postId}")
public Result<PostEditVO> getPostById(@PathVariable Long postId) {
PostEditVO postEditVO = postService.getPostForEdit(postId);
return Result.success(ResultCode.SUCCESS,"查询成功", postEditVO);
}
/**
* 更新动态接口
* @param postId 动态ID
* @param postRequestDTO 动态信息
* @return 更新后的动态对象
*/
@PutMapping("/{postId}")
public Result<PostEditVO> updatePost(@PathVariable Long postId, @RequestBody PostRequestDTO postRequestDTO) {
PostEditVO result = postService.updatePost(postId, postRequestDTO);
return Result.success(ResultCode.SUCCESS_REVIEW, "动态更新成功", result);
}
}

View File

@@ -2,24 +2,39 @@ package com.bao.dating.mapper;
import com.bao.dating.pojo.entity.Post;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PostMapper {
/**
* 插入动态
*
* @param post 动态对象
* @return 插入的行数
* @param post
*/
int insert(Post post);
void insert(Post post);
/**
* 根据ID删除动态
*
* @param postId 动态ID
* @return 删除的行数
*/
int deletePostById(Integer postId);
void deletePostById(@Param("postId") Long postId);
/**
* 根据ID查询动态
*
* @param postId
* @return
*/
Post selectById(@Param("postId") Long postId);
/**
* 根据ID更新动态
*
* @param post
* @return
*/
void updateById(Post post);
/**
* 查询点赞数
@@ -44,4 +59,4 @@ public interface PostMapper {
* @return 影响行数
*/
int decreaseLikeCount(Long postId);
}
}

View File

@@ -1,14 +1,15 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 动态数据传输对象
*/
@Data
public class PostRequestDTO {
private Long userId;
private String content;
private List<String> mediaOssKeys;
private List<String> tags;
private List<String> mediaUrls;
}

View File

@@ -0,0 +1,19 @@
package com.bao.dating.pojo.vo;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* 修改内容查询返回数据
*/
@Data
public class PostEditVO {
private Long postId;
private String content;
private List<String> mediaOssKeys;
private List<String> tags;
private Integer isPublic;
private LocalDateTime updatedAt;
}

View File

@@ -2,6 +2,7 @@ package com.bao.dating.service;
import com.bao.dating.pojo.dto.PostRequestDTO;
import com.bao.dating.pojo.entity.Post;
import com.bao.dating.pojo.vo.PostEditVO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@@ -16,15 +17,30 @@ public interface PostService {
/**
* 创建动态
* @param postRequestDTO 动态请求数据传输对象
* @param userId 用户ID
* @param postRequestDTO 动态数据传输对象
* @return 创建的动态对象
*/
Post createPost(PostRequestDTO postRequestDTO);
Post createPost(Long userId, PostRequestDTO postRequestDTO);
/**
* 删除动态
* @param postId 动态ID
* @return 删除的动态对象
*/
void deletePostById(Integer postId);
void deletePostById(Long postId);
/**
* 查询动态详情(用于编辑)
* @param postId 动态ID
*/
PostEditVO getPostForEdit(Long postId);
/**
* 修改动态
* @param postId 动态ID
* @param postRequestDTO 修改的动态数据传输对象
* @return 修改后的动态对象
*/
PostEditVO updatePost(Long postId, PostRequestDTO postRequestDTO);
}

View File

@@ -5,9 +5,11 @@ import com.bao.dating.common.aliyun.GreenTextScan;
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.vo.PostEditVO;
import com.bao.dating.service.PostService;
import com.bao.dating.util.AliOssUtil;
import com.bao.dating.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -77,11 +79,23 @@ public class PostServiceImpl implements PostService {
/**
* 创建动态
*
* @param postRequestDTO 动态请求数据传输对象
* @param userId 用户ID
* @param postRequestDTO 动态数据传输对象
* @return 创建的动态对象
*/
@Override
public Post createPost(PostRequestDTO postRequestDTO) {
public Post createPost(Long userId, PostRequestDTO postRequestDTO) {
// 创建动态对象
Post post = new Post();
post.setUserId(userId);
post.setContent(postRequestDTO.getContent());
post.setTags(postRequestDTO.getTags());
post.setMediaOssKeys(new ArrayList<>());
// 如果有传入的媒体链接,则使用它们
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
}
// 1. 文本内容审核
Map textResult;
@@ -90,45 +104,45 @@ public class PostServiceImpl implements PostService {
} catch (Exception e) {
throw new RuntimeException("文本审核失败");
}
// 文本审核结果
String textSuggestion = (String) textResult.get("suggestion");
if ("block".equals(textSuggestion)) {
throw new RuntimeException("动态内容违规,禁止发布");
}
// 2. 图片审核(如果有)
if (postRequestDTO.getMediaUrls() != null && !postRequestDTO.getMediaUrls().isEmpty()) {
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
Map imageResult;
try {
imageResult = greenImageScan.imageScan(postRequestDTO.getMediaUrls());
imageResult = greenImageScan.imageScan(postRequestDTO.getMediaOssKeys());
} catch (Exception e) {
throw new RuntimeException(e);
}
if ("block".equals(imageResult.get("suggestion"))) {
throw new RuntimeException("图片内容违规,禁止发布");
// 图片审核结果
String imageSuggestion = (String) imageResult.get("suggestion");
// 根据审核结果设置状态
if ("block".equals(textSuggestion) || "block".equals(imageSuggestion)) {
// 审核未通过,允许用户修改
post.setIsPublic(2);
} else if ("review".equals(textSuggestion) || "review".equals(imageSuggestion)) {
// 待审核,需人工审核
post.setIsPublic(1);
} else {
// 审核通过
post.setIsPublic(0);
}
} else {
// 只有文本内容的情况
if ("block".equals(textSuggestion)) {
// 审核未通过,允许用户修改
post.setIsPublic(2);
} else if ("review".equals(textSuggestion)) {
// 待审核,需人工审核
post.setIsPublic(1);
} else {
// 审核通过
post.setIsPublic(0);
}
}
Post post = new Post();
post.setUserId(Long.valueOf(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());
}
// 4. 根据审核结果设置状态
if ("review".equals(textSuggestion)) {
// 待审核,需人工审核
post.setIsPublic(0);
} else {
// 审核通过
post.setIsPublic(1);
}
post.setUpdatedAt(LocalDateTime.now());
post.setCreatedAt(LocalDateTime.now());
@@ -144,7 +158,95 @@ public class PostServiceImpl implements PostService {
* @return 删除的动态对象
*/
@Override
public void deletePostById(Integer postId) {
public void deletePostById(Long postId) {
postMapper.deletePostById(postId);
}
@Override
public PostEditVO getPostForEdit(Long postId) {
Post post = postMapper.selectById(postId);
if (post == null) {
throw new RuntimeException("动态不存在");
}
PostEditVO postEditVO = new PostEditVO();
BeanUtils.copyProperties(post, postEditVO);
return postEditVO;
}
/**
* 修改动态
* @param postId 动态ID
* @param postRequestDTO 修改的动态数据传输对象
* @return
*/
@Override
public PostEditVO updatePost(Long postId, PostRequestDTO postRequestDTO) {
// 查询动态
Post post = postMapper.selectById(postId);
if (post == null) {
throw new RuntimeException("动态不存在");
}
post.setContent(postRequestDTO.getContent());
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
}
// 1. 文本内容审核
Map textResult;
try {
textResult = greenTextScan.greeTextScan(postRequestDTO.getContent());
} catch (Exception e) {
throw new RuntimeException("文本审核失败");
}
// 文本审核结果
String textSuggestion = (String) textResult.get("suggestion");
// 2. 图片审核(如果有)
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
Map imageResult;
try {
imageResult = greenImageScan.imageScan(postRequestDTO.getMediaOssKeys());
} catch (Exception e) {
throw new RuntimeException(e);
}
// 图片审核结果
String imageSuggestion = (String) imageResult.get("suggestion");
// 根据审核结果设置状态
if ("block".equals(textSuggestion) || "block".equals(imageSuggestion)) {
// 审核未通过,允许用户修改
post.setIsPublic(2);
} else if ("review".equals(textSuggestion) || "review".equals(imageSuggestion)) {
// 待审核,需人工审核
post.setIsPublic(1);
} else {
// 审核通过
post.setIsPublic(0);
}
} else {
// 只有文本内容的情况
if ("block".equals(textSuggestion)) {
// 审核未通过,允许用户修改
post.setIsPublic(2);
} else if ("review".equals(textSuggestion)) {
// 待审核,需人工审核
post.setIsPublic(1);
} else {
// 审核通过
post.setIsPublic(0);
}
}
post.setTags(postRequestDTO.getTags());
post.setUpdatedAt(LocalDateTime.now());
// 更新动态
postMapper.updateById(post);
// 返回动态详情
PostEditVO postEditVO = new PostEditVO();
BeanUtils.copyProperties(post, postEditVO);
return postEditVO;
}
}

View File

@@ -29,10 +29,60 @@
</insert>
<!--动态删除-->
<delete id="deletePostById" parameterType="int">
<delete id="deletePostById">
DELETE FROM post WHERE post_id = #{postId}
</delete>
<!--动态查询-->
<resultMap id="PostResultMap" type="com.bao.dating.pojo.entity.Post">
<id property="postId" column="post_id"/>
<result property="userId" column="user_id"/>
<result property="content" column="content"/>
<result property="mediaOssKeys" column="media_oss_keys" typeHandler="com.bao.dating.handler.ListToVarcharTypeHandler"/>
<result property="tags" column="tags" typeHandler="com.bao.dating.handler.ListToVarcharTypeHandler"/>
<result property="location" column="location"/>
<result property="isPublic" column="is_public"/>
<result property="likeCount" column="like_count"/>
<result property="favoriteCount" column="favorite_count"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
</resultMap>
<select id="selectById" resultMap="PostResultMap">
SELECT
post_id,
user_id,
content,
media_oss_keys,
tags,
location,
is_public,
like_count,
favorite_count,
created_at,
updated_at
FROM post WHERE post_id = #{postId}
</select>
<!--动态更新-->
<update id="updateById">
UPDATE post
<set>
<if test="content != null">
content = #{content},
</if>
<if test="tags != null">
tags = #{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
</if>
<if test="mediaOssKeys != null">
media_oss_keys =
#{mediaOssKeys, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
</if>
is_public = #{isPublic},
updated_at = #{updatedAt}
</set>
WHERE post_id = #{postId}
</update>
<!--增加点赞数量-->
<update id="increaseLikeCount">
update dating.post set like_count = like_count + 1 where post.post_id = #{postId}