This commit is contained in:
125
app/Helpers/functions.php
Normal file
125
app/Helpers/functions.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/7/10
|
||||
* Time: 下午7:37
|
||||
* Description:
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Hyperf\Collection\Arr;
|
||||
|
||||
/**
|
||||
* 合并权限CODE为一维数组.
|
||||
*/
|
||||
function mergePermissions(array $permissions): array
|
||||
{
|
||||
$code_list = [];
|
||||
foreach ($permissions as $value) {
|
||||
foreach ($value as $permission) {
|
||||
$code_list[] = $permission;
|
||||
}
|
||||
}
|
||||
// 合并指定键下的值到一个二维数组中
|
||||
$mergedArray = [];
|
||||
foreach ($code_list as $item) {
|
||||
$values = array_values($item);
|
||||
$mergedArray = array_merge($mergedArray, $values);
|
||||
}
|
||||
return $mergedArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 树形数据转换为线性数据(多维数组转为一维数组).
|
||||
*/
|
||||
function treeToList(mixed $menu): array
|
||||
{
|
||||
$mid = 1; # 初始主键ID
|
||||
$pid = 0; # 初始父ID
|
||||
return getFromChild($menu, $mid, $pid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归计算自增ID.
|
||||
*/
|
||||
function getFromChild(mixed $data, mixed &$mid, mixed $pid, string $childField = 'children'): array
|
||||
{
|
||||
$res = [];
|
||||
foreach ($data as $v) {
|
||||
$vCopy = $v;
|
||||
unset($vCopy[$childField]);
|
||||
$vCopy['id'] = $mid++;
|
||||
$vCopy['parent_id'] = $pid;
|
||||
array_push($res, $vCopy);
|
||||
if (! empty($v[$childField])) {
|
||||
$res = Arr::collapse([$res, getFromChild($v[$childField], $mid, $vCopy['id'], $childField)]);
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统菜单递归,无子集时不返回空子集.
|
||||
*/
|
||||
function listToTree(array $list, string $pk = 'id', string $pid = 'parent_id', string $child = 'children', int $root = 0): array
|
||||
{
|
||||
if (empty($list)) {
|
||||
return [];
|
||||
}
|
||||
$tree = [];
|
||||
# 创建基于主键的数组引用
|
||||
$refer = [];
|
||||
foreach ($list as $key => $data) {
|
||||
$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])) {
|
||||
$parent = &$refer[$parentId];
|
||||
$parent[$child][] = &$list[$key];
|
||||
} else {
|
||||
$tree[] = $list[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 权限选中数组处理.
|
||||
*/
|
||||
function checkPermissions(array &$menu, array $menuPermissions, array $dataPermissions): array
|
||||
{
|
||||
foreach ($menu as $key => $value) {
|
||||
$menu[$key]['checked'] = false;
|
||||
if (in_array($value['meta']['code'], $menuPermissions)) {
|
||||
$menu[$key]['checked'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理数组,为 "data_permission" 添加 "checked" 键值对
|
||||
foreach ($menu as &$item) {
|
||||
if (isset($item['meta']['data_permission'])) {
|
||||
$dataPermission = $item['meta']['data_permission'];
|
||||
|
||||
foreach ($dataPermission as &$permission) {
|
||||
$permission['checked'] = false;
|
||||
if (in_array($permission['actions'], $dataPermissions)) {
|
||||
$permission['checked'] = true;
|
||||
}
|
||||
}
|
||||
$item['meta']['data_permission'] = $dataPermission;
|
||||
}
|
||||
}
|
||||
return $menu;
|
||||
}
|
Reference in New Issue
Block a user