登录、模板操作
This commit is contained in:
@@ -94,14 +94,33 @@ class UtilsService
|
||||
*/
|
||||
public function genOpenId(): string
|
||||
{
|
||||
// 获取当前时间戳(精确到秒)
|
||||
$timestamp = dechex(time());
|
||||
// 获取微秒级时间戳(提高时间精度)
|
||||
$microtime = microtime(true);
|
||||
$timestamp = dechex(floor($microtime)); // 秒部分
|
||||
$microsec = dechex(($microtime - floor($microtime)) * 1000000); // 微秒部分
|
||||
|
||||
// 生成16字节的随机数据,并转换为32个十六进制字符组成的字符串
|
||||
$randomString = bin2hex(random_bytes(16));
|
||||
// 生成更多随机字节(20字节=160位熵)
|
||||
$randomBytes = random_bytes(20);
|
||||
$randomHex = bin2hex($randomBytes);
|
||||
|
||||
// 结合时间和随机字符串,确保总长度为28个字符
|
||||
return substr($timestamp . $randomString, 0, 28);
|
||||
// 获取进程ID和内存ID作为额外熵源
|
||||
$pid = dechex(getmypid() % 65536); // 进程ID
|
||||
$memoryId = dechex(crc32(memory_get_usage(true))); // 内存使用哈希
|
||||
|
||||
// 打乱组合顺序并截取28个字符
|
||||
$components = [
|
||||
$timestamp,
|
||||
$microsec,
|
||||
substr($randomHex, 0, 16),
|
||||
substr($randomHex, 16, 8),
|
||||
$pid,
|
||||
$memoryId
|
||||
];
|
||||
shuffle($components); // 打乱顺序
|
||||
$combined = implode('', $components);
|
||||
|
||||
// 最终截取(确保固定长度)
|
||||
return 'nl_'. substr($combined, 0, 28);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,4 +263,30 @@ class UtilsService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取项目根目录
|
||||
public static function getRootPath(): string
|
||||
{
|
||||
return dirname(__DIR__, 3);
|
||||
}
|
||||
|
||||
// 若文件不存在则创建文件
|
||||
public static function createFileHolder($filePath): bool
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
return mkdir($filePath, 777, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 若文件不存在则创建文件
|
||||
public static function createFile($filePath): bool
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
$file = fopen($filePath, 'w');
|
||||
fclose($file);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user