-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchadd.go
90 lines (78 loc) · 2.71 KB
/
batchadd.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
/**
* Created by GoLand.
* User: 姜伟
* Date: 2020/1/31 0031
* Time: 23:13
*/
package role
import (
"regexp"
"strconv"
"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 batchAdd struct {
dingtalk.BaseCorp
corpId string
agentTag string
atType string
roleList []string // 角色列表
userList []string // 用户列表
}
func (ba *batchAdd) SetRoleList(roleList []int) {
if len(roleList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "角色列表不能为空", nil))
} else if len(roleList) > 20 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "角色不能超过20个", nil))
}
ba.roleList = make([]string, 0)
for _, v := range roleList {
if v > 0 {
ba.roleList = append(ba.roleList, strconv.Itoa(v))
}
}
}
func (ba *batchAdd) SetUserList(userList []string) {
if len(userList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "用户列表不能为空", nil))
} else if len(userList) > 100 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "用户不能超过100个", nil))
}
ba.userList = make([]string, 0)
for _, v := range userList {
match, _ := regexp.MatchString(project.RegexDigitAlpha, v)
if match {
ba.userList = append(ba.userList, v)
}
}
}
func (ba *batchAdd) CheckData() (*fasthttp.Client, *fasthttp.Request) {
if len(ba.roleList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "角色列表不能为空", nil))
}
if len(ba.userList) == 0 {
panic(mperr.NewDingTalkCorp(errorcode.DingTalkCorpParam, "用户列表不能为空", nil))
}
ba.ExtendData["roleIds"] = strings.Join(ba.roleList, ",")
ba.ExtendData["userIds"] = strings.Join(ba.userList, ",")
ba.ReqURI = dingtalk.UrlService + "/topapi/role/addrolesforemps?access_token=" + dingtalk.NewUtil().GetAccessToken(ba.corpId, ba.agentTag, ba.atType)
reqBody := mpf.JSONMarshal(ba.ExtendData)
client, req := ba.GetRequest()
req.SetBody([]byte(reqBody))
return client, req
}
func NewBatchAdd(corpId, agentTag, atType string) *batchAdd {
ba := &batchAdd{dingtalk.NewCorp(), "", "", "", make([]string, 0), make([]string, 0)}
ba.corpId = corpId
ba.agentTag = agentTag
ba.atType = atType
ba.ReqContentType = project.HTTPContentTypeJSON
ba.ReqMethod = fasthttp.MethodPost
return ba
}