LogJob.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the ZhMead/laravel-logger.
  4. *
  5. * (c) Mead <751066209@qql.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs;
  11. use App\Repositories\Models\Base\Permission;
  12. use Carbon\Carbon;
  13. use Illuminate\Bus\Queueable;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\Log;
  18. class LogJob implements ShouldQueue
  19. {
  20. use InteractsWithQueue;
  21. use Queueable;
  22. public $tries = 1;
  23. public $timeout = 60;
  24. private $context;
  25. private $message;
  26. private $serverData;
  27. public function __construct(string $message, array $context = null, array $serverData = null)
  28. {
  29. $this->message = $message;
  30. $this->context = $context;
  31. $this->serverData = $serverData;
  32. }
  33. public function handle()
  34. {
  35. $message = $this->message;
  36. $context = $this->context;
  37. $serverData = $this->serverData;
  38. // if (strtolower($serverData['REQUEST_METHOD']) == 'get') {
  39. // return;
  40. // }
  41. $user = false;
  42. if ($context['user'] && is_array($context['user'])) {
  43. $user = $context['user'];
  44. }
  45. $info = Permission::byUrlGetDescription(parse_url($serverData['REQUEST_URI'])['path'], $serverData['REQUEST_METHOD']);
  46. $description = '未知';
  47. if ($info) {
  48. $description = $info['module'] . '-' . $info['nickname'];
  49. }
  50. //获取登录地址
  51. $ip = getClientIp();
  52. $address = '';
  53. // if ($ip == '127.0.0.1') return;
  54. // $address = Cache::remember($ip, Carbon::now()->addHours(4), function () use ($ip) {
  55. // return get_ip_city($ip);
  56. // });
  57. \App\Repositories\Models\Base\Log::query()->create([
  58. 'description' => $description,
  59. 'url' => $serverData['REQUEST_URI'],
  60. 'log_type' => $message,
  61. 'method' => $serverData['REQUEST_METHOD'],
  62. 'params' => $context['request'],
  63. 'request_ip' => $ip,
  64. 'duration' => $context['duration'],
  65. 'admin_id' => $user ? $user['id'] : 0,
  66. 'username' => $user ? $user['username'] : '',
  67. 'browser' => isset($serverData['HTTP_USER_AGENT']) ? $serverData['HTTP_USER_AGENT'] : '',
  68. 'detail' => $context,
  69. 'address' => $address,
  70. ]);
  71. }
  72. }