12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /*
- * This file is part of the ZhMead/laravel-logger.
- *
- * (c) Mead <751066209@qql.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Jobs;
- use App\Repositories\Models\Base\Permission;
- use Carbon\Carbon;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- class LogJob implements ShouldQueue
- {
- use InteractsWithQueue;
- use Queueable;
- public $tries = 1;
- public $timeout = 60;
- private $context;
- private $message;
- private $serverData;
- public function __construct(string $message, array $context = null, array $serverData = null)
- {
- $this->message = $message;
- $this->context = $context;
- $this->serverData = $serverData;
- }
- public function handle()
- {
- $message = $this->message;
- $context = $this->context;
- $serverData = $this->serverData;
- // if (strtolower($serverData['REQUEST_METHOD']) == 'get') {
- // return;
- // }
- $user = false;
- if ($context['user'] && is_array($context['user'])) {
- $user = $context['user'];
- }
- $info = Permission::byUrlGetDescription(parse_url($serverData['REQUEST_URI'])['path'], $serverData['REQUEST_METHOD']);
- $description = '未知';
- if ($info) {
- $description = $info['module'] . '-' . $info['nickname'];
- }
- //获取登录地址
- $ip = getClientIp();
- $address = '';
- // if ($ip == '127.0.0.1') return;
- // $address = Cache::remember($ip, Carbon::now()->addHours(4), function () use ($ip) {
- // return get_ip_city($ip);
- // });
- \App\Repositories\Models\Base\Log::query()->create([
- 'description' => $description,
- 'url' => $serverData['REQUEST_URI'],
- 'log_type' => $message,
- 'method' => $serverData['REQUEST_METHOD'],
- 'params' => $context['request'],
- 'request_ip' => $ip,
- 'duration' => $context['duration'],
- 'admin_id' => $user ? $user['id'] : 0,
- 'username' => $user ? $user['username'] : '',
- 'browser' => isset($serverData['HTTP_USER_AGENT']) ? $serverData['HTTP_USER_AGENT'] : '',
- 'detail' => $context,
- 'address' => $address,
- ]);
- }
- }
|