Commit fa7e9203 authored by hj's avatar hj

更新

parent 265b7bc5
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace App\Http\Controllers\Middleware; namespace App\Http\Controllers\Middleware;
use App\Exceptions\ServerRunTimeException; use App\Exceptions\ServerRunTimeException;
use App\Jobs\RequestLogJob;
use App\Services\CustomerService; use App\Services\CustomerService;
use App\Services\ThirdPlatform\ThirdPlatformService; use App\Services\ThirdPlatform\ThirdPlatformService;
use App\Services\ThirdPlatform\ZhenKhService; use App\Services\ThirdPlatform\ZhenKhService;
...@@ -19,6 +20,7 @@ use Closure; ...@@ -19,6 +20,7 @@ use Closure;
use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Auth\Factory as Auth; use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Jiannei\Response\Laravel\Support\Facades\Response; use Jiannei\Response\Laravel\Support\Facades\Response;
use App\Support\Traits\Helpers; use App\Support\Traits\Helpers;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
...@@ -57,28 +59,32 @@ class Requestlogs ...@@ -57,28 +59,32 @@ class Requestlogs
*/ */
public function handle($request, Closure $next, $guard = null) public function handle($request, Closure $next, $guard = null)
{ {
if(!$request->header('authorization')){
$start = $request->request->server('REQUEST_TIME_FLOAT'); return Response::fail('token 不存在!',500,'');
//$end = microtime(true);
$request = $request->request->all();
if ($files = $request->request->allFiles()) {
foreach ($files as $key => $uploadedFile) {
$request[$key] = [
'originalName' => $uploadedFile->getClientOriginalName(),
'mimeType' => $uploadedFile->getClientMimeType(),
];
} }
$token = trim(str_ireplace('bearer', '', $request->header('authorization')));
try{
$decodeToken = $this->decodeToken($token);
if(empty($decodeToken) || (!is_array($decodeToken) && !$decodeToken['hash'])){
return Response::fail('客户token无效',500,'');
} }
}catch(\Throwable $exception){
return Response::fail($exception->getMessage(),500,'');
}
$start = $request->server->get('REQUEST_TIME_FLOAT');
$uniqueId = $request->headers->get('X-Unique-Id') ?: Str::uuid()->toString();
$context = [ $context = [
'request' => $request, 'request' => $request->request->all(),
'account' => auth()->user(), 'uniqueId' => $uniqueId,
'token' => $decodeToken['hash'],
'start' => $start, 'start' => $start,
//'end' => $end,
//'duration' => format_duration($end - $start),
]; ];
$job = new RequestLogJob(\config('logging.request.message'), $context, request()->server());
$job->handle();
return $next($request); return $next($request);
} }
......
<?php
namespace App\Jobs;
use App\Services\SystemJournalService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Monolog\Logger;
use Monolog\Processor\WebProcessor;
use Psr\Log\LoggerInterface;
class RequestLogJob extends Job
{
use InteractsWithQueue;
use Queueable;
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()
{
$logger = null;
if (config('logging.request.enabled')) {
app()->forgetInstance(LoggerInterface::class);
$logger = app(LoggerInterface::class)->getLogger();
if ($logger instanceof Logger) {
$logger->pushProcessor(new WebProcessor($this->serverData));
}
}
try{
app(SystemJournalService::class)->writeToDbLog($this->context,$this->serverData);
if ($logger) {
$logger->debug($this->message, $this->context);
}
}catch(\Throwable $exception){
$this->context['exception'] = $exception;
if ($logger) {
$logger->error($this->message, $this->context);
}
}
}
}
<?php
namespace App\Repositories\Contracts;
use Prettus\Repository\Contracts\RepositoryInterface;
interface SystemJournalRepository extends RepositoryInterface
{
}
<?php
namespace App\Repositories\Eloquent;
use App\Repositories\Contracts\SystemJournalRepository;
use App\Repositories\Criteria\RequestCriteria;
use App\Repositories\BaseRepository;
use App\Repositories\Models\SystemJournal;
class SystemJournalRepositoryEloquent extends BaseRepository implements SystemJournalRepository
{
protected $fieldSearchable = [
];
/**
* Specify Model class name.
*
* @return string
*/
public function model()
{
return SystemJournal::class;
}
/**
* Boot up the repository, pushing criteria.
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
*/
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
public function createJournal($logs)
{
$saveParams = $logs;
$saveParams['created_at'] = date('Y-m-d H:i:s',time());
$saveParams['updated_at'] = date('Y-m-d H:i:s',time());
return $this->create($saveParams);
}
}
<?php
/*
* This file is part of the Jiannei/lumen-api-starter.
*
* (c) Jiannei <longjian.huang@foxmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace App\Repositories\Models;
class SystemJournal extends Model
{
protected $table = 'system_journal';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'uniqueid', 'request_type', 'request_route', 'request_params', 'operation_params', 'token',
'ip', 'duration', 'device_info', 'created_at', 'updated_at'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
];
}
<?php
namespace App\Services;
use App\Repositories\Contracts\SystemJournalRepository;
use App\Repositories\Criteria\SystemJournalCriteria;
use App\Repositories\Transformers\SystemJournalTransformer;
use App\Support\Traits\Helpers;
use Illuminate\Support\Str;
use longlang\phpkafka\Protocol\DescribeLogDirs\DescribableLogDirTopic;
class SystemJournalService
{
use Helpers;
public function __construct(SystemJournalRepository $systemJournalRepository)
{
$this->systemJournalRepository = $systemJournalRepository;
}
public function writeToDbLog(&$context, $serverData)
{
$request = $context;
$dbLog = [];
$dbLog['uniqueid'] = $request['uniqueId'];
$dbLog['request_type'] = $serverData['REQUEST_METHOD'];
$dbLog['request_route'] = Str::replace('?'.$serverData['QUERY_STRING'],'',$serverData['REQUEST_URI']);
$dbLog['request_params'] = json_encode($request['request']);
$dbLog['token'] = $request['token'];
$dbLog['ip'] = $serverData['REMOTE_ADDR'];
$dbLog['device_info'] = $serverData['HTTP_USER_AGENT'];
$context['end'] = microtime(true);
$context['duration'] = format_duration($context['end'] - $context['start']);
$dbLog['duration'] = $context['duration'];
$result = $this->systemJournalRepository->createJournal($dbLog);
return (bool)$result;
}
}
<?php
/*
* This file is part of the Jiannei/lumen-api-starter.
*
* (c) Jiannei <longjian.huang@foxmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class CreatePermissionTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tableNames = config('permission.table_names');
$columnNames = config('permission.column_names');
if (empty($tableNames)) {
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
}
/*
* 权限表
* 所有的菜单、搜索、列表中的操作权限都在这里。
*/
Schema::create($tableNames['permissions'], function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('menu_id');
$table->string('action')->comment('权限的行为');
$table->enum('permission_type',['menu','data','button'])->nullable()->comment('权限类型');
$table->unsignedTinyInteger('sys_default')->default(0)->comment('是否系统默认');
$table->text('remark')->nullable()->comment('备注');
$table->string('guard_name')->comment('权限插件保留字段');
$table->timestamps();
});
$columns = ['name','guard_name'];
Schema::table($tableNames['permissions'],function(Blueprint $table) use ($tableNames,$columns){
DB::statement('ALTER TABLE '.$tableNames['permissions']. ' ROW_FORMAT=DYNAMIC;');
//外键关联menus表id
/*$table->foreign('menu_id')
->references('id')
->on('menus')
->onUpdate('CASCADE')
->onDelete('CASCADE');*/
$table->index($columns);
$table->unique($columns);
});
Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->id();
$table->string('name')->comment('名称');
$table->unsignedTinyInteger('is_default')->default(0)->comment('是否默认');
$table->text('remark')->nullable()->comment('备注');
$table->string('guard_name')->comment('权限插件保留字段');
$table->timestamps();
});
$columns = ['name','guard_name'];
Schema::table($tableNames['roles'],function(Blueprint $table) use ($tableNames,$columns){
DB::statement('ALTER TABLE '.$tableNames['roles']. ' ROW_FORMAT=DYNAMIC;');
$table->index($columns);
$table->unique($columns);
});
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger('permission_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
});
Schema::table($tableNames['model_has_permissions'],function(Blueprint $table) use ($tableNames,$columnNames){
DB::statement('ALTER TABLE '.$tableNames['model_has_permissions']. ' ROW_FORMAT=DYNAMIC;');
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
$table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
});
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger('role_id');
$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
});
Schema::table($tableNames['model_has_roles'],function(Blueprint $table) use ($tableNames,$columnNames){
DB::statement('ALTER TABLE '.$tableNames['model_has_roles']. ' ROW_FORMAT=DYNAMIC;');
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
$table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
});
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')
->references('id')
->on($tableNames['permissions'])
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on($tableNames['roles'])
->onDelete('cascade');
});
Schema::table($tableNames['role_has_permissions'],function(Blueprint $table) use ($tableNames,$columnNames){
DB::statement('ALTER TABLE '.$tableNames['role_has_permissions']. ' ROW_FORMAT=DYNAMIC;');
$table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
;
});
app('cache')
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
->forget(config('permission.cache.key'));
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$tableNames = config('permission.table_names');
if (empty($tableNames)) {
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
}
Schema::drop($tableNames['role_has_permissions']);
Schema::drop($tableNames['model_has_roles']);
Schema::drop($tableNames['model_has_permissions']);
Schema::drop($tableNames['roles']);
Schema::drop($tableNames['permissions']);
}
}
...@@ -13,7 +13,7 @@ class CreateFinanceInvoicesRecordUpdateApiResponseTable extends Migration ...@@ -13,7 +13,7 @@ class CreateFinanceInvoicesRecordUpdateApiResponseTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('finance_invoice_record', function (Blueprint $table) { Schema::table('finance_invoice_record', function (Blueprint $table) {
$table->text('api_response')->nullable()->comment('api返回接口'); $table->text('api_response')->nullable()->comment('api返回接口');
}); });
} }
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSystemJournalTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('system_journal', function (Blueprint $table) {
$table->id();
$table->string('uniqueid')->comment('uniqueid')->nullable(false);
$table->string('request_type')->nullable(false)->comment('请求类型');
$table->string('request_route')->nullable(false)->comment('请求路径');
$table->longText('request_params')->nullable(false)->comment('请求参数');
$table->string('token')->nullable(false)->comment('操作账号');
$table->string('ip')->nullable(false)->comment('ip地址');
$table->string('duration')->nullable(false)->comment('操作时长');
$table->text('device_info')->nullable(false)->comment('设备信息');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('system_journal');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment