签入版本
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# 10.1 概述与架构
|
||||
|
||||
:::tip 概述
|
||||
代码生成器可根据数据库表结构一键生成 CRUD 模块的全部代码,包括后端 Controller/Logic/Model/Validate 和前端 API/View。支持 MySQL、PostgreSQL、SQL Server、Oracle、SQLite 五种数据库。
|
||||
代码生成器可根据数据库表结构一键生成 CRUD 模块的全部代码,包括后端 Controller/Service/Model/FormRequest 和前端 API/View。支持 MySQL、PostgreSQL、SQL Server、Oracle、SQLite 五种数据库。
|
||||
:::
|
||||
|
||||
## 使用方式
|
||||
@@ -12,17 +12,17 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
# 代码生成器 CLI 命令
|
||||
# -----------------------------------------------------------------------------
|
||||
# php think generate:code → 调用 ThinkPHP 命令行的代码生成器
|
||||
# Example "案例演示" → 表名与注释(用 | 分隔,如 think_example|案例演示)
|
||||
# --dry-run → 仅预览,不写入文件
|
||||
php think generate:code think_example|案例演示 --dry-run
|
||||
# php artisan generate:code → 调用 Artisan 的代码生成器
|
||||
# laravel_example|案例演示 → 表名与注释(用 | 分隔,注释用于菜单标题)
|
||||
# --dry-run → 仅预览,不写入文件
|
||||
php artisan generate:code "laravel_example|案例演示" --dry-run
|
||||
```
|
||||
|
||||
详见 [CLI 命令行使用](/zh/generator/cli.html)。
|
||||
|
||||
### 方式二:Web 管理界面
|
||||
|
||||
进入「工具管理 → 代码生成」菜单,可视化操作。
|
||||
进入「开发工具 → 代码生成」菜单,可视化操作。
|
||||
|
||||
详见 [Web 管理界面](/zh/generator/web-ui.html)。
|
||||
|
||||
@@ -30,7 +30,7 @@ php think generate:code think_example|案例演示 --dry-run
|
||||
|
||||
```text
|
||||
┌──────────────┐
|
||||
│ 数据库表 │ 如 think_example
|
||||
│ 数据库表 │ 如 laravel_example
|
||||
└──────┬───────┘
|
||||
│
|
||||
▼
|
||||
@@ -42,27 +42,27 @@ php think generate:code think_example|案例演示 --dry-run
|
||||
▼
|
||||
┌──────────────┐
|
||||
│ GeneratorService│ 字段类型归一化 + 属性分析
|
||||
│ ::getTableColumns│ (图片、富文本、筛选项、唯一等)
|
||||
│ (表结构解析) │ (图片、富文本、筛选项、唯一等)
|
||||
└──────┬───────┘
|
||||
│
|
||||
▼
|
||||
┌──────────────┐
|
||||
│ GeneratorService│ 模板变量构建 + 模板渲染
|
||||
│ GeneratorLogic │ 模板变量构建 + 模板渲染
|
||||
│ ::preview() │
|
||||
└──────┬───────┘
|
||||
│
|
||||
▼
|
||||
┌──────────────┐
|
||||
│ GeneratorService│ 文件生成 + 菜单创建
|
||||
│ GeneratorLogic │ 文件生成 + 菜单/字典/路由创建
|
||||
│ ::generate() │
|
||||
└──────┬───────┘
|
||||
│
|
||||
┌────┼────┬────┬────┐
|
||||
▼ ▼ ▼ ▼ ▼
|
||||
┌────┐┌────┐┌────┐┌────┐┌────┐
|
||||
│Ctrl││Logi││Mode││Vali││API │
|
||||
│ . ││ c ││ l ││date││+Vue│
|
||||
│php ││php ││php ││php ││ │
|
||||
│Ctrl││Serv││Mode││Req ││API │
|
||||
│ . ││ ice││ l ││ est││+Vue│
|
||||
│php ││ .php││php ││php ││ │
|
||||
└────┘└────┘└────┘└────┘└────┘
|
||||
```
|
||||
|
||||
@@ -72,19 +72,19 @@ php think generate:code think_example|案例演示 --dry-run
|
||||
|------|------|
|
||||
| 多数据库适配 | MySQL / PostgreSQL / SQL Server / Oracle / SQLite |
|
||||
| 字段分析 | 自动识别图片字段、富文本字段、筛选字段、唯一字段 |
|
||||
| 模板渲染 | 基于 PHP 模板引擎,支持自定义模板 |
|
||||
| 菜单创建 | 自动生成菜单和权限节点 |
|
||||
| 模板渲染 | 基于 PHP 模板引擎(`resources/templates/`),支持自定义模板 |
|
||||
| 菜单创建 | 自动生成菜单和权限节点(挂载位置由 `GENERATOR_PARENT_MENU_ID` 决定) |
|
||||
| 字典创建 | 自动创建关联字典(含字典项),幂等不重复 |
|
||||
| 路由注册 | 自动在 route/app.php 中插入 CRUD 路由组 |
|
||||
| 路由注册 | 自动在 `routes/api.php` 中插入 CRUD 路由组 |
|
||||
| 安全校验 | 表名合法性校验,防止 SQL 注入 |
|
||||
| CLI 命令行 | `php think generate:code` 命令行操作,支持预览、导出配置、批量生成 |
|
||||
| CLI 命令行 | `php artisan generate:code`,支持预览、导出配置、批量生成 |
|
||||
|
||||
## API 接口
|
||||
|
||||
| 方法 | 路径 | 权限码 | 说明 |
|
||||
|------|------|--------|------|
|
||||
| GET | `/api/generator/page` | sys:generator:page | 获取可生成的表列表 |
|
||||
| POST | `/api/generator/preview` | sys:generator:generate | 预览生成代码(不写入文件) |
|
||||
| GET | `/api/generator/preview` | sys:generator:generate | 预览生成代码(不写入文件) |
|
||||
| POST | `/api/generator/generate` | sys:generator:generate | 一键生成 |
|
||||
| POST | `/api/generator/batchGenerate` | sys:generator:generate | 批量生成 |
|
||||
|
||||
@@ -96,30 +96,27 @@ php think generate:code think_example|案例演示 --dry-run
|
||||
# -----------------------------------------------------------------------------
|
||||
# 列出所有可生成的数据库表
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code
|
||||
php artisan generate:code
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 预览生成代码(不写入文件,仅输出到终端)
|
||||
# -----------------------------------------------------------------------------
|
||||
# --dry-run → 干运行模式(只预览不执行)
|
||||
php think generate:code think_example --dry-run
|
||||
php artisan generate:code laravel_example --dry-run
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 一键生成(实际写入文件到 app/ 和 ui/src/)
|
||||
# -----------------------------------------------------------------------------
|
||||
php think generate:code think_example
|
||||
php artisan generate:code laravel_example
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 导出配置(将生成参数保存为 JSON,便于重复使用)
|
||||
# -----------------------------------------------------------------------------
|
||||
# --output config.json → 输出配置文件路径
|
||||
php think generate:code think_example --output config.json
|
||||
php artisan generate:code laravel_example --output config.json
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 从配置生成(读取之前导出的配置文件)
|
||||
# -----------------------------------------------------------------------------
|
||||
# --config config.json → 读取配置文件
|
||||
php think generate:code --config config.json
|
||||
php artisan generate:code --config config.json
|
||||
```
|
||||
|
||||
详见 [10.2 CLI 命令行使用](/zh/generator/cli.html)
|
||||
@@ -131,26 +128,21 @@ preview 接口标注了 `#[DemoAllow]`,演示环境下也可使用。generate
|
||||
## 核心代码
|
||||
|
||||
```php
|
||||
// app/controller/GeneratorController.php — 代码生成器控制器
|
||||
// app/Http/Controllers/Api/GeneratorController.php — 代码生成器控制器(形态示例)
|
||||
|
||||
class GeneratorController extends BaseController
|
||||
class GeneratorController extends Controller
|
||||
{
|
||||
protected GeneratorLogic $logic;
|
||||
|
||||
protected function initialize(): void
|
||||
{
|
||||
$this->logic = new GeneratorLogic(); // 初始化代码生成器业务逻辑
|
||||
}
|
||||
public function __construct(protected GeneratorLogic $logic) {}
|
||||
|
||||
// 预览生成代码(不写入文件,仅返回渲染结果)
|
||||
#[Log('代码生成-预览代码', Log::TYPE_QUERY)]
|
||||
#[OperationLog('代码生成-预览代码', OperationLog::TYPE_QUERY)]
|
||||
#[Permission('sys:generator:generate', '代码生成')]
|
||||
#[DemoAllow] // 演示模式允许(只读操作)
|
||||
public function preview(): Json
|
||||
public function preview(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$params = $this->getJsonBody(); // 获取前端传入的 JSON 参数
|
||||
$result = $this->logic->preview($params); // 调用逻辑层预览方法
|
||||
$params = $this->getJsonBody(); // 获取前端传入的 JSON 参数
|
||||
$result = $this->logic->preview($params); // 调用逻辑层预览方法
|
||||
return $this->success($result);
|
||||
} catch (\Throwable $e) {
|
||||
// 返回详细错误信息(包含文件名和行号,便于调试)
|
||||
@@ -159,13 +151,13 @@ class GeneratorController extends BaseController
|
||||
}
|
||||
|
||||
// 一键生成(实际写入文件到 app/ 和 ui/src/ 目录)
|
||||
#[Log('代码生成-一键生成', Log::TYPE_GENERATE)]
|
||||
#[OperationLog('代码生成-一键生成', OperationLog::TYPE_GENERATE)]
|
||||
#[Permission('sys:generator:generate', '代码生成')]
|
||||
public function generate(): Json
|
||||
public function generate(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$params = $this->getJsonBody();
|
||||
$result = $this->logic->generate($params); // 调用逻辑层生成方法
|
||||
$result = $this->logic->generate($params);
|
||||
|
||||
$generated = count($result['generated']); // 成功生成的文件数
|
||||
$skipped = count($result['skipped']); // 跳过的文件数(已存在)
|
||||
@@ -186,21 +178,36 @@ class GeneratorController extends BaseController
|
||||
## 模板文件
|
||||
|
||||
```text
|
||||
templates/ # 代码生成器模板目录
|
||||
├── controller.php.tpl # 控制器模板
|
||||
├── logic.php.tpl # 业务逻辑模板(含 serializeMaps、导入导出)
|
||||
├── model.php.tpl # 数据模型模板
|
||||
├── validate.php.tpl # 验证器模板
|
||||
├── ui/ # 普通列表模板(6个前端文件)
|
||||
│ ├── api.ts.tpl # API 接口模块
|
||||
│ ├── index.vue.tpl # 列表页
|
||||
│ ├── edit.vue.tpl # 编辑弹窗
|
||||
│ ├── detail.vue.tpl # 详情弹窗
|
||||
│ ├── columns.ts.tpl # 表格列配置
|
||||
│ └── querySchemas.ts.tpl # 查询表单配置
|
||||
└── ui2/ # 树形结构模板(4个前端文件)
|
||||
├── api.ts.tpl
|
||||
├── index.vue.tpl
|
||||
├── edit.vue.tpl
|
||||
└── detail.vue.tpl
|
||||
resources/templates/ # 代码生成器模板目录
|
||||
├── controller.php.tpl # 控制器模板
|
||||
├── service.php.tpl # 服务层模板(含 serializeMaps、导入导出)
|
||||
├── model.php.tpl # 数据模型模板
|
||||
├── request.php.tpl # FormRequest 模板
|
||||
├── ui/ # 普通列表模板(6 个前端文件)
|
||||
│ ├── api.ts.tpl # API 接口模块
|
||||
│ ├── index.vue.tpl # 列表页
|
||||
│ ├── edit.vue.tpl # 编辑弹窗
|
||||
│ ├── detail.vue.tpl # 详情弹窗
|
||||
│ ├── columns.ts.tpl # 表格列配置
|
||||
│ └── querySchemas.ts.tpl # 查询表单配置
|
||||
└── ui2/ # 树形结构模板(前端文件)
|
||||
```
|
||||
|
||||
模板与输出路径的映射关系见 [10.7 配置文件详解](/zh/generator/config.html),自定义模板见 [10.6 自定义模板开发](/zh/generator/templates.html)。
|
||||
|
||||
## 生成流程时序(从建表到可用)
|
||||
|
||||
```text
|
||||
1. 数据库建表(含公共字段,见 3.2)
|
||||
2. 生成(Web 或 CLI)
|
||||
├─ 后端 4 文件:Model / FormRequest / Service / Controller
|
||||
├─ 前端文件:api.ts + views(index/edit/detail/columns/querySchemas)
|
||||
├─ routes/api.php 自动插入 CRUD 路由组
|
||||
├─ laravel_menu 自动创建菜单 + 权限节点(挂 GENERATOR_PARENT_MENU_ID 下)
|
||||
└─ 字典自动创建(带选项字段,幂等)
|
||||
3. 调整(Service 属性 / 钩子 / 菜单图标排序)
|
||||
4. 为角色分配菜单权限
|
||||
5. 前端刷新(重新登录使动态路由生效)→ 测试
|
||||
```
|
||||
|
||||
(完整实战见 [10.8 实战案例](/zh/generator/demo.html),手动开发流程见 [第 3 章 模块开发实战](/zh/guide/overview.html)。)
|
||||
|
||||
Reference in New Issue
Block a user