Beispiel #1
0
        /// <summary>
        /// 添加或修改用户
        /// </summary>
        /// <param entity=""></param>
        /// <returns></returns>
        public async Task <SysUser> ModifyAsync(SysUserView entity)
        {
            if (await CheckLoginNameExists(entity.UserId, entity.LoginName))
            {
                throw new Exception($"登录名:【{entity.LoginName}】已存在");
            }

            if (entity.Password.Equals("@@**@@") && entity.UserId > 0)
            {
                //不修改密码,获取旧密码
                var oldEntity = await _sysUserRepository.GetModelAsync(p => p.UserId == entity.UserId);

                entity.Password = oldEntity.Password;
            }
            else
            {
                entity.Password = entity.Password.EncryptMD5Encode();
            }
            //工作单元
            using (var uow = _fsq.CreateUnitOfWork())
            {
                var userRepo = uow.GetRepository <SysUser>();
                userRepo.UnitOfWork = uow;
                var userRoleRepo = uow.GetRepository <SysRoleUser>();
                userRoleRepo.UnitOfWork = uow;

                var newEntity = await userRepo.InsertOrUpdateAsync(entity);

                var insertRoleList = new List <SysRoleUser>();
                //处理用户组
                if (entity.RoleIdArray != null)
                {
                    foreach (var roleId in entity.RoleIdArray)
                    {
                        insertRoleList.Add(new SysRoleUser()
                        {
                            RoleId = roleId,
                            UserId = newEntity.UserId,
                        });
                    }
                }
                //删除之前的
                await userRoleRepo.DeleteAsync(p => p.UserId == newEntity.Id);

                await userRoleRepo.InsertAsync(insertRoleList);

                uow.Commit();
            }
            entity.Password = "******";
            return(entity);
        }
        public async Task <LoginResultModel> Login(LoginViewModel loginViewModel)
        {
            loginViewModel.PassWord = MD5Helper.MD5Encrypt32(loginViewModel.PassWord);
            var model = await sysUserRepository.GetModelAsync(x => x.UserName == loginViewModel.UserName && x.Password == loginViewModel.PassWord && x.IsDelete == false);

            if (model == null)
            {
                return(null);
            }
            var accesstoken = JwtHelper.IssueJwt(new TokenModelJwt
            {
                Role = model.Role.ToString(),
                Uid  = model.Id
            });

            return(new LoginResultModel
            {
                RoleId = model.Role,
                UserName = loginViewModel.UserName,
                UserId = model.Id,
                AccessToken = accesstoken,
                IsAdmin = model.IsAdmin
            });
        }