| 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\extend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * 随机数码管理扩展 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Class CodeExtend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @package DtApp\ThinkLibrary\extend | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | class CodeExtend | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * 获取随机字符串编码 | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param integer $size 编码长度 | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param integer $type 编码类型(1纯数字,2纯字母,3数字字母) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $prefix 编码前缀 | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public static function random(int $size = 10, int $type = 1, string $prefix = ''): string | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $numbs = '0123456789'; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $chars = 'abcdefghijklmnopqrstuvwxyz'; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         if ($type === 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             $chars = $numbs; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         if ($type === 3) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             $chars = "{$numbs}{$chars}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $code = $prefix . $chars[random_int(1, strlen($chars) - 1)]; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         while (strlen($code) < $size) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $code .= $chars[random_int(0, strlen($chars) - 1)]; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         return $code; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * 唯一日期编码 | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param integer $size 编码长度 | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $prefix 编码前缀 | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public static function uniqidDate(int $size = 16, string $prefix = ''): string | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         if ($size < 14) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $size = 14; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $code = $prefix . date('Ymd') . (date('H') + date('i')) . date('s'); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         while (strlen($code) < $size) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $code .= random_int(0, 9); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         return $code; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * 唯一数字编码 | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * @param integer $size 编码长度 | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $prefix 编码前缀 | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public static function uniqidNumber(int $size = 12, string $prefix = ''): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $time = time() . ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if ($size < 10) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $size = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $code = $prefix . ((int)$time[0] + (int)$time[1]) . substr($time, 2) . random_int(0, 9); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         while (strlen($code) < $size) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $code .= random_int(0, 9); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |         return $code; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 91 |  |  | } |