From bfba89dabaf9b8eee7fb7742cb155d4649ee7cde Mon Sep 17 00:00:00 2001 From: zhang zhuo Date: Wed, 10 Dec 2025 18:03:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Admin/System.php | 185 ----------------- app/Controller/Admin/Tools.php | 219 +++++++++++++++++++++ static/view/templates/formDesktop.vue.twig | 78 ++++++++ static/view/templates/formMobile.vue.twig | 0 4 files changed, 297 insertions(+), 185 deletions(-) create mode 100644 app/Controller/Admin/Tools.php create mode 100644 static/view/templates/formDesktop.vue.twig create mode 100644 static/view/templates/formMobile.vue.twig diff --git a/app/Controller/Admin/System.php b/app/Controller/Admin/System.php index 0b1b833..34d3ded 100644 --- a/app/Controller/Admin/System.php +++ b/app/Controller/Admin/System.php @@ -14,13 +14,11 @@ use App\Model\Menu as mModel; use App\Model\Online as oModel; use App\Model\Post as pModel; use App\Model\Role as rModel; -use App\Model\GenTable as gtModel; use App\Request\Account as aRequest; use App\Request\Dept as dRequest; use App\Request\Menu as mRequest; use App\Request\Post as pRequest; use App\Request\Role as rRequest; -use App\Request\GenTable as gtRequest; use App\Model\AssetCategory as acModel; use App\Request\AssetCategory as acRequest; use App\Model\SystemConfig as scModel; @@ -42,15 +40,12 @@ use App\Utils\Param; use App\Utils\RedisInfoHelper; use App\Utils\Str; use App\Utils\SystemHelper; -use Hyperf\Context\ApplicationContext; -use Hyperf\DbConnection\Db; use Hyperf\Filesystem\FilesystemFactory; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\DeleteMapping; use Hyperf\HttpServer\Annotation\GetMapping; use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\PutMapping; -use function Hyperf\Config\config; #[Controller(prefix: "admin")] class System extends Base @@ -468,186 +463,6 @@ class System extends Base return $res ? $this->success("操作成功") : $this->error("操作失败"); } - #[GetMapping(path: "gen_table/list")] - #[Auth(auth: "gen_table:list")] - public function genTableList() - { - $param = Param::only(['table_name', 'table_comment', 'limit' => 10]); - return $this->success("表列表", gtModel::list($param)); - } - - #[GetMapping(path: "gen_table/info")] - #[Auth(auth: "gen_table:info")] - public function genTableInfo() - { - $id = $this->request->input("id"); - return $this->success("详情", gtModel::info($id)); - } - - #[PostMapping(path: "gen_table/edit")] - #[Auth(auth: "gen_table:edit")] - public function genTableEdit() - { - $request = $this->container->get(gtRequest::class); - $request->scene('edit')->validateResolved(); - $param = Param::only(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'gen_table_columns']); - return $this->toAjax(gtModel::editData($param)); - } - - #[DeleteMapping(path: "gen_table/del")] - #[Auth(auth: "gen_table:del")] - public function genTableDel() - { - $param = Param::only(['ids' => []]); - return $this->toAjax(gtModel::del($param['ids'])); - } - - #[GetMapping(path: "gen_table/select")] - #[Auth(auth: "gen_table:select")] - public function genTableSelect() - { - $param = Param::only(['table_name', 'table_comment', 'limit' => 10, 'page' => 1]); - $offset = ($param['page'] - 1) * $param['limit']; - // 获取所有表 - $rows = Db::select("SHOW TABLE STATUS"); - // 获取已经导入的表 - $tables = gtModel::pluck("table_name")->toArray(); - // 表前缀 - $prefix = config("databases.default.prefix"); - // 构建表不导入 gen_table gen_table_column - $tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables); - $rows = array_filter($rows, function ($item) use ($tables, $param) { - // 过滤表 - if (in_array($item->Name, $tables)) { - return false; - } - // 表名筛选 - if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) { - return false; - } - // 表描述筛选 - if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) { - return false; - } - return true; - }); - $count = count($rows); - $rows = array_slice($rows, $offset, $param['limit']); - // 过滤返回数据 - $data = []; - foreach ($rows as $row) { - $data[] = [ - 'table_name' => $row->Name, - 'table_comment' => $row->Comment, - 'create_time' => $row->Create_time, - 'update_time' => $row->Update_time - ]; - } - return $this->success("表列表", $data, $count); - } - - #[PostMapping(path: "gen_table/build")] - #[Auth(auth: "gen_table:build")] - public function genTableBuild() - { - // 生成表数据 - $param = Param::only(['names' => []]); - if (empty($param['names'])) { - return $this->error("数据表不为空"); - } - // 获取已经导入的表 - $tables = gtModel::pluck("table_name")->toArray(); - // 表前缀 - $prefix = config("databases.default.prefix"); - // 构建表不导入 gen_table gen_table_column - $tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables); - // 获取所有表 - $rows = Db::select("SHOW TABLE STATUS"); - // 去重 - $rows = array_filter($rows, function ($item) use ($tables, $param) { - // 过滤表 - if (in_array($item->Name, $tables)) { - return false; - } - // 表筛选 - return in_array($item->Name, $param['names']); - }); - if (empty($rows)) { - return $this->error("数据表不为空"); - } - // 生成数据 - $res = gtModel::genTable($rows); - return $res ? $this->success("操作成功") : $this->error("操作失败"); - } - - #[PostMapping(path: "gen_table/sync")] - #[Auth(auth: "gen_table:sync")] - public function genTableSync() - { - $table_id = $this->request->input("id"); - if (!$table_id) { - return $this->error("同步失败"); - } - return $this->toAjax(gtModel::syncTable($table_id)); - } - - #[GetMapping(path: "gen_table/show")] - #[Auth(auth: "gen_table:show")] - public function genTableShow() - { - $table_id = $this->request->input("id"); - if (!$table_id) { - return $this->error("生成失败"); - } - $table = gtModel::info($table_id); - if (empty($table)) { - return $this->error("生成失败"); - } - // 表前缀 - $prefix = config("databases.default.prefix"); - // 表名 - $table_name = str_replace($prefix, "", $table['table_name']); - - preg_match_all('/([a-zA-Z])[a-zA-Z]*/', $table_name, $m); - $name1 = implode('', $m[1]); - $name2 = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $table_name)))); - $module_name = $table['module_name']; - $controller_name = $table['controller_name']; - // 搜搜字段 - $query_fields = []; - $insert_fields = []; - $edit_fields = []; - $list_fields = []; - $required_fields = []; - $fields = $table['gen_table_columns']; - foreach ($fields as $column) { - if ($column['is_query']) { - $query_fields[] = $column; - } - if ($column['is_insert'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) { - $insert_fields[] = $column; - } - if ($column['is_edit'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) { - $edit_fields[] = $column; - } - if ($column['is_list']) { - $list_fields[] = $column; - } - if ($column['is_required'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) { - $required_fields[] = $column; - } - } - $data = [ - 'model.php' => $this->render->getContents('templates/model.php.twig', compact("table_name", "fields", "controller_name", "query_fields", "list_fields")), - 'request.php' => $this->render->getContents('templates/request.php.twig', compact("controller_name", "insert_fields", "fields", "required_fields", "edit_fields")), - 'controller.php' => $this->render->getContents('templates/controller.php.twig', compact("controller_name", "module_name", "query_fields", "insert_fields", "edit_fields", "table_name", "name1", "name2")), - 'api.ts' => $this->render->getContents('templates/api.ts.twig', compact('table_name')), - 'index.vue' => $this->render->getContents('templates/index.vue.twig', compact('table_name', 'list_fields', 'query_fields', 'name2')), - 'save.vue' => $this->render->getContents('templates/save.vue.twig', compact('table_name', 'insert_fields', 'required_fields', 'edit_fields')) - ]; - return $this->success("模板信息", $data); - } - #[GetMapping(path: "asset_category/list")] #[Auth(needAuth: false)] public function assetCategoryList() diff --git a/app/Controller/Admin/Tools.php b/app/Controller/Admin/Tools.php new file mode 100644 index 0000000..8b793b5 --- /dev/null +++ b/app/Controller/Admin/Tools.php @@ -0,0 +1,219 @@ + 10]); + return $this->success("表列表", gtModel::list($param)); + } + + #[GetMapping(path: "gen_table/info")] + #[Auth(auth: "gen_table:info")] + public function genTableInfo() + { + $id = $this->request->input("id"); + return $this->success("详情", gtModel::info($id)); + } + + #[PostMapping(path: "gen_table/edit")] + #[Auth(auth: "gen_table:edit")] + public function genTableEdit() + { + $request = $this->container->get(gtRequest::class); + $request->scene('edit')->validateResolved(); + $param = Param::only(['table_id', 'table_name', 'table_comment', 'controller_name', 'module_name', 'remark', 'gen_table_columns']); + return $this->toAjax(gtModel::editData($param)); + } + + #[DeleteMapping(path: "gen_table/del")] + #[Auth(auth: "gen_table:del")] + public function genTableDel() + { + $param = Param::only(['ids' => []]); + return $this->toAjax(gtModel::del($param['ids'])); + } + + #[GetMapping(path: "gen_table/select")] + #[Auth(auth: "gen_table:select")] + public function genTableSelect() + { + $param = Param::only(['table_name', 'table_comment', 'limit' => 10, 'page' => 1]); + $offset = ($param['page'] - 1) * $param['limit']; + // 获取所有表 + $rows = Db::select("SHOW TABLE STATUS"); + // 获取已经导入的表 + $tables = gtModel::pluck("table_name")->toArray(); + // 表前缀 + $prefix = config("databases.default.prefix"); + // 构建表不导入 gen_table gen_table_column + $tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables); + $rows = array_filter($rows, function ($item) use ($tables, $param) { + // 过滤表 + if (in_array($item->Name, $tables)) { + return false; + } + // 表名筛选 + if (isset($param['table_name']) && $param['table_name'] != "" && !str_contains($item->Name, $param['table_name'])) { + return false; + } + // 表描述筛选 + if (isset($param['table_comment']) && $param['table_comment'] != "" && !str_contains($item->Comment, $param['table_comment'])) { + return false; + } + return true; + }); + $count = count($rows); + $rows = array_slice($rows, $offset, $param['limit']); + // 过滤返回数据 + $data = []; + foreach ($rows as $row) { + $data[] = [ + 'table_name' => $row->Name, + 'table_comment' => $row->Comment, + 'create_time' => $row->Create_time, + 'update_time' => $row->Update_time + ]; + } + return $this->success("表列表", $data, $count); + } + + #[PostMapping(path: "gen_table/build")] + #[Auth(auth: "gen_table:build")] + public function genTableBuild() + { + // 生成表数据 + $param = Param::only(['names' => []]); + if (empty($param['names'])) { + return $this->error("数据表不为空"); + } + // 获取已经导入的表 + $tables = gtModel::pluck("table_name")->toArray(); + // 表前缀 + $prefix = config("databases.default.prefix"); + // 构建表不导入 gen_table gen_table_column + $tables = array_merge([$prefix . "gen_table", $prefix . "gen_table_column"], $tables); + // 获取所有表 + $rows = Db::select("SHOW TABLE STATUS"); + // 去重 + $rows = array_filter($rows, function ($item) use ($tables, $param) { + // 过滤表 + if (in_array($item->Name, $tables)) { + return false; + } + // 表筛选 + return in_array($item->Name, $param['names']); + }); + if (empty($rows)) { + return $this->error("数据表不为空"); + } + // 生成数据 + $res = gtModel::genTable($rows); + return $res ? $this->success("操作成功") : $this->error("操作失败"); + } + + #[PostMapping(path: "gen_table/sync")] + #[Auth(auth: "gen_table:sync")] + public function genTableSync() + { + $table_id = $this->request->input("id"); + if (!$table_id) { + return $this->error("同步失败"); + } + return $this->toAjax(gtModel::syncTable($table_id)); + } + + #[GetMapping(path: "gen_table/show")] + #[Auth(auth: "gen_table:show")] + public function genTableShow() + { + $table_id = $this->request->input("id"); + if (!$table_id) { + return $this->error("生成失败"); + } + $table = gtModel::info($table_id); + if (empty($table)) { + return $this->error("生成失败"); + } + // 表前缀 + $prefix = config("databases.default.prefix"); + // 表名 + $table_name = str_replace($prefix, "", $table['table_name']); + + preg_match_all('/([a-zA-Z])[a-zA-Z]*/', $table_name, $m); + $name1 = implode('', $m[1]); + $name2 = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $table_name)))); + $module_name = $table['module_name']; + $controller_name = $table['controller_name']; + // 搜搜字段 + $query_fields = []; + $insert_fields = []; + $edit_fields = []; + $list_fields = []; + $required_fields = []; + $fields = $table['gen_table_columns']; + foreach ($fields as $column) { + if ($column['is_query']) { + $query_fields[] = $column; + } + if ($column['is_insert'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) { + $insert_fields[] = $column; + } + if ($column['is_edit'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) { + $edit_fields[] = $column; + } + if ($column['is_list']) { + $list_fields[] = $column; + } + if ($column['is_required'] && !in_array($column['column_name'], ['create_time', 'update_time', 'deleted_at'])) { + $required_fields[] = $column; + } + } + $data = [ + 'model.php' => $this->render->getContents('templates/model.php.twig', compact("table_name", "fields", "controller_name", "query_fields", "list_fields")), + 'request.php' => $this->render->getContents('templates/request.php.twig', compact("controller_name", "insert_fields", "fields", "required_fields", "edit_fields")), + 'controller.php' => $this->render->getContents('templates/controller.php.twig', compact("controller_name", "module_name", "query_fields", "insert_fields", "edit_fields", "table_name", "name1", "name2")), + 'api.ts' => $this->render->getContents('templates/api.ts.twig', compact('table_name')), + 'index.vue' => $this->render->getContents('templates/index.vue.twig', compact('table_name', 'list_fields', 'query_fields', 'name2')), + 'save.vue' => $this->render->getContents('templates/save.vue.twig', compact('table_name', 'insert_fields', 'required_fields', 'edit_fields')) + ]; + return $this->success("模板信息", $data); + } + + #[PostMapping(path: "form/build")] + #[Auth(auth: "form:build")] + public function formBuild() + { + $param = Param::only(['fields' => [], 'config' => []]); + if (empty($param['fields']) || empty($param['config'])) { + return $this->error("生成失败,重要参数未传递"); + } + if (isset($param['config']['isMobile']) && $param['config']['isMobile']) { + $data = [ + 'form.vue' => $this->render->getContents('templates/formMobile.vue.twig', $param) + ]; + } else { + $data = [ + 'form.vue' => $this->render->getContents('templates/formDesktop.vue.twig', $param) + ]; + } + return $this->success("表单信息", $data); + } +} \ No newline at end of file diff --git a/static/view/templates/formDesktop.vue.twig b/static/view/templates/formDesktop.vue.twig new file mode 100644 index 0000000..1a72a44 --- /dev/null +++ b/static/view/templates/formDesktop.vue.twig @@ -0,0 +1,78 @@ + + + diff --git a/static/view/templates/formMobile.vue.twig b/static/view/templates/formMobile.vue.twig new file mode 100644 index 0000000..e69de29