helpers.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Facades\Session;
  4. if (! function_exists('user_admin_config')) {
  5. function user_admin_config($key = null, $value = null)
  6. {
  7. // 获取 session 实例
  8. $session = session();
  9. // 从 session 中获取 'admin.config',如果没有则使用默认的 'admin' 配置
  10. $config = $session->get('admin.config', function () {
  11. $adminConfig = config('admin');
  12. $adminConfig['lang'] = config('app.locale');
  13. return $adminConfig;
  14. });
  15. // 如果 $key 是数组,表示我们需要批量设置配置项
  16. if (is_array($key)) {
  17. foreach ($key as $k => $v) {
  18. Arr::set($config, $k, $v); // 在配置数组中设置每个键值对
  19. }
  20. $session->put('admin.config', $config); // 将更新后的配置保存到 session 中
  21. return;
  22. }
  23. // 如果没有传递具体的 key,返回整个配置数组
  24. if (is_null($key)) {
  25. return $config;
  26. }
  27. // 获取指定的配置项,如果不存在则返回默认值 $value
  28. return Arr::get($config, $key, $value);
  29. }
  30. }
  31. if (!function_exists('getDistributor')) {
  32. /**
  33. * 获取会话中的 distributor 值
  34. *
  35. * @return mixed
  36. */
  37. function getDistributor() {
  38. return Session::get('distributor');
  39. }
  40. }
  41. if (!function_exists('getDistributorId')) {
  42. /**
  43. * 获取会话中的 distributor 的 ID
  44. *
  45. * @return mixed
  46. */
  47. function getDistributorId() {
  48. $distributor = Session::get('distributor');
  49. return $distributor ? $distributor['id'] : null; // 假设 distributor 是一个数组,包含 id
  50. }
  51. }
  52. //通过parent_id构建树形结构
  53. if (!function_exists('buildTree')) {
  54. function buildTree(array $elements, $parentId = 0)
  55. {
  56. $branch = [];
  57. foreach ($elements as $element) {
  58. if ($element['parent_id'] == $parentId) {
  59. $children = buildTree($elements, $element['id']);
  60. if ($children) {
  61. $element['children'] = $children;
  62. }
  63. $branch[] = $element;
  64. }
  65. }
  66. return $branch;
  67. }
  68. }
  69. // 展平树形结构
  70. if (!function_exists('flattenTree')) {
  71. function flattenTree(array $tree, array &$result = [], $level = 0)
  72. {
  73. foreach ($tree as $node) {
  74. // 复制节点数据,但不包括子节点,并添加 level 字段
  75. $flattenedNode = array_diff_key($node, ['children' => null]);
  76. $flattenedNode['level'] = $level;
  77. $result[] = $flattenedNode;
  78. // 如果有子节点,递归处理子节点,并将 level 增加 1
  79. if (isset($node['children']) && is_array($node['children'])) {
  80. flattenTree($node['children'], $result, $level + 1);
  81. }
  82. }
  83. return $result;
  84. }
  85. }