@robsun/create-keystone-app 0.2.13 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -43
- package/dist/create-keystone-app.js +347 -10
- package/dist/create-module.js +1219 -607
- package/package.json +22 -23
- package/template/.claude/skills/keystone-implement/SKILL.md +113 -0
- package/template/.claude/skills/keystone-implement/references/CHECKLIST.md +91 -0
- package/template/.claude/skills/keystone-implement/references/PATTERNS.md +1088 -0
- package/template/.claude/skills/keystone-implement/references/SCHEMA.md +135 -0
- package/template/.claude/skills/keystone-implement/references/TESTING.md +231 -0
- package/template/.claude/skills/keystone-requirements/SKILL.md +296 -0
- package/template/.claude/skills/keystone-requirements/references/CONFIRM_TEMPLATE.md +170 -0
- package/template/.claude/skills/keystone-requirements/references/SCHEMA.md +135 -0
- package/template/.eslintrc.js +3 -0
- package/template/.github/workflows/ci.yml +30 -0
- package/template/.github/workflows/release.yml +32 -0
- package/template/.golangci.yml +11 -0
- package/template/README.md +81 -73
- package/template/apps/server/README.md +8 -0
- package/template/apps/server/cmd/server/main.go +27 -185
- package/template/apps/server/config.example.yaml +31 -1
- package/template/apps/server/config.yaml +31 -1
- package/template/apps/server/go.mod +60 -18
- package/template/apps/server/go.sum +183 -31
- package/template/apps/server/internal/frontend/embed.go +3 -8
- package/template/apps/server/internal/modules/example/README.md +18 -0
- package/template/apps/server/internal/modules/example/api/handler/handler_test.go +9 -0
- package/template/apps/server/internal/modules/example/api/handler/item_handler.go +468 -165
- package/template/apps/server/internal/modules/example/bootstrap/seeds/item.go +217 -8
- package/template/apps/server/internal/modules/example/domain/models/item.go +40 -7
- package/template/apps/server/internal/modules/example/domain/service/approval_callback.go +68 -0
- package/template/apps/server/internal/modules/example/domain/service/approval_schema.go +41 -0
- package/template/apps/server/internal/modules/example/domain/service/errors.go +20 -22
- package/template/apps/server/internal/modules/example/domain/service/item_service.go +267 -7
- package/template/apps/server/internal/modules/example/domain/service/item_service_test.go +281 -0
- package/template/apps/server/internal/modules/example/i18n/keys.go +32 -20
- package/template/apps/server/internal/modules/example/i18n/locales/en-US.json +30 -18
- package/template/apps/server/internal/modules/example/i18n/locales/zh-CN.json +30 -18
- package/template/apps/server/internal/modules/example/infra/exporter/item_exporter.go +119 -0
- package/template/apps/server/internal/modules/example/infra/importer/item_importer.go +77 -0
- package/template/apps/server/internal/modules/example/infra/repository/item_repository.go +99 -49
- package/template/apps/server/internal/modules/example/module.go +171 -97
- package/template/apps/server/internal/modules/example/tests/integration_test.go +7 -0
- package/template/apps/server/internal/modules/manifest.go +7 -7
- package/template/apps/web/README.md +4 -2
- package/template/apps/web/package.json +1 -1
- package/template/apps/web/src/app.config.ts +8 -6
- package/template/apps/web/src/index.css +7 -3
- package/template/apps/web/src/main.tsx +2 -5
- package/template/apps/web/src/modules/example/help/en-US/faq.md +27 -0
- package/template/apps/web/src/modules/example/help/en-US/items.md +30 -0
- package/template/apps/web/src/modules/example/help/en-US/overview.md +31 -0
- package/template/apps/web/src/modules/example/help/zh-CN/faq.md +27 -0
- package/template/apps/web/src/modules/example/help/zh-CN/items.md +31 -0
- package/template/apps/web/src/modules/example/help/zh-CN/overview.md +32 -0
- package/template/apps/web/src/modules/example/locales/en-US/example.json +99 -32
- package/template/apps/web/src/modules/example/locales/zh-CN/example.json +85 -18
- package/template/apps/web/src/modules/example/pages/ExampleItemsPage.tsx +840 -237
- package/template/apps/web/src/modules/example/services/exampleItems.ts +79 -8
- package/template/apps/web/src/modules/example/types.ts +14 -1
- package/template/apps/web/src/modules/index.ts +1 -0
- package/template/apps/web/vite.config.ts +9 -3
- package/template/docs/CONVENTIONS.md +10 -7
- package/template/package.json +4 -5
- package/template/pnpm-lock.yaml +76 -5
- package/template/scripts/build.bat +15 -3
- package/template/scripts/build.sh +9 -3
- package/template/scripts/check-help.js +249 -0
- package/template/scripts/compress-assets.js +89 -0
- package/template/scripts/test.bat +23 -0
- package/template/scripts/test.sh +16 -0
- package/template/.claude/skills/keystone-dev/SKILL.md +0 -103
- package/template/.claude/skills/keystone-dev/references/APPROVAL.md +0 -121
- package/template/.claude/skills/keystone-dev/references/CAPABILITIES.md +0 -261
- package/template/.claude/skills/keystone-dev/references/TEMPLATES.md +0 -532
- package/template/.claude/skills/keystone-dev/references/TESTING.md +0 -44
- package/template/.codex/skills/keystone-dev/SKILL.md +0 -103
- package/template/.codex/skills/keystone-dev/references/APPROVAL.md +0 -121
- package/template/.codex/skills/keystone-dev/references/CAPABILITIES.md +0 -261
- package/template/.codex/skills/keystone-dev/references/TEMPLATES.md +0 -532
- package/template/.codex/skills/keystone-dev/references/TESTING.md +0 -44
- package/template/apps/server/internal/app/routes/module_routes.go +0 -16
- package/template/apps/server/internal/app/routes/routes.go +0 -226
- package/template/apps/server/internal/app/startup/startup.go +0 -74
- package/template/apps/server/internal/frontend/handler.go +0 -122
- package/template/apps/server/internal/modules/registry.go +0 -145
- package/template/apps/web/src/modules/example/help/faq.md +0 -23
- package/template/apps/web/src/modules/example/help/items.md +0 -26
- package/template/apps/web/src/modules/example/help/overview.md +0 -25
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# Keystone 审批接入流程
|
|
2
|
-
|
|
3
|
-
## 目标
|
|
4
|
-
本文只描述审批接入所需的表单 schema 与 context 映射等开发工作,
|
|
5
|
-
流程节点与审批路径由人工在审批配置页面完成。
|
|
6
|
-
|
|
7
|
-
## 适用场景
|
|
8
|
-
- 新业务需要审批门禁(新业务类型)。
|
|
9
|
-
- 既有业务接入审批门禁(在业务表单提交或关键业务动作前加审批)。
|
|
10
|
-
|
|
11
|
-
## 不在范围
|
|
12
|
-
- 配置审批流程与节点(由人工在流程配置页面完成)。
|
|
13
|
-
- 修改 approval 基础模块或审批页面本身。
|
|
14
|
-
- 改造审批权限体系或审批路由结构。
|
|
15
|
-
|
|
16
|
-
## 开发路径选择
|
|
17
|
-
- Greenfield:新业务从零引入审批。
|
|
18
|
-
- Retrofit:已有业务接入审批。
|
|
19
|
-
|
|
20
|
-
## 开发步骤 A:新业务(Greenfield)
|
|
21
|
-
1) 前端配置 `approval.businessTypes` 标签映射(用于流程配置时选择表单),
|
|
22
|
-
流程配置页面的业务类型选择会作为 schema code 加载表单字段,需保持一致。
|
|
23
|
-
2) 注册表单 schema(条件节点字段来源)。
|
|
24
|
-
3) 在业务提交处创建审批实例,写入 context JSON 用于条件路由。
|
|
25
|
-
4) 绑定 OnApproved/OnRejected 回调更新业务状态。
|
|
26
|
-
5) 按测试清单执行(见 testing.md)。
|
|
27
|
-
|
|
28
|
-
## 开发步骤 B:已有业务接入(Retrofit)
|
|
29
|
-
1) 确认触发点与审批边界(业务表单提交或关键业务动作前)。
|
|
30
|
-
2) 将现有字段映射为 context JSON(供条件节点判断)。
|
|
31
|
-
3) 做幂等:按业务类型 + 业务 ID 查已有实例再创建。
|
|
32
|
-
4) pending 期间阻断业务动作(状态拦截或硬拦截)。
|
|
33
|
-
5) 决定是否回填存量数据(跳过或补建实例)。
|
|
34
|
-
|
|
35
|
-
## 业务侧改动清单
|
|
36
|
-
|
|
37
|
-
后端(业务模块内)
|
|
38
|
-
- 在业务 service/handler 中调用审批服务创建实例(必要时通过 FlowMatcher 选 flow)。
|
|
39
|
-
- 在业务模块注册审批回调,审批通过/驳回时更新业务状态。
|
|
40
|
-
- 需要数据范围过滤时,实现 ApprovalContextResolver 并在应用装配时注入。
|
|
41
|
-
- 如需默认 flow 或 schema,放在业务模块的迁移/seed 中,不改动审批模块。
|
|
42
|
-
|
|
43
|
-
前端(业务模块内)
|
|
44
|
-
- 配置 `approval.businessTypes` 标签映射,保证业务类型显示正确。
|
|
45
|
-
- 流程配置使用已有审批管理页面,不做二次开发。
|
|
46
|
-
|
|
47
|
-
## 权限说明
|
|
48
|
-
使用系统既有审批权限码:
|
|
49
|
-
approval:flow:view|create|edit|delete
|
|
50
|
-
approval:instance:view|approve|cancel
|
|
51
|
-
approval:record:view|export
|
|
52
|
-
|
|
53
|
-
## 流程版本控制
|
|
54
|
-
|
|
55
|
-
审批流程支持版本管理,确保活跃实例不受流程修改影响:
|
|
56
|
-
|
|
57
|
-
**状态生命周期**:`draft` → `published` → `deprecated`
|
|
58
|
-
|
|
59
|
-
- **draft**: 草稿状态,可编辑,不能创建实例
|
|
60
|
-
- **published**: 已发布,不可编辑,可创建实例
|
|
61
|
-
- **deprecated**: 已废弃,不可编辑,不可创建新实例
|
|
62
|
-
|
|
63
|
-
**操作方式**:
|
|
64
|
-
```go
|
|
65
|
-
// 发布流程(draft → published)
|
|
66
|
-
approvalService.PublishFlow(ctx, tenantID, flowID)
|
|
67
|
-
|
|
68
|
-
// 创建新版本(基于已发布流程)
|
|
69
|
-
newFlow, err := approvalService.CreateNewVersion(ctx, tenantID, flowID)
|
|
70
|
-
// 新版本为 draft 状态,ParentFlowID 指向原流程
|
|
71
|
-
|
|
72
|
-
// 废弃旧版本(自动在发布新版本时执行)
|
|
73
|
-
approvalService.DeprecateFlow(ctx, tenantID, flowID)
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## 回调重试机制
|
|
77
|
-
|
|
78
|
-
审批回调(OnApproved/OnRejected)支持自动重试:
|
|
79
|
-
|
|
80
|
-
```go
|
|
81
|
-
import approval "github.com/robsuncn/keystone/domain/approval/service"
|
|
82
|
-
|
|
83
|
-
// 配置重试策略
|
|
84
|
-
config := approval.RetryConfig{
|
|
85
|
-
MaxRetries: 3, // 最大重试次数
|
|
86
|
-
InitialBackoff: time.Second, // 初始退避时间
|
|
87
|
-
MaxBackoff: 30 * time.Second, // 最大退避时间
|
|
88
|
-
BackoffFactor: 2.0, // 退避倍数(指数退避)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 包装回调
|
|
92
|
-
retryable := approval.NewRetryableCallback(myCallback, config)
|
|
93
|
-
|
|
94
|
-
// 或使用异步执行器
|
|
95
|
-
executor := approval.NewAsyncCallbackExecutor(registry, config, 100)
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## 集成自检
|
|
99
|
-
- Flow code <= 50 且使用大写;business type 用短字符串。
|
|
100
|
-
- approve/reject/cancel 使用 version 做乐观锁。
|
|
101
|
-
- 变更接口时更新 OpenAPI 合同。
|
|
102
|
-
- 新增默认流程时补迁移或 seed。
|
|
103
|
-
- **新流程从 draft 开始,发布后才能创建实例**。
|
|
104
|
-
- **使用 RetryableCallback 包装业务回调以增强可靠性**。
|
|
105
|
-
|
|
106
|
-
测试清单见 [testing.md](testing.md)。
|
|
107
|
-
|
|
108
|
-
## 参考入口
|
|
109
|
-
|
|
110
|
-
后端
|
|
111
|
-
- domain/approval/service/service.go
|
|
112
|
-
- domain/approval/service/flow_matcher.go
|
|
113
|
-
- domain/approval/service/callback_registry.go
|
|
114
|
-
- api/routes/approval_routes.go
|
|
115
|
-
- contracts/011-approvals/approvals-api.yaml
|
|
116
|
-
|
|
117
|
-
前端
|
|
118
|
-
- packages/keystone-web-core/src/platform/config/appConfig.ts
|
|
119
|
-
- packages/keystone-web-core/src/platform/core/approval/services/approvalService.ts
|
|
120
|
-
- packages/keystone-web-core/src/platform/core/approval/stores/approvalStore.ts
|
|
121
|
-
- packages/keystone-web-core/src/modules/keystone/approval/pages/*
|
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
# Keystone 包完整能力清单
|
|
2
|
-
|
|
3
|
-
## 一、前端组件 (@robsun/keystone-web-core)
|
|
4
|
-
|
|
5
|
-
### 1. 应用与路由
|
|
6
|
-
```typescript
|
|
7
|
-
import { KeystoneApp, createKeystoneRouter } from '@robsun/keystone-web-core'
|
|
8
|
-
import { registerModule, registerRoutes, registerMenus, registerPermissions } from '@robsun/keystone-web-core'
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
### 2. 高级 UI 组件
|
|
12
|
-
```typescript
|
|
13
|
-
// 高级表格 - 过滤、分页、行选择、导出
|
|
14
|
-
import { ProTable } from '@robsun/keystone-web-core'
|
|
15
|
-
|
|
16
|
-
// 智能表单 - 自动验证、加载状态
|
|
17
|
-
import { ProForm } from '@robsun/keystone-web-core'
|
|
18
|
-
|
|
19
|
-
// 文件上传 - 拖拽、预览、多文件
|
|
20
|
-
import { FileUpload } from '@robsun/keystone-web-core'
|
|
21
|
-
|
|
22
|
-
// 数据导入向导
|
|
23
|
-
import { DataImporter, ImportWizard, ColumnMapper, ValidationResult } from '@robsun/keystone-web-core'
|
|
24
|
-
|
|
25
|
-
// 数据导出向导
|
|
26
|
-
import { DataExporter, ExportWizard } from '@robsun/keystone-web-core'
|
|
27
|
-
|
|
28
|
-
// 权限守卫
|
|
29
|
-
import { PermissionGuard } from '@robsun/keystone-web-core'
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
### 3. 审批流系统
|
|
33
|
-
```typescript
|
|
34
|
-
import {
|
|
35
|
-
ApprovalFlowEditor, // 流程编辑器
|
|
36
|
-
ApprovalFlowCanvas, // 可视化画布
|
|
37
|
-
ApprovalNodeEditor, // 节点编辑
|
|
38
|
-
ConditionEditor, // 条件编辑
|
|
39
|
-
ApprovalStatus, // 状态展示
|
|
40
|
-
ApprovalAction, // 审批按钮
|
|
41
|
-
ApprovalHistory // 审批历史
|
|
42
|
-
} from '@robsun/keystone-web-core'
|
|
43
|
-
```
|
|
44
|
-
审批流程与接入说明见 [approval.md](approval.md)。
|
|
45
|
-
|
|
46
|
-
### 4. 帮助系统
|
|
47
|
-
```typescript
|
|
48
|
-
import {
|
|
49
|
-
HelpButton, HelpDrawer, HelpSearch, HelpContent,
|
|
50
|
-
useCurrentHelpKey, useHelpSearch
|
|
51
|
-
} from '@robsun/keystone-web-core'
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### 5. 布局与主题
|
|
55
|
-
```typescript
|
|
56
|
-
import { MainLayout, antdTheme, useLayoutStore } from '@robsun/keystone-web-core'
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### 6. 认证授权
|
|
60
|
-
```typescript
|
|
61
|
-
import { useAuthStore, useAuth, matchesPermission } from '@robsun/keystone-web-core'
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### 7. 运行时服务
|
|
65
|
-
```typescript
|
|
66
|
-
import { api, setApiBaseUrl } from '@robsun/keystone-web-core'
|
|
67
|
-
import { createExportJob, createImportJob } from '@robsun/keystone-web-core'
|
|
68
|
-
import { listNotifications, markNotificationRead, fetchUnreadCount } from '@robsun/keystone-web-core'
|
|
69
|
-
import { getJob, listJobs } from '@robsun/keystone-web-core'
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### 8. 多语言 (i18n)
|
|
73
|
-
```typescript
|
|
74
|
-
// 翻译 Hook
|
|
75
|
-
import { useTranslation } from 'react-i18next'
|
|
76
|
-
|
|
77
|
-
// 使用示例
|
|
78
|
-
const { t } = useTranslation()
|
|
79
|
-
const title = t('module:key') // 命名空间:键
|
|
80
|
-
const description = t('common:actions.save')
|
|
81
|
-
|
|
82
|
-
// 平台多语言配置
|
|
83
|
-
import { getKeystoneConfig } from '@robsun/keystone-web-core'
|
|
84
|
-
const locale = getKeystoneConfig().ui?.i18n?.defaultLocale // 'zh-CN' | 'en-US'
|
|
85
|
-
|
|
86
|
-
// dayjs 本地化同步(自动完成)
|
|
87
|
-
import dayjs from 'dayjs'
|
|
88
|
-
dayjs.locale(getKeystoneConfig().ui?.locale?.dayjs ?? 'zh-cn')
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
**翻译文件组织**:
|
|
92
|
-
```
|
|
93
|
-
src/modules/{module}/locales/
|
|
94
|
-
├── zh-CN/
|
|
95
|
-
│ └── {namespace}.json
|
|
96
|
-
└── en-US/
|
|
97
|
-
└── {namespace}.json
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
**命名空间约定**:
|
|
101
|
-
- `common` - 通用文案(按钮、操作、状态等)
|
|
102
|
-
- `auth` - 认证相关
|
|
103
|
-
- `system` - 系统管理
|
|
104
|
-
- `{module}` - 业务模块专属翻译
|
|
105
|
-
|
|
106
|
-
### 9. Hooks
|
|
107
|
-
```typescript
|
|
108
|
-
import { useListState } from '@robsun/keystone-web-core'
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### 10. 类型
|
|
112
|
-
```typescript
|
|
113
|
-
import type { ApiResponse, PaginatedData, PaginatedResponse } from '@robsun/keystone-web-core'
|
|
114
|
-
import type { Permission, ModuleName, Action } from '@robsun/keystone-web-core'
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
## 二、后端能力 (Go)
|
|
118
|
-
|
|
119
|
-
### 1. 模块系统
|
|
120
|
-
```go
|
|
121
|
-
type Module interface {
|
|
122
|
-
Name() string
|
|
123
|
-
RegisterRoutes(r *gin.RouterGroup)
|
|
124
|
-
RegisterPermissions() []Permission
|
|
125
|
-
Migrate(db *gorm.DB) error
|
|
126
|
-
Seed(db *gorm.DB) error
|
|
127
|
-
}
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### 2. 审批引擎
|
|
131
|
-
```go
|
|
132
|
-
// 基础操作
|
|
133
|
-
approvalService.CreateInstance(...)
|
|
134
|
-
approvalService.Approve(...)
|
|
135
|
-
approvalService.Reject(...)
|
|
136
|
-
approvalService.Cancel(...)
|
|
137
|
-
|
|
138
|
-
// 流程版本控制
|
|
139
|
-
approvalService.PublishFlow(...) // draft → published
|
|
140
|
-
approvalService.DeprecateFlow(...) // published → deprecated
|
|
141
|
-
approvalService.CreateNewVersion(...) // 基于现有流程创建新版本
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
**流程状态生命周期**:`draft` → `published` → `deprecated`
|
|
145
|
-
|
|
146
|
-
### 3. 审批回调重试
|
|
147
|
-
```go
|
|
148
|
-
import approval "github.com/robsuncn/keystone/domain/approval/service"
|
|
149
|
-
|
|
150
|
-
// 带重试的回调包装
|
|
151
|
-
config := approval.RetryConfig{
|
|
152
|
-
MaxRetries: 3,
|
|
153
|
-
InitialBackoff: time.Second,
|
|
154
|
-
MaxBackoff: 30 * time.Second,
|
|
155
|
-
BackoffFactor: 2.0,
|
|
156
|
-
}
|
|
157
|
-
retryable := approval.NewRetryableCallback(callback, config)
|
|
158
|
-
|
|
159
|
-
// 异步回调执行器
|
|
160
|
-
executor := approval.NewAsyncCallbackExecutor(registry, config, 100)
|
|
161
|
-
executor.ExecuteApproved(ctx, businessType, tenantID, businessID, approverID)
|
|
162
|
-
|
|
163
|
-
// 监听结果
|
|
164
|
-
for result := range executor.Results() {
|
|
165
|
-
if !result.Success {
|
|
166
|
-
log.Error("callback failed", "error", result.Error)
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### 4. Job 队列
|
|
172
|
-
```go
|
|
173
|
-
jobService.CreateJob(...)
|
|
174
|
-
jobService.GetJob(...)
|
|
175
|
-
jobService.ListJobs(...)
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
### 5. 通知服务
|
|
179
|
-
```go
|
|
180
|
-
notificationService.SendNotification(...)
|
|
181
|
-
notificationService.ListNotifications(...)
|
|
182
|
-
notificationService.MarkRead(...)
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
### 6. 权限中间件
|
|
186
|
-
```go
|
|
187
|
-
middleware.RequirePermission("module:resource:action")
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
### 7. 存储服务
|
|
191
|
-
```go
|
|
192
|
-
storageService.Upload(...)
|
|
193
|
-
storageService.Download(...)
|
|
194
|
-
storageService.Delete(...)
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
### 8. 统一分页
|
|
198
|
-
```go
|
|
199
|
-
import "github.com/robsuncn/keystone/infra/pagination"
|
|
200
|
-
|
|
201
|
-
// 解析请求参数
|
|
202
|
-
req := pagination.ParseRequest(c) // 从 Gin context 获取 page, page_size
|
|
203
|
-
|
|
204
|
-
// 方式一:使用 GORM Scope
|
|
205
|
-
db.Scopes(pagination.Scope(req)).Find(&items)
|
|
206
|
-
|
|
207
|
-
// 方式二:一步完成分页查询
|
|
208
|
-
var items []Item
|
|
209
|
-
total, err := pagination.Paginate(db.Where("tenant_id = ?", tenantID), req, &items)
|
|
210
|
-
|
|
211
|
-
// 方式三:带预加载的分页
|
|
212
|
-
total, err := pagination.PaginateWithPreload(db, req, &items, "Category", "Tags")
|
|
213
|
-
|
|
214
|
-
// 构建响应
|
|
215
|
-
resp := pagination.NewResponse(items, total, req)
|
|
216
|
-
// resp.TotalPages(), resp.HasNextPage(), resp.HasPrevPage()
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
### 9. 多语言 (i18n)
|
|
220
|
-
```go
|
|
221
|
-
// 获取当前请求语言环境
|
|
222
|
-
import "github.com/robsuncn/keystone/infra/i18n"
|
|
223
|
-
|
|
224
|
-
locale := i18n.GetLocale(c) // 从请求头或参数获取
|
|
225
|
-
|
|
226
|
-
// 翻译函数
|
|
227
|
-
i18n.T(c, "module.key") // 基础翻译
|
|
228
|
-
i18n.T(c, "module.greeting", "name", "张三") // 带变量
|
|
229
|
-
i18n.Tf(c, "module.count", count) // 复数形式
|
|
230
|
-
|
|
231
|
-
// 错误消息翻译
|
|
232
|
-
return errors.New(i18n.T(c, "module.errors.notFound"))
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
**翻译文件组织**:
|
|
236
|
-
```
|
|
237
|
-
internal/modules/{module}/i18n/
|
|
238
|
-
├── i18n.go # 初始化
|
|
239
|
-
├── keys.go # 翻译键常量
|
|
240
|
-
└── locales/
|
|
241
|
-
├── zh-CN.json
|
|
242
|
-
└── en-US.json
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
**翻译键常量定义**:
|
|
246
|
-
```go
|
|
247
|
-
package i18n
|
|
248
|
-
|
|
249
|
-
const (
|
|
250
|
-
KeyItemCreated = "example.item.created"
|
|
251
|
-
KeyItemNotFound = "example.item.notFound"
|
|
252
|
-
KeyItemInvalid = "example.item.invalid"
|
|
253
|
-
)
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
**模块初始化**:
|
|
257
|
-
```go
|
|
258
|
-
func init() {
|
|
259
|
-
i18n.MustLoadModuleTranslations("example", Translations)
|
|
260
|
-
}
|
|
261
|
-
```
|