function.php 1.9 KB
<?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;
}