停车场管理系统
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.
 
 

254 lines
7.3 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\AdminRoleMenu;
use App\Models\AdminRoles;
use App\Services\AdminMenuService;
use App\Services\ApiResponseService;
use App\Services\AdminRolesService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Psr\SimpleCache\InvalidArgumentException;
class RolesController extends BaseController
{
/**
* @var ApiResponseService
*/
protected ApiResponseService $responseService;
/**
* @var AdminRolesService
*/
protected AdminRolesService $AdminRolesService;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param AdminRolesService $AdminRolesService
*/
public function __construct(
ApiResponseService $responseService,
AdminRolesService $AdminRolesService,
) {
$this->responseService = $responseService;
$this->AdminRolesService = $AdminRolesService;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
{
try {
$query = AdminRoles::query();
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->get();
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()
);
}
}
/**
* Show the form for creating a new resource.
*/
public function create(): JsonResponse
{
try {
$data = [
'menu_list' => (new AdminMenuService())->getMenuTreeCacheList()
];
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @param Request $request
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function store(Request $request): JsonResponse
{
try {
$this->saveValidator($request->all());
$this->AdminRolesService->createModel($request->all());
return $this->responseService->success(
null,
__('admin.save_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_role.create_failed') . ':' . $e->getMessage(
)
);
}
}
/**
* @param array $data
* @param int $id
* @return void
* @throws ValidationException
*/
private function saveValidator(array $data, int $id = 0): void
{
$rules = [
'name' => 'required|max:50',
'menu_ids' => 'required|array',
'remark' => 'max:50'
];
$messages = [
'name.required' => __('validation.admin_role.n_empty'),
'name.max' => __('validation.admin_role.n_max'),
'menu_ids.required' => __('validation.admin_role.m_empty'),
'menu_ids.array' => __('validation.admin_role.m_array'),
'remark.max' => __('validation.admin_role.r_max'),
];
if ($id) {
$this->validateId($id, AdminRoles::class);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
}
/**
* @param string $id
* @return JsonResponse
*/
public function show(string $id): JsonResponse
{
return $this->extracted($id);
}
/**
* @param string $id
* @return JsonResponse
*/
protected function extracted(string $id): JsonResponse
{
try {
$this->validateId($id, AdminRoles::class);
$data = [
'menu_list' => (new AdminMenuService())->getMenuTreeCacheList(
AdminRoleMenu::getMenuIdArr($id)
),
'item' => AdminRoles::query()
->where('id', $id)
->get()
->toArray()
];
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
*/
public function edit(string $id): JsonResponse
{
return $this->extracted($id);
}
/**
* @param Request $request
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function update(Request $request, string $id): JsonResponse
{
try {
$this->saveValidator($request->all(), $id);
$this->AdminRolesService->updateModel($request->all(), $id);
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_role.update_failed') . ':' . $e->getMessage(
)
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, AdminRoles::class);
$this->AdminRolesService->deleteModel($id);
return $this->responseService->success(
null,
__('admin.delete_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_role.destroy_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @return JsonResponse
* @throws InvalidArgumentException
*/
public function rule(): JsonResponse
{
try {
return $this->responseService->success($this->methodShow('roles'));
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
}