feat : user

This commit is contained in:
2025-03-03 16:21:38 +08:00
parent e92a1cd8c0
commit d6f9f348da
12 changed files with 361 additions and 12 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Service\ServiceTrait\Common;
use Random\RandomException;
trait UserTrait
{
/**
* @param int $userId
* @return string
* @throws RandomException
*/
protected function generateStableCode(int $userId): string
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$seed = $userId . random_bytes(16) . microtime(true);
// 使用分段处理避免大数
$hash = hash('sha256', $seed, true);
$code = '';
for ($i=0; $i<8; $i++) {
// 每次取1字节数据0-255
$byte = ord($hash[$i]);
$code .= $chars[$byte % 62];
}
return $code;
}
}