From 55c47434176e5b1480e45899576e1c329dd4648b Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Tue, 20 Aug 2024 09:40:34 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=9A=80=20=E6=9B=B4=E6=96=B0CRUD/CRUD?= =?UTF-8?q?=E5=8F=AF=E8=A7=86=E5=8C=96=E6=93=8D=E4=BD=9C=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=94=AF=E6=8C=81=E5=8F=AF=E8=A7=86=E5=8C=96=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend the CURD generation functionality to support command line operations, supplementing the existing visual generation method. This update includes modifications to the admin interface, enabling the system to process both types of CURD generation commands effectively. --- app/admin/controller/system/CurdGenerate.php | 29 ++- app/admin/service/curd/BuildCurd.php | 13 +- .../view/system/curd_generate/index.html | 195 +++++++++--------- app/common/command/Curd.php | 71 ++++--- .../static/admin/js/system/curd_generate.js | 57 +++++ 5 files changed, 234 insertions(+), 131 deletions(-) diff --git a/app/admin/controller/system/CurdGenerate.php b/app/admin/controller/system/CurdGenerate.php index a7b3d60..d76dfa4 100644 --- a/app/admin/controller/system/CurdGenerate.php +++ b/app/admin/controller/system/CurdGenerate.php @@ -10,6 +10,7 @@ use app\admin\service\annotation\NodeAnnotation; use app\Request; use think\db\exception\PDOException; use think\exception\FileException; +use think\facade\Console; use think\facade\Db; use think\helper\Str; use think\response\Json; @@ -35,11 +36,12 @@ class CurdGenerate extends AdminController public function save(Request $request, string $type = ''): ?Json { if (!$request->isAjax()) $this->error(); - $tb_prefix = $request->param('tb_prefix/s', ''); - $tb_name = $request->param('tb_name/s', ''); - if (empty($tb_name)) $this->error('参数错误'); switch ($type) { case "search": + $tb_prefix = $request->param('tb_prefix/s', ''); + $tb_name = $request->param('tb_name/s', ''); + if (empty($tb_name)) $this->error('参数错误'); + try { $list = Db::query("SHOW FULL COLUMNS FROM {$tb_prefix}{$tb_name}"); $data = []; @@ -59,6 +61,10 @@ class CurdGenerate extends AdminController } break; case "add": + $tb_prefix = $request->param('tb_prefix/s', ''); + $tb_name = $request->param('tb_name/s', ''); + if (empty($tb_name)) $this->error('参数错误'); + $tb_fields = $request->param('tb_fields'); $force = $request->post('force/d', 0); try { @@ -121,6 +127,10 @@ class CurdGenerate extends AdminController } break; case "delete": + $tb_prefix = $request->param('tb_prefix/s', ''); + $tb_name = $request->param('tb_name/s', ''); + if (empty($tb_name)) $this->error('参数错误'); + try { $build = (new BuildCurd())->setTablePrefix($tb_prefix)->setTable($tb_name); $build = $build->render(); @@ -132,6 +142,19 @@ class CurdGenerate extends AdminController return json(['code' => -1, 'msg' => $exception->getMessage()]); } break; + case 'console': + $command = $request->post('command', ''); + if (empty($command)) $this->error('请输入命令'); + $commandExp = explode(' ', $command); + try { + + $output = Console::call('curd', [...$commandExp]); + }catch (\Throwable $exception) { + $this->error($exception->getMessage() . $exception->getLine()); + } + if (empty($output)) $this->error('设置错误'); + $this->success($output->fetch()); + break; default: $this->error('参数错误'); break; diff --git a/app/admin/service/curd/BuildCurd.php b/app/admin/service/curd/BuildCurd.php index bd0f66f..dcfea9d 100644 --- a/app/admin/service/curd/BuildCurd.php +++ b/app/admin/service/curd/BuildCurd.php @@ -1125,6 +1125,7 @@ class BuildCurd // 'selectList' => $selectList, 'selectArrays' => CommonTool::replaceArrayString(var_export($selectArrays, true)), ]); + $this->fileList[$modelFile] = $modelValue; // 关联模型 @@ -1347,13 +1348,13 @@ class BuildCurd continue; }elseif (in_array($field, $this->switchFields)) { if (!empty($val['define'])) { - $templateValue = "{field: '{$field}', search: 'select', selectList: {$field}List, title: '{$val['comment']}', templet: ea.table.switch}"; + $templateValue = "{field: '{$field}', search: 'select', selectList: notes?.{$field} || {}, title: '{$val['comment']}', templet: ea.table.switch}"; }else { $templateValue = "{field: '{$field}', title: '{$val['comment']}', templet: ea.table.switch}"; } }elseif (in_array($val['formType'], ['select', 'checkbox', 'radio', 'switch'])) { if (!empty($val['define'])) { - $templateValue = "{field: '{$field}', search: 'select', selectList: {$field}List, title: '{$val['comment']}'}"; + $templateValue = "{field: '{$field}', search: 'select', selectList: notes?.{$field} || {}, title: '{$val['comment']}'}"; }else { $templateValue = "{field: '{$field}', title: '{$val['comment']}'}"; } @@ -1528,12 +1529,6 @@ class BuildCurd protected function formatNotesScript(): string { - $array = []; - foreach ($this->tableColumns as $key => $column) { - if (empty($column['formType'])) continue; - if (!in_array($column['formType'], ['select', 'switch', 'radio', 'checkbox'])) continue; - $array[] = ' let ' . $key . 'List = JSON.parse(\'{$notes.' . $key . '|json_encode=256|raw}\');'; - } - return implode(PHP_EOL, $array); + return ' let notes = JSON.parse(\'{$notes|json_encode=256|raw}\');'; } } \ No newline at end of file diff --git a/app/admin/view/system/curd_generate/index.html b/app/admin/view/system/curd_generate/index.html index 208474d..9005eae 100644 --- a/app/admin/view/system/curd_generate/index.html +++ b/app/admin/view/system/curd_generate/index.html @@ -5,104 +5,115 @@ }
- 数据表: --