Ver código fonte

添加缓存与同步缓存 系统管理 日志

ANG YU 5 anos atrás
pai
commit
511be1fd17

+ 44 - 0
app/Console/Commands/SyncLogCacheTask.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Log;
+use App\Services\LogService;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class SyncLogCacheTask extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'syncLogCacheTask';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     */
+    public function handle()
+    {
+        LogService::syncRedisLogs();
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -18,6 +18,7 @@ class Kernel extends ConsoleKernel
         \App\Console\Commands\FluxOrderFix::class,
         \App\Console\Commands\InventoryDailyLoggingOwner::class,
         \App\Console\Commands\WASSyncWMSOrderInformation::class,
+        \App\Console\Commands\SyncLogCacheTask::class,
     ];
 
     /**
@@ -33,6 +34,7 @@ class Kernel extends ConsoleKernel
         $schedule->command('FluxOrderFix')->hourlyAt(1);
 //        $schedule->command('FluxOrderFix')->cron('* * * * *');
         $schedule->command('WASSyncWMSOrderInformation')->everyMinute();
+        $schedule->command('syncLogCacheTask')->everyMinute();
     }
 
     /**

+ 3 - 19
app/Http/Controllers/Controller.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Log;
+use App\Services\LogService;
 use App\User;
 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Routing\Controller as BaseController;
@@ -17,27 +18,10 @@ class Controller extends BaseController
 {
     use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
     static public function logS($method,$type,$description,$id_user=null){
-        if(!$id_user){
-            $id_user = '';
-            $user=auth()->user();
-            if($user) $id_user = $user['id'];
-        }
-        (new Log([
-            'operation'=>$method,
-            'type'=>$type,
-            'description'=>$description,
-            'id_user'=>$id_user,
-            'ip'=>Request::ip()
-        ]))->save();
+        LogService::log($method,$type,$description,$id_user=null);
     }
     public function log($method,$type,$description,$id_user=null){
-        (new Log([
-            'operation'=>$method,
-            'type'=>$type,
-            'description'=>$description,
-            'id_user'=>$id_user,
-            'ip'=>Request::ip()
-        ]))->save();
+        LogService::log($method,$type,$description,$id_user);
     }
     public function apiError($method,$description){
         $this->log($method,'apiError',$description);

+ 26 - 6
app/Http/Controllers/LogController.php

@@ -3,8 +3,10 @@
 namespace App\Http\Controllers;
 
 use App\Log;
+use App\Services\LogService;
 use Exception;
 use Illuminate\Contracts\Foundation\Application;
+use Illuminate\Contracts\Pagination\LengthAwarePaginator;
 use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
@@ -20,16 +22,28 @@ class LogController extends Controller
      * Display a listing of the resource.
      *
      * @param Request $request
-     * @return Response
+     * @return LengthAwarePaginator
      */
     public function index(Request $request)
     {
         if (!Gate::allows('日志-查询')) {
             return redirect(url('/'));
         }
+        //没有查询条件,默认展示最近50条
+        if (!$request->has('created_at_start') &&
+            !$request->has('created_at_end') &&
+            !$request->has('operation') &&
+            !$request->has('type') &&
+            !$request->has('description')
+        ) {
+            $logs = Log::query()
+                ->orderBy('id', 'desc')
+                ->paginate(50);
+            return view('maintenance.log.index', ['logs' => $logs]);
+        }
         //不传开始时间提示错误信息并返回
-        if(!$request->has('created_at_start')){
-            session()->flash('warning','请选择开始时间');
+        if (!$request->has('created_at_start')) {
+            session()->flash('warning', '请选择开始时间');
             return view('maintenance.log.index', ['logs' => null]);
         }
         $query = Log::query()
@@ -41,9 +55,9 @@ class LogController extends Controller
             $query->where('type', 'like', $request->type . '%');
         }
         if ($request->has('description')) {
-            $str=$request->description;
-            $str = trim($str,'\\');
-            $str = str_replace('\\','\\\\',$str);
+            $str = $request->description;
+            $str = trim($str, '\\');
+            $str = str_replace('\\', '\\\\', $str);
             $query->where('description', 'like', '%' . $str . '%');
         }
         if ($request->has('created_at_start')) {
@@ -83,4 +97,10 @@ class LogController extends Controller
 //        $re=$log->delete();
 //        return ['success'=>$re];
     }
+
+    public function syncRedisLogs()
+    {
+        LogService::syncRedisLogs();
+        return redirect('maintenance/log');
+    }
 }

+ 1 - 1
app/Log.php

@@ -9,7 +9,7 @@ class Log extends Model
 {
     use ModelTimeFormat;
     protected $fillable = [
-        'operation', 'description','type', 'operator', 'ip','id_user',
+        'operation', 'description','type', 'operator', 'ip','id_user','created_at','updated_at'
     ];
     public function getUserNameAttribute(){
         $idUser=$this['id_user'];

+ 37 - 3
app/Services/LogService.php

@@ -5,6 +5,7 @@ namespace App\Services;
 
 
 use App\Log;
+use Illuminate\Support\Facades\Redis;
 use Illuminate\Support\Facades\Request;
 
 class LogService
@@ -15,12 +16,45 @@ class LogService
             $user=auth()->user();
             if($user) $id_user = $user['id'];
         }
-        (new Log([
+        $date = date('Y-m-d H:i:s');
+        $log = new Log([
             'operation'=>$method,
             'type'=>$type,
             'description'=>$description,
             'id_user'=>$id_user,
-            'ip'=>Request::ip()
-        ]))->save();
+            'ip'=>Request::ip(),
+            'created_at' => $date,
+            'updated_at' => $date
+        ]);
+
+        Redis::LPUSH('LOGS', $log);
+    }
+
+    public static function syncRedisLogs()
+    {
+        $data = [];
+        $length = 0;
+        while (Redis::LLEN('LOGS') > 0) {
+            $log = Redis::LPOP('LOGS');
+            $arr = json_decode($log);
+            if ($length + strlen($arr->description) > 1024 * 512) {
+                Log::query()->insert($data);
+                $length = 0;
+                $data = [];
+            }
+            $length = $length + strlen($arr->description);
+            $data[] = [
+                'operation' => $arr->operation,
+                'id_user' => $arr->id_user,
+                'ip' => $arr->ip,
+                'type' => $arr->type,
+                'description' => $arr->description,
+                'created_at' => $arr->created_at,
+                'updated_at' => $arr->updated_at,
+            ];
+        }
+        if ($data) {
+            Log::query()->insert($data);
+        }
     }
 }

+ 1 - 0
resources/views/maintenance/log/index.blade.php

@@ -7,6 +7,7 @@
         @component('maintenance.log.menu')@endcomponent
     </span>
     <div class="container-fluid">
+        <a class="btn btn-outline-dark"  href="{{url('maintenance/syncRedisLogs')}}">同步日志</a>
         <div class="card">
             <div id="form_div"></div>
             <div class="card-body">

+ 1 - 0
routes/web.php

@@ -78,6 +78,7 @@ Route::group(['prefix'=>'maintenance'],function(){
         Route::get('cities/{province_id}','WaybillPriceModelsController@getCities');
     });
 
+    Route::get('syncRedisLogs','LogController@syncRedisLogs');
     Route::resource('log', 'LogController');
     Route::resource('user', 'UserController');
     Route::resource('role', 'RoleController');