helpers.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Facades\Session;
  4. use Illuminate\Support\Str;
  5. if (! function_exists('user_admin_config')) {
  6. function user_admin_config($key = null, $value = null)
  7. {
  8. // 获取 session 实例
  9. $session = session();
  10. // 从 session 中获取 'admin.config',如果没有则使用默认的 'admin' 配置
  11. $config = $session->get('admin.config', function () {
  12. $adminConfig = config('admin');
  13. $adminConfig['lang'] = config('app.locale');
  14. return $adminConfig;
  15. });
  16. // 如果 $key 是数组,表示我们需要批量设置配置项
  17. if (is_array($key)) {
  18. foreach ($key as $k => $v) {
  19. Arr::set($config, $k, $v); // 在配置数组中设置每个键值对
  20. }
  21. $session->put('admin.config', $config); // 将更新后的配置保存到 session 中
  22. return;
  23. }
  24. // 如果没有传递具体的 key,返回整个配置数组
  25. if (is_null($key)) {
  26. return $config;
  27. }
  28. // 获取指定的配置项,如果不存在则返回默认值 $value
  29. return Arr::get($config, $key, $value);
  30. }
  31. }
  32. if (!function_exists('getDistributor')) {
  33. /**
  34. * 获取会话中的 distributor 值
  35. *
  36. * @return mixed
  37. */
  38. function getDistributor() {
  39. return Session::get('distributor');
  40. }
  41. }
  42. if (!function_exists('getDistributorId')) {
  43. /**
  44. * 获取会话中的 distributor 的 ID
  45. *
  46. * @return mixed
  47. */
  48. function getDistributorId() {
  49. $distributor = Session::get('distributor');
  50. return $distributor ? $distributor['id'] : null; // 假设 distributor 是一个数组,包含 id
  51. }
  52. }
  53. //通过parent_id构建树形结构
  54. if (!function_exists('buildTree')) {
  55. function buildTree(array $elements, $parentId = 0)
  56. {
  57. $branch = [];
  58. foreach ($elements as $element) {
  59. if ($element['parent_id'] == $parentId) {
  60. $children = buildTree($elements, $element['id']);
  61. if ($children) {
  62. $element['children'] = $children;
  63. }
  64. $branch[] = $element;
  65. }
  66. }
  67. return $branch;
  68. }
  69. }
  70. // 展平树形结构
  71. if (!function_exists('flattenTree')) {
  72. function flattenTree(array $tree, array &$result = [], $level = 0)
  73. {
  74. foreach ($tree as $node) {
  75. // 复制节点数据,但不包括子节点,并添加 level 字段
  76. $flattenedNode = array_diff_key($node, ['children' => null]);
  77. $flattenedNode['level'] = $level;
  78. $result[] = $flattenedNode;
  79. // 如果有子节点,递归处理子节点,并将 level 增加 1
  80. if (isset($node['children']) && is_array($node['children'])) {
  81. flattenTree($node['children'], $result, $level + 1);
  82. }
  83. }
  84. return $result;
  85. }
  86. }
  87. if (!function_exists('uniqueCode')) {
  88. function uniqueCode($prefix = '')
  89. {
  90. //$uniqueId = strtolower(Str::random($length));
  91. $uniqueId = uniqid($prefix);
  92. return $uniqueId;
  93. }
  94. }
  95. if (!function_exists('generateVersionNumber')) {
  96. /*
  97. * 12位版本号
  98. */
  99. function generateVersionNumber()
  100. {
  101. // 获取当前的年、月、日
  102. $year = date('y'); // 年份的最后两位
  103. $month = date('m'); // 月份,两位数字
  104. $day = date('d'); // 日期,两位数字
  105. // 获取当前的毫秒级时间戳
  106. $microtime = microtime(true);
  107. $milliseconds = round(($microtime - floor($microtime)) * 1000);
  108. // 将毫秒级时间戳转换为 6 位数字
  109. $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
  110. // 获取当前的时间戳(秒级)
  111. $timestamp = time();
  112. // 将时间戳转换为 6 位数字(如果需要更精确的时间戳,可以使用毫秒级时间戳)
  113. $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
  114. // 组合成 12 位版本号
  115. $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
  116. return $versionNumber;
  117. }
  118. }
  119. //判断是否为json
  120. if (!function_exists('isValidJson')) {
  121. function isValidJson($string) {
  122. json_decode($string);
  123. return (json_last_error() === JSON_ERROR_NONE);
  124. }
  125. }
  126. //判断是否为纯域名
  127. if (!function_exists('isDomainOnly')) {
  128. function isDomainOnly($string) {
  129. // 正则表达式:匹配不带协议或路径的纯域名
  130. $pattern = '/^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/';
  131. return preg_match($pattern, $string) === 1;
  132. }
  133. }