Files
nl-admin-api/app/Http/Controllers/core/CodeGenerationController.php
2026-08-10 15:51:00 +08:00

191 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers\core;
use App\BaseApp\BaseController;
use App\Service\common\ai\AiCodeGenService;
use App\Service\common\ai\AiRequirementPromptService;
use App\Service\core\CodeGenerationService;
use Illuminate\Http\JsonResponse;
class CodeGenerationController extends BaseController
{
//
public function __construct()
{
parent::__construct();
$this->service = CodeGenerationService::getInstance();
}
/**
* 生成代码
* @Method POST
* @return JsonResponse
* @throws \Exception
*/
public function generation()
{
$this->insertField = [
'class_name',
'class_comment',
'sort',
'icon',
'pid',
'field',
];
$params = $this->checkRequiredFields(request()->post());
return jok(
$this->service->generate($params),
'生成成功!'
);
}
/**
* 批量生成代码
* @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) . ' 个模块!');
}
public function download()
{
// $id = request()->post('id');
$id = request()->get('id');
if (!$id) {
return jerr('参数错误!');
}
return $this->service->download($id);
}
public function downloadInfo()
{
// $id = request()->post('id');
$id = request()->get('id');
if (!$id) {
return jerr('参数错误!');
}
return jok(
$this->service->downloadInfo($id)
);
}
/**
* 获取数据库表列表
* @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);
}
/**
* AI 提示词生成代码结构(调用工厂 Agent返回可灌入表单的 JSON
* @Method POST
*/
public function aiGenerate(): JsonResponse
{
$params = request()->post();
return jok(
AiCodeGenService::getInstance()->generate($params),
'AI 生成成功'
);
}
/**
* 根据模块名生成 / 优化需求概览与代码生成提示词(写入生成历史,供前端替换导入)
* @Method POST
*/
public function aiRequirementPrompt(): JsonResponse
{
$params = request()->post();
return jok(
AiRequirementPromptService::getInstance()->generate($params),
'需求概览生成成功'
);
}
}