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 @@ }
-
+
-
+
+
    +
  • 视图生成
  • +
  • 命令生成
  • +
+
+
+
-
+ -
- -
- - 可为空,为空则不带前缀 +
+ +
+ + 可为空,为空则不带前缀 +
+
+
+ +
+ + 数据库表名字 不包含数据库表前缀。 +
+
+ +
+
+ +
+ + + +
+ +
+
+ 数据表: +
+
+
+ 设置忽略字段 +
+
+
+
+
+ 设置下拉字段 +
+
+
+
+
+ 设置单选字段 +
+
+
+
+
+ 设置多选字段 +
+
+
+
+
+ 设置单选图片字段 +
+
+
+
+
+ 设置多选图片字段 +
+
+
+
+
+ 设置日期(Y-m-d)字段 +
+
+
+
+
+ 设置日期时间(Y-m-d H:i:s)字段 +
+
+
+
+
+ 设置编辑器字段 +
+
+
+
+
+
+
+ + +
+
还未生成任何文件
+
+
-
- -
- - 数据库表名字 不包含数据库表前缀。 -
-
- -
-
- -
- - - -
- -
-
- 数据表: -
-
-
- 设置忽略字段 -
-
-
-
-
- 设置下拉字段 -
-
-
-
-
- 设置单选字段 -
-
-
-
-
- 设置多选字段 -
-
-
-
-
- 设置单选图片字段 -
-
-
-
-
- 设置多选图片字段 -
-
-
-
-
- 设置日期(Y-m-d)字段 -
-
-
-
-
- 设置日期时间(Y-m-d H:i:s)字段 -
-
-
-
-
- 设置编辑器字段 -
-
-
-
+
-
-
- - -
-
还未生成任何文件
-
-
\ No newline at end of file diff --git a/app/common/command/Curd.php b/app/common/command/Curd.php index febfbca..776ab5a 100644 --- a/app/common/command/Curd.php +++ b/app/common/command/Curd.php @@ -47,9 +47,6 @@ class Curd extends Command protected function execute(Input $input, Output $output) { - CliEcho::warn('请优先使用系统自带的 CURD/CRUD 可视化生成功能(关联功能增加中~),命令行 CURD/CRUD 功能将逐步下线!'); - CliEcho::notice(PHP_EOL); - $table = $input->getOption('table'); $controllerFilename = $input->getOption('controllerFilename'); $modelFilename = $input->getOption('modelFilename'); @@ -89,7 +86,10 @@ class Curd extends Command } if (empty($table)) { - CliEcho::error('请设置主表'); + if (PHP_SAPI == 'cli') + CliEcho::error('请设置主表'); + else + $output->writeln('请设置主表'); return false; } @@ -123,41 +123,58 @@ class Curd extends Command if (!$delete) { $result = $build->create(); if ($force) { + if (PHP_SAPI == 'cli') { + $output->info(">>>>>>>>>>>>>>>"); + foreach ($fileList as $key => $val) { + $output->info($key); + } + $output->info(">>>>>>>>>>>>>>>"); + $output->info("确定强制生成上方所有文件? 如果文件存在会直接覆盖。 请输入 'yes' 按回车键继续操作: "); + $line = fgets(defined('STDIN') ? STDIN : fopen('php://stdin', 'r')); + if (trim($line) != 'yes') { + throw new Exception("取消文件CURD生成操作"); + } + CliEcho::success('自动生成CURD成功'); + }else { + $output->writeln('自动生成CURD成功'); + } + } + }else { + if (PHP_SAPI == 'cli') { $output->info(">>>>>>>>>>>>>>>"); foreach ($fileList as $key => $val) { $output->info($key); } $output->info(">>>>>>>>>>>>>>>"); - $output->info("确定强制生成上方所有文件? 如果文件存在会直接覆盖。 请输入 'yes' 按回车键继续操作: "); + $output->info("确定删除上方所有文件? 请输入 'yes' 按回车键继续操作: "); $line = fgets(defined('STDIN') ? STDIN : fopen('php://stdin', 'r')); if (trim($line) != 'yes') { - throw new Exception("取消文件CURD生成操作"); + throw new Exception("取消删除文件操作"); + } + $result = $build->delete(); + CliEcho::success('>>>>>>>>>>>>>>>'); + CliEcho::success('删除自动生成CURD文件成功'); + CliEcho::success('>>>>>>>>>>>>>>>'); + foreach ($result as $vo) { + CliEcho::success($vo); + } + }else { + $result = $build->delete(); + $output->writeln('>>>>>>>>>>>>>>>'); + $output->writeln('删除自动生成CURD文件成功'); + $output->writeln('>>>>>>>>>>>>>>>'); + foreach ($result as $vo) { + $output->writeln($vo); } } - CliEcho::success('自动生成CURD成功'); - }else { - $output->info(">>>>>>>>>>>>>>>"); - foreach ($fileList as $key => $val) { - $output->info($key); - } - $output->info(">>>>>>>>>>>>>>>"); - $output->info("确定删除上方所有文件? 请输入 'yes' 按回车键继续操作: "); - $line = fgets(defined('STDIN') ? STDIN : fopen('php://stdin', 'r')); - if (trim($line) != 'yes') { - throw new Exception("取消删除文件操作"); - } - $result = $build->delete(); - CliEcho::success('>>>>>>>>>>>>>>>'); - CliEcho::success('删除自动生成CURD文件成功'); - } - CliEcho::success('>>>>>>>>>>>>>>>'); - foreach ($result as $vo) { - CliEcho::success($vo); } }catch (\Exception $e) { - CliEcho::error($e->getMessage()); - return false; + if (PHP_SAPI == 'cli') + CliEcho::error($e->getMessage()); + else + $output->writeln($e->getMessage()); } + return false; } diff --git a/public/static/admin/js/system/curd_generate.js b/public/static/admin/js/system/curd_generate.js index 7826b27..c9ad0c1 100644 --- a/public/static/admin/js/system/curd_generate.js +++ b/public/static/admin/js/system/curd_generate.js @@ -9,6 +9,63 @@ define(["jquery", "easy-admin", "miniTab"], function ($, ea, miniTab) { return { index: function () { + + let element = layui.element; + element.on('tab(curd-hash)', function (obj) { + let id = obj.id + let _html = ` +
+
+ 提示 + +
+
+
+
+
+ php think curd +
+ +
+
+
+ +
+
+
+` + if (id == '2') { + layer.open({ + title: '命令行一键生成 CRUD/CRUD', + type: 1, + shade: 0.3, + shadeClose: false, + area: ['42%', 'auto'], + content: _html, + success: function () { + form.on('submit(curd-console-submit)', function (data) { + let field = data.field + let url = $(this).attr('lay-submit') + let options = {url: ea.url(url), data: field} + ea.msg.confirm('确认执行该操作?
如果命令行中存在强制覆盖或者删除将会马上执行!', function () { + ea.request.post(options, function (rs) { + let msg = rs.msg || '未知~' + layer.msg(msg.replace(/\n/g, '
'), {shade: 0.3, shadeClose: true}) + let code = rs?.code || '-1' + if (code != '1') return + }) + }) + }) + }, + end: function () { + element.tabChange('curd-hash', '1'); + } + }) + } + }); + miniTab.listen(); let createStatus = false let tb_prefix