helpers.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if (!function_exists('getSiteDomain')) {
  54. //得到分销商域名
  55. function getSiteDomain($hasHttp = true) {
  56. $distributor = Session::get('distributor');
  57. $domain = $distributor['domain_type'] == 0 ? $distributor['secondary_domain'] : $domain = $distributor['custom_domain'];
  58. if ($hasHttp) {
  59. $domain = 'https://'.$domain;
  60. }
  61. return $domain;
  62. }
  63. }
  64. //通过parent_id构建树形结构
  65. if (!function_exists('buildTree')) {
  66. function buildTree(array $elements, $parentId = 0)
  67. {
  68. $branch = [];
  69. foreach ($elements as $element) {
  70. if ($element['parent_id'] == $parentId) {
  71. $children = buildTree($elements, $element['id']);
  72. if ($children) {
  73. $element['children'] = $children;
  74. }
  75. $branch[] = $element;
  76. }
  77. }
  78. return $branch;
  79. }
  80. }
  81. // 展平树形结构
  82. if (!function_exists('flattenTree')) {
  83. function flattenTree(array $tree, array &$result = [], $level = 0)
  84. {
  85. foreach ($tree as $node) {
  86. // 复制节点数据,但不包括子节点,并添加 level 字段
  87. $flattenedNode = array_diff_key($node, ['children' => null]);
  88. $flattenedNode['level'] = $level;
  89. $result[] = $flattenedNode;
  90. // 如果有子节点,递归处理子节点,并将 level 增加 1
  91. if (isset($node['children']) && is_array($node['children'])) {
  92. flattenTree($node['children'], $result, $level + 1);
  93. }
  94. }
  95. return $result;
  96. }
  97. }
  98. if (!function_exists('uniqueCode')) {
  99. function uniqueCode($prefix = '')
  100. {
  101. //$uniqueId = strtolower(Str::random($length));
  102. $uniqueId = uniqid($prefix);
  103. return $uniqueId;
  104. }
  105. }
  106. if (!function_exists('generateVersionNumber')) {
  107. /*
  108. * 12位版本号
  109. */
  110. function generateVersionNumber()
  111. {
  112. // 获取当前的年、月、日
  113. $year = date('y'); // 年份的最后两位
  114. $month = date('m'); // 月份,两位数字
  115. $day = date('d'); // 日期,两位数字
  116. // 获取当前的毫秒级时间戳
  117. $microtime = microtime(true);
  118. $milliseconds = round(($microtime - floor($microtime)) * 1000);
  119. // 将毫秒级时间戳转换为 6 位数字
  120. $milliseconds = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
  121. // 获取当前的时间戳(秒级)
  122. $timestamp = time();
  123. // 将时间戳转换为 6 位数字(如果需要更精确的时间戳,可以使用毫秒级时间戳)
  124. $timestamp = str_pad($timestamp % 1000000, 6, '0', STR_PAD_LEFT);
  125. // 组合成 12 位版本号
  126. $versionNumber = $year . $month . $day. $timestamp. $milliseconds ;
  127. return $versionNumber;
  128. }
  129. }
  130. //判断是否为json
  131. if (!function_exists('isValidJson')) {
  132. function isValidJson($string) {
  133. json_decode($string);
  134. return (json_last_error() === JSON_ERROR_NONE);
  135. }
  136. }
  137. //判断是否为纯域名
  138. if (!function_exists('isDomainOnly')) {
  139. function isDomainOnly($string) {
  140. // 正则表达式:匹配不带协议或路径的纯域名
  141. $pattern = '/^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/';
  142. return preg_match($pattern, $string) === 1;
  143. }
  144. }
  145. //生成slug
  146. if (!function_exists('generateSlug')) {
  147. function generateSlug($title)
  148. {
  149. // 1. 将所有字符转换为小写
  150. $slug = strtolower($title);
  151. // 2. 将空格替换为短横线(-)
  152. $slug = str_replace(' ', '-', $slug);
  153. // 3. 将不合法的字符(!@#$%^&*?=+)替换为空
  154. $slug = preg_replace('/[!@#$%^&*()?=+]+/', '', $slug);
  155. // 4. 清理多余的短横线
  156. $slug = preg_replace('/-+/', '-', $slug);
  157. // 5. 去除开头和结尾的短横线
  158. $slug = trim($slug, '-');
  159. return $slug;
  160. }
  161. }
  162. //生成随机小写英文组成的字符串
  163. if (!function_exists('generateRandomString')) {
  164. function generateRandomString($length = 3) {
  165. $characters = 'abcdefghijklmnopqrstuvwxyz';
  166. $charactersLength = strlen($characters);
  167. $randomString = '';
  168. for ($i = 0; $i < $length; $i++) {
  169. $randomString .= $characters[rand(0, $charactersLength - 1)];
  170. }
  171. return $randomString;
  172. }
  173. }