274 lines
9.4 KiB
PHP
274 lines
9.4 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\generator;
|
||
|
|
|
||
|
|
use app\adminapi\controller\Crud;
|
||
|
|
use app\adminapi\middleware\AccessTokenMiddleware;
|
||
|
|
use app\adminapi\middleware\OperationMiddleware;
|
||
|
|
use app\adminapi\middleware\PermissionMiddleware;
|
||
|
|
use app\adminapi\validate\generator\GeneratorValidate;
|
||
|
|
use app\schema\request\BatchDeleteRequest;
|
||
|
|
use app\schema\request\IdRequest;
|
||
|
|
use app\service\admin\generator\GeneratorService;
|
||
|
|
use app\service\admin\generator\GeneratorTableService;
|
||
|
|
use madong\swagger\annotation\response\DataResponse;
|
||
|
|
use madong\swagger\annotation\response\PageResponse;
|
||
|
|
use madong\swagger\annotation\response\SimpleResponse;
|
||
|
|
use madong\swagger\attribute\Permission;
|
||
|
|
use core\exception\handler\AdminException;
|
||
|
|
use core\tool\Json;
|
||
|
|
use madong\helper\Arr;
|
||
|
|
use support\Container;
|
||
|
|
use support\Request;
|
||
|
|
use support\annotation\Middleware;
|
||
|
|
use OpenApi\Attributes as OA;
|
||
|
|
use WebmanTech\Swagger\DTO\SchemaConstants;
|
||
|
|
|
||
|
|
#[Middleware(AccessTokenMiddleware::class, PermissionMiddleware::class, OperationMiddleware::class)]
|
||
|
|
final class GeneratorController extends Crud
|
||
|
|
{
|
||
|
|
public function __construct(GeneratorService $service, GeneratorValidate $validate)
|
||
|
|
{
|
||
|
|
$this->service = $service;
|
||
|
|
$this->validate = $validate;
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/generator/code',
|
||
|
|
summary: '列表',
|
||
|
|
tags: ['代码生成器']
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:list')]
|
||
|
|
#[PageResponse(schema: [], example: [[
|
||
|
|
'id' => '273048217217138688',
|
||
|
|
'table_name' => 'sys_admin_post',
|
||
|
|
'table_content' => '用户与岗位关联表',
|
||
|
|
'module_name' => 'sys_admin_post',
|
||
|
|
'class_name' => 'sys_admin_post',
|
||
|
|
'edit_type' => 1,
|
||
|
|
'plugin_name' => '',
|
||
|
|
'order_type' => 0,
|
||
|
|
'parent_menu' => '',
|
||
|
|
'relations' => null,
|
||
|
|
'push_sync_count' => 0,
|
||
|
|
'created_at' => '2026-01-23T03:16:07.000000Z',
|
||
|
|
'updated_at' => '2026-01-23T03:16:07.000000Z',
|
||
|
|
'created_date' => '2026-01-23 11:16:07',
|
||
|
|
'updated_date' => '2026-01-23 11:16:07',
|
||
|
|
]])]
|
||
|
|
public function index(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
return parent::index($request); // TODO: Change the autogenerated stub
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/generator/code/table-list',
|
||
|
|
summary: '数据库表-列表',
|
||
|
|
tags: ['代码生成器']
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:list')]
|
||
|
|
#[DataResponse(schema: [], example: [])]
|
||
|
|
public function tableList(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
$data = $request->all();
|
||
|
|
$page = $request->input('page', 1);
|
||
|
|
$limit = $request->input('limit', 10);
|
||
|
|
$result = $this->service->tableList($data, (int)$page, (int)$limit);
|
||
|
|
return Json::success($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Post(
|
||
|
|
path: '/generator/code',
|
||
|
|
summary: '生成代码',
|
||
|
|
tags: ['代码生成器']
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:create')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function store(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = $request->input('table', []);
|
||
|
|
foreach ($data as $item) {
|
||
|
|
if (!$this->validate->scene('store')->check($item)) {
|
||
|
|
return Json::fail($this->validate->getError());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
foreach ($data as $item) {
|
||
|
|
$this->service->store($item);
|
||
|
|
}
|
||
|
|
return Json::success();
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Delete(
|
||
|
|
path: '/generator/code/{id}',
|
||
|
|
summary: '删除',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:delete')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function destroy(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id');
|
||
|
|
$this->service->destroy(Arr::normalize($id));
|
||
|
|
return Json::success();
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Delete(
|
||
|
|
path: '/generator/code',
|
||
|
|
summary: '批量删除',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => BatchDeleteRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:delete')]
|
||
|
|
#[SimpleResponse(schema: [], example: [])]
|
||
|
|
public function remove(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$data = $request->input('ids', []);
|
||
|
|
$this->service->destroy(Arr::normalize($data));
|
||
|
|
return Json::success();
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/generator/code/{id}',
|
||
|
|
summary: '详情',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:read')]
|
||
|
|
#[DataResponse(schema: [], example: [])]
|
||
|
|
public function show(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id');
|
||
|
|
$service = Container::make(GeneratorTableService::class);
|
||
|
|
$data = $service->getDetail($id);
|
||
|
|
if (empty($data)) {
|
||
|
|
throw new AdminException('数据未找到', ['errorCode' => 400]);
|
||
|
|
}
|
||
|
|
return Json::success('ok', $data->toArray());
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage(), [], $e->getCode());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Put(
|
||
|
|
path: '/generator/code/{id}',
|
||
|
|
summary: '更新',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:update')]
|
||
|
|
public function update(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id');
|
||
|
|
$data = $request->all();
|
||
|
|
if (!$this->validate->scene('update')->check($data)) {
|
||
|
|
return Json::fail($this->validate->getError());
|
||
|
|
}
|
||
|
|
$this->service->update($id, $data);
|
||
|
|
return Json::success();
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/generator/code/{id}/preview',
|
||
|
|
summary: '预览',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:preview')]
|
||
|
|
public function preview(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id');
|
||
|
|
$result = $this->service->preview(['id' => $id]);
|
||
|
|
return Json::success($result);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Get(
|
||
|
|
path: '/generator/code/{id}/download',
|
||
|
|
summary: '下载',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:download')]
|
||
|
|
public function download(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id');
|
||
|
|
$result = $this->service->download($id);
|
||
|
|
return response()->download($result['file_path'], $result['file_name']);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[OA\Put(
|
||
|
|
path: '/generator/code/{id}/deploy',
|
||
|
|
summary: '部署',
|
||
|
|
tags: ['代码生成器'],
|
||
|
|
x: [
|
||
|
|
SchemaConstants::X_PROPERTY_IN => 'id',
|
||
|
|
SchemaConstants::X_SCHEMA_REQUEST => IdRequest::class,
|
||
|
|
]
|
||
|
|
)]
|
||
|
|
#[Permission(code: 'generator:code:deploy')]
|
||
|
|
public function deploy(Request $request): \support\Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $request->route->param('id');
|
||
|
|
$result = $this->service->deploy(['id' => $id]);
|
||
|
|
return Json::success($result);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return Json::fail($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|