Files
nl-admin-api/app/Http/Controllers/core/CodeGenerationController.php

164 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2025-05-13 10:39:21 +08:00
<?php
namespace App\Http\Controllers\core;
use App\BaseApp\BaseController;
use App\Service\core\CodeGenerationService;
use App\Service\LoginService;
use Illuminate\Http\JsonResponse;
class CodeGenerationController extends BaseController
{
//
public function __construct()
{
parent::__construct();
$this->service = CodeGenerationService::getInstance();
}
2025-11-28 13:11:55 +08:00
/**
* 生成代码
* @Method POST
* @return JsonResponse
* @throws \Exception
*/
2025-05-13 10:39:21 +08:00
public function generation()
{
2025-05-13 13:16:59 +08:00
$this->insertField = [
'class_name',
'class_comment',
'sort',
'icon',
'pid',
'field',
];
$params = $this->checkRequiredFields(request()->post());
return jok(
$this->service->generate($params),
'生成成功!'
);
}
2026-01-23 17:13:23 +08:00
/**
* 批量生成代码
* @Method POST
* @return JsonResponse
* @throws \Exception
*/
public function batchGeneration()
{
$params = request()->post();
if (!is_array($params) || empty($params)) {
return jerr('参数错误,需要数组格式!');
}
$results = [];
$errors = [];
foreach ($params as $index => $item) {
try {
// 验证必填字段
if (empty($item['class_name']) || empty($item['field']) || !is_array($item['field'])) {
$errors[] = "模块 " . ($index + 1) . " 参数不完整";
continue;
}
$result = $this->service->generate($item);
$results[] = $result;
} catch (\Exception $e) {
$errors[] = "模块 " . ($index + 1) . " 生成失败: " . $e->getMessage();
}
}
if (empty($results)) {
return jerr('批量生成失败:' . implode('; ', $errors));
}
if (!empty($errors)) {
return jok($results, '部分生成成功,共生成 ' . count($results) . ' 个模块。错误:' . implode('; ', $errors));
}
return jok($results, '批量生成成功,共生成 ' . count($results) . ' 个模块!');
}
2025-05-13 13:16:59 +08:00
public function download()
{
// $id = request()->post('id');
$id = request()->get('id');
if (!$id) {
return jerr('参数错误!');
}
return $this->service->download($id);
2025-05-13 10:39:21 +08:00
}
2025-05-13 16:35:18 +08:00
public function downloadInfo()
{
// $id = request()->post('id');
$id = request()->get('id');
if (!$id) {
return jerr('参数错误!');
}
return jok(
$this->service->downloadInfo($id)
);
}
2026-01-23 17:13:23 +08:00
/**
* 获取数据库表列表
* @Method GET
* @return JsonResponse
*/
public function getTables()
{
return jok(
$this->service->getTableList()
);
}
/**
* 获取表结构
* @Method GET
* @return JsonResponse
* @throws \Exception
*/
public function getTableStructure()
{
$tableName = request()->get('table_name');
if (!$tableName) {
return jerr('表名不能为空!');
}
return jok(
$this->service->getTableColumns($tableName)
);
}
/**
* 从数据库表生成代码生成数据
* @Method POST
* @return JsonResponse
* @throws \Exception
*/
public function generateFromTable()
{
$tableName = request()->post('table_name');
$className = request()->post('class_name', '');
$classComment = request()->post('class_comment', '');
$icon = request()->post('icon', '');
$sort = request()->post('sort', 9999);
$pid = request()->post('pid', 0);
if (!$tableName) {
return jerr('表名不能为空!');
}
$codeGenData = $this->service->convertTableToCodeGenData($tableName, $className, $classComment, $icon, $sort, $pid);
return jok($codeGenData);
}
2025-05-13 10:39:21 +08:00
}