5 Commits

Author SHA1 Message Date
lihaiyang
c96af708df 评论功能 2025-12-23 13:48:40 +08:00
KilLze
05e71576c6 添加注释 2025-12-19 01:42:40 +08:00
KilLze
c6f3cb72dd 添加注释 2025-12-19 01:41:04 +08:00
bao
0f3c9e78a6 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	src/test/java/com/bao/dating/DatingApplicationTests.java
2025-12-19 01:19:22 +08:00
bao
1051a7b84f 图片审核 文本审核测试类 2025-12-19 01:18:25 +08:00
15 changed files with 267 additions and 27 deletions

11
pom.xml
View File

@@ -19,6 +19,13 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@@ -123,8 +130,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>1.8</source> <source>9</source>
<target>1.8</target> <target>9</target>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
</configuration> </configuration>
</plugin> </plugin>

View File

@@ -0,0 +1,19 @@
package com.bao.dating.common;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 评论表
*/
@Data
public class Comments {
private Long comment_id; // 评论ID
private String content; // 评论内容
private Long user_id; // 评论人ID
private Long post_id; // 关联动态ID
private LocalDateTime created_at; // 评论时间
}

View File

@@ -0,0 +1,16 @@
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; // 创建时间
}

View File

@@ -25,7 +25,7 @@ public class GreenImageScan {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId) .setAccessKeyId(accessKeyId)
.setAccessKeySecret(secret); .setAccessKeySecret(secret);
// 访问的域名 // 访问的域名 - 修改为北京区域
config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com"; config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
com.aliyun.imageaudit20191230.Client client = new com.aliyun.imageaudit20191230.Client(config); com.aliyun.imageaudit20191230.Client client = new com.aliyun.imageaudit20191230.Client(config);
@@ -34,21 +34,21 @@ public class GreenImageScan {
for (String img: imageList) { for (String img: imageList) {
ScanImageRequest.ScanImageRequestTask task = new ScanImageRequest.ScanImageRequestTask(); ScanImageRequest.ScanImageRequestTask task = new ScanImageRequest.ScanImageRequestTask();
task.setImageURL(img); // 修复直接使用图片URL而不是转换为字符数组 task.setDataId(UUID.randomUUID().toString()); task.setImageURL(img);
task.setDataId(UUID.randomUUID().toString());
task.setImageTimeMillisecond(1L); task.setImageTimeMillisecond(1L);
task.setInterval(1); task.setInterval(1);
task.setMaxFrames(1); task.setMaxFrames(1);
taskList.add(task); taskList.add(task);
} }
//场景 //场景
List<String> sceneList = new ArrayList<>(); List<String> sceneList = new ArrayList<>();
// 移除了不支持的"antispam"场景,使用支持的场景 // 移除了不支持的"antispam"场景,使用支持的场景
sceneList.add("porn"); // 涉黄识别 sceneList.add("porn"); // 涉黄识别
sceneList.add("terrorism"); // 暴恐识别 sceneList.add("terrorism"); // 暴恐识别
sceneList.add("ad"); // 图片广告 sceneList.add("ad"); // 图片广告
sceneList.add("live"); // 不良场景 sceneList.add("live"); // 不良场景
sceneList.add("logo"); // logo识别 sceneList.add("logo"); // logo识别
com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest() com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
@@ -66,9 +66,6 @@ public class GreenImageScan {
ListIterator<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> listIterator = subResults.listIterator(); ListIterator<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> listIterator = subResults.listIterator();
while (listIterator.hasNext()) { while (listIterator.hasNext()) {
ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults item = listIterator.next(); ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults item = listIterator.next();
/* System.out.println("scene = [" + item.scene + "]");
System.out.println("suggestion = [" + item.suggestion + "]");
System.out.println("label = [" + item.label + "]");*/
if (!item.suggestion.equals("pass")) { if (!item.suggestion.equals("pass")) {
resultMap.put("suggestion", item.suggestion); resultMap.put("suggestion", item.suggestion);
@@ -82,14 +79,10 @@ public class GreenImageScan {
return resultMap; return resultMap;
} else { } else {
/* *
* 表明请求整体处理失败,原因视具体的情况详细分析
*/
System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scanImageResponse)); System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scanImageResponse));
return null; return null;
} }
} catch (com.aliyun.tea.TeaException teaException) { } catch (com.aliyun.tea.TeaException teaException) {
// 获取整体报错信息 // 获取整体报错信息
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException)); System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
@@ -100,9 +93,5 @@ public class GreenImageScan {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
/* Map<String, String> resultMap = new HashMap<>();
resultMap.put("suggestion", "pass");
return resultMap;*/
} }
} }

