diff --git a/app/Http/Controllers/Admin/OperationLog.php b/app/Http/Controllers/Admin/OperationLog.php new file mode 100644 index 0000000..6339843 --- /dev/null +++ b/app/Http/Controllers/Admin/OperationLog.php @@ -0,0 +1,110 @@ +responseService = $responseService; + } + + public function search(): JsonResponse + { + try { + $data = [ + 'type_data' => get_select_data( + AdminOperationLog::getActionArr(), true + ) + ]; + return $this->responseService->success($data); + } catch (Exception $e) { + $m_prefix = __('exception.exception_handler.resource'); + return $this->responseService->systemError( + $m_prefix . ':' . $e->getMessage() + ); + } + } + + /** + * @param Request $request + * @return JsonResponse + */ + public function index(Request $request): JsonResponse + { + try { + $query = AdminOperationLog::query(); + + // 关键词搜索 + if ($request->has('type')) { + $type = $request->input('type'); + $query->where('action', '=', $type); + } + + if ($request->has('name')) { + $name = $request->input('name'); + $user_id = AdminUsers::query()->where('name', $name)->value( + 'id' + ); + $user_id = $user_id ?: 0; + $query->where('user_id', '=', $user_id); + } + + if ($request->has('start_time')) { + $start_time = $request->input('start_time'); + $query->where('created_at', '>=', $start_time); + } + + if ($request->has('end_time')) { + $end_time = $request->input('end_time'); + $query->where('created_at', '<=', $end_time); + } + + // 分页 + $page = $request->input('page', 1); + $perPage = $request->input('per_page', 10); + + $total = $query->count(); + $items = $query->latest()->forPage($page, $perPage)->get()->each( + function ($item) { + $actionArr = AdminOperationLog::getActionArr(); + $item['operation_name'] = $item->user->value('name'); + $item['action'] = $actionArr[$item['action']]; + unset($item['user']); + return $item; + } + ); + + return $this->responseService->success([ + 'items' => $items, + 'total' => $total, + 'page' => $page, + 'per_page' => $perPage, + 'last_page' => ceil($total / $perPage), + ]); + } catch (Exception $e) { + $m_prefix = __('exception.exception_handler.resource'); + return $this->responseService->systemError( + $m_prefix . ':' . $e->getMessage() + ); + } + } +} diff --git a/app/Models/AdminOperationLog.php b/app/Models/AdminOperationLog.php index 6f4eb8f..578d819 100644 --- a/app/Models/AdminOperationLog.php +++ b/app/Models/AdminOperationLog.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use JetBrains\PhpStorm\ArrayShape; class AdminOperationLog extends Model { @@ -36,6 +37,10 @@ class AdminOperationLog extends Model protected $table = 'admin_operation_log'; + protected $hidden = [ + 'updated_at' + ]; + /** * 获取进行此操作的用户 */ @@ -51,4 +56,23 @@ class AdminOperationLog extends Model { return $this->morphTo(); } + + /** + * @return string[] + */ + #[ArrayShape(['login' => "string", + 'logout' => "string", + 'create' => "string", + 'update' => "string", + 'delete' => "string" + ])] public static function getActionArr(): array + { + return [ + 'login' => __('admin.login'), + 'logout' => __('admin.logout'), + 'create' => __('admin.new'), + 'update' => __('admin.edit'), + 'delete' => __('admin.delete') + ]; + } } diff --git a/app/common.php b/app/common.php index c91296c..87f5b25 100644 --- a/app/common.php +++ b/app/common.php @@ -39,3 +39,24 @@ if (!function_exists('get_datetime')) { return date($format, $times); } } + +if (!function_exists('get_select_data')) { + function get_select_data( + array $data = [], + bool $is_all = false, + string $str1 = 'label', + string $str2 = 'value' + ): array { + $newData = []; + if ($is_all) { + $newData[] = [$str1 => __('admin.all'), $str2 => '']; + } + foreach ($data as $key => $value) { + $newData[] = [ + $str1 => $value, + $str2 => $key + ]; + } + return $newData; + } +} diff --git a/database/migrations/2026_02_10_110429_create_admin_configs_table.php b/database/migrations/2026_02_10_110429_create_admin_configs_table.php index e14d738..1825fae 100644 --- a/database/migrations/2026_02_10_110429_create_admin_configs_table.php +++ b/database/migrations/2026_02_10_110429_create_admin_configs_table.php @@ -19,6 +19,7 @@ return new class extends Migration $table->tinyInteger('status')->default(1)->comment('状态:0禁用 1启用'); $table->timestamps(); $table->softDeletes(); + $table->innoDb(); }); } diff --git a/database/migrations/2026_02_10_175744_create_admin_translation_table.php b/database/migrations/2026_02_10_175744_create_admin_translation_table.php index cb9d9e4..7c447c6 100644 --- a/database/migrations/2026_02_10_175744_create_admin_translation_table.php +++ b/database/migrations/2026_02_10_175744_create_admin_translation_table.php @@ -18,6 +18,7 @@ return new class extends Migration $table->string('zh_tw')->comment('中文繁体'); $table->softDeletes(); $table->timestamps(); + $table->innoDb(); }); } diff --git a/routes/admin/api.php b/routes/admin/api.php index c39521b..1e9b6d6 100644 --- a/routes/admin/api.php +++ b/routes/admin/api.php @@ -3,6 +3,7 @@ use App\Http\Controllers\Admin\AuthController; use App\Http\Controllers\Admin\ConfigController; use App\Http\Controllers\Admin\IndexController; +use App\Http\Controllers\Admin\OperationLog; use App\Http\Controllers\Admin\RolesController; use App\Http\Controllers\Admin\TranslationController; use App\Http\Controllers\Admin\UserController; @@ -23,6 +24,10 @@ Route::group(['prefix' => 'admin'], function () { // 首页 Route::get('/index', [IndexController::class, 'index']); Route::get('/menu', [IndexController::class, 'menu']); + // + Route::get('/operationLog/index', [OperationLog::class, 'index']); + Route::get('/operationLog/search', [OperationLog::class, 'search']); + // 系统总配置 Route::get('/config/index', [ConfigController::class, 'index']); Route::put('/config/{id}', [ConfigController::class, 'update']);