diff --git a/tests/workflows/assign.wflow.json b/tests/workflows/assign.wflow.json index c328dba0..bb637b37 100644 --- a/tests/workflows/assign.wflow.json +++ b/tests/workflows/assign.wflow.json @@ -4,27 +4,27 @@ "description": "指派商务负责人", "apis": { "/save": { - "name": "保存", + "label": "保存", "process": "xiang.assign.save" }, "/terminal": { - "name": "关闭", + "label": "关闭", "process": "xiang.assign.terminal" }, "/submit": { - "name": "提交审批", + "label": "提交审批", "process": "flows.assign.submit" }, "/reject/reset": { - "name": "审批驳回并重填", + "label": "审批驳回并重填", "process": "flows.assign.reject.rest" }, "/reject/terminal": { - "name": "审批驳回并关闭", + "label": "审批驳回并关闭", "process": "flows.assign.reject.terminal" }, "/resolve": { - "name": "审批同意", + "label": "审批同意", "process": "flows.assign.resolve" } }, diff --git a/workflow/api.go b/workflow/api.go new file mode 100644 index 00000000..7a40196b --- /dev/null +++ b/workflow/api.go @@ -0,0 +1,107 @@ +package workflow + +import ( + "path/filepath" + + jsoniter "github.com/json-iterator/go" + "github.com/yaoapp/gou" +) + +// API: +// 读取工作流 GET /api/xiang/workflow/<工作流名称>/find/:id +// 读取工作流 GET /api/xiang/workflow/<工作流名称>/open/:data_id +// 读取工作流配置 GET /api/xiang/workflow/<工作流名称>/setting/:data_id +// 调用自定义API POST /api/xiang/workflow/<工作流名称>/<自定义API路由> + +// SetupAPIs 注册API +func (workflow *WorkFlow) SetupAPIs() error { + api := &gou.HTTP{ + Name: "工作流接口", + Version: "1.0.0", + Description: "工作流接口 API", + Group: "xiang/workflow", + Guard: "bearer-jwt", + Paths: []gou.Path{}, + } + + workflow.AddAPI(api, workflow.apiFind()) + workflow.AddAPI(api, workflow.apiOpen()) + workflow.AddAPI(api, workflow.apiSetting()) + + for name, custAPI := range workflow.APIs { + workflow.AddAPI(api, gou.Path{ + Label: custAPI.Label, + Description: custAPI.Description, + Path: filepath.Join("/", workflow.Name, name), + Method: "POST", + Process: custAPI.Process, + In: []string{workflow.Name, "$query.data_id", ":payload"}, + Out: gou.Out{ + Status: 200, + Type: "application/json", + }, + }) + } + + // 注册API + source, err := jsoniter.Marshal(api) + if err != nil { + return err + } + + gou.LoadAPI(string(source), "xiang.workflow."+workflow.Name) + return nil +} + +// AddAPI 添加API +func (workflow *WorkFlow) AddAPI(api *gou.HTTP, path gou.Path) { + api.Paths = append(api.Paths, path) +} + +// apiFind 使用ID读取工作流 +func (workflow *WorkFlow) apiFind() gou.Path { + return gou.Path{ + Label: "读取工作流", + Description: "使用工作流ID读取工作流", + Path: filepath.Join("/", workflow.Name, "find", ":id"), + Method: "GET", + Process: "xiang.workflow.Find", + In: []string{workflow.Name, "$param.id"}, + Out: gou.Out{ + Status: 200, + Type: "application/json", + }, + } +} + +// apiOpen 使用用户ID和数据ID 读取工作流 +func (workflow *WorkFlow) apiOpen() gou.Path { + return gou.Path{ + Label: "读取工作流", + Description: "使用数据ID读取工作流", + Path: filepath.Join("/", workflow.Name, "open", ":data_id"), + Method: "GET", + Process: "xiang.workflow.Open", + In: []string{workflow.Name, "$session.user_id", "$param.data_id"}, + Out: gou.Out{ + Status: 200, + Type: "application/json", + }, + } +} + +// apiSetting 读取工作流配置 +func (workflow *WorkFlow) apiSetting() gou.Path { + return gou.Path{ + Label: "读取工作流配置", + Description: "使用数据ID读取工作流配置", + Path: filepath.Join("/", workflow.Name, "setting", ":data_id"), + Method: "GET", + Process: "xiang.workflow.Setting", + In: []string{workflow.Name, "$session.user_id", "$param.data_id"}, + Out: gou.Out{ + Status: 200, + Type: "application/json", + }, + } +} diff --git a/workflow/api_test.go b/workflow/api_test.go new file mode 100644 index 00000000..873196a9 --- /dev/null +++ b/workflow/api_test.go @@ -0,0 +1,17 @@ +package workflow + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/yaoapp/gou" +) + +func TestSetupAPIs(t *testing.T) { + assignFlow := Select("assign") + assignFlow.SetupAPIs() + + api := gou.SelectAPI("xiang.workflow.assign") + assert.Equal(t, "xiang.workflow.assign", api.Name) + assert.Equal(t, 9, len(api.HTTP.Paths)) +} diff --git a/workflow/types.go b/workflow/types.go index f30205f7..024f059f 100644 --- a/workflow/types.go +++ b/workflow/types.go @@ -40,9 +40,10 @@ type User struct { // API 工作相关API type API struct { - Name string `json:"name"` - Process string `json:"process"` - Args []interface{} `json:"args"` + Label string `json:"label,omitempty"` + Description string `json:"description,omitempty"` + Process string `json:"process"` + Args []interface{} `json:"args"` } // Input 用户输入数据 diff --git a/workflow/workflow.go b/workflow/workflow.go index ee2eaf99..2ec94153 100644 --- a/workflow/workflow.go +++ b/workflow/workflow.go @@ -17,15 +17,15 @@ import ( ) // Process -// 读取工作流 xiang.workflow.Find(workflow_id) -// 读取工作流 xiang.workflow.Open(uid, data_id) -// 保存工作流 xiang.workflow.Save(uid, node_name, data_id, input, ...output) -// 进入下一个节点 xiang.workflow.Next(uid, workflow_id, output) -// 跳转到指定节点 xiang.workflow.Goto(uid, workflow_id, node_name, output) -// 更新工作流状态 xiang.workflow.Status(uid, workflow_id, status_name, output) -// 标记结束流程 xiang.workflow.Done(uid, workflow_id, output) -// 标记关闭流程 xiang.workflow.Close(uid, workflow_id, output) -// 标记重置流程 xiang.workflow.Reset(uid, workflow_id, output) +// 读取工作流 xiang.workflow.Find(name, workflow_id) +// 读取工作流 xiang.workflow.Open(name, uid, data_id) +// 保存工作流 xiang.workflow.Save(name, uid, node_name, data_id, input, ...output) +// 进入下一个节点 xiang.workflow.Next(name, uid, workflow_id, output) +// 跳转到指定节点 xiang.workflow.Goto(name, uid, workflow_id, node_name, output) +// 更新工作流状态 xiang.workflow.Status(name, uid, workflow_id, status_name, output) +// 标记结束流程 xiang.workflow.Done(name, uid, workflow_id, output) +// 标记关闭流程 xiang.workflow.Close(name, uid, workflow_id, output) +// 标记重置流程 xiang.workflow.Reset(name, uid, workflow_id, output) // API: // 读取工作流 GET /api/xiang/workflow/<工作流名称>/find/:id @@ -66,6 +66,8 @@ func LoadWorkFlow(source []byte, name string) (*WorkFlow, error) { xlog.Println(string(source)) return nil, err } + + workflow.SetupAPIs() WorkFlows[workflow.Name] = &workflow return WorkFlows[workflow.Name], nil } @@ -98,9 +100,6 @@ func (workflow *WorkFlow) Reload() *WorkFlow { // Setting 返回配置信息 func (workflow *WorkFlow) Setting(id int) {} -// SetupAPIs 注册API -func (workflow *WorkFlow) SetupAPIs(id int) {} - // Find 读取给定ID的工作流 // uid 当前处理人ID, id 数据ID func (workflow *WorkFlow) Find(id int) map[string]interface{} {