2024-10-20 12:10:57 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library.Utils
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 消息ID生成工具
|
|
|
|
|
|
/// </summary>
|
2025-08-23 14:48:19 +08:00
|
|
|
|
public class IdGeneratorHelper
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
private static readonly object _lock = new object();
|
|
|
|
|
|
private static int _counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成一个不重复的标识
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="theme"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string GenerateMessageId(string theme)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (_lock)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 时间戳
|
|
|
|
|
|
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
|
|
|
2025-07-23 16:20:41 +08:00
|
|
|
|
// 机器标识
|
2024-10-20 12:10:57 +08:00
|
|
|
|
string machineId = GetMachineId();
|
|
|
|
|
|
|
|
|
|
|
|
// 进程ID
|
|
|
|
|
|
int processId = Process.GetCurrentProcess().Id;
|
|
|
|
|
|
|
|
|
|
|
|
// 递增计数器,确保在同一毫秒内的多次生成也不重复
|
|
|
|
|
|
int count = _counter++;
|
|
|
|
|
|
|
|
|
|
|
|
// 随机数
|
|
|
|
|
|
byte[] randomBytes = new byte[8];
|
|
|
|
|
|
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
|
|
|
|
|
|
{
|
|
|
|
|
|
rng.GetBytes(randomBytes);
|
|
|
|
|
|
}
|
|
|
|
|
|
string randomPart = BitConverter.ToString(randomBytes).Replace("-", "");
|
|
|
|
|
|
|
|
|
|
|
|
// 将所有部分组合起来
|
|
|
|
|
|
return $"{timestamp}-{machineId}-{processId}-{count}-{randomPart}-{theme}";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetMachineId()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 这里使用 GUID 模拟机器标识
|
|
|
|
|
|
return Guid.NewGuid().ToString("N");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|