16 changed files with 542 additions and 14 deletions
@ -0,0 +1,238 @@ |
|||||
|
<?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() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
use Illuminate\Database\Eloquent\SoftDeletes; |
||||
|
|
||||
|
class AdminChannelRole extends Model |
||||
|
{ |
||||
|
use HasFactory, SoftDeletes; |
||||
|
|
||||
|
protected $table = 'admin_channel_role'; |
||||
|
|
||||
|
protected $fillable |
||||
|
= [ |
||||
|
'member_type', |
||||
|
'status', |
||||
|
'describe', |
||||
|
'channel_ids' |
||||
|
]; |
||||
|
|
||||
|
protected $hidden |
||||
|
= [ |
||||
|
'created_at', |
||||
|
'updated_at', |
||||
|
'deleted_at' |
||||
|
]; |
||||
|
|
||||
|
public function getChannelIdsAttribute($value): array |
||||
|
{ |
||||
|
return $value ? explode(',', $value) : []; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,141 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Services; |
||||
|
|
||||
|
use App\Models\AdminChannelRole; |
||||
|
use Exception; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
class AdminChannelRoleService extends BaseService |
||||
|
{ |
||||
|
|
||||
|
protected string $menuTitle = 'channel_permissions'; |
||||
|
|
||||
|
private static array $statusArr = ['disabled', 'enable']; |
||||
|
|
||||
|
/** |
||||
|
* @return array|string[] |
||||
|
*/ |
||||
|
public static function getStatus(): array |
||||
|
{ |
||||
|
$statusArr = self::$statusArr; |
||||
|
foreach ($statusArr as $key => $value) { |
||||
|
$statusArr[$key] = __('admin.' . $value); |
||||
|
} |
||||
|
return $statusArr; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function createModel(array $data) |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
$existsWhere = [ |
||||
|
['member_type', '=', $data['member_type']] |
||||
|
]; |
||||
|
if (AdminChannelRole::query()->where($existsWhere)->exists() |
||||
|
) { |
||||
|
throw new Exception( |
||||
|
__('service.' . $this->menuTitle . '.type_exists') |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
$channel_ids = implode(',', $data['channel_ids']); |
||||
|
$model = AdminChannelRole::query()->create([ |
||||
|
'member_type' => $data['member_type'], |
||||
|
'status' => $data['status'] ?? 1, |
||||
|
'describe' => $data['describe'] ?? '', |
||||
|
'channel_ids' => $channel_ids ?? '', |
||||
|
'created_at' => get_datetime() |
||||
|
]); |
||||
|
|
||||
|
$this->logService->logCreated( |
||||
|
$model, |
||||
|
$this->menuTitle . '.create' |
||||
|
); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return $model; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @param int $id |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function updateModel(array $data, int $id) |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
// 验证 |
||||
|
$existsWhere = [ |
||||
|
['member_type', '=', $data['member_type']], |
||||
|
['id', '<>', $id] |
||||
|
]; |
||||
|
if (AdminChannelRole::query()->where($existsWhere)->exists() |
||||
|
) { |
||||
|
throw new Exception( |
||||
|
__('service.' . $this->menuTitle . '.reason_exists') |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
// 更新 |
||||
|
$model = AdminChannelRole::query()->findOrFail($id); |
||||
|
$oldValues = $model->toArray(); |
||||
|
|
||||
|
$channel_ids = implode(',', $data['channel_ids']); |
||||
|
$model->update([ |
||||
|
'member_type' => $data['member_type'], |
||||
|
'status' => $data['status'] ?? 1, |
||||
|
'describe' => $data['describe'] ?? '', |
||||
|
'channel_ids' => $channel_ids ?? '', |
||||
|
'updated_at' => get_datetime() |
||||
|
]); |
||||
|
|
||||
|
$this->logService->logUpdated( |
||||
|
$model, |
||||
|
$oldValues, |
||||
|
$this->menuTitle . '.update' |
||||
|
); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return $model; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $id |
||||
|
* @return bool |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function deleteModel($id): bool |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
$model = AdminChannelRole::query()->findOrFail($id); |
||||
|
|
||||
|
$this->logService->logDeleted($model, $this->menuTitle . '.delete'); |
||||
|
|
||||
|
$model->delete(); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return true; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
|
||||
|
return new class extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
*/ |
||||
|
public function up(): void |
||||
|
{ |
||||
|
Schema::create('admin_channel_role', function (Blueprint $table) { |
||||
|
$table->id(); |
||||
|
$table->tinyInteger('member_type')->default(1)->comment('会员类型'); |
||||
|
$table->tinyInteger('status')->default(1)->comment('状态'); |
||||
|
$table->string('describe', 255)->default('')->comment('描述'); |
||||
|
$table->text('channel_ids')->comment('允许通道'); |
||||
|
$table->timestamps(); |
||||
|
$table->softDeletes(); |
||||
|
$table->innoDb(); |
||||
|
$table->comment('通道权限'); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
*/ |
||||
|
public function down(): void |
||||
|
{ |
||||
|
Schema::dropIfExists('admin_channel_role'); |
||||
|
} |
||||
|
}; |
||||
Loading…
Reference in new issue