-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtop_api_approval.go
187 lines (169 loc) · 6.47 KB
/
top_api_approval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Author Kevin Zhu
*
* Direct questions, comments to <[email protected]>
*/
package godingtalk
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"strings"
"time"
)
const (
topAPICreateProcInstMethod = "dingtalk.smartwork.bpms.processinstance.create"
topAPIGetProcInstMethod = "dingtalk.smartwork.bpms.processinstance.get"
topAPIListProcInstMethod = "dingtalk.smartwork.bpms.processinstance.list"
)
type TopAPICreateProcInst struct {
// 审批模板code
ProcessCode string `json:"process_code"`
// 发起人UID
OriginatorUID string `json:"originator_user_id"`
// 发起人所在部门
DeptID int `json:"dept_id"`
// 审批人列表
Approvers []string `json:"approvers"`
// 抄送人列表
CCList []string `json:"cc_list"`
//抄送时间,分为(START,FINISH,START_FINISH
CCPosition string `json:"cc_position"`
// 审批单内容, Name为审批模板中的列名, value 为该列的值
FormCompntValues []ProcInstCompntValues `json:"form_component_values"`
}
type ProcInst struct {
ProcInstID string `json:"process_instance_id"`
Title string `json:"title"`
CreateTime string `json:"create_time"`
FinishTime string `json:"finish_time"`
OriginatorUID string `json:"originator_userid"`
Status string `json:"status"`
ApproverUIDS []string `json:"approver_userids"`
CCUIDS []string `json:"cc_userids"`
Result string `json:"result"`
BusinessID string `json:"business_id"`
FormCompntValues []ProcInstCompntValues `json:"form_component_values"` // 表单详情列表
Tasks []_ProcInstTasks `json:"tasks"` // 任务列表
OperationRecords []_ProcInstOperationRecords `json:"operation_records"` // 操作记录列表
OriginatorDeptID string `json:"originator_dept_id"`
OriginatorDeptName string `json:"originator_dept_name"`
}
type ProcInstCompntValues struct {
Name string `json:"name"`
Value string `json:"value"`
ExtValue string `json:"ext_value"`
}
type _ProcInstOperationRecords struct {
UID string `json:"userid"`
Date string `json:"date"`
Type string `json:"operation_type"`
Result string `json:"operation_result"`
Remark string `json:"remark"`
}
type _ProcInstTasks struct {
UID string `json:"userid"`
Status string `json:"task_status"`
Result string `json:"task_result"`
CreateTime string `json:"create_time"`
FinishTime string `json:"finish_time"`
}
type topAPICreateProcInstResp struct {
topAPIErrResponse
OK struct {
Errcode int `json:"ding_open_errcode"`
ErrMsg string `json:"error_msg"`
IsSuccess bool `jons:"is_success"`
ProcInstID string `json:"process_instance_id"`
} `json:"result"`
RequestID string `json:"request_id"`
}
// 发起审批
func (c *DingTalkClient) TopAPICreateProcInst(data TopAPICreateProcInst) (string, error) {
var resp topAPICreateProcInstResp
values, err := json.Marshal(data.FormCompntValues)
if err != nil {
return "", err
}
form := url.Values{}
form.Add("method", topAPICreateProcInstMethod)
form.Add("cc_list", strings.Join(data.CCList, ","))
form.Add("dept_id", strconv.Itoa(data.DeptID))
form.Add("approvers", strings.Join(data.Approvers, ","))
form.Add("cc_position", data.CCPosition)
form.Add("process_code", data.ProcessCode)
form.Add("originator_user_id", data.OriginatorUID)
form.Add("form_component_values", string(values))
if c.AgentID != "" {
form.Add("agent_id", c.AgentID)
}
return resp.OK.ProcInstID, c.topAPIRequest(form, &resp)
}
type topAPIGetProcInstResp struct {
Ok struct {
ErrCode int `json:"ding_open_errcode"`
ErrMsg string `json:"error_msg"`
Success bool `json:"success"`
ProcInst ProcInst `json:"process_instance"`
} `json:"result"`
RequestID string `json:"request_id"`
topAPIErrResponse
}
// 根据审批实例id获取单条审批实例详情
func (c *DingTalkClient) TopAPIGetProcInst(pid string) (ProcInst, error) {
var resp topAPIGetProcInstResp
reqForm := url.Values{}
reqForm.Add("process_instance_id", pid)
reqForm.Add("method", topAPIGetProcInstMethod)
err := c.topAPIRequest(reqForm, &resp)
if err != nil {
return resp.Ok.ProcInst, err
}
resp.Ok.ProcInst.ProcInstID = pid
return resp.Ok.ProcInst, err
}
type ListProcInst struct {
ApproverUIDS []string `json:"approver_userid_list"`
CCUIDS []string `json:"cc_userid_list"`
FormCompntValues []ProcInstCompntValues `json:"form_component_values"`
ProcInstID string `json:"process_instance_id"`
Title string `json:"title"`
CreateTime string `json:"create_time"`
FinishTime string `json:"finish_time"`
OriginatorUID string `json:"originator_userid"`
Status string `json:"status"`
BusinessID string `json:"business_id"`
OriginatorDeptID string `json:"originator_dept_id"`
ProcInstResult string `json:"process_instance_result"` // "agree",
}
type TopAPIListProcInstResp struct {
topAPIErrResponse
OK struct {
ErrCode int `json:"ding_open_errcode"`
ErrMsg string `json:"error_msg"`
Success bool `json:"success"`
Result struct {
List []ListProcInst `json:"list"`
NextCursor int `json:"next_cursor"`
} `json:"result"`
} `json:"result"`
RequestID string `json:"request_id"`
}
// 获取审批实例列表
// Note: processCode 官方不会检查错误,请保证processCode正确
func (c *DingTalkClient) TopAPIListProcInst(processCode string, startTime, endTime time.Time, size, cursor int, useridList []string) (TopAPIListProcInstResp, error) {
var resp TopAPIListProcInstResp
if size > 10 {
return resp, errors.New("Max size is 10")
}
reqForm := url.Values{}
reqForm.Add("process_code", processCode)
reqForm.Add("start_time", strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10))
reqForm.Add("end_time", strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10))
reqForm.Add("size", strconv.Itoa(size))
reqForm.Add("cursor", strconv.Itoa(cursor))
reqForm.Add("userid_list", strings.Join(useridList, ","))
reqForm.Add("method", topAPIListProcInstMethod)
return resp, c.topAPIRequest(reqForm, &resp)
}