Helper.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. if (! function_exists('success')) {
  3. function success($data = [], $message = 'success', $code = 1)
  4. {
  5. return response()->json([
  6. 'code' => $code,
  7. 'msg' => $message,
  8. 'data' => $data,
  9. ],200);
  10. }
  11. }
  12. if (! function_exists('error')) {
  13. function error($message = 'error', $code = 0)
  14. {
  15. return response()->json([
  16. 'code' => $code,
  17. 'msg' => $message,
  18. ],400);
  19. }
  20. }
  21. if (!function_exists('_microtime')) {
  22. function _microtime($length = 13)
  23. {
  24. $val = pow(10, $length - 10);
  25. list($t1, $t2) = explode(' ', microtime());
  26. return (int)sprintf('%.0f', (floatval($t1) + floatval($t2)) * $val);
  27. }
  28. }
  29. if (!function_exists('params')) {
  30. function params(): array
  31. {
  32. return request()->all();
  33. }
  34. }
  35. if (!function_exists('_header')) {
  36. function _header($key)
  37. {
  38. return request()->header($key);
  39. }
  40. }
  41. if (!function_exists('ip')) {
  42. function ip()
  43. {
  44. if (isset($_SERVER)) {
  45. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  46. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  47. foreach ($arr as $ip) {
  48. $ip = trim($ip);
  49. if ($ip != 'unknown') {
  50. $realip = $ip;
  51. break;
  52. }
  53. }
  54. } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  55. $realip = $_SERVER['HTTP_CLIENT_IP'];
  56. } else if (isset($_SERVER['REMOTE_ADDR'])) {
  57. $realip = $_SERVER['REMOTE_ADDR'];
  58. } else {
  59. $realip = '0.0.0.0';
  60. }
  61. } else if (getenv('HTTP_X_FORWARDED_FOR')) {
  62. $realip = getenv('HTTP_X_FORWARDED_FOR');
  63. } else if (getenv('HTTP_CLIENT_IP')) {
  64. $realip = getenv('HTTP_CLIENT_IP');
  65. } else {
  66. $realip = getenv('REMOTE_ADDR');
  67. }
  68. preg_match('/[\\d\\.]{7,15}/', $realip, $onlineip);
  69. return (!empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0');
  70. }
  71. }
  72. if (!function_exists('ua')) {
  73. function ua(): string
  74. {
  75. return strtolower($_SERVER['HTTP_USER_AGENT']);
  76. }
  77. }