From 7e910bf5fa89d7e38046714f8b13c0ba75b3db0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E4=B8=AB=E8=AE=B2=E6=A2=B5?= Date: Sun, 10 Jul 2022 17:40:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=89=E4=B8=AA=E4=B8=89=E6=96=B9IM?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=9A=84=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20(#62)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yml | 3 ++- config/config.go | 8 ++++--- logic/a_logic.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 24 +++---------------- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/config.yml b/config.yml index f3cd07b..5a0a79c 100644 --- a/config.yml +++ b/config.yml @@ -117,4 +117,5 @@ feishu: # 配置获取详细文档参考:http://ldapdoc.eryajf.net/pages/83c90b/ flag: "feishu" # 作为飞书在平台的标识 app-id: "xxxxxxx" # 飞书的app-id - app-secret: "xxxxxxxxxxx" # 飞书的app-secret \ No newline at end of file + app-secret: "xxxxxxxxxxx" # 飞书的app-secret + enable-sync: false # 是否开启定时同步飞书的任务 \ No newline at end of file diff --git a/config/config.go b/config/config.go index b23c3e5..da2cab7 100644 --- a/config/config.go +++ b/config/config.go @@ -165,10 +165,12 @@ type WeComConfig struct { CorpID string `mapstructure:"corp-id" json:"corpId"` AgentID int `mapstructure:"agent-id" json:"agentId"` CorpSecret string `mapstructure:"corp-secret" json:"corpSecret"` + EnableSync bool `mapstructure:"enable-sync" json:"enableSync"` } type FeiShuConfig struct { - Flag string `mapstructure:"flag" json:"flag"` - AppID string `mapstructure:"app-id" json:"appId"` - AppSecret string `mapstructure:"app-secret" json:"appSecret"` + Flag string `mapstructure:"flag" json:"flag"` + AppID string `mapstructure:"app-id" json:"appId"` + AppSecret string `mapstructure:"app-secret" json:"appSecret"` + EnableSync bool `mapstructure:"enable-sync" json:"enableSync"` } diff --git a/logic/a_logic.go b/logic/a_logic.go index 2499406..a31e9c9 100644 --- a/logic/a_logic.go +++ b/logic/a_logic.go @@ -5,10 +5,12 @@ import ( "github.com/eryajf/go-ldap-admin/config" "github.com/eryajf/go-ldap-admin/model" + "github.com/eryajf/go-ldap-admin/public/common" "github.com/eryajf/go-ldap-admin/public/tools" "github.com/eryajf/go-ldap-admin/service/ildap" "github.com/eryajf/go-ldap-admin/service/isql" jsoniter "github.com/json-iterator/go" + "github.com/robfig/cron/v3" "github.com/tidwall/gjson" ) @@ -304,3 +306,61 @@ func ConvertUserData(flag string, remoteData []map[string]interface{}) (users [] } return } + +func InitCron() { + c := cron.New(cron.WithSeconds()) + + if config.Conf.DingTalk.EnableSync { + //启动定时任务 + _, err := c.AddFunc("0 1 5 * * *", func() { + common.Log.Info("每天凌晨5点1分0秒执行一次同步钉钉部门信息到ldap") + DingTalk.SyncDingTalkDepts(nil, nil) + }) + if err != nil { + common.Log.Errorf("启动同步部门的定时任务失败: %v", err) + } + //每天凌晨1点执行一次 + _, err = c.AddFunc("0 30 5 * * *", func() { + common.Log.Info("每天凌晨5点30分执行一次同步钉钉用户信息到ldap") + DingTalk.SyncDingTalkUsers(nil, nil) + }) + if err != nil { + common.Log.Errorf("启动同步用户的定时任务失败: %v", err) + } + } + if config.Conf.WeCom.EnableSync { + _, err := c.AddFunc("0 1 5 * * *", func() { + common.Log.Info("每天凌晨5点1分0秒执行一次同步企业微信部门信息到ldap") + WeCom.SyncWeComDepts(nil, nil) + }) + if err != nil { + common.Log.Errorf("启动同步部门的定时任务失败: %v", err) + } + //每天凌晨1点执行一次 + _, err = c.AddFunc("0 30 5 * * *", func() { + common.Log.Info("每天凌晨5点30分执行一次同步企业微信用户信息到ldap") + WeCom.SyncWeComUsers(nil, nil) + }) + if err != nil { + common.Log.Errorf("启动同步用户的定时任务失败: %v", err) + } + } + if config.Conf.FeiShu.EnableSync { + _, err := c.AddFunc("0 1 5 * * *", func() { + common.Log.Info("每天凌晨5点1分0秒执行一次同步飞书部门信息到ldap") + FeiShu.SyncFeiShuDepts(nil, nil) + }) + if err != nil { + common.Log.Errorf("启动同步部门的定时任务失败: %v", err) + } + //每天凌晨1点执行一次 + _, err = c.AddFunc("0 30 5 * * *", func() { + common.Log.Info("每天凌晨5点30分执行一次同步飞书用户信息到ldap") + FeiShu.SyncFeiShuUsers(nil, nil) + }) + if err != nil { + common.Log.Errorf("启动同步用户的定时任务失败: %v", err) + } + } + c.Start() +} diff --git a/main.go b/main.go index 60340d5..3e58bd3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "time" "github.com/eryajf/go-ldap-admin/logic" - "github.com/robfig/cron/v3" "github.com/eryajf/go-ldap-admin/config" "github.com/eryajf/go-ldap-admin/middleware" @@ -65,26 +64,9 @@ func main() { common.Log.Fatalf("listen: %s\n", err) } }() - if config.Conf.DingTalk.EnableSync { - //启动定时任务 - c := cron.New(cron.WithSeconds()) - _, err := c.AddFunc("0 1 0 * * *", func() { - common.Log.Info("每天0点1分0秒执行一次同步钉钉部门和用户信息到ldap") - logic.DingTalk.SyncDingTalkDepts(nil, nil) - }) - if err != nil { - common.Log.Errorf("启动同步部门的定时任务失败: %v", err) - } - //每天凌晨1点执行一次 - _, err = c.AddFunc("0 15 0 * * *", func() { - common.Log.Info("每天凌晨00点15分执行一次同步钉钉部门和用户信息到ldap") - logic.DingTalk.SyncDingTalkUsers(nil, nil) - }) - if err != nil { - common.Log.Errorf("启动同步用户的定时任务失败: %v", err) - } - c.Start() - } + + // 启动定时任务 + logic.InitCron() common.Log.Info(fmt.Sprintf("Server is running at %s:%d/%s", host, port, config.Conf.System.UrlPathPrefix))