View File

@@ -0,0 +1,48 @@
package com.bao.dating.controller;
import com.bao.dating.common.Comments;
import com.bao.dating.service.CommentsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/comments")
@CrossOrigin // 解决跨域问题(前后端分离必加)
public class CommentController {
@Resource
private CommentsService commentsService;
/**
* 添加评论
* @param comment 评论对象含content、userId、dynamicId
*/
@PostMapping("/add")
public Map<String, Object> addComment(@RequestBody Comments comment) {
boolean success = commentsService.addComment(comment);
return success ? Map.of("code", 200, "msg", "评论成功") : Map.of("code", 500, "msg", "评论失败");
}
/**
* 删除评论
* @param user_id 评论ID
*/
@DeleteMapping("/delete/{user_id}")
public Map<String, Object> deleteComment(@PathVariable Long user_id) {
boolean success = commentsService.deleteComment(user_id);
return success ? Map.of("code", 200, "msg", "删除成功") : Map.of("code", 500, "msg", "删除失败");
}
/**
* 根据动态ID查询评论列表
* @param post_id 动态ID
*/
@GetMapping("/list/{post_id}")
public Map<String, Object> getCommentList(@PathVariable Long post_id) {
List<Comments> commentList = commentsService.getCommentByPostId(post_id);
return Map.of("code", 200, "data", commentList);
}
}

View File

@@ -0,0 +1,22 @@
package com.bao.dating.mapper;
import com.bao.dating.common.Comments;
import com.bao.dating.common.Post;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CommentsMapper {
// 添加评论
@Insert("INSERT INTO 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}")
int deleteComments(@Param("user_id") Long user_id);
// 根据动态ID查询评论列表
@Select("SELECT * FROM comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
}

View File

@@ -6,11 +6,29 @@ import org.apache.ibatis.annotations.Param;
@Mapper @Mapper
public interface PostLikeMapper { public interface PostLikeMapper {
//根据 postId 和 userId 查询点赞记录 用于判断是否已经点赞 /**
* 根据 postId 和 userId 查询点赞记录 用于判断是否已经点赞
* @param postId
* @param userId
* @return
*/
PostLike findByPostAndUser(@Param("postId") Long postId, @Param("userId") Long userId); PostLike findByPostAndUser(@Param("postId") Long postId, @Param("userId") Long userId);
/**
* 插入点赞记录
*
* @param postLike
* @return
*/
int insert(PostLike postLike); int insert(PostLike postLike);
// 根据 postId + userId 删除点赞 /**
* 删除点赞记录
* 根据 postId + userId 删除点赞
*
* @param postId
* @param userId
* @return
*/
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId); int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
} }

View File

@@ -5,5 +5,43 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface PostMapper { public interface PostMapper {
/**
* 插入动态
*
* @param post 动态对象
* @return 插入的行数
*/
int insert(Post post); int insert(Post post);
/**
* 根据ID删除动态
*
* @param postId 动态ID
* @return 删除的行数
*/
int deletePostById(Integer postId);
/**
* 查询点赞数
*
* @param postId
* @return
*/
int selectLikeCount(Long postId);
/**
* 点赞数+1
*
* @param postId 动态ID
* @return 影响行数
*/
int increaseLikeCount(Long postId);
/**
* 点赞数-1
*
* @param postId 动态ID
* @return 影响行数
*/
int decreaseLikeCount(Long postId);
} }

View File

