1
0

helpers.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. use Carbon\Carbon;
  3. use Carbon\CarbonInterval;
  4. const MiB = 1048576;
  5. const GiB = 1073741824;
  6. const TiB = 1099511627776;
  7. const Minute = 60;
  8. const Hour = 3600;
  9. const Day = 86400;
  10. const Mbps = 125000;
  11. // base64加密(处理URL)
  12. if (! function_exists('base64url_encode')) {
  13. function base64url_encode(string $data): string
  14. {
  15. return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
  16. }
  17. }
  18. // base64解密(处理URL)
  19. if (! function_exists('base64url_decode')) {
  20. function base64url_decode(string $data): false|string
  21. {
  22. return base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
  23. }
  24. }
  25. // 根据流量值自动转换单位输出
  26. if (! function_exists('formatBytes')) {
  27. function formatBytes(int $bytes, ?string $base = null, int $precision = 2): string
  28. {
  29. $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  30. $bytes = max($bytes, 0);
  31. $power = floor(($bytes ? log($bytes) : 0) / log(1024));
  32. $power = min($power, count($units) - 1);
  33. $bytes /= 1024 ** $power;
  34. if ($base) {
  35. $power += max(array_search($base, $units), 0);
  36. }
  37. return round($bytes, $precision).' '.$units[$power];
  38. }
  39. }
  40. // 秒转时间
  41. if (! function_exists('formatTime')) {
  42. function formatTime(?int $seconds): string
  43. {
  44. if (! $seconds) {
  45. return '-';
  46. }
  47. $interval = CarbonInterval::seconds($seconds);
  48. return $interval->cascade()->forHumans();
  49. }
  50. }
  51. // 获取系统设置
  52. if (! function_exists('sysConfig')) {
  53. function sysConfig(?string $key = null, ?string $default = null): array|string|int|null
  54. {
  55. return $key ? config("settings.$key", $default) : config('settings');
  56. }
  57. }
  58. // Array values and indexes clean
  59. if (! function_exists('array_clean')) {
  60. function array_clean(array &$array): array
  61. {
  62. foreach ($array as $key => &$value) {
  63. if (is_array($value)) {
  64. $value = array_clean($value);
  65. }
  66. if (empty($value)) {
  67. unset($array[$key]);
  68. }
  69. }
  70. return $array;
  71. }
  72. }
  73. // string url safe sanitize
  74. if (! function_exists('string_urlsafe')) {
  75. function string_urlsafe(string $string, bool $force_lowercase = true, bool $anal = false): string
  76. {
  77. $clean = preg_replace('/[~`!@#$%^&*()_=+\[\]{}\\|;:"\'<>,.?\/]/', '_', strip_tags($string));
  78. $clean = preg_replace('/\s+/', '-', $clean);
  79. $clean = ($anal) ? preg_replace('/[^a-zA-Z0-9]/', '', $clean) : $clean;
  80. if ($force_lowercase) {
  81. $clean = function_exists('mb_strtolower') ? mb_strtolower($clean, 'UTF-8') : strtolower($clean);
  82. }
  83. return $clean;
  84. }
  85. }
  86. if (! function_exists('localized_date')) {
  87. function localized_date($date): string
  88. {
  89. if (! $date) {
  90. return '';
  91. }
  92. $carbon = Carbon::parse($date);
  93. $locale = app()->getLocale();
  94. $carbon->setLocale($locale);
  95. // 获取原始字符串表示
  96. $dateStr = is_string($date) ? $date : $date->format('Y-m-d H:i:s');
  97. // 使用正则检测精度
  98. if (preg_match('/(\d{4}-\d{2}-\d{2}) (\d{2}):(\d{2}):(\d{2})/', $dateStr, $matches)) {
  99. $hours = (int) $matches[2];
  100. $minutes = (int) $matches[3];
  101. $seconds = (int) $matches[4];
  102. if ($seconds > 0) {
  103. return $carbon->isoFormat('LL LTS'); // 显示完整时间
  104. }
  105. if ($minutes > 0 || $hours > 0) {
  106. return $carbon->isoFormat('LL LT'); // 显示到分钟
  107. }
  108. }
  109. return $carbon->isoFormat('LL'); // 只显示日期
  110. }
  111. }