Sfoglia il codice sorgente

个人信息,评论新增地址代码,优化

fangqing 5 mesi fa
parent
commit
dc570a5518

+ 22 - 1
ruoyi-common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java

@@ -9,9 +9,11 @@ import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.http.HttpUtils;
 
+import java.util.concurrent.ExecutionException;
+
 /**
  * 获取地址类
- * 
+ *
  * @author ruoyi
  */
 public class AddressUtils
@@ -53,4 +55,23 @@ public class AddressUtils
         }
         return UNKNOWN;
     }
+
+    public static String getAddress() throws ExecutionException, InterruptedException {
+
+        //IP地址从当前登录用户的表里拿
+        String ipAddr = IpUtils.getIpAddr();
+        String address = "未知";
+        String  realAddressByIP = getRealAddressByIP(ipAddr);
+        if (realAddressByIP.isEmpty() || realAddressByIP.equals("内网IP")){
+            return address;
+        }
+
+        String[] parts = realAddressByIP.split(" ", 2); // 这里的2表示只分割一次
+        address = parts[0].replace("市", "").replace("省", "");
+        return address;
+    }
+
+
+
+
 }

+ 23 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityCommentController.java

@@ -8,6 +8,7 @@ import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.exception.user.ProjectException;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.ip.AddressUtils;
 import com.ruoyi.generator.domain.Community.CommunityArticleComment;
 import com.ruoyi.generator.domain.Community.CommunityCommentLike;
 import com.ruoyi.generator.domain.Community.CommunityCommentReply;
@@ -28,6 +29,7 @@ import org.springframework.web.bind.annotation.*;
 
 import java.text.ParseException;
 import java.util.*;
+import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 
 /**
@@ -83,6 +85,10 @@ public class CommunityCommentController extends BaseController {
             CommunityArticleCommentVo commentVo = null;
 
             for (CommunityArticleComment record : records) {
+                if (record.getAddress() == null){
+                    record.setAddress("未知");
+                }
+
                 commentVo = new CommunityArticleCommentVo();
                 BeanUtils.copyProperties(record, commentVo);
                 commentVos.add(commentVo);
@@ -157,6 +163,9 @@ public class CommunityCommentController extends BaseController {
 
             for (final CommunityCommentReply reply : replies) {
                 replyVo = new CommunityCommentReplyVo();
+                if (reply.getAddress() == null){
+                    reply.setAddress("未知");
+                }
                 SysUser sysUser = sysUserService.selectUserById(reply.getUserId());
                 final Long parentReplyId = reply.getParentReplyId();
                 BeanUtils.copyProperties(reply, replyVo);
@@ -175,6 +184,8 @@ public class CommunityCommentController extends BaseController {
                         .eq("reply_id", replyVo.getId()));
                 replyVo.setLikeCount(commentLikes.size());
                 replyVo.setLike(commentLikes.stream().anyMatch(item -> SecurityUtils.getUserId().equals(item.getUserId())));
+
+
                 replyVos.add(replyVo);
             }
         } catch (Exception e) {
@@ -208,7 +219,19 @@ public class CommunityCommentController extends BaseController {
             communityCommentReply.setDelete(false);
             communityCommentReply.setCreateBy(userId);
             communityCommentReply.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
+            String address = null;
+            try {
+                address = AddressUtils.getAddress();
+            } catch (ExecutionException e) {
+                throw new RuntimeException(e);
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+            communityCommentReply.setAddress(address);
             communityCommentReplyService.save(communityCommentReply);
+
+
+
         } catch (Exception e) {
             System.out.println(e.getMessage());
             throw new ProjectException();

+ 7 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticleComment.java

@@ -73,4 +73,11 @@ public class CommunityArticleComment implements Serializable {
     @ApiModelProperty("图片地址")
     private String imageUrl;
 
+    /**
+     * 用户地址
+     */
+    @ApiModelProperty("用户地址")
+    private String address;
+
+
 }

+ 6 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityCommentReply.java

@@ -89,4 +89,10 @@ public class CommunityCommentReply implements Serializable {
     @ApiModelProperty("图片地址")
     private String imageUrl;
 
+    /**
+     * 用户地址
+     */
+    @ApiModelProperty("用户地址")
+    private String address;
+
 }

+ 14 - 8
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityArticleServiceImpl.java

@@ -384,6 +384,17 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
         communityArticleComment.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
         communityArticleComment.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
         communityArticleComment.setDelete(false);
+        String address = null;
+        try {
+            address = AddressUtils.getAddress();
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+
+        communityArticleComment.setAddress(address);
+
         communityArticleCommentMapper.insert(communityArticleComment);
     }
 
@@ -563,20 +574,15 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
         communityUserInfoVo.setCompanionCount(0);
 
 
-        String truncatedString = null;
+        String address = null;
         try {
-            String ip = IpUtils.get();
-            String address = AddressUtils.getRealAddressByIP(ip);
-            // 使用空格分割字符串
-            String[] parts = address.split(" ", 2); // 这里的2表示只分割一次
-            truncatedString = parts[0].replace("市", "").replace("省", "");
+            address = AddressUtils.getAddress();
         } catch (ExecutionException e) {
             throw new RuntimeException(e);
         } catch (InterruptedException e) {
             throw new RuntimeException(e);
         }
-
-        communityUserInfoVo.setAddress(truncatedString);
+        communityUserInfoVo.setAddress(address);
         //设置是否被当前登录的用户关注
         communityUserInfoVo.setLike(false);
         Long loginUserId = SecurityUtils.getLoginUser().getUserId();

+ 5 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/vo/CommunityArticleCommentVo.java

@@ -100,4 +100,9 @@ public class CommunityArticleCommentVo extends BaseEntity implements Serializabl
     @ApiModelProperty("图片地址")
     private String imageUrl;
 
+    /**
+     * 用户地址
+     */
+    @ApiModelProperty("用户地址")
+    private String address;
 }

+ 6 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/vo/CommunityCommentReplyVo.java

@@ -122,4 +122,10 @@ public class CommunityCommentReplyVo implements Serializable {
      */
     @ApiModelProperty("图片地址")
     private String imageUrl;
+
+    /**
+     * 用户地址
+     */
+    @ApiModelProperty("用户地址")
+    private String address;
 }