-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestIdGenerator.php
More file actions
73 lines (60 loc) · 1.63 KB
/
RequestIdGenerator.php
File metadata and controls
73 lines (60 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* php 唯一RequestID生成器类
*
* @author fdipzone
* @DateTime 2023-03-30 14:15:39
*
* Description:
* PHP实现唯一RequestID生成类,使用session_create_id()与uniqid()方法实现,保证唯一性。
*
* Func:
* public generate 生成唯一请求id
* private format 格式化请求id
*/
class RequestIdGenerator{
/**
* 生成唯一请求id
*
* @author fdipzone
* @DateTime 2023-03-30 14:16:12
*
* @return string
*/
public static function generate():string{
// 使用session_create_id()方法创建前缀
$prefix = session_create_id(date('YmdHis'));
// 使用uniqid()方法创建唯一id
$request_id = strtoupper(md5(uniqid($prefix, true)));
// 格式化请求id
return self::format($request_id);
}
/**
* 格式化请求id
*
* @author fdipzone
* @DateTime 2023-03-30 16:41:36
*
* @param string $request_id 请求id
* @param string $format 格式(将长度为32的字符串拆分为多段字符串)
* @return string
*/
private static function format(string $request_id, string $format='8,4,4,4,12'):string{
$tmp = array();
$offset = 0;
$cut = explode(',', $format);
// 根据设定格式化
if($cut){
foreach($cut as $v){
$tmp[] = substr($request_id, $offset, $v);
$offset += $v;
}
}
// 加入剩余部分
if($offset<strlen($request_id)){
$tmp[] = substr($request_id, $offset);
}
return implode('-', $tmp);
}
}
?>