function.php
1.9 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
use Helper\Response;
/**
* 数据返回
* @return Response
* @author:dc
* @time 2023/2/5 9:37
*/
function res():Response {
return new Response();
}
/**
* @return string
* @author:dc
* @time 2023/2/5 14:01
*/
function token_get():string {
$token = request()->header('__TOKEN__');
if($token){
return token_de($token);
}
return '';
}
/**
* @param string $str
* @return string
* @author:dc
* @time 2023/2/5 13:56
*/
function token_de(string $str):string {
$str = decrypt($str);
list($base,$sign) = explode('+',$str);
$base = @base64_decode($base);
if(md5($base.'.1') != $sign){
return '';
}
return $base;
}
/**
* @param string $str
* @return string
* @author:dc
* @time 2023/2/5 13:57
*/
function token_en(string $str):string {
$str = base64_encode($str) . '+' . md5($str.'.1');
return encrypt($str);
}
/**
* 数组转树组
* @param $list
* @param string $pk
* @param string $pid
* @param string $child
* @param int $root
* @param bool $empty_child
* @return array
* @author:dc
* @time 2023/2/6 9:14
*/
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0, $empty_child=true):array {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
if($empty_child){
$list[$key][$child] = [];
}
$refer[$data[$pk]] = &$list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = &$list[$key];
}else{
if (isset($refer[$parentId])) {
$refer[$parentId][$child][] = &$list[$key];
}
}
}
}
return $tree;
}