代码下载接口

This commit is contained in:
2025-05-13 13:16:59 +08:00
parent 636fc411d2
commit a764794a5f
8 changed files with 206 additions and 183 deletions

View File

@@ -2,7 +2,9 @@
namespace App\Service\core;
use App\Models\CodeGenerationModel;
use App\Service\common\UtilsService;
use App\Service\MenuService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
@@ -75,10 +77,20 @@ class CodeGenerationService
return self::$_instance[$name];
}
public function download($id)
{
$model = CodeGenerationModel::find($id);
if (!$model) {
$this->utils->errorThrow('文件不存在');
}
// 返回下载 $path 文件流
return response()->download($model['path'], $model['file_name'], ['Content-Type' => 'application/zip']);
}
/**
* 创建临时文件夹,复制模板文件,压缩并返回文件流
*/
public function download(): BinaryFileResponse|JsonResponse
public function downloadUrl()
{
try {
// 将后端模板文件内容写入临时目录中
@@ -98,8 +110,15 @@ class CodeGenerationService
// 执行文件压缩
$zip->zip($zipFile, $path);
// 返回下载 $path 文件流
return response()->download($zipFile, "{$this->classComment}-{$this->uniqid}.zip", ['Content-Type' => 'application/zip']);
CodeGenerationModel::create([
'title' => $this->classComment,
'file_name' => "{$this->classComment}-{$this->uniqid}.zip",
'path' => $zipFile,
'params' => json_encode($this->param),
'created_at' => time(),
'updated_at' => time(),
]);
return $zipFile;
} catch (\Exception $e) {
return response()->json([
'error' => '生成模板文件失败',
@@ -134,9 +153,10 @@ class CodeGenerationService
* 代码生成主逻辑
*
* @param $params
* @return mixed
* @throws Exception
*/
public function generate($params)
public function generate($params): mixed
{
$this->param = $params;
DB::beginTransaction();
@@ -157,24 +177,19 @@ class CodeGenerationService
$this->querySql();
// 生成视图
$this->genViews();
// // 生成菜单
// $this->genMenu();
return $this->download();
// // 生成导出层
// $this->genExport();
// // 生成导入层
// $this->genImport();
// 生成菜单
$this->genMenu();
} catch (Exception $e) {
DB::rollBack();
ds([
'error' => '生成代码失败',
'message' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
// ds([
// 'error' => '生成代码失败',
// 'message' => $e->getMessage(),
// 'line' => $e->getLine(),
// 'file' => $e->getFile(),
// ]);
$this->utils->errorThrow($e->getMessage());
}
return $this->download();
return $this->downloadUrl();
}
@@ -240,7 +255,7 @@ class CodeGenerationService
$apiPath = app_path('../routes/api.php');
$apiFile = file_get_contents($apiPath, 'y');
$newApi =
"'{$this->dashCase}' => \App\Http\Controllers\Api\CodeGeneration\\{$this->upperCamelCase}Controller::class, // {$this->classComment}} \r\n // 需要登录的路由生成地址";
"'{$this->dashCase}' => \App\Http\Controllers\Api\CodeGeneration\\{$this->upperCamelCase}Controller::class, // {$this->classComment} \r\n // 需要登录的路由生成地址";
$updateNewFile = str_replace('// 需要登录的路由生成地址', $newApi, $apiFile);
@@ -289,35 +304,7 @@ class CodeGenerationService
'model'
);
}
//
// /**
// * 生成导出层文件
// * @return void
// */
// private function genExport(): void
// {
// $this->strReplaceTmp(
// $this->basePath . $this->tmpPath['server']['export'],
// $this->basePath . $this->newPath['server']['export'],
// 'server',
// 'export'
// );
// }
//
// /**
// * 生成导入层文件
// * @return void
// */
// private function genImport(): void
// {
// $this->strReplaceTmp(
// $this->basePath . $this->tmpPath['server']['import'],
// $this->basePath . $this->newPath['server']['import'],
// 'server',
// 'import'
// );
// }
//
/**
* 生成前端部分文件
* @return void
@@ -343,16 +330,24 @@ class CodeGenerationService
}
}
// /**
// * 生成菜单/授权
// *
// * @return void
// * @throws Exception
// */
// private function genMenu(): void
// {
// RoleService::getInstance()->createRoleMenuRelation(UsersEnum::ADMIN_ID, MenuService::getInstance()->save($this->menu));
// }
/**
* 生成菜单/授权
*
* @return void
* @throws Exception
*/
private function genMenu(): void
{
MenuService::getInstance()->create([
'title' => $this->classComment,
'icon' => $this->param['icon']?? '',
'name' => $this->upperCamelCase,
'path' => "/{$this->dashCase}",
'component' => "/my-gen/{$this->dashCase}/index",
'pid' => $this->param['pid'],
'sort' => $this->param['sort'],
]);
}
/**
* 模板内容替换
@@ -425,15 +420,15 @@ class CodeGenerationService
}
// 设置表单字段
if ($item['formShow']) {
$isNull = $item['null'] == 1 ? 'required' : '';
$isNull = $item['null'] == 1 ? "'required'" : "''";
$this->insertField .= !empty($this->insertField) ? '","' . $item['name'] : $item['name'];
$this->updateField .= !empty($this->updateField) ? '","' . $item['name'] : 'id","' . $item['name'];
$this->viewForm .= "{\n fieldName: '" . $item['name'] . "',\n label: '" . $item['comment'] . "',\n component: '" . $item['formType'] . "',\n rules: " . $isNull . ",\n },\n ";
$this->viewForm .= "{\n fieldName: '" . $item['name'] . "',\n label: '" . $item['comment'] . "',\n component: '" . $item['formType'] . "',\n rules: " . $isNull . ",\n },\n";
}
// 设置搜索字段
if ($item['search']) {
$this->viewSearch .= "{\n fieldName: '" . $item['name'] . "',\n label: '" . $item['comment'] . "',\n component: '" . $item['formType'] . "',\n },\n ";
$this->searchField .= !empty($this->searchField) ? ',' . $item['name'] . '\' => \'' . $item['searchValue'] : $item['name'] . '\' => \'' . $item['searchValue'];
$this->viewSearch .= "{\n fieldName: '" . $item['name'] . "',\n label: '" . $item['comment'] . "',\n component: '" . $item['formType'] . "',\n },\n ";
$this->searchField .= !empty($this->searchField) ? "','" . $item['name'] . '\' => \'' . $item['searchValue'] : $item['name'] . '\' => \'' . $item['searchValue'];
}
// // 设置导出字段
// $this->exportField .= !empty($this->exportField) ? ',"' . $item['comment'] . '"' : '"' . $item['comment'] . '"';
@@ -441,7 +436,7 @@ class CodeGenerationService
// // 设置导入字段
// $this->importField .= "'" . $item['name'] . "' => \$row['" . $item['comment'] . "'],\n ";
// 设置视图数据字段
$this->viewData .= "{ field: '{$item['name']}', title: '{$item['comment']}' },\n ";
$this->viewData .= "{ field: '{$item['name']}', title: '{$item['comment']}' },\n ";
}
/**