diff --git a/langguanApi/Service/MenuService.cs b/langguanApi/Service/MenuService.cs
index 611a8de..e22dfb7 100644
--- a/langguanApi/Service/MenuService.cs
+++ b/langguanApi/Service/MenuService.cs
@@ -72,7 +72,7 @@ namespace langguanApi.Service
///
public async Task> GetChildList(string parentId)
{
- Expression> exp = filter => filter.IsDelete == false&& filter.ParentId == parentId;
+ Expression> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
var list = (await GetListWithExp(exp))
.OrderBy(x => x.Sort)
.ToList().Adapt>();
@@ -93,30 +93,13 @@ namespace langguanApi.Service
///
public async Task DeleteMenu(string id)
{
- try
+ var entity = await base.GetAsync(id);
+ if (entity == null)
{
- var entity = await base.GetAsync(id);
- if (entity == null)
- {
- return new ApiResult() { code = 0, data = false, msg = "删除菜单失败,Id不存在" };
- }
- entity.IsDelete = true;
- await base.UpdateAsync(id, entity);
- }
- catch (Exception ex)
- {
-
- _logger.LogError($"删除菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(id)}," +
- $"请求接口:'api/Menu/DeleteMenu'," +
- $"异常信息:{ex.Message}," +
- $"异常位置:{ex.StackTrace}"
- );
- return new ApiResult { code = 1, msg = "删除菜单失败", data = false };
- }
- finally
- {
- _logger.LogInformation($"删除菜单参数:menuId:{id}");
+ return new ApiResult() { code = 0, data = false, msg = "删除菜单失败,Id不存在" };
}
+ entity.IsDelete = true;
+ await base.UpdateAsync(id, entity);
return new ApiResult() { code = 0, data = true, msg = "删除菜单成功" };
}
}
diff --git a/langguanApi/Service/UserService.cs b/langguanApi/Service/UserService.cs
index 606448b..fda7d9c 100644
--- a/langguanApi/Service/UserService.cs
+++ b/langguanApi/Service/UserService.cs
@@ -118,55 +118,40 @@ namespace langguanApi.Service
public async Task GetUserList(UserQueryDto input)
{
List list = new List();
- try
+
+ #region 组织查询条件
+ Expression> exp = filter => filter.IsDelete == false;
+ if (!string.IsNullOrEmpty(input.key))
{
- #region 组织查询条件
- Expression> exp = filter => filter.IsDelete == false;
- if (!string.IsNullOrEmpty(input.key))
- {
- exp = filter => filter.Username.Contains(input.key) || filter.Phone.Contains(input.key) || filter.Email.Contains(input.key);
- }
- #endregion
- #region 获取数据
- var result = await base.GetListWithExp(exp);//获取人员信息
- if (result.Count() > 0)
- {
- result = result.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
- }
- else
- {
- return new ApiResult { code = 0, data = null, msg = "没有用户信息" };
- }
- var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.RoleId).ToList());//根据角色Id获取角色信息
- #endregion
- #region 组装返回数据
- foreach (var item in result)
- {
- list.Add(new UserDetailDto
- {
- UserId = item.Id,
- Username = item.Username,
- Email = item.Email,
- RoleId = item.RoleId,
- RoleName = roleList.FirstOrDefault(s => s.Id == item.RoleId)?.RoleName,
- Tel = item.Phone,
- });
- }
- #endregion
+ exp = filter => filter.Username.Contains(input.key) || filter.Phone.Contains(input.key) || filter.Email.Contains(input.key);
}
- catch (Exception ex)
+ #endregion
+ #region 获取数据
+ var result = await base.GetListWithExp(exp);//获取人员信息
+ if (result.Count() > 0)
{
- _logger.LogError($"获取用户列表出现异常,请求参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
- $"请求接口:'api/User/GetUserList'," +
- $"异常信息:{ex.Message}," +
- $"异常位置:{ex.StackTrace}"
- );
- return new ApiResult { code = 1, msg = "获取用户列表失败" };
+ result = result.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
}
- finally
+ else
{
- _logger.LogInformation($"获取用户列表参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
+ return new ApiResult { code = 0, data = null, msg = "没有用户信息" };
}
+ var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.RoleId).ToList());//根据角色Id获取角色信息
+ #endregion
+ #region 组装返回数据
+ foreach (var item in result)
+ {
+ list.Add(new UserDetailDto
+ {
+ UserId = item.Id,
+ Username = item.Username,
+ Email = item.Email,
+ RoleId = item.RoleId,
+ RoleName = roleList.FirstOrDefault(s => s.Id == item.RoleId)?.RoleName,
+ Tel = item.Phone,
+ });
+ }
+ #endregion
return new ApiResult { code = 0, data = list, msg = "获取信息成功" };
}
@@ -178,51 +163,36 @@ namespace langguanApi.Service
public async Task GetUserById(string userId)
{
UserDetailDto userDetail = null;
- try
+
+ #region 校验数据
+ if (string.IsNullOrEmpty(userId))
{
- #region 校验数据
- if (string.IsNullOrEmpty(userId))
- {
- return new ApiResult { code = 1, msg = "获取用户信息失败,userId非空" };
- }
- #endregion
- #region 获取数据
- var user = await base.GetAsync(userId);//根据userId获取用户信息
- if (user == null)
- {
- return new ApiResult { code = 1, msg = "用户不存在" };
- }
- var role = await _roleService.GetRoleById(user.RoleId);//根据角色Id获取角色信息
- if (role == null)
- {
- return new ApiResult { code = 1, msg = "角色不存在" };
- }
- #endregion
- #region 组织需要展示的数据
- userDetail = new UserDetailDto
- {
- UserId = user.Id,
- Username = user.Username,
- Email = user.Email,
- RoleId = user.RoleId,
- RoleName = role.RoleName,
- Tel = user.Phone
- };
- #endregion
+ return new ApiResult { code = 1, msg = "获取用户信息失败,userId非空" };
}
- catch (Exception ex)
+ #endregion
+ #region 获取数据
+ var user = await base.GetAsync(userId);//根据userId获取用户信息
+ if (user == null)
{
- _logger.LogError($"获取用户信息出现异常,请求参数:userId:{userId}," +
- $"请求接口:'api/User/GetUserById'," +
- $"异常信息:{ex.Message}," +
- $"异常位置:{ex.StackTrace}"
- );
- return new ApiResult { code = 1, msg = "获取用户信息失败" };
+ return new ApiResult { code = 1, msg = "用户不存在" };
}
- finally
+ var role = await _roleService.GetRoleById(user.RoleId);//根据角色Id获取角色信息
+ if (role == null)
{
- _logger.LogInformation($"获取用户信息参数:userId:{userId}");
+ return new ApiResult { code = 1, msg = "角色不存在" };
}
+ #endregion
+ #region 组织需要展示的数据
+ userDetail = new UserDetailDto
+ {
+ UserId = user.Id,
+ Username = user.Username,
+ Email = user.Email,
+ RoleId = user.RoleId,
+ RoleName = role.RoleName,
+ Tel = user.Phone
+ };
+ #endregion
return new ApiResult { code = 0, data = userDetail };
}
@@ -230,55 +200,40 @@ namespace langguanApi.Service
//更新用户信息方法
public async Task UpdateUser(UserDto input)
{
- try
+
+ #region 校验参数
+ if (string.IsNullOrEmpty(input.Id))
{
- #region 校验参数
- if (string.IsNullOrEmpty(input.Id))
- {
- return new ApiResult { code = 1, msg = "用户Id不能为空" };
- }
- if (string.IsNullOrEmpty(input.Username))
- {
- return new ApiResult { code = 1, msg = "用户名不能为空" };
- }
- if (string.IsNullOrEmpty(input.Tel))
- {
- return new ApiResult { code = 1, msg = "手机号不能为空" };
- }
- if (string.IsNullOrEmpty(input.Email))
- {
- return new ApiResult { code = 1, msg = "邮箱不能为空" };
- }
- if (string.IsNullOrEmpty(input.RoleId))
- {
- return new ApiResult { code = 1, msg = "角色不能为空" };
- }
- #endregion
- #region 组织数据
- var userEntity = input.Adapt();
- userEntity.Phone = input.Tel;
- if (!string.IsNullOrEmpty(input.Password))
- {
- userEntity.Password = StringHelper.MD5Encrypt32(input.Password);
- }
- #endregion
- #region 更新数据
- await base.UpdateAsync(input.Id, userEntity);//更新用户信息
- #endregion
+ return new ApiResult { code = 1, msg = "用户Id不能为空" };
}
- catch (Exception ex)
+ if (string.IsNullOrEmpty(input.Username))
{
- _logger.LogError($"更新用户信息出现异常,请求参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
- $"请求接口:'api/User/UpdateUser'," +
- $"异常信息:{ex.Message}," +
- $"异常位置:{ex.StackTrace}"
- );
- return new ApiResult { code = 1, msg = "更新用户信息失败" };
+ return new ApiResult { code = 1, msg = "用户名不能为空" };
}
- finally
+ if (string.IsNullOrEmpty(input.Tel))
{
- _logger.LogInformation($"更新用户信息参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
+ return new ApiResult { code = 1, msg = "手机号不能为空" };
}
+ if (string.IsNullOrEmpty(input.Email))
+ {
+ return new ApiResult { code = 1, msg = "邮箱不能为空" };
+ }
+ if (string.IsNullOrEmpty(input.RoleId))
+ {
+ return new ApiResult { code = 1, msg = "角色不能为空" };
+ }
+ #endregion
+ #region 组织数据
+ var userEntity = input.Adapt();
+ userEntity.Phone = input.Tel;
+ if (!string.IsNullOrEmpty(input.Password))
+ {
+ userEntity.Password = StringHelper.MD5Encrypt32(input.Password);
+ }
+ #endregion
+ #region 更新数据
+ await base.UpdateAsync(input.Id, userEntity);//更新用户信息
+ #endregion
return new ApiResult { code = 0, msg = "更新用户信息成功" };
}
@@ -289,37 +244,22 @@ namespace langguanApi.Service
///
public async Task DeleteUser(string userId)
{
- try
+
+ #region 校验数据
+ if (string.IsNullOrEmpty(userId))
{
- #region 校验数据
- if (string.IsNullOrEmpty(userId))
- {
- return new ApiResult { code = 1, msg = "用户Id不能为空" };
- }
- #endregion
- #region 更新数据
- var user = await base.GetAsync(userId);//根据userId获取用户信息
- if (user == null)
- {
- return new ApiResult { code = 1, msg = "用户不存在" };
- }
- user.IsDelete = true;
- await base.UpdateAsync(userId, user);//更新用户信息
- #endregion
+ return new ApiResult { code = 1, msg = "用户Id不能为空" };
}
- catch (Exception ex)
+ #endregion
+ #region 更新数据
+ var user = await base.GetAsync(userId);//根据userId获取用户信息
+ if (user == null)
{
- _logger.LogError($"删除用户信息出现异常,请求参数:userId:{userId}," +
- $"请求接口:'api/User/DeleteUser'," +
- $"异常信息:{ex.Message}," +
- $"异常位置:{ex.StackTrace}"
- );
- return new ApiResult { code = 1, msg = "删除用户信息失败" };
- }
- finally
- {
- _logger.LogInformation($"删除用户信息参数:userId:{userId}");
+ return new ApiResult { code = 1, msg = "用户不存在" };
}
+ user.IsDelete = true;
+ await base.UpdateAsync(userId, user);//更新用户信息
+ #endregion
return new ApiResult { code = 0, msg = "删除用户信息成功" };
}
#endregion