-
Notifications
You must be signed in to change notification settings - Fork 94
/
corp_dept.go
128 lines (113 loc) · 2.9 KB
/
corp_dept.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
package wechat
import (
"fmt"
"log"
"strings"
"github.com/esap/wechat/util"
)
// CorpAPIDeptList 企业微信部门列表接口
const (
CorpAPIDeptList = CorpAPI + `department/list?access_token=%s`
CorpAPIDeptAdd = CorpAPI + `department/create?access_token=`
CorpAPIDeptUpdate = CorpAPI + `department/update?access_token=`
CorpAPIDeptDel = CorpAPI + `department/delete?access_token=`
)
type (
// DeptList 部门列表
DeptList struct {
WxErr
Department []Department
}
// Department 部门
Department struct {
Id int `json:"id"`
Name string `json:"name"`
ParentId int `json:"parentid"`
Order1 int64 `json:"order"`
}
)
// SyncDeptList 更新部门列表
func (s *Server) SyncDeptList() (err error) {
s.DeptList, err = s.GetDeptList()
if err != nil {
log.Printf("[%v::%v]获取部门列表失败:%v", s.AppId, s.AgentId, err)
}
return
}
// GetDeptList 获取部门列表
func (s *Server) GetDeptList() (dl DeptList, err error) {
url := fmt.Sprintf(CorpAPIDeptList, s.GetUserAccessToken())
if err = util.GetJson(url, &dl); err != nil {
return
}
err = dl.Error()
return
}
// GetDeptIdList 获取部门id列表
func (s *Server) GetDeptIdList() (deptIdlist []int) {
deptIdlist = make([]int, 0)
s.SyncDeptList()
for _, v := range s.DeptList.Department {
deptIdlist = append(deptIdlist, v.Id)
}
return
}
// DeptAdd 获取部门列表
func (s *Server) DeptAdd(dept *Department) (err error) {
return s.doUpdate(CorpAPIDeptAdd, dept)
}
// DeptUpdate 获取部门列表
func (s *Server) DeptUpdate(dept *Department) (err error) {
return s.doUpdate(CorpAPIDeptUpdate, dept)
}
// DeptDelete 删除部门
func (s *Server) DeptDelete(Id int) (err error) {
e := new(WxErr)
if err = util.GetJson(CorpAPIDeptDel+s.GetUserAccessToken()+"&id="+fmt.Sprint(Id), e); err != nil {
return
}
return e.Error()
}
// GetDeptName 通过部门id获取部门名称
func (s *Server) GetDeptName(id int) string {
for _, v := range s.DeptList.Department {
if v.Id == id {
return v.Name
}
}
return ""
}
// GetToParty 获取acl所包含的所有部门ID,结果形式:tagId1|tagId2|tagId3...
func (s *Server) GetToParty(acl interface{}) string {
s1 := strings.TrimSpace(acl.(string))
arr := strings.Split(toUserReplacer.Replace(s1), "|")
for k, totag := range arr {
for _, v := range s.DeptList.Department {
if v.Name == totag {
arr[k] = fmt.Sprint(v.Id)
}
}
}
return strings.Join(arr, "|")
}
// CheckDeptAcl 测试权限,对比user是否包含于acl
func (s *Server) CheckDeptAcl(userid, acl string) bool {
acl = strings.TrimSpace(acl)
if acl == "" {
return false
}
u := s.GetUser(userid)
if u == nil {
return false
}
acl = "|" + toUserReplacer.Replace(acl) + "|"
for _, id := range u.Department {
if strings.Contains(acl, "|"+s.GetDeptName(id)+"|") {
return true
}
if strings.Contains(acl, "|"+fmt.Sprint(id)+"|") {
return true
}
}
return false
}