mirror of
https://gitee.com/wonderful-code/buildadmin
synced 2024-11-22 07:04:24 +00:00
60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace ba;
|
|
|
|
class Random
|
|
{
|
|
/**
|
|
* 获取全球唯一标识
|
|
* @return string
|
|
*/
|
|
public static function uuid()
|
|
{
|
|
return sprintf(
|
|
'%04x%04x-%04x-%04x-%04x-',
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0x0fff) | 0x4000,
|
|
mt_rand(0, 0x3fff) | 0x8000
|
|
) . substr(md5(uniqid(mt_rand(), true)), 0, 12);
|
|
}
|
|
|
|
/**
|
|
* 随机字符生成
|
|
* @param string $type 类型 alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1
|
|
* @param int $len 长度
|
|
* @return string
|
|
*/
|
|
public static function build($type = 'alnum', $len = 8)
|
|
{
|
|
switch ($type) {
|
|
case 'alpha':
|
|
case 'alnum':
|
|
case 'numeric':
|
|
case 'nozero':
|
|
switch ($type) {
|
|
case 'alpha':
|
|
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
break;
|
|
case 'alnum':
|
|
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
break;
|
|
case 'numeric':
|
|
$pool = '0123456789';
|
|
break;
|
|
case 'nozero':
|
|
$pool = '123456789';
|
|
break;
|
|
}
|
|
return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
|
|
case 'unique':
|
|
case 'md5':
|
|
return md5(uniqid(mt_rand()));
|
|
case 'encrypt':
|
|
case 'sha1':
|
|
return sha1(uniqid(mt_rand(), true));
|
|
}
|
|
}
|
|
|
|
} |