Ejemplo n.º 1
0
        /// <summary>
        /// GetOrGenerateItemByMobileAsync
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="generateStatus"></param>
        /// <param name="mobile"></param>
        /// <param name="mobileIsValid"></param>
        /// <returns></returns>
        public async Task <XM.UserInfo> GetOrGenerateItemByMobileAsync(Guid groupId, XM.UserStatus generateStatus, string mobile, bool mobileIsValid)
        {
            if (mobile.IsNullOrWhiteSpace())
            {
                return(null);
            }
            var user = await _userManager.GetItemByMobileAsync(mobile, null, null);

            if (user == null)
            {
                var newUser = new User
                {
                    Status        = generateStatus,
                    CreationTime  = DateTime.Now,
                    Mobile        = mobile,
                    MobileIsValid = mobileIsValid,
                    GroupId       = groupId, // new Guid("11111111-1111-1111-1111-111111111111") 等待分配组
                    Username      = "******" + Guid.NewGuid().ToString("N").Substring(19),
                    Password      = mobile,
                };

                _context.User.Add(newUser);
                await _context.SaveChangesAsync();

                user = await _userManager.GetItemByMobileAsync(mobile, null, null);
            }
            return(user);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// ChangeStatusAsync
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public async Task <bool> ChangeStatusAsync(int userId, XM.UserStatus status)
        {
            User user = await _context.User.FirstOrDefaultAsync(m => m.UserId == userId);

            if (user == null)
            {
                return(false);
            }
            user.Status = status;
            await _context.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// ChangeStatusAsync
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="status"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <bool> ChangeStatusAsync(int userId, XM.UserStatus status, ModelStateDictionary modelState)
        {
            User user = await _context.User.FirstOrDefaultAsync(m => m.UserId == userId);

            if (user == null)
            {
                modelState.AddModelError("Error", "用户不存在");
                return(false);
            }
            user.Status = status;
            await _context.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// GenerateItemAsync
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="status"></param>
        /// <param name="mobile"></param>
        /// <param name="password"></param>
        /// <param name="modelState"></param>
        /// <returns></returns>
        public async Task <XM.UserInfo> GenerateItemAsync(Guid groupId, XM.UserStatus status, string mobile, string password, ModelStateDictionary modelState)
        {
            if (await _context.User.AnyAsync(m => m.Mobile == mobile))
            {
                modelState.AddModelError(nameof(mobile), $"手机号 {mobile} 已被注册。");
                return(null);
            }
            var newUser = new User
            {
                Status        = status,
                CreationTime  = DateTime.Now,
                Mobile        = mobile,
                MobileIsValid = true,
                GroupId       = groupId,
                Username      = "******" + Guid.NewGuid().ToString("N").Substring(19),
                Password      = password,
            };

            _context.User.Add(newUser);
            await _context.SaveChangesAsync();

            return(await _userManager.GetItemByUserIdAsync(newUser.UserId));
        }
Ejemplo n.º 5
0
        public async Task <XM.UserInfo> GenerateItemAsync(Guid groupId, XM.UserStatus status, string mobile, string password, ModelStateDictionary modelState)
        {
            if (await _tubumuContext.User.AnyAsync(m => m.Mobile == mobile))
            {
                modelState.AddModelError(nameof(mobile), $"手机号 {mobile} 已被注册。");
                return(null);
            }

            var newUser = new User
            {
                Status        = status,
                CreationDate  = DateTime.Now,
                Mobile        = mobile,
                MobileIsValid = true,
                GroupId       = groupId,
                Username      = "******" + Guid.NewGuid().ToString("N").Substring(19),
                Password      = password,
            };

            _tubumuContext.User.Add(newUser);
            await _tubumuContext.SaveChangesAsync();

            return(await _tubumuContext.User.AsNoTracking().Select(_selector).FirstOrDefaultAsync(m => m.UserId == newUser.UserId));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// GetOrGenerateItemByWeixinUnionIdAsync
        /// </summary>
        /// <param name="generateGroupId"></param>
        /// <param name="generateStatus"></param>
        /// <param name="unionId"></param>
        /// <returns></returns>
        public async Task <XM.UserInfo> GetOrGenerateItemByWeixinUnionIdAsync(Guid generateGroupId, XM.UserStatus generateStatus, string unionId)
        {
            if (unionId.IsNullOrWhiteSpace())
            {
                return(null);
            }
            var user = await GetItemByWeixinUnionIdAsync(unionId);

            if (user == null)
            {
                var newUser = new User
                {
                    Status        = generateStatus,
                    CreationTime  = DateTime.Now,
                    WeixinUnionId = unionId,
                    GroupId       = generateGroupId, // new Guid("11111111-1111-1111-1111-111111111111") 等待分配组
                    Username      = "******" + Guid.NewGuid().ToString("N").Substring(19),
                    Password      = unionId,
                };

                _context.User.Add(newUser);
                await _context.SaveChangesAsync();

                user = await GetItemByWeixinUnionIdAsync(unionId);
            }
            return(user);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// GetOrGenerateItemByWeixinAppOpenIdAsync
        /// </summary>
        /// <param name="generateGroupId"></param>
        /// <param name="generateStatus"></param>
        /// <param name="openId"></param>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public async Task <XM.UserInfo> GetOrGenerateItemByWeixinAppOpenIdAsync(Guid generateGroupId, XM.UserStatus generateStatus, string openId, string mobile)
        {
            if (openId.IsNullOrWhiteSpace())
            {
                return(null);
            }
            var user = await GetItemByWeixinAppOpenIdAsync(openId);

            if (user == null)
            {
                // 验证手机号是否被使用
                if (!mobile.IsNullOrWhiteSpace())
                {
                    if (await _context.User.AnyAsync(m => m.Mobile == mobile))
                    {
                        return(null);
                    }
                }
                var newUser = new User
                {
                    Status          = generateStatus,
                    CreationTime    = DateTime.Now,
                    WeixinAppOpenId = openId,
                    GroupId         = generateGroupId, // new Guid("11111111-1111-1111-1111-111111111111") 等待分配组
                    Username        = "******" + Guid.NewGuid().ToString("N").Substring(19),
                    Mobile          = mobile,
                    MobileIsValid   = !mobile.IsNullOrWhiteSpace(),
                    Password        = openId,
                };

                _context.User.Add(newUser);
                await _context.SaveChangesAsync();

                user = await GetItemByWeixinAppOpenIdAsync(openId);
            }
            else
            {
                if (user.Mobile != mobile)
                {
                    if (await _context.User.AnyAsync(m => m.UserId != user.UserId && m.Mobile == mobile))
                    {
                        // 更换手机号,但手机号被他人使用
                        return(null);
                    }
                }
                var userEntity = await _context.User.Where(m => m.UserId == user.UserId).FirstAsync();

                userEntity.Mobile        = mobile;
                userEntity.MobileIsValid = !mobile.IsNullOrWhiteSpace();
                await _context.SaveChangesAsync();

                user = await GetItemByWeixinAppOpenIdAsync(openId);
            }

            return(user);
        }