164 lines
4.0 KiB
PHP
164 lines
4.0 KiB
PHP
<?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();
|
|
}
|
|
|
|
/**
|
|
* 生成代码
|
|
* @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);
|
|
}
|
|
|
|
}
|