diff --git a/app/admin/controller/system/CurdGenerate.php b/app/admin/controller/system/CurdGenerate.php new file mode 100644 index 0000000..1eae8b9 --- /dev/null +++ b/app/admin/controller/system/CurdGenerate.php @@ -0,0 +1,92 @@ +fetch(); + } + + /** + * @NodeAnnotation(title="操作") + * @throws TableException + */ + public function save(Request $request, string $type = ''): ?Json + { + if (!$request->isAjax()) return $this->error(); + $tb_prefix = $request->post('tb_prefix/s', ''); + $tb_name = $request->post('tb_name/s', ''); + if (empty($tb_name) || empty($tb_prefix)) return $this->error('参数错误'); + switch ($type) { + case "search": + try { + $list = Db::query("SHOW FULL COLUMNS FROM {$tb_prefix}{$tb_name}"); + $data = []; + foreach ($list as $value) { + $data[] = [ + 'name' => $value['Field'], + 'type' => $value['Type'], + 'key' => $value['Key'], + 'extra' => $value['Extra'], + 'null' => $value['Null'], + 'desc' => $value['Comment'], + ]; + } + return $this->success('查询成功', compact('data', 'list')); + } catch (PDOException $exception) { + return $this->error($exception->getMessage()); + } + break; + case "add": + $force = $request->post('force/d', 0); + try { + $build = (new BuildCurd())->setTablePrefix($tb_prefix)->setTable($tb_name); + $build->setForce($force); // 强制覆盖 + $build = $build->render(); + $fileList = $build->getFileList(); + if (empty($fileList)) return $this->error('这里什么都没有'); + $result = $build->create(); + return $this->success('生成成功', compact('result')); + } catch (FileException $exception) { + return json(['code' => -1, 'msg' => $exception->getMessage()]); + } + break; + case "delete": + try { + $build = (new BuildCurd())->setTablePrefix($tb_prefix)->setTable($tb_name); + $build = $build->render(); + $fileList = $build->getFileList(); + if (empty($fileList)) return $this->error('这里什么都没有'); + $result = $build->delete(); + return $this->success('删除自动生成CURD文件成功', compact('result')); + } catch (FileException $exception) { + return json(['code' => -1, 'msg' => $exception->getMessage()]); + } + break; + default: + return $this->error('参数错误'); + break; + } + } +} \ No newline at end of file diff --git a/app/admin/service/curd/BuildCurd.php b/app/admin/service/curd/BuildCurd.php index 565f5bb..31512de 100644 --- a/app/admin/service/curd/BuildCurd.php +++ b/app/admin/service/curd/BuildCurd.php @@ -239,6 +239,12 @@ class BuildCurd return $this; } + public function setTablePrefix($prefix): static + { + $this->tablePrefix = $prefix; + return $this; + } + /** * 设置主表 * @param $table @@ -1059,12 +1065,12 @@ class BuildCurd array_pop($extendNamespaceArray); $extendNamespace = '\\' . implode('\\', $extendNamespaceArray); } - $modelValue = CommonTool::replaceTemplate( $this->getTemplate("model{$this->DS}model"), [ 'modelName' => $this->modelName, 'modelNamespace' => "app\admin\model{$extendNamespace}", + 'prefix_table' => $this->tablePrefix == config('database.connections.mysql.prefix') ? "" : $this->tablePrefix . $this->table, 'table' => $this->table, 'deleteTime' => $this->delete ? '"delete_time"' : 'false', 'relationList' => $relationList, diff --git a/app/admin/service/curd/templates/model/model.code b/app/admin/service/curd/templates/model/model.code index 8194111..bec8223 100644 --- a/app/admin/service/curd/templates/model/model.code +++ b/app/admin/service/curd/templates/model/model.code @@ -9,6 +9,8 @@ class {{modelName}} extends TimeModel protected $name = "{{table}}"; + protected $table = "{{prefix_table}}"; + protected $deleteTime = {{deleteTime}}; {{relationList}} diff --git a/app/admin/view/system/curd_generate/index.html b/app/admin/view/system/curd_generate/index.html new file mode 100644 index 0000000..88ea1c3 --- /dev/null +++ b/app/admin/view/system/curd_generate/index.html @@ -0,0 +1,46 @@ +
+
+ +
+ +
+ +
+ +
+ +
+ +
+
+ +
+ + 数据库表名字 不包含数据库表前缀。 +
+
+ +
+
+ +
+ +
+ +
+ +
+
+ 数据表: +
+
+
+ + +
+
还未生成任何文件
+
+
+
+
+
\ No newline at end of file diff --git a/config/install/sql/install.sql b/config/install/sql/install.sql index 252ee2f..80a65f1 100644 --- a/config/install/sql/install.sql +++ b/config/install/sql/install.sql @@ -13,7 +13,8 @@ File Encoding : 65001 Date: 2020-05-17 23:24:06 */ -SET FOREIGN_KEY_CHECKS=0; +SET +FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for ea_mall_cate @@ -323,6 +324,8 @@ INSERT INTO `ea_system_menu` VALUES ('252', '228', '快捷入口', 'fa fa-list', 'system.quick/index', '', '_self', '0', '1', '', '1589623683', '1589623683', null); INSERT INTO `ea_system_menu` VALUES ('253', '228', '日志管理', 'fa fa-connectdevelop', 'system.log/index', '', '_self', '0', '1', '', '1589623684', '1589623684', null); +INSERT INTO `ea_system_menu` +VALUES ('254', '228', 'CURD可视化', 'fa fa fa-shower', 'system.curd_generate/index', '', '_self', '0', '1', '', '1589623684', '1589623684', null); -- ---------------------------- -- Table structure for ea_system_node @@ -480,6 +483,12 @@ INSERT INTO `ea_system_node` VALUES ('67', 'system.log', '操作日志管理', '1', '1', '1589623188', '1589623188'); INSERT INTO `ea_system_node` VALUES ('68', 'system.log/index', '列表', '2', '1', '1589623188', '1589623188'); +INSERT INTO `ea_system_node` +VALUES ('69', 'system.curd_generate', 'CURD可视化管理', '1', '1', '1589623188', '1589623188'); +INSERT INTO `ea_system_node` +VALUES ('70', 'system.curd_generate/index', '列表', '2', '1', '1589623188', '1589623188'); +INSERT INTO `ea_system_node` +VALUES ('71', 'system.curd_generate/save', '操作', '2', '1', '1589623188', '1589623188'); -- ---------------------------- -- Table structure for ea_system_quick diff --git a/public/static/admin/js/system/curd_generate.js b/public/static/admin/js/system/curd_generate.js new file mode 100644 index 0000000..2589f65 --- /dev/null +++ b/public/static/admin/js/system/curd_generate.js @@ -0,0 +1,110 @@ +define(["jquery", "easy-admin"], function ($, ea, Vue) { + + var form = layui.form; + var table = layui.table; + + var init = { + save_url: 'system.curd_generate/save', + }; + + var Controller = { + index: function () { + + let tb_prefix + let tb_name + form.on('submit(search)', function (data) { + let field = data.field + tb_prefix = field.tb_prefix + tb_name = field.tb_name + ea.request.post({url: $(this).attr('lay-submit'), prefix: true, data: field}, function (res) { + let code = res.code || '0' + if (code != '1') { + ea.msg.error(res.msg) + return + } + $('.tableShow').removeClass('layui-hide') + $('.table-text').text(field.tb_prefix + field.tb_name) + let _data = res.data + table.render({ + elem: '#currentTable', cols: [ + [ + {field: 'name', title: '字段', minWidth: 80}, + {field: 'type', title: '类型', minWidth: 80}, + {field: 'key', title: '键', minWidth: 80}, + {field: 'extra', title: '是否自增', minWidth: 80}, + {field: 'null', title: '是否为空', minWidth: 80}, + {field: 'desc', title: '描述', minWidth: 80}, + ] + ], + data: _data.data, + page: false, + }); + + }) + form.on('submit(add)', function (data) { + let table = $('.table-text').text() + if (!table) { + ea.msg.error('请先查询数据') + return + } + let url = $(this).attr('lay-submit') + let options = {url: url, prefix: true, data: {tb_prefix: tb_prefix, tb_name: tb_name}} + layer.confirm('确定要自动生成【' + table + '】对应的CURD?', function (index) { + ea.request.post(options, function (res) { + ea.msg.success(res.msg) + let html = '' + $.each(res['data']['result'], function (idx, item) { + html += '
  • ' + item + '
  • ' + }) + $('.file-list').html(html) + }, function (error) { + let code = error.code + if (code != '1') { + if (code < 0) { + layer.confirm(error.msg, { + btn: ['确定强制覆盖生成'], title: '提示', icon: 0, + yes: function () { + options.prefix = false + options.data.force = 1 + ea.request.post(options, function (rs) { + ea.msg.success(rs.msg) + $('.file-list').empty() + let html = '' + $.each(rs['data']['result'], function (idx, item) { + html += '
  • ' + item + '
  • ' + }) + $('.file-list').html(html) + }) + } + }); + return + } + ea.msg.error(error.msg) + return + } + }) + }) + }) + + form.on('submit(delete)', function (data) { + let table = $('.table-text').text() + if (!table) { + ea.msg.error('请先查询数据') + return + } + let url = $(this).attr('lay-submit') + let options = {url: url, prefix: true, data: {tb_prefix: tb_prefix, tb_name: tb_name}} + layer.confirm('确定要删除【' + table + '】对应CURD的文件?
    确定清楚自己在做什么!', function (index) { + ea.request.post(options, function (res) { + ea.msg.success(res.msg) + $('.table-text').text('') + $('.file-list').empty() + }) + }) + }) + return + }) + } + }; + return Controller; +}); \ No newline at end of file