From cfd619fa4f1f6b693a97b7865d57f8b9982d29a4 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Thu, 08 Jan 2026 13:42:00 +0800
Subject: [PATCH] 优化防重提交间隔时间可自定义
---
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java | 314 +++++++++++++++++++++++++++++++++------------------
1 files changed, 202 insertions(+), 112 deletions(-)
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java
index 954dd03..a27ab62 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java
@@ -3,6 +3,8 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
+import org.springframework.util.AntPathMatcher;
+import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.text.StrFormatter;
/**
@@ -19,10 +21,7 @@
private static final char SEPARATOR = '_';
/** 星号 */
- private static final String START = "*";
-
- /** 斜杠 */
- private static final String SLASH = "/";
+ private static final char ASTERISK = '*';
/**
* 获取参数不为空值
@@ -165,6 +164,49 @@
}
/**
+ * 替换指定字符串的指定区间内字符为"*"
+ *
+ * @param str 字符串
+ * @param startInclude 开始位置(包含)
+ * @param endExclude 结束位置(不包含)
+ * @return 替换后的字符串
+ */
+ public static String hide(CharSequence str, int startInclude, int endExclude)
+ {
+ if (isEmpty(str))
+ {
+ return NULLSTR;
+ }
+ final int strLength = str.length();
+ if (startInclude > strLength)
+ {
+ return NULLSTR;
+ }
+ if (endExclude > strLength)
+ {
+ endExclude = strLength;
+ }
+ if (startInclude > endExclude)
+ {
+ // 如果起始位置大于结束位置,不替换
+ return NULLSTR;
+ }
+ final char[] chars = new char[strLength];
+ for (int i = 0; i < strLength; i++)
+ {
+ if (i >= startInclude && i < endExclude)
+ {
+ chars[i] = ASTERISK;
+ }
+ else
+ {
+ chars[i] = str.charAt(i);
+ }
+ }
+ return new String(chars);
+ }
+
+ /**
* 截取字符串
*
* @param str 字符串
@@ -242,6 +284,56 @@
}
/**
+ * 在字符串中查找第一个出现的 `open` 和最后一个出现的 `close` 之间的子字符串
+ *
+ * @param str 要截取的字符串
+ * @param open 起始字符串
+ * @param close 结束字符串
+ * @return 截取结果
+ */
+ public static String substringBetweenLast(final String str, final String open, final String close)
+ {
+ if (isEmpty(str) || isEmpty(open) || isEmpty(close))
+ {
+ return NULLSTR;
+ }
+ final int start = str.indexOf(open);
+ if (start != INDEX_NOT_FOUND)
+ {
+ final int end = str.lastIndexOf(close);
+ if (end != INDEX_NOT_FOUND)
+ {
+ return str.substring(start + open.length(), end);
+ }
+ }
+ return NULLSTR;
+ }
+
+ /**
+ * 判断是否为空,并且不是空白字符
+ *
+ * @param str 要判断的value
+ * @return 结果
+ */
+ public static boolean hasText(String str)
+ {
+ return (str != null && !str.isEmpty() && containsText(str));
+ }
+
+ private static boolean containsText(CharSequence str)
+ {
+ int strLen = str.length();
+ for (int i = 0; i < strLen; i++)
+ {
+ if (!Character.isWhitespace(str.charAt(i)))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
* 格式化文本, {} 表示占位符<br>
* 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
* 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
@@ -264,7 +356,44 @@
}
/**
- * 下划线转驼峰命名
+ * 是否为http(s)://开头
+ *
+ * @param link 链接
+ * @return 结果
+ */
+ public static boolean ishttp(String link)
+ {
+ return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
+ }
+
+ /**
+ * 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
+ *
+ * @param collection 给定的集合
+ * @param array 给定的数组
+ * @return boolean 结果
+ */
+ public static boolean containsAny(Collection<String> collection, String... array)
+ {
+ if (isEmpty(collection) || isEmpty(array))
+ {
+ return false;
+ }
+ else
+ {
+ for (String str : array)
+ {
+ if (collection.contains(str))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ /**
+ * 驼峰转下划线命名
*/
public static String toUnderScoreCase(String str)
{
@@ -371,13 +500,18 @@
}
/**
- * 驼峰式命名法 例如:user_name->userName
+ * 驼峰式命名法
+ * 例如:user_name->userName
*/
public static String toCamelCase(String s)
{
if (s == null)
{
return null;
+ }
+ if (s.indexOf(SEPARATOR) == -1)
+ {
+ return s;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
@@ -416,9 +550,9 @@
{
return false;
}
- for (String testStr : strs)
+ for (String pattern : strs)
{
- if (matches(str, testStr))
+ if (isMatch(pattern, str))
{
return true;
}
@@ -427,112 +561,19 @@
}
/**
- * 查找指定字符串是否匹配指定字符串数组中的任意一个字符串
+ * 判断url是否与规则配置:
+ * ? 表示单个字符;
+ * * 表示一层路径内的任意字符串,不可跨层级;
+ * ** 表示任意层路径;
*
- * @param str 指定字符串
- * @param strs 需要检查的字符串数组
- * @return 是否匹配
+ * @param pattern 匹配规则
+ * @param url 需要匹配的url
+ * @return
*/
- public static boolean matches(String str, String... strs)
+ public static boolean isMatch(String pattern, String url)
{
- if (isEmpty(str) || isEmpty(strs))
- {
- return false;
- }
- for (String testStr : strs)
- {
- if (matches(str, testStr))
- {
- return true;
- }
- }
- return false;
- }
-
- /**
- * 查找指定字符串是否匹配
- *
- * @param str 指定字符串
- * @param pattern 需要检查的字符串
- * @return 是否匹配
- */
- public static boolean matches(String str, String pattern)
- {
- if (isEmpty(pattern) || isEmpty(str))
- {
- return false;
- }
-
- pattern = pattern.replaceAll("\\s*", ""); // 替换空格
- int beginOffset = 0; // pattern截取开始位置
- int formerStarOffset = -1; // 前星号的偏移位置
- int latterStarOffset = -1; // 后星号的偏移位置
-
- String remainingURI = str;
- String prefixPattern = "";
- String suffixPattern = "";
-
- boolean result = false;
- do
- {
- formerStarOffset = indexOf(pattern, START, beginOffset);
- prefixPattern = substring(pattern, beginOffset, formerStarOffset > -1 ? formerStarOffset : pattern.length());
-
- // 匹配前缀Pattern
- result = remainingURI.contains(prefixPattern);
-
- // 匹配失败,直接返回
- if (!result)
- return false;
-
- // 已经没有星号,判断长度是否符合,并返回
- if (formerStarOffset == -1)
- {
- // 清洗请求路径
- if (remainingURI.endsWith(SLASH)) {
- remainingURI = remainingURI.substring(0, remainingURI.length() - 1);
- }
- return remainingURI.length() == prefixPattern.length();
- }
-
- if (!isEmpty(prefixPattern))
- {
- // 如果前面还有路径 直接返回
- if (!isEmpty(substringBefore(str, prefixPattern))) {
- return false;
- }
- remainingURI = substringAfter(str, prefixPattern);
- }
-
- // 匹配后缀Pattern
- latterStarOffset = indexOf(pattern, START, formerStarOffset + 1);
- suffixPattern = substring(pattern, formerStarOffset + 1, latterStarOffset > -1 ? latterStarOffset : pattern.length());
-
- // 判断是否存在第二个 *
- if (latterStarOffset == -1) {
- // 判断是否还包含斜杠
- if(remainingURI.contains(SLASH)) {
- return false;
- }
- }
-
- result = remainingURI.contains(suffixPattern);
- // 匹配失败,直接返回
- if (!result)
- return false;
-
- if (!isEmpty(suffixPattern))
- {
- remainingURI = substringAfter(str, suffixPattern);
- }
-
- // 移动指针
- beginOffset = latterStarOffset + 1;
-
- }
- while (!isEmpty(suffixPattern) && !isEmpty(remainingURI));
-
- return true;
+ AntPathMatcher matcher = new AntPathMatcher();
+ return matcher.match(pattern, url);
}
@SuppressWarnings("unchecked")
@@ -540,4 +581,53 @@
{
return (T) obj;
}
-}
\ No newline at end of file
+
+ /**
+ * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
+ *
+ * @param num 数字对象
+ * @param size 字符串指定长度
+ * @return 返回数字的字符串格式,该字符串为指定长度。
+ */
+ public static final String padl(final Number num, final int size)
+ {
+ return padl(num.toString(), size, '0');
+ }
+
+ /**
+ * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。
+ *
+ * @param s 原始字符串
+ * @param size 字符串指定长度
+ * @param c 用于补齐的字符
+ * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。
+ */
+ public static final String padl(final String s, final int size, final char c)
+ {
+ final StringBuilder sb = new StringBuilder(size);
+ if (s != null)
+ {
+ final int len = s.length();
+ if (s.length() <= size)
+ {
+ for (int i = size - len; i > 0; i--)
+ {
+ sb.append(c);
+ }
+ sb.append(s);
+ }
+ else
+ {
+ return s.substring(len - size, len);
+ }
+ }
+ else
+ {
+ for (int i = size; i > 0; i--)
+ {
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+}
--
Gitblit v1.9.3