浏览代码

营销系统-IP修改

wangcl 6 天之前
父节点
当前提交
0d10b7cc33

+ 1 - 2
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/PigMarketingApplication.java

@@ -1,7 +1,6 @@
 package com.pig4cloud.pig.marketing;
 
 
-import com.pig4cloud.pig.common.core.util.ip.IPUtils;
 import com.pig4cloud.pig.common.feign.annotation.EnablePigFeignClients;
 import com.pig4cloud.pig.common.security.annotation.EnablePigResourceServer;
 import com.pig4cloud.pig.common.swagger.annotation.EnablePigDoc;
@@ -22,7 +21,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 public class PigMarketingApplication {
 
 	public static void main(String[] args) {
-		System.out.println("xxxxtest---"+IPUtils.getIpRegion("1.1.1.1"));
+//		System.out.println("xxxxtest---"+IPUtils.getIpRegion("1.1.1.1"));
 		SpringApplication.run(PigMarketingApplication.class, args);
 
 	}

+ 1 - 1
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/controller/TcpDataController.java

@@ -46,7 +46,7 @@ public class TcpDataController {
 	@GetMapping("iptest")
 	@Operation(summary = "测试IP地址")
 	public R<String> iptest() {
-		return R.ok(IPUtils.getIpRegion("1.1.1.1"));
+		return R.ok(IPUtils.getIpRegion("223.104.68.55"));
 	}
 
 	/******************************************* 设备 *******************************************/

+ 53 - 25
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/service/impl/MktMgmtHandPushServiceImpl.java

@@ -111,10 +111,11 @@ public class MktMgmtHandPushServiceImpl implements MktMgmtHandPushService {
                 globalRule.getPushAddr() != null && !globalRule.getPushAddr().isEmpty()) {
 
 				if (!"All".equals(globalRule.getPushAddr().get(0))){
-					String fullAddress = ipLocationUtil.getCityByIP(saveDTO.getPushIP());
-					if (fullAddress == null) {
+					String resolvedAddress = ipLocationUtil.getCityByIP(saveDTO.getPushIP());
+					if (resolvedAddress == null) {
 						return "无法解析推送IP【" + saveDTO.getPushIP() + "】的地址信息";
 					}
+					String fullAddress = normalizeAddress(resolvedAddress);
 
 					// 检查是否匹配pushAddr中的任一地址
 					boolean addressMatched = false;
@@ -405,39 +406,66 @@ public class MktMgmtHandPushServiceImpl implements MktMgmtHandPushService {
         if (fullAddress == null || allowedAddr == null) {
             return false;
         }
-        
-        // 如果允许的地址配置为空,则不匹配
-        if (allowedAddr.trim().isEmpty()) {
+
+        String normalizedFull = normalizeAddress(fullAddress);
+        String normalizedAllowed = normalizeAddress(allowedAddr);
+        if (!org.springframework.util.StringUtils.hasText(normalizedAllowed)) {
             return false;
         }
-        
-        String trimmedAllowedAddr = allowedAddr.trim();
-        
-        // 1. 完全匹配
-        if (fullAddress.equals(trimmedAllowedAddr)) {
+        if ("all".equalsIgnoreCase(normalizedAllowed)) {
             return true;
         }
-        
-        // 2. 检查是否匹配国家级别(如果允许的地址只有国家)
-        if (!trimmedAllowedAddr.contains("-")) {
-            return fullAddress.startsWith(trimmedAllowedAddr + "-");
+
+        if (normalizedFull.equals(normalizedAllowed)) {
+            return true;
         }
-        
-        // 3. 检查是否匹配国家-省级别
-        String[] allowedParts = trimmedAllowedAddr.split("-");
-        String[] fullParts = fullAddress.split("-");
-        
+
+        String[] allowedParts = normalizedAllowed.split("-");
+        String[] fullParts = normalizedFull.split("-");
+
+        if (allowedParts.length == 1 && fullParts.length >= 1) {
+            return allowedParts[0].equals(fullParts[0]);
+        }
+
         if (allowedParts.length == 2 && fullParts.length >= 2) {
             return allowedParts[0].equals(fullParts[0]) && allowedParts[1].equals(fullParts[1]);
         }
-        
-        // 4. 检查是否匹配国家-省-市级别
-        if (allowedParts.length == 3 && fullParts.length >= 3) {
-            return allowedParts[0].equals(fullParts[0]) && 
-                   allowedParts[1].equals(fullParts[1]) && 
+
+        if (allowedParts.length >= 3 && fullParts.length >= 3) {
+            return allowedParts[0].equals(fullParts[0]) &&
+                   allowedParts[1].equals(fullParts[1]) &&
                    allowedParts[2].equals(fullParts[2]);
         }
-        
+
         return false;
     }
+
+    /**
+     * 规范化地址字符串:
+     * 1) 将分隔符统一为 '-'; 2) 去除分隔符两侧空格; 3) 去除多余空格
+     */
+    private String normalizeAddress(String input) {
+        if (input == null) {
+            return null;
+        }
+        String s = input.trim();
+        // 统一分隔符,去掉可能的 ISP 信息分隔符
+        s = s.replace('|', '-');
+        s = s.replace('-', '-');
+        s = s.replace('—', '-');
+        // 去除分隔符两侧空格
+        s = s.replaceAll("\\s*-\\s*", "-");
+        // 拆分并重组,移除空段
+        String[] parts = s.split("-");
+        StringBuilder sb = new StringBuilder();
+        for (String p : parts) {
+            if (org.springframework.util.StringUtils.hasText(p)) {
+                if (sb.length() > 0) {
+                    sb.append('-');
+                }
+                sb.append(p.trim());
+            }
+        }
+        return sb.toString();
+    }
 }

+ 36 - 28
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/util/IPLocationUtil.java

@@ -9,7 +9,6 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
 import java.io.File;
-import java.nio.file.Path;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -49,17 +48,7 @@ public class IPLocationUtil {
 		}
 	}
 
-	/**
-	 * 尝试标记临时文件在JVM退出时删除
-	 */
-	private static void subtleSetDeleteOnExit(Path tmp) {
-		try {
-			java.io.File f = tmp.toFile();
-			f.deleteOnExit();
-		} catch (Exception ignore) {
-			// no-op
-		}
-	}
+
 
     /**
      * 根据IP地址获取三级地址信息
@@ -128,31 +117,50 @@ public class IPLocationUtil {
      */
     private String getFullAddressFromIp2region(String ip) {
         try {
-            // 统一委托给 IPUtils,避免本地 Searcher 与库/数据版本不一致
             String region = searcher.search(ip);
-            log.info("IPLocationUtil 取临时文件 返回: ip={}, region={}", ip, region);
-            if (region == null || region.trim().isEmpty() || "未知".equals(region) || "内网".equals(region)) {
+            log.info("IPLocationUtil xdb结果: ip={}, region={}", ip, region);
+            if (region == null || region.trim().isEmpty()) {
+                return null;
+            }
+
+            // ip2region xdb 返回格式通常为: 国家|区域|省|市|运营商
+            List<String> parts = cn.hutool.core.util.StrUtil.split(region, '|');
+            if (parts == null || parts.isEmpty()) {
                 return null;
             }
 
-            // IPUtils 返回通常为 "国家-省" 或仅 "国家"
-            List<String> arr = cn.hutool.core.util.StrUtil.split(region, '-');
-            String country = arr.size() >= 1 ? arr.get(0) : null;
-            String province = arr.size() >= 2 ? arr.get(1) : null;
+            String country = parts.size() >= 1 ? parts.get(0) : null;
+            String province = parts.size() >= 3 ? parts.get(2) : null;
+            String city = parts.size() >= 4 ? parts.get(3) : null;
 
-            if (country == null || country.trim().isEmpty()) {
+            // 标准化无效字段
+            if ("0".equals(country) || cn.hutool.core.util.StrUtil.isBlank(country)) {
+                country = null;
+            }
+            if ("0".equals(province) || cn.hutool.core.util.StrUtil.isBlank(province)) {
+                province = null;
+            }
+            if ("0".equals(city) || cn.hutool.core.util.StrUtil.isBlank(city)) {
+                city = null;
+            }
+
+            if (country == null) {
                 return null;
             }
 
-            StringBuilder address = new StringBuilder();
-            address.append(country);
-            if (province != null && !province.trim().isEmpty()) {
-                address.append("-").append(province).append("-").append(province);
-            } else {
-                // 没有省份信息,用国家补齐三级
-                address.append("-").append(country).append("-").append(country);
+            // 仅返回地理层级,不包含运营商,不做重复拼接
+            if (province == null) {
+                // 仅国家
+                return country;
             }
-            return address.toString();
+
+            if (city == null) {
+                // 国家-省
+                return country + "-" + province;
+            }
+
+            // 国家-省-市
+            return country + "-" + province + "-" + city;
         } catch (Exception e) {
             log.warn("查询IP[{}]归属地出错", ip, e);
         }