helpers.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  80. // 字段加密
  81. if (! function_exists('string_encrypt')) {
  82. function string_encrypt(string $data): string
  83. {
  84. return base64url_encode(openssl_encrypt($data, 'aes-128-ctr', hash('sha256', config('app.key')), OPENSSL_RAW_DATA, substr(sha1(config('app.key')), 0, 16)));
  85. }
  86. }
  87. // 字段解密
  88. if (! function_exists('string_decrypt')) {
  89. function string_decrypt(string $data): string
  90. {
  91. return openssl_decrypt(base64url_decode($data), 'aes-128-ctr', hash('sha256', config('app.key')), OPENSSL_RAW_DATA, substr(sha1(config('app.key')), 0, 16));
  92. }
  93. }