helpers.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. const KB = 1024;
  3. const MB = 1048576;
  4. const GB = 1073741824;
  5. const TB = 1099511627776;
  6. const PB = 1125899906842624;
  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($data): string
  14. {
  15. return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
  16. }
  17. }
  18. // base64解密(处理URL)
  19. if (! function_exists('base64url_decode')) {
  20. function base64url_decode($data)
  21. {
  22. return base64_decode(strtr($data, '-_', '+/'));
  23. }
  24. }
  25. // 根据流量值自动转换单位输出
  26. if (! function_exists('flowAutoShow')) {
  27. function flowAutoShow($value): string
  28. {
  29. $value = abs($value);
  30. if ($value >= PB) {
  31. return round($value / PB, 2).'PB';
  32. }
  33. if ($value >= TB) {
  34. return round($value / TB, 2).'TB';
  35. }
  36. if ($value >= GB) {
  37. return round($value / GB, 2).'GB';
  38. }
  39. if ($value >= MB) {
  40. return round($value / MB, 2).'MB';
  41. }
  42. if ($value >= KB) {
  43. return round($value / KB, 2).'KB';
  44. }
  45. return round($value, 2).'B';
  46. }
  47. }
  48. // 秒转时间
  49. if (! function_exists('seconds2time')) {
  50. function seconds2time($seconds): string
  51. {
  52. $day = floor($seconds / Day);
  53. $hour = floor(($seconds % Day) / Hour);
  54. $minute = floor((($seconds % Day) % Hour) / Minute);
  55. if ($day > 0) {
  56. return $day.trans_choice('validation.attributes.day', 1).$hour.trans_choice('validation.attributes.hour', 1).$minute.trans('validation.attributes.minute');
  57. }
  58. if ($hour != 0) {
  59. return $hour.trans_choice('validation.attributes.hour', 1).$minute.trans('validation.attributes.minute');
  60. }
  61. return $minute.trans('validation.attributes.minute');
  62. }
  63. }
  64. // 过滤emoji表情
  65. if (! function_exists('filterEmoji')) {
  66. function filterEmoji($str)
  67. {
  68. return preg_replace_callback('/./u', static function (array $match) {
  69. return strlen($match[0]) >= 4 ? '' : $match[0];
  70. }, $str);
  71. }
  72. }
  73. // 获取系统设置
  74. if (! function_exists('sysConfig')) {
  75. function sysConfig($name)
  76. {
  77. return config('settings.'.$name);
  78. }
  79. }