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.
238 lines
7.3 KiB
238 lines
7.3 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Models\AdminChannelRole;
|
|
use App\Models\ParkingChannel;
|
|
use App\Services\AdminChannelRoleService;
|
|
use App\Services\AdminTranslationService;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\ParkingAdminUserService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ChannelPermissionsController extends BaseController
|
|
{
|
|
protected string $menuUri = 'channelPermissions';
|
|
/**
|
|
* @var AdminChannelRoleService
|
|
*/
|
|
protected AdminChannelRoleService $service;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param AdminChannelRoleService $service
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
AdminChannelRoleService $service
|
|
) {
|
|
parent::__construct($responseService);
|
|
$this->service = $service;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = AdminChannelRole::query();
|
|
|
|
$memberTypeArr = ParkingAdminUserService::$memberType;
|
|
$statusArr = AdminChannelRoleService::getStatus();
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
$total = $query->count();
|
|
$items = $query->latest()->forPage($page, $perPage)->get()->each(
|
|
function ($item) use ($memberTypeArr, $statusArr) {
|
|
$item['channel_list'] = ParkingChannel::getChannelData(
|
|
$item['channel_ids']
|
|
);
|
|
$item['member_type_str']
|
|
= $memberTypeArr[$item['member_type']];
|
|
$item['status_str'] = $statusArr[$item['status']];
|
|
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()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return JsonResponse
|
|
*/
|
|
public function create(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'member_type_list' => get_select_data(
|
|
ParkingAdminUserService::$memberType
|
|
),
|
|
'channel_list' => ParkingChannel::getData(),
|
|
'status_list' => get_select_data(
|
|
AdminChannelRoleService::getStatus()
|
|
)
|
|
];
|
|
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->service->createModel($request->all());
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.save_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('admin.operation_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @return void
|
|
* @throws ValidationException
|
|
*/
|
|
protected function saveValidator(array $data, int $id = 0): void
|
|
{
|
|
$rules = [
|
|
'member_type' => 'required',
|
|
'channel_ids' => 'required|array'
|
|
];
|
|
$messages = [
|
|
'member_type.required' => __(
|
|
'validation.channel_permissions.m_empty'
|
|
),
|
|
'channel_ids.required' => __(
|
|
'validation.channel_permissions.c_empty'
|
|
),
|
|
'channel_ids.array' => __(
|
|
'validation.channel_permissions.c_array'
|
|
)
|
|
];
|
|
if ($id) {
|
|
$this->validateId($id, AdminChannelRole::class);
|
|
}
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
*/
|
|
public function edit(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, AdminChannelRole::class);
|
|
$data = [
|
|
'member_type_list' => get_select_data(
|
|
ParkingAdminUserService::$memberType
|
|
),
|
|
'channel_list' => ParkingChannel::getData(),
|
|
'status_list' => get_select_data(
|
|
AdminChannelRoleService::getStatus()
|
|
),
|
|
'item' => AdminChannelRole::query()->find($id)
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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->service->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(
|
|
__('admin.operation_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, AdminChannelRole::class);
|
|
$this->service->deleteModel($id);
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.delete_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('admin.operation_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|