[feature]添加按部门ID同步钉钉指定部门 (#252)

This commit is contained in:
huxiangquan 2023-09-25 19:55:33 +08:00 committed by GitHub
parent 10bc8361c0
commit 1ac8743156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 10 deletions

View File

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

View File

@ -167,6 +167,7 @@ type DingTalkConfig struct {
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"` EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
DeptSyncTime string `mapstructure:"dept-sync-time" json:"deptSyncTime"` DeptSyncTime string `mapstructure:"dept-sync-time" json:"deptSyncTime"`
UserSyncTime string `mapstructure:"user-sync-time" json:"userSyncTime"` UserSyncTime string `mapstructure:"user-sync-time" json:"userSyncTime"`
DeptList []string `mapstructure:"dept-list" json:"deptList"`
} }
type WeComConfig struct { type WeComConfig struct {

View File

@ -2,6 +2,7 @@ package dingtalk
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/eryajf/go-ldap-admin/config" "github.com/eryajf/go-ldap-admin/config"
@ -16,6 +17,8 @@ func GetAllDepts() (ret []map[string]interface{}, err error) {
if err != nil { if err != nil {
return ret, err return ret, err
} }
if len(config.Conf.DingTalk.DeptList) == 0 {
ret = make([]map[string]interface{}, 0) ret = make([]map[string]interface{}, 0)
for _, dept := range depts.Dept { for _, dept := range depts.Dept {
ele := make(map[string]interface{}) ele := make(map[string]interface{})
@ -25,6 +28,57 @@ func GetAllDepts() (ret []map[string]interface{}, err error) {
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name) ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele) ret = append(ret, ele)
} }
} else {
// 遍历配置的部门ID列表获取数据进行处理
// 从取得的所有部门列表中将配置的部门ID筛选出来再去请求其子部门过滤为1和为配置值的部门ID
ret = make([]map[string]interface{}, 0)
for _, dept := range depts.Dept {
inset := false
for _, dep_s := range config.Conf.DingTalk.DeptList {
if strings.HasPrefix(dep_s, "^") {
continue
}
setdepid, _ := strconv.Atoi(dep_s)
if dept.Id == setdepid {
inset = true
break
}
}
if dept.Id == 1 || inset {
ele := make(map[string]interface{})
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
}
}
for _, dep_s := range config.Conf.DingTalk.DeptList {
dept_id := dep_s
if strings.HasPrefix(dep_s, "^") || dept_id == "1" {
continue
}
depid, _ := strconv.Atoi(dept_id)
depts, err := InitDingTalkClient().FetchDeptList(depid, true, "zh_CN")
if err != nil {
return ret, err
}
for _, dept := range depts.Dept {
ele := make(map[string]interface{})
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
}
}
}
return return
} }