6 changed files with 162 additions and 0 deletions
@ -0,0 +1,110 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers\Admin; |
|||
|
|||
use App\Http\Controllers\Controller; |
|||
use App\Models\AdminOperationLog; |
|||
use App\Models\AdminUsers; |
|||
use App\Services\ApiResponseService; |
|||
use Exception; |
|||
use Illuminate\Http\JsonResponse; |
|||
use Illuminate\Http\Request; |
|||
|
|||
class OperationLog extends Controller |
|||
{ |
|||
/** |
|||
* @var ApiResponseService |
|||
*/ |
|||
protected ApiResponseService $responseService; |
|||
|
|||
/** |
|||
* 构造函数 |
|||
* @param ApiResponseService $responseService |
|||
*/ |
|||
public function __construct( |
|||
ApiResponseService $responseService, |
|||
) { |
|||
$this->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() |
|||
); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue