272 lines
9.7 KiB
PHP
272 lines
9.7 KiB
PHP
<?php
|
|||
|
|
declare(strict_types=1);
|
||
|
|
/**
|
||
|
|
*+------------------
|
||
|
|
* madong
|
||
|
|
*+------------------
|
||
|
|
* Copyright (c) https://gitee.com/motion-code All rights reserved.
|
||
|
|
*+------------------
|
||
|
|
* Author: Mr. April (405784684@qq.com)
|
||
|
|
*+------------------
|
||
|
|
* Official Website: http://www.madong.tech
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace app\adminapi\controller\gateway;
|
||
|
|
|
||
|
|
use app\adminapi\controller\Crud;
|
||
|
|
use app\adminapi\middleware\AccessTokenMiddleware;
|
||
|
|
use app\adminapi\middleware\OperationMiddleware;
|
||
|
|
use app\adminapi\middleware\PermissionMiddleware;
|
||
|
|
use app\adminapi\schema\request\gateway\RateRestrictionsFormRequest;
|
||
|
|
use app\adminapi\schema\request\gateway\RateRestrictionsQueryRequest;
|
||
|
|
use app\adminapi\schema\response\gateway\RateRestrictionsResponse;
|
||
|
|
use app\adminapi\validate\gateway\RateRestrictionsValidate;
|
||
|
|
use app\schema\request\BatchDeleteRequest;
|
||
|
|
use app\schema\request\ChangeStatusRequest;
|
||
|
|
use app\schema\request\IdRequest;
|
||
|
|
use app\service\admin\gateway\RetaRestrictionsService;
|
||
|
|
use core\exception\handler\AdminException;
|
||
|
|
use core\tool\Json;
|
||
|
|
use madong\swagger\annotation\response\PageResponse;
|
||
|
|
use madong\swagger\annotation\response\SimpleResponse;
|
||
|
|
use madong\swagger\attribute\Permission;
|
||
|
|
use OpenApi\Attributes as OA;
|
||
|
|
use support\Request;
|
||
|
|
use support\annotation\Middleware;
|
||
|
|
use WebmanTech\Swagger\DTO\SchemaConstants;
|
||
|
|
|
||
|
|
#[Middleware(AccessTokenMiddleware::class, PermissionMiddleware::class, OperationMiddleware::class)]
|
||
|
|
final class RateRestrictionsController extends Crud
|
||
|
|
{
|
||
|
|
|
||
|
|
public function __construct(RetaRestrictionsService $service, RateRestrictionsValidate $validate)
|
||
|
|
{
|
||
|
|
$this->service = $service;
|
||
|
|
$this->validate = $validate;
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/gateway/blacklist',
|
||
|
|
summary: '列表',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => RateRestrictionsQueryRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'gateway:blacklist:list')]
|
||
|
|
#[PageResponse(schema: RateRestrictionsResponse::class, example: [])]
|
||
|
|
public function index(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
return parent::index($request); // TODO: Change the autogenerated stub
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/gateway/blacklist/{id}',
|
||
|
|
summary: '详情',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'gateway:blacklist:read')]
|
||
|
|
#[SimpleResponse(schema: RateRestrictionsResponse::class, example: [])]
|
||
|
|
public function show(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
return parent::show($request);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Post(
|
||
|
|
path: '/gateway/blacklist',
|
||
|
|
summary: '保存',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => RateRestrictionsFormRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'gateway:blacklist:create')]
|
||
|
|
#[SimpleResponse(schema: RateRestrictionsResponse::class, example: [])]
|
||
|
|
public function store(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = $this->insertInput($request);
|
||
|
|
if (isset($this->validate) && $this->validate) {
|
||
|
|
if (!$this->validate->scene('store')->check($data)) {
|
||
|
|
throw new \Exception($this->validate->getError());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset($data['start_date']) && !empty($data['start_date'])) {
|
||
|
|
$data['start_time'] = strtotime($data['start_date']);
|
||
|
|
unset($data['start_date']);
|
||
|
|
}
|
||
|
|
if (isset($data['end_date']) && !empty($data['end_date'])) {
|
||
|
|
$data['end_time'] = strtotime($data['end_date']);
|
||
|
|
unset($data['end_date']);
|
||
|
|
}
|
||
|
|
$model = $this->service->save($data);
|
||
|
|
if (empty($model)) {
|
||
|
|
throw new AdminException('插入失败');
|
||
|
|
}
|
||
|
|
$pk = $model->getPk();
|
||
|
|
//添加后移除缓存
|
||
|
|
$this->service->cacheDriver()->delete(RetaRestrictionsService::CACHE_KEY);
|
||
|
|
return Json::success('ok', [$pk => $model->getData($pk)]);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Put(
|
||
|
|
path: '/gateway/blacklist/{id}/status',
|
||
|
|
summary: '切换状态',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'gateway:blacklist:status')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function changeStatus(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = $this->insertInput($request);
|
||
|
|
$model = $this->service->getModel();
|
||
|
|
$primaryKey = $model->getKeyName();
|
||
|
|
if (!array_key_exists($primaryKey, $data)) {
|
||
|
|
throw new \Exception('参数异常缺少主键');
|
||
|
|
}
|
||
|
|
$targetModel = $model->findOrFail($data[$primaryKey]);
|
||
|
|
if (empty($targetModel)) {
|
||
|
|
throw new \Exception('资源不存在' . $primaryKey . '=', $data[$primaryKey]);
|
||
|
|
}
|
||
|
|
$targetModel->fill($data);
|
||
|
|
if (!$targetModel->save()) {
|
||
|
|
throw new \RuntimeException('数据保存失败');
|
||
|
|
}
|
||
|
|
//清空缓存
|
||
|
|
$this->service->cacheDriver()->delete(RetaRestrictionsService::CACHE_KEY);
|
||
|
|
return Json::success('ok', []);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Put(
|
||
|
|
path: '/gateway/blacklist/{id}',
|
||
|
|
summary: '更新',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => RateRestrictionsFormRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[OA\Parameter(
|
||
|
|
name: 'id',
|
||
|
|
description: 'ID',
|
||
|
|
in: 'path',
|
||
|
|
required: true,
|
||
|
|
schema: new OA\Schema(type: 'string'))]
|
||
|
|
#[Permission(code: 'gateway:blacklist:update')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function update(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = $this->insertInput($request);
|
||
|
|
if (isset($this->validate) && $this->validate) {
|
||
|
|
if (!$this->validate->scene('update')->check($data)) {
|
||
|
|
throw new \Exception($this->validate->getError());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$this->service->update($data['id'], $data);
|
||
|
|
//清空缓存
|
||
|
|
$this->service->cacheDriver()->delete(RetaRestrictionsService::CACHE_KEY);
|
||
|
|
return Json::success('ok', []);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Delete(
|
||
|
|
path: '/gateway/blacklist/{id}',
|
||
|
|
summary: '删除',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'gateway:blacklist:delete')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function destroy(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id'); // 获取路由地址 id从
|
||
|
|
$data = $request->input('data', []);
|
||
|
|
$data = !empty($id) && $id !== '0' ? $id : $data;
|
||
|
|
if (empty($data)) {
|
||
|
|
throw new AdminException('参数错误');
|
||
|
|
}
|
||
|
|
$result = $this->service->transaction(function () use ($data) {
|
||
|
|
$data = is_array($data) ? $data : explode(',', $data);
|
||
|
|
$deletedIds = [];
|
||
|
|
foreach ($data as $id) {
|
||
|
|
$item = $this->service->get($id);
|
||
|
|
if (!$item) {
|
||
|
|
continue; // 如果找不到项,跳过
|
||
|
|
}
|
||
|
|
$item->delete();
|
||
|
|
$primaryKey = $item->getPk();
|
||
|
|
$deletedIds[] = $item->{$primaryKey};
|
||
|
|
}
|
||
|
|
return $deletedIds;
|
||
|
|
});
|
||
|
|
//清空缓存
|
||
|
|
$this->service->cacheDriver()->delete(RetaRestrictionsService::CACHE_KEY);
|
||
|
|
return Json::success('ok', $result);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Delete(
|
||
|
|
path: '/gateway/blacklist',
|
||
|
|
summary: '批量删除',
|
||
|
|
tags: ['限访名单'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => BatchDeleteRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'gateway:blacklist:delete')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function batchDelete(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
|
||
|
|
$data = $request->input('ids', []);
|
||
|
|
if (empty($data)) {
|
||
|
|
throw new AdminException('参数错误');
|
||
|
|
}
|
||
|
|
$result = $this->service->transaction(function () use ($data) {
|
||
|
|
$data = is_array($data) ? $data : explode(',', $data);
|
||
|
|
$deletedIds = [];
|
||
|
|
foreach ($data as $id) {
|
||
|
|
$item = $this->service->get($id);
|
||
|
|
if (!$item) {
|
||
|
|
continue; // 如果找不到项,跳过
|
||
|
|
}
|
||
|
|
$item->delete();
|
||
|
|
$primaryKey = $item->getPk();
|
||
|
|
$deletedIds[] = $item->{$primaryKey};
|
||
|
|
}
|
||
|
|
return $deletedIds;
|
||
|
|
});
|
||
|
|
//清空缓存
|
||
|
|
$this->service->cacheDriver()->delete(RetaRestrictionsService::CACHE_KEY);
|
||
|
|
return Json::success('ok', $result);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|