99
1010namespace Toolkit \Stdlib \Str ;
1111
12+ use DateTime ;
1213use Exception ;
1314use Toolkit \Stdlib \Str \Traits \StringCaseHelperTrait ;
1415use Toolkit \Stdlib \Str \Traits \StringCheckHelperTrait ;
1516use Toolkit \Stdlib \Str \Traits \StringLengthHelperTrait ;
1617use Toolkit \Stdlib \Str \Traits \StringConvertTrait ;
1718use Toolkit \Stdlib \Str \Traits \StringTruncateHelperTrait ;
1819use Toolkit \Stdlib \Util \UUID ;
20+ use function abs ;
1921use function array_merge ;
2022use function base64_encode ;
2123use function count ;
24+ use function crc32 ;
25+ use function gethostname ;
2226use function hash ;
2327use function hex2bin ;
2428use function is_int ;
2529use function is_string ;
2630use function mb_strwidth ;
31+ use function microtime ;
2732use function preg_split ;
2833use function random_bytes ;
34+ use function random_int ;
2935use function str_pad ;
3036use function str_repeat ;
3137use function str_replace ;
@@ -173,6 +179,16 @@ public static function genUid(int $length = 7): string
173179 return substr (hash ('md5 ' , uniqid ('' , true )), 0 , $ length );
174180 }
175181
182+ /**
183+ * @param string $prefix
184+ *
185+ * @return string
186+ */
187+ public static function uniqId (string $ prefix = '' ): string
188+ {
189+ return uniqid ($ prefix , true );
190+ }
191+
176192 /**
177193 * gen UUID
178194 *
@@ -181,12 +197,63 @@ public static function genUid(int $length = 7): string
181197 * @param null $ns
182198 *
183199 * @return UUID
200+ * @throws Exception
184201 */
185- public static function genUUID (int $ version = 1 , $ node = null , $ ns = null )
202+ public static function genUUID (int $ version = 1 , $ node = null , $ ns = null ): UUID
186203 {
187204 return UUID ::generate ($ version , $ node , $ ns );
188205 }
189206
207+ /**
208+ * Generate order number
209+ *
210+ * @param string|int $prefix
211+ *
212+ * @return string If no prefix, default length is 20
213+ * @throws Exception
214+ */
215+ public static function genNOV1 ($ prefix = '' , array $ randomRange = []): string
216+ {
217+ $ host = gethostname ();
218+ $ time = microtime (true ) * 10000 ;
219+
220+ $ id = (string )abs (crc32 ($ host ) % 100 );
221+ $ id = str_pad ($ id , 2 , '0 ' , STR_PAD_LEFT );
222+
223+ $ randomRange = $ randomRange ?: [1000 , 9999 ];
224+ [$ min , $ max ] = $ randomRange ;
225+ /** @noinspection PhpUnhandledExceptionInspection */
226+ $ random = random_int ($ min , $ max );
227+
228+ return $ prefix . $ time . $ id . $ random ;
229+ }
230+
231+ /**
232+ * Generate order number v2
233+ *
234+ * @param string|int $prefix
235+ *
236+ * @return string If no prefix, default length is 26
237+ * @throws Exception
238+ */
239+ public static function genNOV2 ($ prefix = '' , array $ randomRange = []): string
240+ {
241+ $ host = gethostname ();
242+ // u - 可以打印微妙,但是使用 date 函数时无效
243+ // $date = date('YmdHisu');
244+ $ date = (new DateTime ())->format ('YmdHisu ' );
245+
246+ $ id = (string )abs (crc32 ($ host ) % 100 );
247+ $ id = str_pad ($ id , 2 , '0 ' , STR_PAD_LEFT );
248+
249+ $ randomRange = $ randomRange ?: [100 , 999 ];
250+ [$ min , $ max ] = $ randomRange ;
251+ /** @noinspection PhpUnhandledExceptionInspection */
252+ $ random = random_int ($ min , $ max );
253+
254+ return $ prefix . $ date . $ id . $ random ;
255+ }
256+
190257 ////////////////////////////////////////////////////////////////////////
191258 /// Format
192259 ////////////////////////////////////////////////////////////////////////
0 commit comments