|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
|
4
|
|
|
// | ThinkLibrary 6.0 for ThinkPhP 6.0 |
|
5
|
|
|
// +---------------------------------------------------------------------- |
|
6
|
|
|
// | 版权所有 2017~2020 [ https://www.dtapp.net ] |
|
7
|
|
|
// +---------------------------------------------------------------------- |
|
8
|
|
|
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
|
9
|
|
|
// +---------------------------------------------------------------------- |
|
10
|
|
|
// | 开源协议 ( https://mit-license.org ) |
|
11
|
|
|
// +---------------------------------------------------------------------- |
|
12
|
|
|
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
|
13
|
|
|
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
|
14
|
|
|
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
|
15
|
|
|
// +---------------------------------------------------------------------- |
|
16
|
|
|
|
|
17
|
|
|
declare (strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace DtApp\ThinkLibrary\helper; |
|
20
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 随机管理类 |
|
25
|
|
|
* @mixin Randoms |
|
26
|
|
|
* @package DtApp\ThinkLibrary\helper |
|
27
|
|
|
*/ |
|
28
|
|
|
class Randoms |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* 生成随机 |
|
32
|
|
|
* @param int $length 长度 |
|
33
|
|
|
* @param int $type 类型,1 纯数字,2 纯小写字母,3 纯大写字母,4 数字和小写字母,5 数字和大写字母,6 大小写字母,7 数字和大小写字母 |
|
34
|
|
|
* @return string |
|
35
|
|
|
* @throws Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
public function generate(int $length = 6, int $type = 1): string |
|
38
|
|
|
{ |
|
39
|
|
|
// 取字符集数组 |
|
40
|
|
|
$number = range(0, 9); |
|
41
|
|
|
$lowerLetter = range('a', 'z'); |
|
42
|
|
|
$upperLetter = range('A', 'Z'); |
|
43
|
|
|
// 根据type合并字符集 |
|
44
|
|
|
if ($type === 1) { |
|
45
|
|
|
$charset = $number; |
|
46
|
|
|
} elseif ($type === 2) { |
|
47
|
|
|
$charset = $lowerLetter; |
|
48
|
|
|
} elseif ($type === 3) { |
|
49
|
|
|
$charset = $upperLetter; |
|
50
|
|
|
} elseif ($type === 4) { |
|
51
|
|
|
$charset = array_merge($number, $lowerLetter); |
|
52
|
|
|
} elseif ($type === 5) { |
|
53
|
|
|
$charset = array_merge($number, $upperLetter); |
|
54
|
|
|
} elseif ($type === 6) { |
|
55
|
|
|
$charset = array_merge($lowerLetter, $upperLetter); |
|
56
|
|
|
} elseif ($type === 7) { |
|
57
|
|
|
$charset = array_merge($number, $lowerLetter, $upperLetter); |
|
58
|
|
|
} else { |
|
59
|
|
|
$charset = $number; |
|
60
|
|
|
} |
|
61
|
|
|
$str = ''; |
|
62
|
|
|
// 生成字符串 |
|
63
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
64
|
|
|
$str .= $charset[random_int(0, count($charset) - 1)]; |
|
65
|
|
|
// 验证规则 |
|
66
|
|
|
if ($type === 4 && strlen($str) >= 2) { |
|
67
|
|
|
if (!preg_match('/\d+/', $str) || !preg_match('/[a-z]+/', $str)) { |
|
68
|
|
|
$str = substr($str, 0, -1); |
|
69
|
|
|
--$i; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
if ($type === 5 && strlen($str) >= 2) { |
|
73
|
|
|
if (!preg_match('/\d+/', $str) || !preg_match('/[A-Z]+/', $str)) { |
|
74
|
|
|
$str = substr($str, 0, -1); |
|
75
|
|
|
--$i; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
if ($type === 6 && strlen($str) >= 2) { |
|
79
|
|
|
if (!preg_match('/[a-z]+/', $str) || !preg_match('/[A-Z]+/', $str)) { |
|
80
|
|
|
$str = substr($str, 0, -1); |
|
81
|
|
|
--$i; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
if ($type === 7 && strlen($str) >= 3) { |
|
85
|
|
|
if (!preg_match('/\d+/', $str) || !preg_match('/[a-z]+/', $str) || !preg_match('/[A-Z]+/', $str)) { |
|
86
|
|
|
$str = substr($str, 0, -2); |
|
87
|
|
|
$i -= 2; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return $str; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|