Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-cloud-core
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-cloud-core
Commits
2ea71f39
Commit
2ea71f39
authored
Sep 23, 2025
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
配置
parent
940f6df3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
190 additions
and
0 deletions
+190
-0
git
+0
-0
yd-common/src/main/java/com/yd/common/enums/CommonEnum.java
+2
-0
yd-common/src/main/java/com/yd/common/utils/UUIDUtil.java
+188
-0
No files found.
git
0 → 100644
View file @
2ea71f39
yd-common/src/main/java/com/yd/common/enums/CommonEnum.java
View file @
2ea71f39
...
...
@@ -34,6 +34,8 @@ public enum CommonEnum {
UID_TYPE_EMAIL_CONTACT
(
"email_contact"
,
"邮箱联系人(收件人)表"
),
UID_TYPE_EMAIL_PROVIDER_CONFIG
(
"email_provider_config"
,
"SMTP邮箱服务商配置表"
),
UID_TYPE_EMAIL_VARIABLE
(
"email_variable"
,
"邮箱变量表"
),
UID_TYPE_EMAIL_VARIABLE_GROUP
(
"email_variable_group"
,
"变量分组表"
),
UID_TYPE_EMAIL_CONTACT_IMPORT
(
"email_contact_import"
,
"邮件联系人导入临时表"
),
//作用域枚举
SCOPE_SYS
(
"1"
,
"系统级(全局)"
),
...
...
yd-common/src/main/java/com/yd/common/utils/UUIDUtil.java
0 → 100644
View file @
2ea71f39
package
com
.
yd
.
common
.
utils
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.SecureRandom
;
import
java.util.UUID
;
/**
* UUID生成工具类
* 提供多种UUID生成方式
*/
public
class
UUIDUtil
{
private
UUIDUtil
()
{
// 工具类,防止实例化
}
/**
* 生成随机UUID (版本4)
* @return 随机UUID字符串
*/
public
static
String
randomUUID
()
{
return
UUID
.
randomUUID
().
toString
();
}
/**
* 生成不带连字符的随机UUID
* @return 不带连字符的UUID字符串
*/
public
static
String
randomUUIDWithoutHyphen
()
{
return
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
);
}
/**
* 生成大写UUID
* @return 大写UUID字符串
*/
public
static
String
randomUUIDUpperCase
()
{
return
UUID
.
randomUUID
().
toString
().
toUpperCase
();
}
/**
* 生成基于名称的UUID (版本3 - MD5)
* @param name 名称
* @return 基于名称的UUID
*/
public
static
String
nameBasedUUID
(
String
name
)
{
return
UUID
.
nameUUIDFromBytes
(
name
.
getBytes
()).
toString
();
}
/**
* 生成基于名称空间的UUID (版本5 - SHA-1)
* @param namespace 命名空间
* @param name 名称
* @return 基于名称空间的UUID
*/
public
static
String
namespaceBasedUUID
(
String
namespace
,
String
name
)
{
try
{
// 将命名空间转换为UUID字节
byte
[]
namespaceBytes
=
UUID
.
fromString
(
namespace
).
toString
().
getBytes
();
MessageDigest
md
=
MessageDigest
.
getInstance
(
"SHA-1"
);
md
.
update
(
namespaceBytes
);
byte
[]
hash
=
md
.
digest
(
name
.
getBytes
());
// 设置版本号为5
hash
[
6
]
&=
0x0F
;
hash
[
6
]
|=
0x50
;
// 设置变体
hash
[
8
]
&=
0x3F
;
hash
[
8
]
|=
0x80
;
return
bytesToUUID
(
hash
);
}
catch
(
NoSuchAlgorithmException
e
)
{
throw
new
RuntimeException
(
"SHA-1 algorithm not available"
,
e
);
}
}
/**
* 生成时间序UUID (版本1)
* 模拟版本1UUID,包含时间戳和随机节点信息
* @return 时间序UUID
*/
public
static
String
timeBasedUUID
()
{
try
{
SecureRandom
secureRandom
=
SecureRandom
.
getInstanceStrong
();
long
time
=
System
.
currentTimeMillis
();
long
timeLow
=
(
time
&
0xFFFFFFFF
L
)
<<
32
;
long
timeMid
=
((
time
>>
32
)
&
0xFFFF
L
)
<<
16
;
long
timeHi
=
((
time
>>
48
)
&
0x0FFF
L
)
|
0x1000
L
;
// 设置版本号为1
long
mostSigBits
=
timeLow
|
timeMid
|
timeHi
;
long
leastSigBits
=
secureRandom
.
nextLong
();
leastSigBits
=
(
leastSigBits
&
0x3FFFFFFFFFFFFFFF
L
)
|
0x8000000000000000
L
;
// 设置变体
UUID
uuid
=
new
UUID
(
mostSigBits
,
leastSigBits
);
return
uuid
.
toString
();
}
catch
(
NoSuchAlgorithmException
e
)
{
return
UUID
.
randomUUID
().
toString
();
}
}
/**
* 生成短UUID (8位)
* @return 8位短UUID
*/
public
static
String
shortUUID
()
{
UUID
uuid
=
UUID
.
randomUUID
();
long
mostSigBits
=
uuid
.
getMostSignificantBits
();
long
leastSigBits
=
uuid
.
getLeastSignificantBits
();
// 取部分位进行组合
long
combined
=
mostSigBits
^
leastSigBits
;
return
Long
.
toHexString
(
combined
).
substring
(
0
,
8
);
}
/**
* 验证UUID格式是否有效
* @param uuid 待验证的UUID字符串
* @return 是否有效
*/
public
static
boolean
isValidUUID
(
String
uuid
)
{
if
(
uuid
==
null
)
{
return
false
;
}
try
{
UUID
.
fromString
(
uuid
);
return
true
;
}
catch
(
IllegalArgumentException
e
)
{
// 检查不带连字符的格式
if
(
uuid
.
length
()
==
32
)
{
try
{
String
formatted
=
uuid
.
substring
(
0
,
8
)
+
"-"
+
uuid
.
substring
(
8
,
12
)
+
"-"
+
uuid
.
substring
(
12
,
16
)
+
"-"
+
uuid
.
substring
(
16
,
20
)
+
"-"
+
uuid
.
substring
(
20
,
32
);
UUID
.
fromString
(
formatted
);
return
true
;
}
catch
(
IllegalArgumentException
ex
)
{
return
false
;
}
}
return
false
;
}
}
/**
* 将字节数组转换为UUID
* @param bytes 字节数组(长度至少为16)
* @return UUID字符串
*/
private
static
String
bytesToUUID
(
byte
[]
bytes
)
{
if
(
bytes
.
length
<
16
)
{
throw
new
IllegalArgumentException
(
"Byte array must be at least 16 bytes long"
);
}
long
mostSigBits
=
0
;
long
leastSigBits
=
0
;
for
(
int
i
=
0
;
i
<
8
;
i
++)
{
mostSigBits
=
(
mostSigBits
<<
8
)
|
(
bytes
[
i
]
&
0xff
);
}
for
(
int
i
=
8
;
i
<
16
;
i
++)
{
leastSigBits
=
(
leastSigBits
<<
8
)
|
(
bytes
[
i
]
&
0xff
);
}
return
new
UUID
(
mostSigBits
,
leastSigBits
).
toString
();
}
/**
* 获取UUID的版本号
* @param uuid UUID字符串
* @return 版本号,如果无效返回-1
*/
public
static
int
getVersion
(
String
uuid
)
{
try
{
UUID
u
=
UUID
.
fromString
(
uuid
);
return
u
.
version
();
}
catch
(
IllegalArgumentException
e
)
{
return
-
1
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment