-
Notifications
You must be signed in to change notification settings - Fork 0
/
instancecreate.go
147 lines (129 loc) · 5.2 KB
/
instancecreate.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
package process
import (
"regexp"
"strings"
"github.com/a07061625/gompf/mpf"
"github.com/a07061625/gompf/mpf/api/dingtalk"
"github.com/a07061625/gompf/mpf/mpconstant/errorcode"
"github.com/a07061625/gompf/mpf/mpconstant/project"
"github.com/a07061625/gompf/mpf/mperr"
"github.com/valyala/fasthttp"
)
// 发起审批实例
type instanceCreate struct {
dingtalk.BaseCorp
corpId string
agentTag string
atType string
processCode string // 审批码
userId string // 发起人用户ID
departId int // 发起人部门ID
approverList []string // 审批人列表
formValueList []map[string]interface{} // 表单参数列表
}
func (ic *instanceCreate) SetProcessCode(processCode string) {
if len(processCode) > 0 {
ic.processCode = processCode
} else {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "审批码不合法", nil))
}
}
func (ic *instanceCreate) SetUserId(userId string) {
match, _ := regexp.MatchString(project.RegexDigitAlpha, userId)
if match {
ic.userId = userId
} else {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "发起人用户ID不合法", nil))
}
}
func (ic *instanceCreate) SetDepartId(departId int) {
if (departId == -1) || (departId > 0) {
ic.departId = departId
} else {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "发起人部门ID不合法", nil))
}
}
func (ic *instanceCreate) SetApproverList(approverList []string) {
if len(approverList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "审批人列表不能为空", nil))
} else if len(approverList) > 20 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "审批人不能超过20个", nil))
}
approvers := make([]string, 0)
for _, v := range approverList {
match, _ := regexp.MatchString(project.RegexDigitAlpha, v)
if match {
approvers = append(approvers, v)
}
}
ic.approverList = approvers
}
func (ic *instanceCreate) SetFormValueList(formValueList []map[string]interface{}) {
if len(formValueList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "表单参数不能为空", nil))
} else if len(formValueList) > 20 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "表单参数不能超过20个", nil))
}
ic.formValueList = formValueList
}
func (ic *instanceCreate) SetCcList(ccList []string) {
if len(ccList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "抄送人列表不能为空", nil))
} else if len(ccList) > 20 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "抄送人不能超过20个", nil))
}
userList := make([]string, 0)
for _, v := range ccList {
match, _ := regexp.MatchString(project.RegexDigitAlpha, v)
if match {
userList = append(userList, v)
}
}
ic.ExtendData["cc_list"] = strings.Join(userList, ",")
}
func (ic *instanceCreate) SetCcPosition(ccPosition string) {
if len(ccPosition) > 0 {
ic.ExtendData["cc_position"] = ccPosition
} else {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "抄送时间不合法", nil))
}
}
func (ic *instanceCreate) CheckData() (*fasthttp.Client, *fasthttp.Request) {
if len(ic.processCode) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "审批码不能为空", nil))
}
if len(ic.userId) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "发起人用户ID不能为空", nil))
}
if ic.departId == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "发起人部门ID不能为空", nil))
}
if len(ic.approverList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "审批人列表不能为空", nil))
}
if len(ic.formValueList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "表单参数列表不能为空", nil))
}
ic.ExtendData["process_code"] = ic.processCode
ic.ExtendData["originator_user_id"] = ic.userId
ic.ExtendData["dept_id"] = ic.departId
ic.ExtendData["approvers"] = strings.Join(ic.approverList, ",")
ic.ExtendData["form_component_values"] = ic.formValueList
if ic.atType == dingtalk.AccessTokenTypeProviderAuthorize {
ic.ExtendData["agent_id"] = dingtalk.NewConfig().GetProvider().GetSuiteId()
}
ic.ReqURI = dingtalk.UrlService + "/topapi/processinstance/create?access_token=" + dingtalk.NewUtil().GetAccessToken(ic.corpId, ic.agentTag, ic.atType)
reqBody := mpf.JSONMarshal(ic.ExtendData)
client, req := ic.GetRequest()
req.SetBody([]byte(reqBody))
return client, req
}
func NewInstanceCreate(corpId, agentTag, atType string) *instanceCreate {
ic := &instanceCreate{dingtalk.NewCorp(), "", "", "", "", "", 0, make([]string, 0), make([]map[string]interface{}, 0)}
ic.corpId = corpId
ic.agentTag = agentTag
ic.atType = atType
ic.ReqContentType = project.HTTPContentTypeJSON
ic.ReqMethod = fasthttp.MethodPost
return ic
}