添加角色操作日志模块
parent
6227b790a0
commit
f08ae4e411
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum OperationType: string
|
||||
{
|
||||
case Create = 'create';
|
||||
case Update = 'update';
|
||||
case Delete = 'delete';
|
||||
|
||||
public static function types(): array
|
||||
{
|
||||
return [
|
||||
static::Create->value => '创建',
|
||||
static::Update->value => '修改',
|
||||
static::Delete->value => '删除',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -5,8 +5,8 @@ namespace App\Http\Controllers;
|
|||
use App\Helpers\Paginator;
|
||||
use App\Http\Requestes\AdminRoleRequest;
|
||||
use App\Http\Resources\AdminRoleResource;
|
||||
// use App\Services\OperationLogService;
|
||||
// use App\Enums\OperationType;
|
||||
use App\Services\OperationLogService;
|
||||
use App\Enums\OperationType;
|
||||
use App\Models\AdminRole;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
|
@ -41,7 +41,7 @@ class AdminRoleController extends Controller
|
|||
|
||||
return $this->error('添加失败,请稍后再试');
|
||||
}
|
||||
// (new OperationLogService())->inLog(OperationType::Create, '', $role, $request->input());
|
||||
(new OperationLogService())->inLog(OperationType::Create, '', $role, $request->input());
|
||||
|
||||
return $this->success('添加成功');
|
||||
}
|
||||
|
|
@ -71,10 +71,12 @@ class AdminRoleController extends Controller
|
|||
DB::rollBack();
|
||||
report($th);
|
||||
|
||||
return $this->error('添加失败,请稍后再试');
|
||||
return $this->error('修改失败,请稍后再试');
|
||||
}
|
||||
|
||||
return $this->success('添加成功');
|
||||
(new OperationLogService())->inLog(OperationType::Update, '', $adminRole, $request->input());
|
||||
|
||||
return $this->success('修改成功');
|
||||
}
|
||||
|
||||
public function destroy(AdminRole $adminRole)
|
||||
|
|
@ -91,6 +93,8 @@ class AdminRoleController extends Controller
|
|||
return $this->error('删除失败,请稍后再试');
|
||||
}
|
||||
|
||||
(new OperationLogService())->inLog(OperationType::Delete, '', $adminRole);
|
||||
|
||||
return $this->success('删除成功!');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Enums\OperationType;
|
||||
|
||||
class OperationLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'type' => OperationType::class,
|
||||
'input_data' => 'array'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id', 'user_name', 'data_type', 'data_id',
|
||||
'message',
|
||||
'type', 'input_data'
|
||||
];
|
||||
}
|
||||
|
|
@ -34,18 +34,18 @@ class AppServiceProvider extends ServiceProvider
|
|||
//
|
||||
Schema::defaultStringLength(191);
|
||||
|
||||
Relation::morphMap([
|
||||
'user' => \App\Models\AdminUser::class,
|
||||
'role' => \App\Models\AdminRole::class,
|
||||
'agricultura_base' => \App\Models\AgriculturalBase::class,
|
||||
'crop' => \App\Models\Crop::class,
|
||||
'crop_yield' => \App\Models\CropYield::class,
|
||||
'device' => \App\Models\Device::class,
|
||||
'friend_link' => \App\Models\FriendLink::class,
|
||||
'rice_shrimp_price' => \App\Models\RiceShrimpPrice::class,
|
||||
'rice_shrimp_flow' => \App\Models\RiceShrimpFlow::class,
|
||||
'rice_shrimp_industry' => \App\Models\RiceShrimpIndustry::class,
|
||||
]);
|
||||
// Relation::morphMap([
|
||||
// 'user' => \App\Models\AdminUser::class,
|
||||
// 'role' => \App\Models\AdminRole::class,
|
||||
// 'agricultura_base' => \App\Models\AgriculturalBase::class,
|
||||
// 'crop' => \App\Models\Crop::class,
|
||||
// 'crop_yield' => \App\Models\CropYield::class,
|
||||
// 'device' => \App\Models\Device::class,
|
||||
// 'friend_link' => \App\Models\FriendLink::class,
|
||||
// 'rice_shrimp_price' => \App\Models\RiceShrimpPrice::class,
|
||||
// 'rice_shrimp_flow' => \App\Models\RiceShrimpFlow::class,
|
||||
// 'rice_shrimp_industry' => \App\Models\RiceShrimpIndustry::class,
|
||||
// ]);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\OperationLog;
|
||||
use App\Enums\OperationType;
|
||||
|
||||
class OperationLogService
|
||||
{
|
||||
public function inLog(OperationType $type, ? String $message = '',? Model $model = null, ? Array $parms = null){
|
||||
$user = auth('api')->user();
|
||||
if(!$message){//如果没传message
|
||||
$message = (OperationType::types()[$type->value] ?? '') . __("models.".($model ? $model::class : "")). '【'.($model?->name ?? ($model?->id ?? 0)) .'】';
|
||||
}
|
||||
$data = [
|
||||
'user_id' => $user?->id ?? 0,
|
||||
'user_name' => $user?->name ?? '',
|
||||
'message' => $message,
|
||||
'data_type' => $model ? $model::class : '',
|
||||
'data_id' => $model?->id ?? '',
|
||||
'type' => $type,
|
||||
'input_data' => $parms,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
|
||||
OperationLog::create($data);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('operation_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id')->comment("操作人");
|
||||
$table->string('user_name')->nullable()->comment("名称");
|
||||
$table->string('type')->comment('操作类型');
|
||||
$table->string('message')->nullable()->comment("操作描述");
|
||||
$table->text('input_data')->nullable()->comment('输入数据');
|
||||
$table->string('data_type')->nullable()->comment('数据类型');
|
||||
$table->unsignedBigInteger('data_id')->nullable()->comment('数据id');
|
||||
// $table->text('old_data')->nullable()->comment('老数据');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('operation_logs');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
use App\Models\AdminRole;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\AgriculturalBase;
|
||||
use App\Models\Crop;
|
||||
use App\Models\CropYield;
|
||||
use App\Models\Device;
|
||||
use App\Models\FriendLink;
|
||||
use App\Models\RiceShrimpPrice;
|
||||
use App\Models\RiceShrimpFlow;
|
||||
use App\Models\RiceShrimpIndustry;
|
||||
|
||||
return [
|
||||
AdminRole::class => "角色",
|
||||
AdminUser::class => "用户",
|
||||
AgriculturalBase::class => "区域",
|
||||
Crop::class => "农作物",
|
||||
CropYield::class => "农作物产量",
|
||||
Device::class => "设备",
|
||||
FriendLink::class => "友情链接",
|
||||
RiceShrimpPrice::class => "稻虾价格",
|
||||
RiceShrimpFlow::class => "稻虾流向",
|
||||
RiceShrimpIndustry::class => "稻虾产业",
|
||||
];
|
||||
Loading…
Reference in New Issue