Ejemplo n.º 1
0
        /// <summary>
        /// 缓存钉钉部门用户
        /// </summary>
        private static void SaveDingTalkUserListCache(string key, DingTalkUserList token)
        {
            var data = token.ToJSON();

            RedisHelper.SetForever(key, data);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 服务器获取钉钉access_token
        /// </summary>
        private static void GetDingTalkUserList(bool init = false)
        {
            var nKey = nUserListLockKey + NotifyPlatformEnum.DingTalk;

            if (init || ServerConfig.IsAnalysis && RedisHelper.SetIfNotExist(nKey, ServerConfig.GetLockValue))
            {
                RedisHelper.SetExpireAt(nKey, DateTime.Now.AddMinutes(30));
                var now            = DateTime.Now;
                var dingTalk       = Configs.Where(x => !x.IsChat && x.Platform == NotifyPlatformEnum.DingTalk).Where(x => !x.CorpId.IsNullOrEmpty() && !x.AgentId.IsNullOrEmpty());
                var dingTalkGroups = dingTalk.GroupBy(x => new { x.CorpId, x.AgentId }).ToDictionary(x => x.Key);
                foreach (var(key, value) in dingTalkGroups)
                {
                    var corpId  = key.CorpId;
                    var agentId = key.AgentId;
                    var tKey    = $"{nRedisPre}:Token_{corpId}_{agentId}";
                    var token   = GetDingTalkAccessToken(tKey);
                    if (token.IsNullOrEmpty())
                    {
                        continue;
                    }

                    var uKey     = $"{nRedisPre}:UserList_{corpId}_{agentId}";
                    var userList = GetDingTalkUserListCache(uKey);
                    var get      = false;
                    var refresh  = false;
                    if (userList != null)
                    {
                        if (userList.refreshed >= _interval)
                        {
                            refresh = true;
                        }
                    }
                    else
                    {
                        get = true;
                    }

                    if (!get && !refresh)
                    {
                        continue;
                    }

                    var nList = new DingTalkUserList(now);
                    foreach (var dep in value)
                    {
                        try
                        {
                            var b = true;
                            while (b)
                            {
                                b = false;
                                var offset = dep.Offset;
                                var url    = "https://oapi.dingtalk.com/user/listbypage";
                                var f      = HttpServer.Get(url, new Dictionary <string, string>
                                {
                                    { "access_token", token },
                                    { "department_id", dep.DepId.ToString() },
                                    { "offset", offset.ToString() },
                                    { "size", dep.Size.ToString() },
                                });
                                if (f == "fail")
                                {
                                    Log.Error($"GetDingTalkUserList 请求失败,url:{url}");
                                }
                                else
                                {
                                    var res = JsonConvert.DeserializeObject <DingTalkUserList>(f);
                                    if (res.errcode != 0)
                                    {
                                        Log.Error($"GetDingTalkUserList 返回失败,url:{url},返回码:{res.errcode},原因:{res.errmsg}");
                                    }
                                    else
                                    {
                                        b = res.hasMore;
                                        foreach (var user in res.userlist)
                                        {
                                            var u = nList.userlist.FirstOrDefault(x => x.userid == user.userid);
                                            if (u != null)
                                            {
                                                u.name       = user.name;
                                                u.unionid    = user.unionid;
                                                u.department = u.department.Concat(user.department).Distinct().OrderBy(x => x).ToList();
                                                u.mobile     = user.mobile;
                                                u.email      = user.email;
                                            }
                                            else
                                            {
                                                nList.userlist.Add(user);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"GetDingTalkUserList 失败,原因:{e.Message},错误:{e.StackTrace}");
                        }
                    }

                    //nList.userlist = nList.userlist.OrderBy(x => x.userid).ThenBy(x => x.name).ToList();
                    if (get)
                    {
                        SaveDingTalkUserListCache(uKey, nList);
                    }
                    else
                    {
                        userList.refresh = nList.refresh;
                        SaveDingTalkUserListCache(uKey, userList);
                    }
                }

                RedisHelper.Remove(nKey);
            }
        }