Files
2026-09-29 10:14:04 +08:00

74 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 10.8 实战案例
:::tip 概述
以「案例演示」模块为例,演示代码生成器的完整使用流程。
:::
## 第 1 步:建表
```sql
CREATE TABLE think_example (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(200) NOT NULL COMMENT '案例名称',
type TINYINT(1) DEFAULT 0 COMMENT '案例类型',
status TINYINT(1) DEFAULT 0 COMMENT '案例状态',
sort INT DEFAULT 0 COMMENT '排序',
avatar VARCHAR(500) DEFAULT '' COMMENT '头像',
is_delete TINYINT(1) DEFAULT 0,
create_user VARCHAR(50) DEFAULT '',
create_time DATETIME,
update_user VARCHAR(50) DEFAULT '',
update_time DATETIME
);
```
## 第 2 步:代码生成器操作
1. 进入「工具管理 → 代码生成」
2. 找到 `think_example` 表
3. 配置:
- 模块名:`example`
- 功能名:`案例演示`
- 作者:`admin`
- 是否导入导出:是
4. 点击「预览」查看生成代码
5. 点击「生成」创建文件
## 第 3 步:生成的文件
```text
app/controller/ExampleController.php # 控制器
app/logic/ExampleLogic.php # 业务逻辑
app/model/Example.php # 数据模型
app/validate/ExampleValidate.php # 验证器
ui/src/api/tool/example.ts # 前端 API
ui/src/views/tool/example/index.vue # 列表页
ui/src/views/tool/example/edit.vue # 编辑弹窗
ui/src/views/tool/example/detail.vue # 详情弹窗
ui/src/views/tool/example/columns.ts # 表格列配置
ui/src/views/tool/example/querySchemas.ts # 查询表单配置
```
## 第 4 步:后续操作
生成完成后,以下内容已自动创建:
| 项目 | 状态 | 说明 |
|------|------|------|
| 路由 | ✅ 自动注册 | `route/app.php` 中已插入 CRUD 路由组 |
| 菜单 | ✅ 自动创建 | 主菜单和权限节点已创建 |
| 字典 | ✅ 自动创建 | 带选项的字段已创建字典和字典项 |
通常还需要手动调整:
1. 根据业务需求调整 Logic 属性配置(如 `pageLikeFields`、`pageEqFields`)
2. 补充自定义钩子(如 `beforeDelete`、`afterDetail` 等)
3. 在菜单管理中调整菜单排序和图标
:::tip 生成后调整
生成的代码是基础骨架,通常需要:
- 调整 Logic 的查询条件配置
- 添加删除前校验(beforeDelete)
- 补充详情后的关联数据(afterDetail)
- 配置文件字段和富文本字段
:::