@@ -7,7 +7,7 @@ import java.util.List;
@Data @Data
public class PostRequestDTO { public class PostRequestDTO {
private Integer userId; private Long userId;
private String content; private String content;
private List<String> tags; private List<String> tags;
private List<String> mediaUrls; private List<String> mediaUrls;

View File

@@ -0,0 +1,20 @@
package com.bao.dating.service;
import com.bao.dating.common.Comments;
import java.util.List;
public interface CommentsService{
// 添加评论
boolean addComment(Comments comments);
// 删除评论
boolean deleteComment(Long user_id);
// 根据动态ID查询评论
List<Comments> getCommentByPostId(Long post_id);
}

View File

@@ -11,6 +11,12 @@ public interface PostLikeService {
* @return 统一返回结果 * @return 统一返回结果
*/ */
Result<?> likePost(Long postId, Long userId); Result<?> likePost(Long postId, Long userId);
//取消点赞
/**
* 取消点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
*/
void unlike(Long postId, Long userId); void unlike(Long postId, Long userId);
} }

View File

@@ -0,0 +1,39 @@
package com.bao.dating.service.impl;
import com.bao.dating.common.Comments;
import com.bao.dating.mapper.CommentsMapper;
import com.bao.dating.service.CommentsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class CommentsServiceImpl implements CommentsService {
@Resource
private CommentsMapper commentsMapper;
@Override
public boolean addComment(Comments comments) {
// 设置创建时间
comments.setCreated_at(LocalDateTime.now());
return commentsMapper.addComment(comments) > 0;
}
@Override
public boolean deleteComment(Long user_id) {
return commentsMapper.deleteComments(user_id) > 0;
}
@Override
public List<Comments> getCommentByPostId(Long post_id) {
return commentsMapper.getCommentsByPostId(post_id);
}
}

View File

@@ -24,6 +24,13 @@ public class PostLikeServiceImpl implements PostLikeService {
/**
* 点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
* @return 统一返回结果
*/
@Override @Override
@Transactional // 保证点赞 + 点赞数更新是一个事务 @Transactional // 保证点赞 + 点赞数更新是一个事务
public Result<?> likePost(Long postId, Long userId) { public Result<?> likePost(Long postId, Long userId) {
@@ -51,6 +58,12 @@ public class PostLikeServiceImpl implements PostLikeService {
return Result.success(ResultCode.SUCCESS_REVIEW,"点赞成功",data); return Result.success(ResultCode.SUCCESS_REVIEW,"点赞成功",data);
} }
/**
* 取消点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
*/
@Override @Override
@Transactional @Transactional
public void unlike(Long postId, Long userId) { public void unlike(Long postId, Long userId) {

View File

@@ -111,7 +111,7 @@ public class PostServiceImpl implements PostService {
} }
Post post = new Post(); Post post = new Post();
post.setUserId(postRequestDTO.getUserId()); post.setUserId(Long.valueOf(postRequestDTO.getUserId()));
post.setContent(postRequestDTO.getContent()); post.setContent(postRequestDTO.getContent());
post.setTags(postRequestDTO.getTags()); post.setTags(postRequestDTO.getTags());
post.setMediaOssKeys(new ArrayList<>()); post.setMediaOssKeys(new ArrayList<>());

View File

@@ -3,7 +3,8 @@
"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="postId"> <insert id="insert" useGeneratedKeys="true" keyProperty="postId">
INSERT INTO post INSERT INTO post
(user_id, content, (user_id, content,
@@ -26,10 +27,18 @@
</if> </if>
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt}) #{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
</insert> </insert>
<!--动态删除-->
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE post_id = #{postId}
</delete>
<!--增加点赞数量--> <!--增加点赞数量-->
<update id="increaseLikeCount"> <update id="increaseLikeCount">
update dating.post set like_count = like_count + 1 where post.post_id = #{postId} update dating.post set like_count = like_count + 1 where post.post_id = #{postId}
</update> </update>
<!--减少点赞数量-->
<update id="decreaseLikeCount"> <update id="decreaseLikeCount">
update dating.post set like_count= like_count - 1 where post.post_id = #{postId} update dating.post set like_count= like_count - 1 where post.post_id = #{postId}
</update> </update>
@@ -39,8 +48,4 @@
select dating.post.like_count from dating.post where post.post_id = #{postId} select dating.post.like_count from dating.post where post.post_id = #{postId}
</select> </select>
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE post_id = #{postId}
</delete>
</mapper> </mapper>