helpers.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. use App\Components\Helpers;
  3. define('KB', 1024);
  4. define('MB', 1048576);
  5. define('GB', 1073741824);
  6. define('TB', 1099511627776);
  7. define('PB', 1125899906842624);
  8. define('Minute', 60);
  9. define('Hour', 3600);
  10. define('Day', 86400);
  11. define('Mbps', 125000);
  12. // base64加密(处理URL)
  13. if ( ! function_exists('base64url_encode')) {
  14. function base64url_encode($data)
  15. {
  16. return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
  17. }
  18. }
  19. // base64解密(处理URL)
  20. if ( ! function_exists('base64url_decode')) {
  21. function base64url_decode($data)
  22. {
  23. return base64_decode(strtr($data, '-_', '+/'));
  24. }
  25. }
  26. // 根据流量值自动转换单位输出
  27. if ( ! function_exists('flowAutoShow')) {
  28. function flowAutoShow($value)
  29. {
  30. $value = abs($value);
  31. if ($value >= PB) {
  32. return round($value / PB, 2) . "PB";
  33. }
  34. if ($value >= TB) {
  35. return round($value / TB, 2) . "TB";
  36. }
  37. if ($value >= GB) {
  38. return round($value / GB, 2) . "GB";
  39. }
  40. if ($value >= MB) {
  41. return round($value / MB, 2) . "MB";
  42. }
  43. if ($value >= KB) {
  44. return round($value / KB, 2) . "KB";
  45. }
  46. return round($value, 2) . "B";
  47. }
  48. }
  49. if ( ! function_exists('toMB')) {
  50. function toMB($traffic)
  51. {
  52. return $traffic * MB;
  53. }
  54. }
  55. if ( ! function_exists('toGB')) {
  56. function toGB($traffic)
  57. {
  58. return $traffic * GB;
  59. }
  60. }
  61. if ( ! function_exists('flowToGB')) {
  62. function flowToGB($traffic)
  63. {
  64. return $traffic / GB;
  65. }
  66. }
  67. // 文件大小转换
  68. if ( ! function_exists('formatBytes')) {
  69. function formatBytes($bytes, $precision = 2)
  70. {
  71. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  72. $bytes = max($bytes, 0);
  73. $pow = floor(($bytes ? log($bytes) : 0) / log(KB));
  74. $pow = min($pow, count($units) - 1);
  75. $bytes /= KB ** $pow;
  76. return round($bytes, $precision) . ' ' . $units[$pow];
  77. }
  78. }
  79. // 秒转时间
  80. if ( ! function_exists('seconds2time')) {
  81. function seconds2time($seconds)
  82. {
  83. $day = floor($seconds / Day);
  84. $hour = floor(($seconds % Day) / Hour);
  85. $minute = floor((($seconds % Day) % Hour) / Minute);
  86. if ($day > 0) {
  87. return $day . '天' . $hour . '小时' . $minute . '分';
  88. }
  89. if ($hour != 0) {
  90. return $hour . '小时' . $minute . '分';
  91. }
  92. return $minute . '分';
  93. }
  94. }
  95. // 过滤emoji表情
  96. if ( ! function_exists('filterEmoji')) {
  97. function filterEmoji($str)
  98. {
  99. $str = preg_replace_callback(
  100. '/./u',
  101. static function (array $match) {
  102. return strlen($match[0]) >= 4 ? '' : $match[0];
  103. },
  104. $str
  105. );
  106. return $str;
  107. }
  108. }
  109. // 获取系统设置
  110. if ( ! function_exists('sysConfig')) {
  111. function sysConfig($name)
  112. {
  113. $ret = Cache::tags('sysConfig')->get($name);
  114. if (is_null($ret)) {
  115. return Helpers::cacheSysConfig($name);
  116. }
  117. return $ret;
  118. }
  119. }