helpers.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // 秒转时间
  50. if (! function_exists('seconds2time')) {
  51. function seconds2time($seconds)
  52. {
  53. $day = floor($seconds / Day);
  54. $hour = floor(($seconds % Day) / Hour);
  55. $minute = floor((($seconds % Day) % Hour) / Minute);
  56. if ($day > 0) {
  57. return $day.trans('validation.attributes.day').$hour.trans('validation.attributes.hour').$minute.trans('validation.attributes.minute');
  58. }
  59. if ($hour != 0) {
  60. return $hour.trans('validation.attributes.hour').$minute.trans('validation.attributes.minute');
  61. }
  62. return $minute.trans('validation.attributes.minute');
  63. }
  64. }
  65. // 过滤emoji表情
  66. if (! function_exists('filterEmoji')) {
  67. function filterEmoji($str)
  68. {
  69. return preg_replace_callback('/./u', static function (array $match) {
  70. return strlen($match[0]) >= 4 ? '' : $match[0];
  71. }, $str);
  72. }
  73. }
  74. // 获取系统设置
  75. if (! function_exists('sysConfig')) {
  76. function sysConfig($name)
  77. {
  78. $ret = Cache::tags('sysConfig')->get($name);
  79. if (is_null($ret)) {
  80. return Helpers::cacheSysConfig($name);
  81. }
  82. return $ret;
  83. }
  84. }