示例#1
0
        private void SyncWeChatUserGroup()
        {
            var notify   = NotifyInfo;
            var notifier = Notifier;
            var strs     = Parameter.ToString().Split(';');
            var tenantId = int.Parse(strs[0]);
            var userId   = strs[1];
            var logger   = Logger;

            try
            {
                ReportProgress(1, "正在初始化同步信息");

                var userGroupApi = new UserGroupApi();
                userGroupApi.SetKey(tenantId);

                var result = userGroupApi.Get();
                if (result.IsSuccess())
                {
                    ReportProgress(30, "已成功获取数据:" + result.Groups.Count);
                    using (var db = new AppDbContext())
                    {
                        TenantManager.Current.EnableTenantFilter(db, tenantId);
                        db.WeiChat_UserGroups.RemoveRange(
                            db.WeiChat_UserGroups.Where(p => !db.WeiChat_Users.Any(p1 => p1.GroupIds.Any(d => d == p.GroupId))));
                        db.SaveChanges();

                        var tenantGroups = db.WeiChat_UserGroups.ToList();
                        var groups       = (from item in result.Groups
                                            //where !tenantGroups.Any(p => p.GroupIds == item.Id)
                                            where !tenantGroups.Any(p => p.GroupId == item.Id)
                                            select new WeiChat_UserGroup
                        {
                            //GroupIds = item.Id,
                            Name = item.Name,
                            UsersCount = item.Count,
                            TenantId = tenantId
                        }).ToList();
                        db.WeiChat_UserGroups.AddRange(groups);

                        var count = result.Groups == null ? 0 : result.Groups.Count;
                        ReportProgress(90, "正在写入数据,写入数量(" + count + ")...");
                        db.SaveChanges();
                        ReportProgress(100, "同步成功!同步数量(" + count + ")。");
                    }
                }
                else
                {
                    ReportProgress(100, "同步失败!" + result.GetFriendlyMessage());
                }
            }
            catch (Exception ex)
            {
                logger.Log(LoggerLevels.Debug, Title + " 失败!");
                logger.LogException(ex);

                ReportProgress(100, "同步失败!" + ex.Message);
                return;
            }
        }
示例#2
0
 static UserCenterApi()
 {
     appKey        = ConfigurationManager.AppSettings["UC_AppKey"];
     appSecret     = ConfigurationManager.AppSettings["UC_AppSecret"];
     appServerRoot = ConfigurationManager.AppSettings["UC_ServerRoot"];
     User          = new UserApi(appKey, appSecret, appServerRoot);
     UserGroup     = new UserGroupApi(appKey, appSecret, appServerRoot);
 }
示例#3
0
        static void Main(string[] args)
        {
            UserApi user = new UserApi("qqqqq", "12345", "http://127.0.0.1:8888/api/v1/");


            //var u = user.GetByIdAsync(1).Result;
            //Console.WriteLine(u.Id+" "+u.NickName+" "+u.PhoneNum+" ");

            //var id = user.AddNewAsync("13872303747", "Aun", "qqqqq").Result;
            // Console.WriteLine(id+" ");

            //var res = user.CheckLoginAsync("13272303747", "qqqqq").Result;
            // Console.WriteLine(res + " ");

            //var u = user.GetByPhoneNumAsync("13272303747").Result;
            //Console.WriteLine(u.Id + " " + u.NickName + " " + u.PhoneNum + " ");


            UserGroupApi userGroup = new UserGroupApi("qqqqq", "12345", "http://127.0.0.1:8888/api/v1/");
            // userGroup.AddUserToGroupAsync(2, 3).Wait();

            //var userGroupArr = userGroup.GetGroupsAsync(3).Result;
            //foreach (var ug in userGroupArr)
            //{
            //    Console.WriteLine(ug.Id+" Name:"+ug.Name);
            //}

            var ug1 = userGroup.GetByIdAsync(2).Result;

            Console.WriteLine("Group信息:" + ug1.Id + " UserGroupName:" + ug1.Name);

            Console.WriteLine("该Group中的User:");

            var userArr = userGroup.GetGroupUsersAsync(2).Result;

            foreach (var u in userArr)
            {
                Console.WriteLine(u.Id + " Name:" + u.NickName + "  " + u.PhoneNum);
            }

            userGroup.RemoveUserFromGroupAsync(2, 3).Wait();
            Console.WriteLine("删除成功");

            Console.WriteLine("ok");
            Console.ReadKey();
        }
示例#4
0
        /// <summary>
        ///     同步粉丝组
        /// </summary>
        private async Task <bool> SyncWeChatGroups(bool isUserSync = false, string createBy = null)
        {
            var userGroupApi = new UserGroupApi();

            userGroupApi.SetKey(_tenantId);

            var getResult = userGroupApi.Get();

            if (!getResult.IsSuccess())
            {
                return(await System.Threading.Tasks.Task.FromResult(false));
            }

            _db.WeiChat_UserGroups.RemoveRange(
                _db.WeiChat_UserGroups.Where(p => !_db.WeiChat_Users.Any(p1 => p1.GroupId == p.GroupId)));
            await _db.SaveChangesAsync();

            var tenantGroups = _db.WeiChat_UserGroups.ToList();
            var groups       = (from item in getResult.Groups
                                where !tenantGroups.Any(p => p.GroupId == item.Id)
                                select new WeiChat_UserGroup
            {
                GroupId = item.Id,
                Name = item.Name,
                UsersCount = item.Count,
                TenantId = _tenantId
            }).ToList();

            _db.WeiChat_UserGroups.AddRange(groups);
            var log = new WeiChat_SyncLog
            {
                Type       = WeiChat_SyncTypes.Sync_WeiChat_UserGroup,
                IsUserSync = isUserSync,
                TenantId   = _tenantId,
                CreateBy   = createBy,
                Message    = string.Format("同步成功!同步组数:{0}。", groups.Count)
            };

            _db.WeiChat_SyncLogs.Add(log);
            await _db.SaveChangesAsync();

            return(await System.Threading.Tasks.Task.FromResult(true));
        }