Пример #1
0
        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <SetUserOutput> SetUser(SetUserInput input)
        {
            var model = _mapper.Map <SetUserInput, UserInfo>(input);

            if (model.Id > 0)
            {
                model.UpdateTime = DateTime.Now;
                if (!model.Password.IsEmpty())
                {
                    model.Salt     = Randoms.CreateRandomValue(8, false);
                    model.Password = Encrypt.SHA256(model.Password + model.Salt);
                    // 基础字段不容许更新
                    await _dbContext.Updateable(model)
                    .IgnoreColumns(it => new { it.UserName, it.Mobile, it.CreateTime })
                    .ExecuteCommandAsync();
                }
                else
                {
                    // 基础字段不容许更新
                    await _dbContext.Updateable(model)
                    .IgnoreColumns(it => new { it.UserName, it.Password, it.Salt, it.Mobile, it.CreateTime })
                    .ExecuteCommandAsync();
                }
            }
            else
            {
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.Salt       = Randoms.CreateRandomValue(8, false);
                model.Password   = Encrypt.SHA256(model.Password + model.Salt);
                model.Id         = Convert.ToInt64($"{Time.GetUnixTimestamp()}{ Randoms.CreateRandomValue(3, true) }");
                await _dbContext.Insertable(model).ExecuteCommandAsync();
            }
            // 用户角色操作
            List <UserRoleInfo> userRoleList = new List <UserRoleInfo>();

            foreach (var id in input.RoleIdList)
            {
                // 防止重复数据
                if (!userRoleList.Exists(it => it.RoleId == id))
                {
                    userRoleList.Add(new UserRoleInfo
                    {
                        Uid    = model.Id,
                        RoleId = id
                    });
                }
            }
            // 删除用户当前角色
            await _dbContext.Deleteable <UserRoleInfo>().Where(f => f.Uid == model.Id).ExecuteCommandAsync();

            // 添加用户角色
            if (userRoleList.Count > 0)
            {
                await _dbContext.Insertable(userRoleList).ExecuteCommandAsync();
            }

            return(new SetUserOutput {
            });
        }
Пример #2
0
        public BaseOutput SetUser([FromBody] SetUserInput input)
        {
            try
            {
                _adminDbContext.Ado.BeginTran();

                var model = _mapper.Map <SetUserInput, UserModel>(input);
                if (model.Id > 0)
                {
                    model.UpdateTime = DateTime.Now;
                    if (!model.Password.IsEmpty())
                    {
                        model.Salt     = Randoms.CreateRandomValue(8, false);
                        model.Password = Encrypt.SHA256(model.Password + model.Salt);
                        // 基础字段不容许更新
                        _adminDbContext.Updateable(model)
                        .IgnoreColumns(it => new { it.UserName, it.Mobile, it.CreateTime })
                        .ExecuteCommand();
                    }
                    else
                    {
                        // 基础字段不容许更新
                        _adminDbContext.Updateable(model)
                        .IgnoreColumns(it => new { it.UserName, it.Password, it.Salt, it.Mobile, it.CreateTime })
                        .ExecuteCommand();
                    }
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.UpdateTime = DateTime.Now;
                    model.Salt       = Randoms.CreateRandomValue(8, false);
                    model.Password   = Encrypt.SHA256(model.Password + model.Salt);
                    model.Id         = Convert.ToInt64($"{Time.GetUnixTimestamp()}{ Randoms.CreateRandomValue(3, true) }");
                    _adminDbContext.Insertable(model).ExecuteCommand();
                }
                // 用户角色操作
                var userRoleList = new List <UserRoleModel>();
                foreach (var id in input.RoleIdList)
                {
                    // 防止重复数据
                    if (!userRoleList.Exists(it => it.RoleId == id))
                    {
                        userRoleList.Add(new UserRoleModel
                        {
                            Uid    = model.Id,
                            RoleId = id
                        });
                    }
                }
                // 删除用户当前角色
                _adminDbContext.Deleteable <UserRoleModel>().Where(f => f.Uid == model.Id).ExecuteCommand();
                // 添加用户角色
                if (userRoleList.Count > 0)
                {
                    _adminDbContext.Insertable(userRoleList).ExecuteCommand();
                }

                _adminDbContext.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _adminDbContext.Ado.RollbackTran();
                throw new Exception("事务执行失败", ex);
            }
            return(new BaseOutput {
            });
        }
Пример #3
0
 public async Task <SetUserOutput> SetUser([FromBody] SetUserInput input)
 {
     return(await _userBusiness.SetUser(input));
 }