diff --git a/app/Enums/OperationType.php b/app/Enums/OperationType.php new file mode 100644 index 0000000..f61d6e9 --- /dev/null +++ b/app/Enums/OperationType.php @@ -0,0 +1,19 @@ +value => '创建', + static::Update->value => '修改', + static::Delete->value => '删除', + ]; + } +} diff --git a/app/Http/Controllers/AdminRoleController.php b/app/Http/Controllers/AdminRoleController.php index fdf151a..2fb04c7 100644 --- a/app/Http/Controllers/AdminRoleController.php +++ b/app/Http/Controllers/AdminRoleController.php @@ -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('删除成功!'); } } diff --git a/app/Models/OperationLog.php b/app/Models/OperationLog.php new file mode 100644 index 0000000..f238d9f --- /dev/null +++ b/app/Models/OperationLog.php @@ -0,0 +1,23 @@ + OperationType::class, + 'input_data' => 'array' + ]; + + protected $fillable = [ + 'user_id', 'user_name', 'data_type', 'data_id', + 'message', + 'type', 'input_data' + ]; +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 5e2c658..ab2bcb9 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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, + // ]); } } diff --git a/app/Services/OperationLogService.php b/app/Services/OperationLogService.php new file mode 100644 index 0000000..6bb75f2 --- /dev/null +++ b/app/Services/OperationLogService.php @@ -0,0 +1,30 @@ +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); + } +} \ No newline at end of file diff --git a/database/migrations/2022_11_14_104051_create_operation_logs_table.php b/database/migrations/2022_11_14_104051_create_operation_logs_table.php new file mode 100644 index 0000000..45cffab --- /dev/null +++ b/database/migrations/2022_11_14_104051_create_operation_logs_table.php @@ -0,0 +1,39 @@ +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'); + } +}; diff --git a/lang/zh_CN/models.php b/lang/zh_CN/models.php new file mode 100644 index 0000000..f6356b5 --- /dev/null +++ b/lang/zh_CN/models.php @@ -0,0 +1,25 @@ + "角色", + AdminUser::class => "用户", + AgriculturalBase::class => "区域", + Crop::class => "农作物", + CropYield::class => "农作物产量", + Device::class => "设备", + FriendLink::class => "友情链接", + RiceShrimpPrice::class => "稻虾价格", + RiceShrimpFlow::class => "稻虾流向", + RiceShrimpIndustry::class => "稻虾产业", +]; \ No newline at end of file