You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.6 KiB
170 lines
5.6 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminMenu;
|
|
use App\Models\AdminOperationLog;
|
|
use App\Models\AdminUsers;
|
|
use App\Services\AdminMenuService;
|
|
use App\Services\ApiResponseService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class OperationLogController extends Controller
|
|
{
|
|
|
|
protected int $adminUserId;
|
|
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
) {
|
|
$this->responseService = $responseService;
|
|
$adminUser = Auth::guard('sanctum')->user();
|
|
$this->adminUserId = $adminUser['id'] ?? 0;
|
|
}
|
|
|
|
public function search(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'type_data' => get_select_data(
|
|
AdminOperationLog::getActionArr(),
|
|
true
|
|
),
|
|
'main_directory_list' => AdminMenuService::getMenuList(
|
|
$this->adminUserId
|
|
)
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return JsonResponse
|
|
*/
|
|
public function getSubDirectoryList($id): JsonResponse
|
|
{
|
|
try {
|
|
$data = AdminMenuService::getMenuList($this->adminUserId, $id);
|
|
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');
|
|
if (!empty($type)) {
|
|
$query->where('action', '=', $type);
|
|
}
|
|
}
|
|
|
|
if ($request->has('name')) {
|
|
$name = $request->input('name');
|
|
if (!empty($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');
|
|
if (!empty($start_time)) {
|
|
$query->where('created_at', '>=', $start_time);
|
|
}
|
|
}
|
|
|
|
if ($request->has('end_time')) {
|
|
$end_time = $request->input('end_time');
|
|
if (!empty($end_time)) {
|
|
$query->where('created_at', '<=', $end_time);
|
|
}
|
|
}
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$total = $query->count();
|
|
$field = [
|
|
'id', 'user_id', 'action', 'description', 'sub_directory',
|
|
'created_at'
|
|
];
|
|
$items = $query->latest()->forPage($page, $perPage)->select($field)
|
|
->get()->each(
|
|
function ($item) {
|
|
$actionArr = AdminOperationLog::getActionArr();
|
|
$username = AdminUsers::getUsername($item['user_id']);
|
|
$item['operation_name'] = $username ?? '';
|
|
$item['action_str'] = $actionArr[$item['action']] ??
|
|
$item['action'];
|
|
$description = __('log.' . $item['description']);
|
|
if (strpos($description, 'log.') === false) {
|
|
$item['description'] = $description;
|
|
}
|
|
$item['main_directory'] = '';
|
|
if ($item['sub_directory']) {
|
|
$item['main_directory']
|
|
= AdminMenu::getLastParentTitle(
|
|
$item['sub_directory']
|
|
);
|
|
$item['sub_directory'] = AdminMenu::getParentTitle(
|
|
$item['sub_directory']
|
|
);
|
|
} else {
|
|
$item['sub_directory'] = '';
|
|
}
|
|
unset($item['user_id']);
|
|
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()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|