From 200f66839b22b341d1ae49b0910193cbd6703b96 Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Tue, 25 Nov 2025 16:36:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=92=A8=E8=AF=A2=E5=88=97=E8=A1=A8=E5=92=8C?= =?UTF-8?q?=E5=BA=97=E9=93=BA=20api=20=E6=8E=A5=E5=8F=A3=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E7=99=BD=E5=90=8D=E5=8D=95=EF=BC=8C=E4=B8=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 2 + .../service/impl/SnsStoryBaseServiceImpl.java | 202 ++++++++++++------ .../impl/SnsStoryCategoryServiceImpl.java | 178 ++++++++++----- 3 files changed, 263 insertions(+), 119 deletions(-) diff --git a/mall-gateway/src/main/resources/application.yml b/mall-gateway/src/main/resources/application.yml index ab1ee4e9..e0a0fbcb 100644 --- a/mall-gateway/src/main/resources/application.yml +++ b/mall-gateway/src/main/resources/application.yml @@ -93,6 +93,8 @@ secure: - "/mobile/shop/lakala/sign/ec/**" - "/mobile/**/**/test/case" - "/**/**/testcase" + - "/mobile/sns/Story/listComment" + - "/mobile/sns/Story/getStory" universal: urls: - "/admin/account/account-user-base/info" diff --git a/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryBaseServiceImpl.java b/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryBaseServiceImpl.java index 35ac0cbb..40b707fb 100644 --- a/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryBaseServiceImpl.java +++ b/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryBaseServiceImpl.java @@ -185,7 +185,7 @@ public class SnsStoryBaseServiceImpl extends BaseServiceImpl column_row = new QueryWrapper<>(); //查询获得相关的SnsStoryBase列表集合 if (storySearchDTO.getStory_type() != null) { @@ -266,44 +266,44 @@ public class SnsStoryBaseServiceImpl extends BaseServiceImpl queryWrapper=new QueryWrapper<>(); - queryWrapper.eq("user_id",user.getId()); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("user_id", user.getId()); List snsStoryLikes = snsStoryLikeService.find(queryWrapper); List collect = snsStoryLikes.stream().map(s -> s.getStory_id()).distinct().collect(Collectors.toList()); - for(Map item :items){ + for (Map item : items) { Integer story_id = Convert.toInt(item.get("story_id")); - if(collect.contains(story_id)){ - item.put("IsFabulous",1); - }else{ - item.put("IsFabulous",0); + if (collect.contains(story_id)) { + item.put("IsFabulous", 1); + } else { + item.put("IsFabulous", 0); } } //用户是否收藏 - QueryWrapper snsStoryCollectionQueryWrapper=new QueryWrapper<>(); - snsStoryCollectionQueryWrapper.eq("user_id",user.getId()); + QueryWrapper snsStoryCollectionQueryWrapper = new QueryWrapper<>(); + snsStoryCollectionQueryWrapper.eq("user_id", user.getId()); List snsStoryCollections = snsStoryCollectionService.find(snsStoryCollectionQueryWrapper); List collectionList = snsStoryCollections.stream().map(s -> s.getStory_id()).distinct().collect(Collectors.toList()); - for(Map item :items){ + for (Map item : items) { Integer story_id = Convert.toInt(item.get("story_id")); - if(collectionList.contains(story_id)){ - item.put("IsCollection",1); - }else{ - item.put("IsCollection",0); + if (collectionList.contains(story_id)) { + item.put("IsCollection", 1); + } else { + item.put("IsCollection", 0); } } //用户是否关注 - for(Map item:items){ - QueryWrapper friendQueryWrapper=new QueryWrapper<>(); - friendQueryWrapper.eq("user_id",user.getId()).eq("friend_id",Convert.toInt(item.get("user_id"))); + for (Map item : items) { + QueryWrapper friendQueryWrapper = new QueryWrapper<>(); + friendQueryWrapper.eq("user_id", user.getId()).eq("friend_id", Convert.toInt(item.get("user_id"))); SnsUserFriend one = snsUserFriendService.findOne(friendQueryWrapper); - if(one!=null){ - item.put("IsFollow",true); - }else{ - item.put("IsFollow",false); + if (one != null) { + item.put("IsFollow", true); + } else { + item.put("IsFollow", false); } } } @@ -382,7 +382,7 @@ public class SnsStoryBaseServiceImpl extends BaseServiceImpl item_ids = Convert.toList(Long.class, str_item_id); - Map product_row = shopService.getProductItemOne(item_ids.get(0)); + try { + List item_ids = Convert.toList(Long.class, str_item_id); + // 检查列表是否为空以及索引是否有效 + if (CollUtil.isNotEmpty(item_ids) && item_ids.get(0) != null) { + Map product_row = shopService.getProductItemOne(item_ids.get(0)); - if (CollUtil.isNotEmpty(product_row)) { - String product_name = Convert.toStr(product_row.get("product_name"), ""); - String item_name = Convert.toStr(product_row.get("item_name"), ""); - row.put("product_item_name", product_name + " " + item_name); - row.put("item_unit_price", product_row.get("item_unit_price")); - row.put("item_market_price", product_row.get("item_market_price")); - row.put("product_image", product_row.get("product_image")); + if (CollUtil.isNotEmpty(product_row)) { + String product_name = Convert.toStr(product_row.get("product_name"), ""); + String item_name = Convert.toStr(product_row.get("item_name"), ""); + row.put("product_item_name", product_name + " " + item_name); + row.put("item_unit_price", product_row.get("item_unit_price")); + row.put("item_market_price", product_row.get("item_market_price")); + row.put("product_image", product_row.get("product_image")); + } + } + } catch (Exception e) { + log.warn("获取商品信息失败, story_id: {}, item_id: {}", story_id, str_item_id, e); + // 发生异常时保持默认值 } } - // 是否点赞 - QueryWrapper likeQueryWrapper = new QueryWrapper<>(); - likeQueryWrapper.eq("story_id", story_id).eq("user_id", user_id); - SnsStoryLike story_like_row = snsStoryLikeService.findOne(likeQueryWrapper); - row.put("IsFabulous", story_like_row != null ? 1 : 0); + // 是否点赞 - 只有登录用户才能查看点赞状态 + if (user_id != null) { + try { + QueryWrapper likeQueryWrapper = new QueryWrapper<>(); + likeQueryWrapper.eq("story_id", story_id).eq("user_id", user_id); + SnsStoryLike story_like_row = snsStoryLikeService.findOne(likeQueryWrapper); + row.put("IsFabulous", story_like_row != null ? 1 : 0); + } catch (Exception e) { + log.warn("查询点赞状态失败, story_id: {}, user_id: {}", story_id, user_id, e); + row.put("IsFabulous", 0); + } + } else { + row.put("IsFabulous", 0); // 未登录用户默认未点赞 + } // 下面两个查询用不到 - /*QueryWrapper advertisementQueryWrapper = new QueryWrapper<>(); - advertisementQueryWrapper.eq("adv_enable", 1).eq("adv_is_top", 1); - row.put("adv_top", snsStoryAdvertisementService.findOne(advertisementQueryWrapper)); + /*QueryWrapper advertisementQueryWrapper = new QueryWrapper<>(); + advertisementQueryWrapper.eq("adv_enable", 1).eq("adv_is_top", 1); + row.put("adv_top", snsStoryAdvertisementService.findOne(advertisementQueryWrapper)); - QueryWrapper advListQueryWrapper = new QueryWrapper<>(); - advListQueryWrapper.eq("adv_enable", 1).eq("adv_is_top", 0); - row.put("adv_lists", snsStoryAdvertisementService.find(advListQueryWrapper));*/ + QueryWrapper advListQueryWrapper = new QueryWrapper<>(); + advListQueryWrapper.eq("adv_enable", 1).eq("adv_is_top", 0); + row.put("adv_lists", snsStoryAdvertisementService.find(advListQueryWrapper));*/ // 移动端不需要分类 if (!isMobile()) { - row.put("category_row", snsStoryCategoryService.find(new QueryWrapper<>())); - QueryWrapper baseQueryWrapper = new QueryWrapper<>(); - baseQueryWrapper.eq("user_id", row.get("user_id")).eq("story_status", 1).eq("story_enable", 1).eq("story_privacy", 0).orderByDesc("story_time"); - row.put("story_user_items", snsStoryBaseService.lists(baseQueryWrapper, 1, 5).getRecords()); + try { + row.put("category_row", snsStoryCategoryService.find(new QueryWrapper<>())); + QueryWrapper baseQueryWrapper = new QueryWrapper<>(); + baseQueryWrapper.eq("user_id", row.get("user_id")) + .eq("story_status", 1) + .eq("story_enable", 1) + .eq("story_privacy", 0) + .orderByDesc("story_time"); + + Object userId = row.get("user_id"); + if (userId != null) { + Page storyPage = snsStoryBaseService.lists(baseQueryWrapper, 1, 5); + row.put("story_user_items", storyPage.getRecords()); + } else { + row.put("story_user_items", new ArrayList<>()); + } + } catch (Exception e) { + log.warn("获取用户故事列表失败, user_id: {}", row.get("user_id"), e); + row.put("story_user_items", new ArrayList<>()); + } } - QueryWrapper friendQueryWrapper = new QueryWrapper<>(); - friendQueryWrapper.eq("friend_id", row.get("user_id")).eq("user_id", user_id); - SnsUserFriend friend_row = snsUserFriendService.findOne(friendQueryWrapper); + // 关注状态 - 只有登录用户才能查看关注状态 + if (user_id != null && row.get("user_id") != null) { + try { + QueryWrapper friendQueryWrapper = new QueryWrapper<>(); + friendQueryWrapper.eq("friend_id", row.get("user_id")).eq("user_id", user_id); + SnsUserFriend friend_row = snsUserFriendService.findOne(friendQueryWrapper); - if (friend_row != null) { - row.put("IsFollow", 1); + if (friend_row != null) { + row.put("IsFollow", 1); + } else { + row.put("IsFollow", 0); + } + } catch (Exception e) { + log.warn("查询关注状态失败, friend_id: {}, user_id: {}", row.get("user_id"), user_id, e); + row.put("IsFollow", 0); + } } else { - row.put("IsFollow", 0); + row.put("IsFollow", 0); // 未登录用户默认未关注 } - //row.put("story_file", StrUtil.split(row.get("story_file").toString(), ",")); - row.put("story_file", Convert.toList(String.class, row.get("story_file"))); + // 处理故事文件 + try { + //row.put("story_file", StrUtil.split(row.get("story_file").toString(), ",")); + Object storyFileObj = row.get("story_file"); + if (storyFileObj != null) { + row.put("story_file", Convert.toList(String.class, storyFileObj)); + } else { + row.put("story_file", new ArrayList<>()); + } + } catch (Exception e) { + log.warn("处理故事文件失败, story_id: {}", story_id, e); + row.put("story_file", new ArrayList<>()); + } return row; } diff --git a/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryCategoryServiceImpl.java b/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryCategoryServiceImpl.java index b9e4292d..86336148 100644 --- a/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryCategoryServiceImpl.java +++ b/mall-sns/src/main/java/com/suisung/mall/sns/service/impl/SnsStoryCategoryServiceImpl.java @@ -189,99 +189,170 @@ public class SnsStoryCategoryServiceImpl extends BaseServiceImpl wrapper = new QueryWrapper<>(); Integer story_id = Convert.toInt(getParameter("story_id")); + + // 参数校验 + if (story_id == null || story_id <= 0) { + return new HashMap(); + } + wrapper.eq("story_id", story_id); wrapper.orderByDesc("comment_time"); - Map data = snsStoryCommentService.getLists(wrapper, page, rows); + + Map data = new HashMap(); + try { + data = snsStoryCommentService.getLists(wrapper, page, rows); + } catch (Exception e) { + // 处理查询异常 + data.put("items", new ArrayList<>()); + data.put("total", 0); + } + + // 确保 items 字段存在 + if (data.get("items") == null) { + data.put("items", new ArrayList<>()); + } + data.put("items", accountService.fixUserAvatar((List) data.get("items"), false)); //读取评论回复 List items = (List) data.get("items"); if (CollUtil.isEmpty(items)) { - return new HashMap(); + return data; } - List comment_id_row = items.stream().map(s -> Convert.toInt(s.get("comment_id"))).collect(Collectors.toList()); + // 避免转换中的空值问题 + List comment_id_row = items.stream() + .map(s -> Convert.toInt(s.get("comment_id"))) + .filter(id -> id != null) + .collect(Collectors.toList()); + + if (CollUtil.isEmpty(comment_id_row)) { + return data; + } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.in("comment_id", comment_id_row); - queryWrapper.eq("user_id", user_id); - // + if (user_id != null) { + queryWrapper.eq("user_id", user_id); + } + //判断评论是否IsFabulous - List snsStoryCommentHelpfuls = snsStoryCommentHelpfulService.find(queryWrapper); + List snsStoryCommentHelpfuls = new ArrayList<>(); + try { + snsStoryCommentHelpfuls = snsStoryCommentHelpfulService.find(queryWrapper); + } catch (Exception e) { + // 忽略查询异常 + } + List comment_helpful_rows = Convert.toList(Map.class, snsStoryCommentHelpfuls); + if (comment_helpful_rows == null) { + comment_helpful_rows = new ArrayList<>(); + } QueryWrapper snsStoryCommentReplyQueryWrapper = new QueryWrapper<>(); snsStoryCommentReplyQueryWrapper.in("comment_id", comment_id_row); snsStoryCommentReplyQueryWrapper.eq("comment_reply_show_flag", 1); snsStoryCommentReplyQueryWrapper.orderByDesc("comment_reply_time"); - List snsStoryCommentReplies = snsStoryCommentReplyService.find(snsStoryCommentReplyQueryWrapper); - List comment_reply_rows = Convert.toList(Map.class, snsStoryCommentReplies); + List snsStoryCommentReplies = new ArrayList<>(); + try { + snsStoryCommentReplies = snsStoryCommentReplyService.find(snsStoryCommentReplyQueryWrapper); + } catch (Exception e) { + // 忽略查询异常 + } + + List comment_reply_rows = Convert.toList(Map.class, snsStoryCommentReplies); + if (comment_reply_rows == null) { + comment_reply_rows = new ArrayList<>(); + } + + try { + accountService.fixUserAvatar(comment_reply_rows, false); + } catch (Exception e) { + // 忽略头像修复异常 + } - accountService.fixUserAvatar(comment_reply_rows, false); Map comment_reply_tmp_rows = new HashMap(); for (Map comment_reply_row : comment_reply_rows) { Integer comment_id = Convert.toInt(comment_reply_row.get("comment_id")); + if (comment_id == null) continue; // 避免空值 + List comment_rows = (List) ObjectUtil.defaultIfNull(comment_reply_tmp_rows.get(comment_id), new ArrayList()); - if (CollUtil.isEmpty(comment_rows)) comment_reply_tmp_rows.put(comment_id, comment_rows); + if (CollUtil.isEmpty(comment_rows)) { + comment_reply_tmp_rows.put(comment_id, new ArrayList<>()); // 使用新实例 + comment_rows = (List) comment_reply_tmp_rows.get(comment_id); + } comment_rows.add(comment_reply_row); } //判断子评论是否IsFabulous - List comment_reply_id_row = comment_reply_rows.stream().map(s -> Convert - .toInt(s.get("comment_reply_id"))).collect(Collectors.toList()); + List comment_reply_id_row = comment_reply_rows.stream() + .map(s -> Convert.toInt(s.get("comment_reply_id"))) + .filter(id -> id != null) + .collect(Collectors.toList()); - QueryWrapper helpfulQueryWrapper = new QueryWrapper<>(); - if (CollUtil.isNotEmpty(comment_reply_id_row)) helpfulQueryWrapper.in("comment_reply_id", comment_reply_id_row); + Map comment_reply_helpful_tmp_rows = new HashMap(); // 提前定义变量 + if (CollUtil.isNotEmpty(comment_reply_id_row)) { + QueryWrapper helpfulQueryWrapper = new QueryWrapper<>(); + helpfulQueryWrapper.in("comment_reply_id", comment_reply_id_row); + if (user_id != null) { + helpfulQueryWrapper.eq("user_id", user_id); + } - helpfulQueryWrapper.eq("user_id", user_id); + List storyCommentHelpfuls = new ArrayList<>(); + try { + storyCommentHelpfuls = snsStoryCommentReplyHelpfulService.find(helpfulQueryWrapper); + } catch (Exception e) { + // 忽略查询异常 + } - List storyCommentHelpfuls = snsStoryCommentReplyHelpfulService.find(helpfulQueryWrapper); - List comment_reply_helpful_rows = Convert.toList(Map.class, storyCommentHelpfuls); + List comment_reply_helpful_rows = Convert.toList(Map.class, storyCommentHelpfuls); + if (comment_reply_helpful_rows == null) { + comment_reply_helpful_rows = new ArrayList<>(); + } - Map comment_reply_helpful_tmp_rows = new HashMap(); - for (Map comment_reply_helpful_row : comment_reply_helpful_rows) { - Integer comment_reply_id = Convert.toInt(comment_reply_helpful_row.get("comment_reply_id")); - List comment_rows = (List) ObjectUtil.defaultIfNull(comment_reply_helpful_tmp_rows.get(comment_reply_id), new ArrayList()); - if (CollUtil.isEmpty(comment_rows)) comment_reply_helpful_tmp_rows.put(comment_reply_id, comment_rows); - comment_rows.add(comment_reply_helpful_row); + for (Map comment_reply_helpful_row : comment_reply_helpful_rows) { + Integer comment_reply_id = Convert.toInt(comment_reply_helpful_row.get("comment_reply_id")); + if (comment_reply_id == null) continue; // 避免空值 + + List comment_rows = (List) ObjectUtil.defaultIfNull(comment_reply_helpful_tmp_rows.get(comment_reply_id), new ArrayList()); + if (CollUtil.isEmpty(comment_rows)) { + comment_reply_helpful_tmp_rows.put(comment_reply_id, new ArrayList<>()); // 使用新实例 + comment_rows = (List) comment_reply_helpful_tmp_rows.get(comment_reply_id); + } + comment_rows.add(comment_reply_helpful_row); + } } - /* - QueryWrapper wrapper1 = new QueryWrapper<>(); - wrapper1.in("comment_id", comment_id_row); - wrapper1.eq("user_id", user_id); - List storyCommentReplies = snsStoryCommentReplyService.find(wrapper1); - - */ - - comment_helpful_rows = Convert.toList(Map.class, snsStoryCommentHelpfuls); - Map comment_helpful_tmp_rows = new HashMap(); for (Map comment_helpful_row : comment_helpful_rows) { - Integer comment_id = Convert.toInt(comment_helpful_row.get("comment_id")); - List comment_rows = (List) ObjectUtil.defaultIfNull(comment_helpful_tmp_rows.get(comment_id), new ArrayList()); - if (CollUtil.isEmpty(comment_rows)) comment_reply_helpful_tmp_rows.put(comment_id, comment_rows); + if (comment_id == null) continue; // 避免空值 + List comment_rows = (List) ObjectUtil.defaultIfNull(comment_helpful_tmp_rows.get(comment_id), new ArrayList()); + if (CollUtil.isEmpty(comment_rows)) { + comment_helpful_tmp_rows.put(comment_id, new ArrayList<>()); // 使用新实例 + comment_rows = (List) comment_helpful_tmp_rows.get(comment_id); + } comment_rows.add(comment_helpful_row); - comment_helpful_tmp_rows.put(comment_helpful_row.get("comment_id"), comment_helpful_row); } for (Map item : items) { Integer comment_id = Convert.toInt(item.get("comment_id")); + if (comment_id == null) continue; // 避免空值 + if (ObjectUtil.isNotNull(comment_reply_tmp_rows.get(comment_id))) { item.put("commentList", comment_reply_tmp_rows.get(comment_id)); } else { @@ -292,22 +363,22 @@ public class SnsStoryCategoryServiceImpl extends BaseServiceImpl