Skip to content

Commit 1ac8743

Browse files
authored
[feature]添加按部门ID同步钉钉指定部门 (#252)
1 parent 10bc836 commit 1ac8743

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ dingtalk:
117117
enable-sync: false # 是否开启定时同步钉钉的任务
118118
dept-sync-time: "0 30 2 * * *" # 部门同步任务的时间点 * * * * * * 秒 分 时 日 月 周, 请把时间设置在凌晨 1 ~ 5 点
119119
user-sync-time: "0 30 3 * * *" # 用户同步任务的时间点 * * * * * * 秒 分 时 日 月 周, 请把时间设置在凌晨 1 ~ 5 点,注意请把用户同步的任务滞后于部门同步时间,比如部门为2点,则用户为3点
120+
dept-list: #部门列表,不设置则使用公司根部门,在开头加^表示不同步此部门,只需配置需要同步的部门ID
121+
#- "1" #根组织ID
122+
- "48456726" #需要同步的部门ID
123+
#- "^61213417" #不同步的ID,不配置即只同步上一ID
120124
wecom:
121125
# 配置获取详细文档参考:http://ldapdoc.eryajf.net/pages/cf1698/
122126
flag: "wecom" # 作为微信在平台的标识

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ type DingTalkConfig struct {
167167
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
168168
DeptSyncTime string `mapstructure:"dept-sync-time" json:"deptSyncTime"`
169169
UserSyncTime string `mapstructure:"user-sync-time" json:"userSyncTime"`
170+
DeptList []string `mapstructure:"dept-list" json:"deptList"`
170171
}
171172

172173
type WeComConfig struct {

public/client/dingtalk/dingtalk.go

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dingtalk
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/eryajf/go-ldap-admin/config"
@@ -14,16 +15,69 @@ import (
1415
func GetAllDepts() (ret []map[string]interface{}, err error) {
1516
depts, err := InitDingTalkClient().FetchDeptList(1, true, "zh_CN")
1617
if err != nil {
17-
return ret, err
18-
}
19-
ret = make([]map[string]interface{}, 0)
20-
for _, dept := range depts.Dept {
21-
ele := make(map[string]interface{})
22-
ele["id"] = dept.Id
23-
ele["name"] = dept.Name
24-
ele["parentid"] = dept.ParentId
25-
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
26-
ret = append(ret, ele)
18+
return ret, err
19+
}
20+
if len(config.Conf.DingTalk.DeptList) == 0 {
21+
22+
ret = make([]map[string]interface{}, 0)
23+
for _, dept := range depts.Dept {
24+
ele := make(map[string]interface{})
25+
ele["id"] = dept.Id
26+
ele["name"] = dept.Name
27+
ele["parentid"] = dept.ParentId
28+
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
29+
ret = append(ret, ele)
30+
}
31+
} else {
32+
33+
// 遍历配置的部门ID列表获取数据进行处理
34+
// 从取得的所有部门列表中将配置的部门ID筛选出来再去请求其子部门过滤为1和为配置值的部门ID
35+
ret = make([]map[string]interface{}, 0)
36+
37+
for _, dept := range depts.Dept {
38+
inset := false
39+
for _, dep_s := range config.Conf.DingTalk.DeptList {
40+
if strings.HasPrefix(dep_s, "^") {
41+
continue
42+
}
43+
setdepid, _ := strconv.Atoi(dep_s)
44+
if dept.Id == setdepid {
45+
inset = true
46+
break
47+
}
48+
}
49+
if dept.Id == 1 || inset {
50+
ele := make(map[string]interface{})
51+
ele["id"] = dept.Id
52+
ele["name"] = dept.Name
53+
ele["parentid"] = dept.ParentId
54+
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
55+
ret = append(ret, ele)
56+
}
57+
}
58+
59+
for _, dep_s := range config.Conf.DingTalk.DeptList {
60+
dept_id := dep_s
61+
62+
if strings.HasPrefix(dep_s, "^") || dept_id == "1" {
63+
continue
64+
}
65+
depid, _ := strconv.Atoi(dept_id)
66+
depts, err := InitDingTalkClient().FetchDeptList(depid, true, "zh_CN")
67+
68+
if err != nil {
69+
return ret, err
70+
}
71+
72+
for _, dept := range depts.Dept {
73+
ele := make(map[string]interface{})
74+
ele["id"] = dept.Id
75+
ele["name"] = dept.Name
76+
ele["parentid"] = dept.ParentId
77+
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
78+
ret = append(ret, ele)
79+
}
80+
}
2781
}
2882
return
2983
}

0 commit comments

Comments
 (0)