@robsun/create-keystone-app 0.2.3 → 0.2.5

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 CHANGED
@@ -31,6 +31,10 @@ pnpm dlx --package @robsun/create-keystone-app create-keystone-module <module-na
31
31
  - `--with-crud`:包含 CRUD 示例代码。
32
32
  - `--skip-register`:跳过自动注册步骤。
33
33
 
34
+ ## AI Skills
35
+ - Codex:`.codex/skills/keystone-dev`
36
+ - Claude:`.claude/skills/keystone-dev`
37
+
34
38
  ## 初始化后操作
35
39
  ```bash
36
40
  cd <dir>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robsun/create-keystone-app",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "scripts": {
5
5
  "build": "node scripts/build.js",
6
6
  "prepublishOnly": "node scripts/build.js && node scripts/prune-template-deps.js"
@@ -0,0 +1,127 @@
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
+
45
+ ### 4. 帮助系统
46
+ ```typescript
47
+ import {
48
+ HelpButton, HelpDrawer, HelpSearch, HelpContent,
49
+ useCurrentHelpKey, useHelpSearch
50
+ } from '@robsun/keystone-web-core'
51
+ ```
52
+
53
+ ### 5. 布局与主题
54
+ ```typescript
55
+ import { MainLayout, antdTheme, useLayoutStore } from '@robsun/keystone-web-core'
56
+ ```
57
+
58
+ ### 6. 认证授权
59
+ ```typescript
60
+ import { useAuthStore, useAuth, matchesPermission } from '@robsun/keystone-web-core'
61
+ ```
62
+
63
+ ### 7. 运行时服务
64
+ ```typescript
65
+ import { api, setApiBaseUrl } from '@robsun/keystone-web-core'
66
+ import { createExportJob, createImportJob } from '@robsun/keystone-web-core'
67
+ import { listNotifications, markNotificationRead, fetchUnreadCount } from '@robsun/keystone-web-core'
68
+ import { getJob, listJobs } from '@robsun/keystone-web-core'
69
+ ```
70
+
71
+ ### 8. Hooks
72
+ ```typescript
73
+ import { useListState } from '@robsun/keystone-web-core'
74
+ ```
75
+
76
+ ### 9. 类型
77
+ ```typescript
78
+ import type { ApiResponse, PaginatedData, PaginatedResponse } from '@robsun/keystone-web-core'
79
+ import type { Permission, ModuleName, Action } from '@robsun/keystone-web-core'
80
+ ```
81
+
82
+ ## 二、后端能力 (Go)
83
+
84
+ ### 1. 模块系统
85
+ ```go
86
+ type Module interface {
87
+ Name() string
88
+ RegisterRoutes(r *gin.RouterGroup)
89
+ RegisterPermissions() []Permission
90
+ Migrate(db *gorm.DB) error
91
+ Seed(db *gorm.DB) error
92
+ }
93
+ ```
94
+
95
+ ### 2. 审批引擎
96
+ ```go
97
+ approvalService.CreateInstance(...)
98
+ approvalService.Approve(...)
99
+ approvalService.Reject(...)
100
+ approvalService.Cancel(...)
101
+ ```
102
+
103
+ ### 3. Job 队列
104
+ ```go
105
+ jobService.CreateJob(...)
106
+ jobService.GetJob(...)
107
+ jobService.ListJobs(...)
108
+ ```
109
+
110
+ ### 4. 通知服务
111
+ ```go
112
+ notificationService.SendNotification(...)
113
+ notificationService.ListNotifications(...)
114
+ notificationService.MarkRead(...)
115
+ ```
116
+
117
+ ### 5. 权限中间件
118
+ ```go
119
+ middleware.RequirePermission("module:resource:action")
120
+ ```
121
+
122
+ ### 6. 存储服务
123
+ ```go
124
+ storageService.Upload(...)
125
+ storageService.Download(...)
126
+ storageService.Delete(...)
127
+ ```
@@ -0,0 +1,75 @@
1
+ ---
2
+ name: keystone-dev
3
+ description: 基于 Keystone 平台开发业务模块。当用户需要创建新模块、添加业务功能、实现 CRUD、审批流程、数据导入导出、文件上传时使用。Use when creating modules, adding features, implementing CRUD, approval workflows, import/export, or file upload for Keystone platform.
4
+ ---
5
+
6
+ # Keystone 业务模块开发
7
+
8
+ 使用 Keystone 平台快速开发业务模块。
9
+
10
+ ## 工作流程
11
+
12
+ 1. **解析需求** → 识别所需能力
13
+ 2. **生成前端模块** → routes.tsx, pages/, services/, types.ts
14
+ 3. **生成后端模块** → handler/, service/, repository/, migrations/
15
+ 4. **自动注册** → main.tsx + manifest.go + config.yaml
16
+ 5. **输出调整建议**
17
+
18
+ ## 能力选择矩阵
19
+
20
+ 根据需求关键词选用对应能力:
21
+
22
+ | 需求关键词 | 前端能力 | 后端能力 |
23
+ |-----------|---------|---------|
24
+ | 列表、查询 | ProTable + 过滤 + 分页 | ListHandler + Repository |
25
+ | 表单、编辑 | ProForm + 验证 | CreateHandler + UpdateHandler |
26
+ | 导入、批量 | DataImporter | ImportHandler + Job队列 |
27
+ | 导出、下载 | DataExporter | ExportHandler + Job队列 |
28
+ | 上传、附件 | FileUpload | UploadHandler + 存储服务 |
29
+ | 审批、流程 | ApprovalFlowEditor | 审批引擎 |
30
+ | 权限 | PermissionGuard | 权限中间件 |
31
+
32
+ 详细能力清单见 [CAPABILITIES.md](CAPABILITIES.md)。
33
+ 代码模板见 [TEMPLATES.md](TEMPLATES.md)。
34
+
35
+ ## 模块结构约定
36
+
37
+ ### 前端模块
38
+ ```
39
+ apps/web/src/modules/{name}/
40
+ ├── index.ts # registerModule()
41
+ ├── routes.tsx # 路由 + 菜单 + 权限
42
+ ├── pages/
43
+ │ ├── List.tsx # ProTable
44
+ │ ├── Detail.tsx
45
+ │ └── Form.tsx # ProForm
46
+ ├── services/api.ts
47
+ ├── types.ts
48
+ └── help/index.md
49
+ ```
50
+
51
+ ### 后端模块
52
+ ```
53
+ apps/server/internal/modules/{name}/
54
+ ├── module.go
55
+ ├── api/handler/
56
+ ├── domain/models/
57
+ ├── domain/service/
58
+ ├── infra/repository/
59
+ └── bootstrap/migrations/
60
+ ```
61
+
62
+ ## 权限命名
63
+
64
+ 格式: `{module}:{resource}:{action}`
65
+
66
+ 动作: view | create | update | delete | manage | export | import | approve
67
+
68
+ ## 注册步骤
69
+
70
+ 生成代码后需完成以下注册(**缺一不可**):
71
+
72
+ 1. **前端导入** `main.tsx`: `import './modules/{name}'`
73
+ 2. **前端启用** `app.config.ts`: 添加到 `modules.enabled` 数组 ⚠️ **必须,否则菜单不显示**
74
+ 3. **后端注册** `manifest.go`: `import {name}module "app/internal/modules/{name}"` + `Register({name}module.NewModule())`
75
+ 4. **后端启用** `config.yaml`: 添加到 `modules.enabled`
@@ -0,0 +1,306 @@
1
+ # Keystone 代码模板
2
+
3
+ ## 前端模板
4
+
5
+ ### routes.tsx
6
+ ```tsx
7
+ import { RouteObject } from 'react-router-dom'
8
+ import { registerModule } from '@robsun/keystone-web-core'
9
+
10
+ const routes: RouteObject[] = [
11
+ {
12
+ path: '{name}',
13
+ handle: {
14
+ menu: { title: '{Title}', icon: 'icon-name' },
15
+ permission: '{name}:{resource}:view',
16
+ helpKey: '{name}/index',
17
+ },
18
+ children: [
19
+ { index: true, lazy: () => import('./pages/List') },
20
+ { path: 'create', lazy: () => import('./pages/Form') },
21
+ { path: ':id', lazy: () => import('./pages/Detail') },
22
+ { path: ':id/edit', lazy: () => import('./pages/Form') },
23
+ ],
24
+ },
25
+ ]
26
+
27
+ registerModule({ name: '{name}', routes })
28
+ ```
29
+
30
+ ### List.tsx (ProTable)
31
+ ```tsx
32
+ import { useEffect, useState } from 'react'
33
+ import { ProTable } from '@robsun/keystone-web-core'
34
+ import type { PaginatedData } from '@robsun/keystone-web-core'
35
+ import type { {Entity} } from '../types'
36
+ import { list{Entity} } from '../services/api'
37
+
38
+ const INITIAL_DATA: PaginatedData<{Entity}> = {
39
+ items: [],
40
+ total: 0,
41
+ page: 1,
42
+ page_size: 10,
43
+ }
44
+
45
+ export function Component() {
46
+ const [loading, setLoading] = useState(false)
47
+ const [data, setData] = useState(INITIAL_DATA)
48
+
49
+ const columns = [
50
+ { title: 'ID', dataIndex: 'id' },
51
+ { title: '名称', dataIndex: 'name' },
52
+ ]
53
+
54
+ const fetchData = async (page = data.page, pageSize = data.page_size) => {
55
+ setLoading(true)
56
+ try {
57
+ const result = await list{Entity}({ page, page_size: pageSize })
58
+ setData(result)
59
+ } finally {
60
+ setLoading(false)
61
+ }
62
+ }
63
+
64
+ useEffect(() => {
65
+ void fetchData()
66
+ }, [])
67
+
68
+ return (
69
+ <ProTable
70
+ columns={columns}
71
+ dataSource={data.items}
72
+ rowKey="id"
73
+ loading={loading}
74
+ pagination={{
75
+ current: data.page,
76
+ pageSize: data.page_size,
77
+ total: data.total,
78
+ onChange: (page, pageSize) => {
79
+ void fetchData(page, pageSize)
80
+ },
81
+ }}
82
+ />
83
+ )
84
+ }
85
+ ```
86
+
87
+ ### Form.tsx (ProForm)
88
+ ```tsx
89
+ import { ProForm } from '@robsun/keystone-web-core'
90
+ import { api } from '@robsun/keystone-web-core'
91
+ import { useParams, useNavigate } from 'react-router-dom'
92
+
93
+ export function Component() {
94
+ const { id } = useParams()
95
+ const navigate = useNavigate()
96
+ const isEdit = !!id
97
+
98
+ const onSubmit = async (values: any) => {
99
+ if (isEdit) {
100
+ await api.patch(`/api/{name}/${id}`, values)
101
+ } else {
102
+ await api.post('/api/{name}', values)
103
+ }
104
+ navigate('/{name}')
105
+ }
106
+
107
+ return (
108
+ <ProForm onFinish={onSubmit}>
109
+ {/* 表单字段 */}
110
+ </ProForm>
111
+ )
112
+ }
113
+ ```
114
+
115
+ ### services/api.ts
116
+ ```typescript
117
+ import { api, type ApiResponse, type PaginatedData, type PaginationParams } from '@robsun/keystone-web-core'
118
+ import type { {Entity} } from '../types'
119
+
120
+ // 注意:使用 api 而非 apiClient,响应格式为 { data: { ... } }
121
+ export const list{Entity} = async (params: PaginationParams = {}) => {
122
+ const { data } = await api.get<ApiResponse<PaginatedData<{Entity}>>>(
123
+ '/{module}/{resources}',
124
+ { params }
125
+ )
126
+ return data.data
127
+ }
128
+
129
+ export const create{Entity} = async (payload: Partial<{Entity}>) => {
130
+ const { data } = await api.post<ApiResponse<{Entity}>>('/{module}/{resources}', payload)
131
+ return data.data
132
+ }
133
+
134
+ export const update{Entity} = async (id: number, payload: Partial<{Entity}>) => {
135
+ const { data } = await api.patch<ApiResponse<{Entity}>>(`/{module}/{resources}/${id}`, payload)
136
+ return data.data
137
+ }
138
+
139
+ export const delete{Entity} = async (id: number) => {
140
+ await api.delete<ApiResponse<{ id: number }>>(`/{module}/{resources}/${id}`)
141
+ }
142
+ ```
143
+
144
+ ### types.ts
145
+ ```typescript
146
+ export interface {Entity} {
147
+ id: string
148
+ name: string
149
+ createdAt: string
150
+ updatedAt: string
151
+ }
152
+ ```
153
+
154
+ ## 后端模板
155
+
156
+ ### module.go
157
+ ```go
158
+ package {name}
159
+
160
+ import (
161
+ "github.com/gin-gonic/gin"
162
+ "gorm.io/gorm"
163
+ )
164
+
165
+ type Module struct{}
166
+
167
+ func (m *Module) Name() string { return "{name}" }
168
+
169
+ func (m *Module) RegisterRoutes(r *gin.RouterGroup) {
170
+ g := r.Group("/{name}")
171
+ g.GET("", handler.List)
172
+ g.GET("/:id", handler.Detail)
173
+ g.POST("", handler.Create)
174
+ g.PATCH("/:id", handler.Update)
175
+ g.DELETE("/:id", handler.Delete)
176
+ }
177
+
178
+ func (m *Module) RegisterPermissions() []Permission {
179
+ return []Permission{
180
+ {Name: "{name}:{resource}:view", Title: "查看{Title}"},
181
+ {Name: "{name}:{resource}:create", Title: "创建{Title}"},
182
+ {Name: "{name}:{resource}:update", Title: "编辑{Title}"},
183
+ {Name: "{name}:{resource}:delete", Title: "删除{Title}"},
184
+ }
185
+ }
186
+
187
+ func (m *Module) Migrate(db *gorm.DB) error {
188
+ return db.AutoMigrate(&models.{Entity}{})
189
+ }
190
+
191
+ func (m *Module) Seed(db *gorm.DB) error { return nil }
192
+ ```
193
+
194
+ ### handler/list.go
195
+ ```go
196
+ package handler
197
+
198
+ import (
199
+ "github.com/gin-gonic/gin"
200
+ )
201
+
202
+ func (h *Handler) List(c *gin.Context) {
203
+ page, pageSize := parsePagination(c)
204
+ items, total, err := h.svc.List(c, page, pageSize)
205
+ if err != nil {
206
+ respondError(c, err)
207
+ return
208
+ }
209
+ respondPaginated(c, items, total, page, pageSize)
210
+ }
211
+ ```
212
+
213
+ ### domain/models/entity.go
214
+ ```go
215
+ package models
216
+
217
+ import "time"
218
+
219
+ type {Entity} struct {
220
+ ID string `json:"id" gorm:"primaryKey"`
221
+ Name string `json:"name"`
222
+ CreatedAt time.Time `json:"createdAt"`
223
+ UpdatedAt time.Time `json:"updatedAt"`
224
+ }
225
+ ```
226
+
227
+ ### domain/service/service.go
228
+ ```go
229
+ package service
230
+
231
+ type Service struct {
232
+ repo *repository.Repository
233
+ }
234
+
235
+ func NewService(repo *repository.Repository) *Service {
236
+ return &Service{repo: repo}
237
+ }
238
+
239
+ func (s *Service) List(ctx context.Context, page, pageSize int) ([]models.{Entity}, int64, error) {
240
+ return s.repo.List(ctx, page, pageSize)
241
+ }
242
+
243
+ func (s *Service) Get(ctx context.Context, id string) (*models.{Entity}, error) {
244
+ return s.repo.Get(ctx, id)
245
+ }
246
+
247
+ func (s *Service) Create(ctx context.Context, entity *models.{Entity}) error {
248
+ return s.repo.Create(ctx, entity)
249
+ }
250
+
251
+ func (s *Service) Update(ctx context.Context, id string, entity *models.{Entity}) error {
252
+ return s.repo.Update(ctx, id, entity)
253
+ }
254
+
255
+ func (s *Service) Delete(ctx context.Context, id string) error {
256
+ return s.repo.Delete(ctx, id)
257
+ }
258
+ ```
259
+
260
+ ### infra/repository/repo.go
261
+ ```go
262
+ package repository
263
+
264
+ import (
265
+ "context"
266
+ "gorm.io/gorm"
267
+ )
268
+
269
+ type Repository struct {
270
+ db *gorm.DB
271
+ }
272
+
273
+ func NewRepository(db *gorm.DB) *Repository {
274
+ return &Repository{db: db}
275
+ }
276
+
277
+ func (r *Repository) List(ctx context.Context, page, pageSize int) ([]models.{Entity}, int64, error) {
278
+ var items []models.{Entity}
279
+ var total int64
280
+
281
+ r.db.Model(&models.{Entity}{}).Count(&total)
282
+ r.db.Offset((page - 1) * pageSize).Limit(pageSize).Find(&items)
283
+
284
+ return items, total, nil
285
+ }
286
+
287
+ func (r *Repository) Get(ctx context.Context, id string) (*models.{Entity}, error) {
288
+ var item models.{Entity}
289
+ if err := r.db.First(&item, "id = ?", id).Error; err != nil {
290
+ return nil, err
291
+ }
292
+ return &item, nil
293
+ }
294
+
295
+ func (r *Repository) Create(ctx context.Context, entity *models.{Entity}) error {
296
+ return r.db.Create(entity).Error
297
+ }
298
+
299
+ func (r *Repository) Update(ctx context.Context, id string, entity *models.{Entity}) error {
300
+ return r.db.Model(&models.{Entity}{}).Where("id = ?", id).Updates(entity).Error
301
+ }
302
+
303
+ func (r *Repository) Delete(ctx context.Context, id string) error {
304
+ return r.db.Delete(&models.{Entity}{}, "id = ?", id).Error
305
+ }
306
+ ```
@@ -0,0 +1,127 @@
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
+
45
+ ### 4. 帮助系统
46
+ ```typescript
47
+ import {
48
+ HelpButton, HelpDrawer, HelpSearch, HelpContent,
49
+ useCurrentHelpKey, useHelpSearch
50
+ } from '@robsun/keystone-web-core'
51
+ ```
52
+
53
+ ### 5. 布局与主题
54
+ ```typescript
55
+ import { MainLayout, antdTheme, useLayoutStore } from '@robsun/keystone-web-core'
56
+ ```
57
+
58
+ ### 6. 认证授权
59
+ ```typescript
60
+ import { useAuthStore, useAuth, matchesPermission } from '@robsun/keystone-web-core'
61
+ ```
62
+
63
+ ### 7. 运行时服务
64
+ ```typescript
65
+ import { api, setApiBaseUrl } from '@robsun/keystone-web-core'
66
+ import { createExportJob, createImportJob } from '@robsun/keystone-web-core'
67
+ import { listNotifications, markNotificationRead, fetchUnreadCount } from '@robsun/keystone-web-core'
68
+ import { getJob, listJobs } from '@robsun/keystone-web-core'
69
+ ```
70
+
71
+ ### 8. Hooks
72
+ ```typescript
73
+ import { useListState } from '@robsun/keystone-web-core'
74
+ ```
75
+
76
+ ### 9. 类型
77
+ ```typescript
78
+ import type { ApiResponse, PaginatedData, PaginatedResponse } from '@robsun/keystone-web-core'
79
+ import type { Permission, ModuleName, Action } from '@robsun/keystone-web-core'
80
+ ```
81
+
82
+ ## 二、后端能力 (Go)
83
+
84
+ ### 1. 模块系统
85
+ ```go
86
+ type Module interface {
87
+ Name() string
88
+ RegisterRoutes(r *gin.RouterGroup)
89
+ RegisterPermissions() []Permission
90
+ Migrate(db *gorm.DB) error
91
+ Seed(db *gorm.DB) error
92
+ }
93
+ ```
94
+
95
+ ### 2. 审批引擎
96
+ ```go
97
+ approvalService.CreateInstance(...)
98
+ approvalService.Approve(...)
99
+ approvalService.Reject(...)
100
+ approvalService.Cancel(...)
101
+ ```
102
+
103
+ ### 3. Job 队列
104
+ ```go
105
+ jobService.CreateJob(...)
106
+ jobService.GetJob(...)
107
+ jobService.ListJobs(...)
108
+ ```
109
+
110
+ ### 4. 通知服务
111
+ ```go
112
+ notificationService.SendNotification(...)
113
+ notificationService.ListNotifications(...)
114
+ notificationService.MarkRead(...)
115
+ ```
116
+
117
+ ### 5. 权限中间件
118
+ ```go
119
+ middleware.RequirePermission("module:resource:action")
120
+ ```
121
+
122
+ ### 6. 存储服务
123
+ ```go
124
+ storageService.Upload(...)
125
+ storageService.Download(...)
126
+ storageService.Delete(...)
127
+ ```
@@ -0,0 +1,75 @@
1
+ ---
2
+ name: keystone-dev
3
+ description: 基于 Keystone 平台开发业务模块。当用户需要创建新模块、添加业务功能、实现 CRUD、审批流程、数据导入导出、文件上传时使用。Use when creating modules, adding features, implementing CRUD, approval workflows, import/export, or file upload for Keystone platform.
4
+ ---
5
+
6
+ # Keystone 业务模块开发
7
+
8
+ 使用 Keystone 平台快速开发业务模块。
9
+
10
+ ## 工作流程
11
+
12
+ 1. **解析需求** → 识别所需能力
13
+ 2. **生成前端模块** → routes.tsx, pages/, services/, types.ts
14
+ 3. **生成后端模块** → handler/, service/, repository/, migrations/
15
+ 4. **自动注册** → main.tsx + manifest.go + config.yaml
16
+ 5. **输出调整建议**
17
+
18
+ ## 能力选择矩阵
19
+
20
+ 根据需求关键词选用对应能力:
21
+
22
+ | 需求关键词 | 前端能力 | 后端能力 |
23
+ |-----------|---------|---------|
24
+ | 列表、查询 | ProTable + 过滤 + 分页 | ListHandler + Repository |
25
+ | 表单、编辑 | ProForm + 验证 | CreateHandler + UpdateHandler |
26
+ | 导入、批量 | DataImporter | ImportHandler + Job队列 |
27
+ | 导出、下载 | DataExporter | ExportHandler + Job队列 |
28
+ | 上传、附件 | FileUpload | UploadHandler + 存储服务 |
29
+ | 审批、流程 | ApprovalFlowEditor | 审批引擎 |
30
+ | 权限 | PermissionGuard | 权限中间件 |
31
+
32
+ 详细能力清单见 [CAPABILITIES.md](CAPABILITIES.md)。
33
+ 代码模板见 [TEMPLATES.md](TEMPLATES.md)。
34
+
35
+ ## 模块结构约定
36
+
37
+ ### 前端模块
38
+ ```
39
+ apps/web/src/modules/{name}/
40
+ ├── index.ts # registerModule()
41
+ ├── routes.tsx # 路由 + 菜单 + 权限
42
+ ├── pages/
43
+ │ ├── List.tsx # ProTable
44
+ │ ├── Detail.tsx
45
+ │ └── Form.tsx # ProForm
46
+ ├── services/api.ts
47
+ ├── types.ts
48
+ └── help/index.md
49
+ ```
50
+
51
+ ### 后端模块
52
+ ```
53
+ apps/server/internal/modules/{name}/
54
+ ├── module.go
55
+ ├── api/handler/
56
+ ├── domain/models/
57
+ ├── domain/service/
58
+ ├── infra/repository/
59
+ └── bootstrap/migrations/
60
+ ```
61
+
62
+ ## 权限命名
63
+
64
+ 格式: `{module}:{resource}:{action}`
65
+
66
+ 动作: view | create | update | delete | manage | export | import | approve
67
+
68
+ ## 注册步骤
69
+
70
+ 生成代码后需完成以下注册(**缺一不可**):
71
+
72
+ 1. **前端导入** `main.tsx`: `import './modules/{name}'`
73
+ 2. **前端启用** `app.config.ts`: 添加到 `modules.enabled` 数组 ⚠️ **必须,否则菜单不显示**
74
+ 3. **后端注册** `manifest.go`: `import {name}module "app/internal/modules/{name}"` + `Register({name}module.NewModule())`
75
+ 4. **后端启用** `config.yaml`: 添加到 `modules.enabled`
@@ -0,0 +1,306 @@
1
+ # Keystone 代码模板
2
+
3
+ ## 前端模板
4
+
5
+ ### routes.tsx
6
+ ```tsx
7
+ import { RouteObject } from 'react-router-dom'
8
+ import { registerModule } from '@robsun/keystone-web-core'
9
+
10
+ const routes: RouteObject[] = [
11
+ {
12
+ path: '{name}',
13
+ handle: {
14
+ menu: { title: '{Title}', icon: 'icon-name' },
15
+ permission: '{name}:{resource}:view',
16
+ helpKey: '{name}/index',
17
+ },
18
+ children: [
19
+ { index: true, lazy: () => import('./pages/List') },
20
+ { path: 'create', lazy: () => import('./pages/Form') },
21
+ { path: ':id', lazy: () => import('./pages/Detail') },
22
+ { path: ':id/edit', lazy: () => import('./pages/Form') },
23
+ ],
24
+ },
25
+ ]
26
+
27
+ registerModule({ name: '{name}', routes })
28
+ ```
29
+
30
+ ### List.tsx (ProTable)
31
+ ```tsx
32
+ import { useEffect, useState } from 'react'
33
+ import { ProTable } from '@robsun/keystone-web-core'
34
+ import type { PaginatedData } from '@robsun/keystone-web-core'
35
+ import type { {Entity} } from '../types'
36
+ import { list{Entity} } from '../services/api'
37
+
38
+ const INITIAL_DATA: PaginatedData<{Entity}> = {
39
+ items: [],
40
+ total: 0,
41
+ page: 1,
42
+ page_size: 10,
43
+ }
44
+
45
+ export function Component() {
46
+ const [loading, setLoading] = useState(false)
47
+ const [data, setData] = useState(INITIAL_DATA)
48
+
49
+ const columns = [
50
+ { title: 'ID', dataIndex: 'id' },
51
+ { title: '名称', dataIndex: 'name' },
52
+ ]
53
+
54
+ const fetchData = async (page = data.page, pageSize = data.page_size) => {
55
+ setLoading(true)
56
+ try {
57
+ const result = await list{Entity}({ page, page_size: pageSize })
58
+ setData(result)
59
+ } finally {
60
+ setLoading(false)
61
+ }
62
+ }
63
+
64
+ useEffect(() => {
65
+ void fetchData()
66
+ }, [])
67
+
68
+ return (
69
+ <ProTable
70
+ columns={columns}
71
+ dataSource={data.items}
72
+ rowKey="id"
73
+ loading={loading}
74
+ pagination={{
75
+ current: data.page,
76
+ pageSize: data.page_size,
77
+ total: data.total,
78
+ onChange: (page, pageSize) => {
79
+ void fetchData(page, pageSize)
80
+ },
81
+ }}
82
+ />
83
+ )
84
+ }
85
+ ```
86
+
87
+ ### Form.tsx (ProForm)
88
+ ```tsx
89
+ import { ProForm } from '@robsun/keystone-web-core'
90
+ import { api } from '@robsun/keystone-web-core'
91
+ import { useParams, useNavigate } from 'react-router-dom'
92
+
93
+ export function Component() {
94
+ const { id } = useParams()
95
+ const navigate = useNavigate()
96
+ const isEdit = !!id
97
+
98
+ const onSubmit = async (values: any) => {
99
+ if (isEdit) {
100
+ await api.patch(`/api/{name}/${id}`, values)
101
+ } else {
102
+ await api.post('/api/{name}', values)
103
+ }
104
+ navigate('/{name}')
105
+ }
106
+
107
+ return (
108
+ <ProForm onFinish={onSubmit}>
109
+ {/* 表单字段 */}
110
+ </ProForm>
111
+ )
112
+ }
113
+ ```
114
+
115
+ ### services/api.ts
116
+ ```typescript
117
+ import { api, type ApiResponse, type PaginatedData, type PaginationParams } from '@robsun/keystone-web-core'
118
+ import type { {Entity} } from '../types'
119
+
120
+ // 注意:使用 api 而非 apiClient,响应格式为 { data: { ... } }
121
+ export const list{Entity} = async (params: PaginationParams = {}) => {
122
+ const { data } = await api.get<ApiResponse<PaginatedData<{Entity}>>>(
123
+ '/{module}/{resources}',
124
+ { params }
125
+ )
126
+ return data.data
127
+ }
128
+
129
+ export const create{Entity} = async (payload: Partial<{Entity}>) => {
130
+ const { data } = await api.post<ApiResponse<{Entity}>>('/{module}/{resources}', payload)
131
+ return data.data
132
+ }
133
+
134
+ export const update{Entity} = async (id: number, payload: Partial<{Entity}>) => {
135
+ const { data } = await api.patch<ApiResponse<{Entity}>>(`/{module}/{resources}/${id}`, payload)
136
+ return data.data
137
+ }
138
+
139
+ export const delete{Entity} = async (id: number) => {
140
+ await api.delete<ApiResponse<{ id: number }>>(`/{module}/{resources}/${id}`)
141
+ }
142
+ ```
143
+
144
+ ### types.ts
145
+ ```typescript
146
+ export interface {Entity} {
147
+ id: string
148
+ name: string
149
+ createdAt: string
150
+ updatedAt: string
151
+ }
152
+ ```
153
+
154
+ ## 后端模板
155
+
156
+ ### module.go
157
+ ```go
158
+ package {name}
159
+
160
+ import (
161
+ "github.com/gin-gonic/gin"
162
+ "gorm.io/gorm"
163
+ )
164
+
165
+ type Module struct{}
166
+
167
+ func (m *Module) Name() string { return "{name}" }
168
+
169
+ func (m *Module) RegisterRoutes(r *gin.RouterGroup) {
170
+ g := r.Group("/{name}")
171
+ g.GET("", handler.List)
172
+ g.GET("/:id", handler.Detail)
173
+ g.POST("", handler.Create)
174
+ g.PATCH("/:id", handler.Update)
175
+ g.DELETE("/:id", handler.Delete)
176
+ }
177
+
178
+ func (m *Module) RegisterPermissions() []Permission {
179
+ return []Permission{
180
+ {Name: "{name}:{resource}:view", Title: "查看{Title}"},
181
+ {Name: "{name}:{resource}:create", Title: "创建{Title}"},
182
+ {Name: "{name}:{resource}:update", Title: "编辑{Title}"},
183
+ {Name: "{name}:{resource}:delete", Title: "删除{Title}"},
184
+ }
185
+ }
186
+
187
+ func (m *Module) Migrate(db *gorm.DB) error {
188
+ return db.AutoMigrate(&models.{Entity}{})
189
+ }
190
+
191
+ func (m *Module) Seed(db *gorm.DB) error { return nil }
192
+ ```
193
+
194
+ ### handler/list.go
195
+ ```go
196
+ package handler
197
+
198
+ import (
199
+ "github.com/gin-gonic/gin"
200
+ )
201
+
202
+ func (h *Handler) List(c *gin.Context) {
203
+ page, pageSize := parsePagination(c)
204
+ items, total, err := h.svc.List(c, page, pageSize)
205
+ if err != nil {
206
+ respondError(c, err)
207
+ return
208
+ }
209
+ respondPaginated(c, items, total, page, pageSize)
210
+ }
211
+ ```
212
+
213
+ ### domain/models/entity.go
214
+ ```go
215
+ package models
216
+
217
+ import "time"
218
+
219
+ type {Entity} struct {
220
+ ID string `json:"id" gorm:"primaryKey"`
221
+ Name string `json:"name"`
222
+ CreatedAt time.Time `json:"createdAt"`
223
+ UpdatedAt time.Time `json:"updatedAt"`
224
+ }
225
+ ```
226
+
227
+ ### domain/service/service.go
228
+ ```go
229
+ package service
230
+
231
+ type Service struct {
232
+ repo *repository.Repository
233
+ }
234
+
235
+ func NewService(repo *repository.Repository) *Service {
236
+ return &Service{repo: repo}
237
+ }
238
+
239
+ func (s *Service) List(ctx context.Context, page, pageSize int) ([]models.{Entity}, int64, error) {
240
+ return s.repo.List(ctx, page, pageSize)
241
+ }
242
+
243
+ func (s *Service) Get(ctx context.Context, id string) (*models.{Entity}, error) {
244
+ return s.repo.Get(ctx, id)
245
+ }
246
+
247
+ func (s *Service) Create(ctx context.Context, entity *models.{Entity}) error {
248
+ return s.repo.Create(ctx, entity)
249
+ }
250
+
251
+ func (s *Service) Update(ctx context.Context, id string, entity *models.{Entity}) error {
252
+ return s.repo.Update(ctx, id, entity)
253
+ }
254
+
255
+ func (s *Service) Delete(ctx context.Context, id string) error {
256
+ return s.repo.Delete(ctx, id)
257
+ }
258
+ ```
259
+
260
+ ### infra/repository/repo.go
261
+ ```go
262
+ package repository
263
+
264
+ import (
265
+ "context"
266
+ "gorm.io/gorm"
267
+ )
268
+
269
+ type Repository struct {
270
+ db *gorm.DB
271
+ }
272
+
273
+ func NewRepository(db *gorm.DB) *Repository {
274
+ return &Repository{db: db}
275
+ }
276
+
277
+ func (r *Repository) List(ctx context.Context, page, pageSize int) ([]models.{Entity}, int64, error) {
278
+ var items []models.{Entity}
279
+ var total int64
280
+
281
+ r.db.Model(&models.{Entity}{}).Count(&total)
282
+ r.db.Offset((page - 1) * pageSize).Limit(pageSize).Find(&items)
283
+
284
+ return items, total, nil
285
+ }
286
+
287
+ func (r *Repository) Get(ctx context.Context, id string) (*models.{Entity}, error) {
288
+ var item models.{Entity}
289
+ if err := r.db.First(&item, "id = ?", id).Error; err != nil {
290
+ return nil, err
291
+ }
292
+ return &item, nil
293
+ }
294
+
295
+ func (r *Repository) Create(ctx context.Context, entity *models.{Entity}) error {
296
+ return r.db.Create(entity).Error
297
+ }
298
+
299
+ func (r *Repository) Update(ctx context.Context, id string, entity *models.{Entity}) error {
300
+ return r.db.Model(&models.{Entity}{}).Where("id = ?", id).Updates(entity).Error
301
+ }
302
+
303
+ func (r *Repository) Delete(ctx context.Context, id string) error {
304
+ return r.db.Delete(&models.{Entity}{}, "id = ?", id).Error
305
+ }
306
+ ```
@@ -43,6 +43,10 @@ pnpm create:module <module-name> [options]
43
43
  - `--with-crud`:包含 CRUD 示例代码。
44
44
  - `--skip-register`:跳过自动注册步骤。
45
45
 
46
+ ## AI Skills
47
+ - Codex:`.codex/skills/keystone-dev`
48
+ - Claude:`.claude/skills/keystone-dev`
49
+
46
50
  ## 目录结构
47
51
  - `apps/web/`:React + Vite 壳与业务模块(`src/modules/*`)。
48
52
  - `apps/server/`:Go API + 模块注册/迁移/种子数据(含 `config.yaml`)。
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@ant-design/icons": "^6.1.0",
20
- "@robsun/keystone-web-core": "0.1.8",
20
+ "@robsun/keystone-web-core": "0.1.9",
21
21
  "antd": "^6.0.1",
22
22
  "dayjs": "^1.11.19",
23
23
  "react": "^19.2.0",
@@ -27,8 +27,8 @@ importers:
27
27
  specifier: ^6.1.0
28
28
  version: 6.1.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
29
29
  '@robsun/keystone-web-core':
30
- specifier: 0.1.8
31
- version: 0.1.8(@ant-design/icons@6.1.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(@types/react@19.2.7)(antd@6.1.3(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react-dom@19.2.3(react@19.2.3))(react-router-dom@7.11.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)(use-sync-external-store@1.6.0(react@19.2.3))(vite@7.3.0(@types/node@24.10.4)(jiti@2.6.1)(lightningcss@1.30.2)(yaml@2.8.2))
30
+ specifier: 0.1.9
31
+ version: 0.1.9(@ant-design/icons@6.1.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(@types/react@19.2.7)(antd@6.1.3(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react-dom@19.2.3(react@19.2.3))(react-router-dom@7.11.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)(use-sync-external-store@1.6.0(react@19.2.3))(vite@7.3.0(@types/node@24.10.4)(jiti@2.6.1)(lightningcss@1.30.2)(yaml@2.8.2))
32
32
  antd:
33
33
  specifier: ^6.0.1
34
34
  version: 6.1.3(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
@@ -924,8 +924,8 @@ packages:
924
924
  react: '>=16.9.0'
925
925
  react-dom: '>=16.9.0'
926
926
 
927
- '@robsun/keystone-web-core@0.1.8':
928
- resolution: {integrity: sha512-r6Qfs1LxTZBv6GVIf5s6LREuTl/lskq8L3Y9YynUFKvL1tuZ3h3sThxPvvP7VQbBZn5As/XUv16kpqqVXX1HvQ==}
927
+ '@robsun/keystone-web-core@0.1.9':
928
+ resolution: {integrity: sha512-zbN6yiq3p/wwVOZvGj5YI6lT+xXCyWx87QIsM3IXnNyMxJl4OLRvv1xXN3u6K4uQpp2q+no4CeYLAfRwFIKNNw==}
929
929
  peerDependencies:
930
930
  '@ant-design/icons': ^6.1.0
931
931
  antd: ^6.0.1
@@ -3744,7 +3744,7 @@ snapshots:
3744
3744
  react: 19.2.3
3745
3745
  react-dom: 19.2.3(react@19.2.3)
3746
3746
 
3747
- '@robsun/keystone-web-core@0.1.8(@ant-design/icons@6.1.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(@types/react@19.2.7)(antd@6.1.3(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react-dom@19.2.3(react@19.2.3))(react-router-dom@7.11.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)(use-sync-external-store@1.6.0(react@19.2.3))(vite@7.3.0(@types/node@24.10.4)(jiti@2.6.1)(lightningcss@1.30.2)(yaml@2.8.2))':
3747
+ '@robsun/keystone-web-core@0.1.9(@ant-design/icons@6.1.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(@types/react@19.2.7)(antd@6.1.3(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react-dom@19.2.3(react@19.2.3))(react-router-dom@7.11.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)(use-sync-external-store@1.6.0(react@19.2.3))(vite@7.3.0(@types/node@24.10.4)(jiti@2.6.1)(lightningcss@1.30.2)(yaml@2.8.2))':
3748
3748
  dependencies:
3749
3749
  '@ant-design/icons': 6.1.0(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
3750
3750
  '@vitejs/plugin-react': 5.1.2(vite@7.3.0(@types/node@24.10.4)(jiti@2.6.1)(lightningcss@1.30.2)(yaml@2.8.2))
@@ -6027,4 +6027,3 @@ snapshots:
6027
6027
  use-sync-external-store: 1.6.0(react@19.2.3)
6028
6028
 
6029
6029
  zwitch@2.0.4: {}
6030
-